Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | 
         @@ -2,8 +2,9 @@ import torch 
     | 
|
| 2 | 
         
             
            from diffusers import StableDiffusion3Pipeline
         
     | 
| 3 | 
         
             
            import gradio as gr
         
     | 
| 4 | 
         
             
            import os
         
     | 
| 5 | 
         
            -
            import  
     | 
| 6 | 
         
             
            from huggingface_hub import snapshot_download
         
     | 
| 
         | 
|
| 7 | 
         | 
| 8 | 
         
             
            HF_TOKEN = os.getenv("HF_TOKEN")
         
     | 
| 9 | 
         | 
| 
         @@ -27,9 +28,34 @@ else: 
     | 
|
| 27 | 
         
             
            pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
         
     | 
| 28 | 
         
             
            pipe.to(device)
         
     | 
| 29 | 
         | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 30 | 
         
             
            # Define the image generation function
         
     | 
| 31 | 
         
             
            @spaces.GPU(duration=60)
         
     | 
| 32 | 
         
            -
            def generate_image(prompt, negative_prompt, num_inference_steps, height, width, guidance_scale, num_images_per_prompt):
         
     | 
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 33 | 
         
             
                output = pipe(
         
     | 
| 34 | 
         
             
                    prompt=prompt,
         
     | 
| 35 | 
         
             
                    negative_prompt=negative_prompt,
         
     | 
| 
         @@ -37,6 +63,7 @@ def generate_image(prompt, negative_prompt, num_inference_steps, height, width, 
     | 
|
| 37 | 
         
             
                    height=height,
         
     | 
| 38 | 
         
             
                    width=width,
         
     | 
| 39 | 
         
             
                    guidance_scale=guidance_scale,
         
     | 
| 
         | 
|
| 40 | 
         
             
                    num_images_per_prompt=num_images_per_prompt
         
     | 
| 41 | 
         
             
                ).images
         
     | 
| 42 | 
         
             
                return output
         
     | 
| 
         @@ -45,6 +72,8 @@ def generate_image(prompt, negative_prompt, num_inference_steps, height, width, 
     | 
|
| 45 | 
         | 
| 46 | 
         
             
            prompt = gr.Textbox(label="Prompt", info="Describe the image you want", placeholder="A cat...")
         
     | 
| 47 | 
         | 
| 
         | 
|
| 
         | 
|
| 48 | 
         
             
            negative_prompt = gr.Textbox(label="Negative Prompt", info="Describe what you don't want in the image", placeholder="Ugly, bad anatomy...")
         
     | 
| 49 | 
         | 
| 50 | 
         
             
            num_inference_steps = gr.Number(label="Number of Inference Steps", precision=0, value=25)
         
     | 
| 
         @@ -55,11 +84,13 @@ width = gr.Slider(label="Width", info="Width of the Image", minimum=256, maximum 
     | 
|
| 55 | 
         | 
| 56 | 
         
             
            guidance_scale = gr.Number(minimum=0.1, value=7.5, label="Guidance Scale", info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference")
         
     | 
| 57 | 
         | 
| 
         | 
|
| 
         | 
|
| 58 | 
         
             
            num_images_per_prompt = gr.Slider(label="Number of Images to generate with the settings",minimum=1, maximum=4, step=1, value=1)
         
     | 
| 59 | 
         | 
| 60 | 
         
             
            interface = gr.Interface(
         
     | 
| 61 | 
         
             
                fn=generate_image,
         
     | 
| 62 | 
         
            -
                inputs=[prompt, negative_prompt, num_inference_steps, height, width, guidance_scale, num_images_per_prompt],
         
     | 
| 63 | 
         
             
                outputs=gr.Gallery(label="Generated AI Images", elem_id="gallery", show_label=False),
         
     | 
| 64 | 
         
             
                title="Stable Diffusion 3 Medium",
         
     | 
| 65 | 
         
             
                description="Made by <a href='https://linktr.ee/Nick088' target='_blank'>Nick088</a> \n Join https://discord.gg/osai to talk about Open Source AI"
         
     | 
| 
         | 
|
| 2 | 
         
             
            from diffusers import StableDiffusion3Pipeline
         
     | 
| 3 | 
         
             
            import gradio as gr
         
     | 
| 4 | 
         
             
            import os
         
     | 
| 5 | 
         
            +
            from transformers import T5Tokenizer, T5ForConditionalGeneration
         
     | 
| 6 | 
         
             
            from huggingface_hub import snapshot_download
         
     | 
| 7 | 
         
            +
            import spaces
         
     | 
| 8 | 
         | 
| 9 | 
         
             
            HF_TOKEN = os.getenv("HF_TOKEN")
         
     | 
| 10 | 
         | 
| 
         | 
|
| 28 | 
         
             
            pipe = StableDiffusion3Pipeline.from_pretrained(model_path, torch_dtype=torch.float16)
         
     | 
| 29 | 
         
             
            pipe.to(device)
         
     | 
| 30 | 
         | 
| 31 | 
         
            +
            tokenizer = T5Tokenizer.from_pretrained("roborovski/superprompt-v1")
         
     | 
| 32 | 
         
            +
            model = T5ForConditionalGeneration.from_pretrained("roborovski/superprompt-v1", device_map="auto", torch_dtype="auto")
         
     | 
| 33 | 
         
            +
            model.to(device)
         
     | 
| 34 | 
         
            +
             
     | 
| 35 | 
         
             
            # Define the image generation function
         
     | 
| 36 | 
         
             
            @spaces.GPU(duration=60)
         
     | 
| 37 | 
         
            +
            def generate_image(prompt, negative_prompt, num_inference_steps, height, width, guidance_scale, seed, num_images_per_prompt):
         
     | 
| 38 | 
         
            +
                if seed == 0:
         
     | 
| 39 | 
         
            +
                    seed = random.randint(1, 2**32-1)
         
     | 
| 40 | 
         
            +
                    
         
     | 
| 41 | 
         
            +
                if enhance_prompt:
         
     | 
| 42 | 
         
            +
                    transformers.set_seed(seed)
         
     | 
| 43 | 
         
            +
                    
         
     | 
| 44 | 
         
            +
                    input_text = f"Expand the following prompt to add more detail: {prompt}"
         
     | 
| 45 | 
         
            +
                    input_ids = tokenizer(input_text, return_tensors="pt").input_ids.to(device)
         
     | 
| 46 | 
         
            +
                    
         
     | 
| 47 | 
         
            +
                    outputs = model.generate(
         
     | 
| 48 | 
         
            +
                    input_ids,
         
     | 
| 49 | 
         
            +
                    max_new_tokens=512,
         
     | 
| 50 | 
         
            +
                    repetition_penalty=1.2,
         
     | 
| 51 | 
         
            +
                    do_sample=True,
         
     | 
| 52 | 
         
            +
                    temperature=0.7,
         
     | 
| 53 | 
         
            +
                    top_p=1,
         
     | 
| 54 | 
         
            +
                    top_k=50,
         
     | 
| 55 | 
         
            +
                )
         
     | 
| 56 | 
         
            +
                    
         
     | 
| 57 | 
         
            +
                generator = torch.Generator().manual_seed(seed)
         
     | 
| 58 | 
         
            +
                
         
     | 
| 59 | 
         
             
                output = pipe(
         
     | 
| 60 | 
         
             
                    prompt=prompt,
         
     | 
| 61 | 
         
             
                    negative_prompt=negative_prompt,
         
     | 
| 
         | 
|
| 63 | 
         
             
                    height=height,
         
     | 
| 64 | 
         
             
                    width=width,
         
     | 
| 65 | 
         
             
                    guidance_scale=guidance_scale,
         
     | 
| 66 | 
         
            +
                    generator=generator,
         
     | 
| 67 | 
         
             
                    num_images_per_prompt=num_images_per_prompt
         
     | 
| 68 | 
         
             
                ).images
         
     | 
| 69 | 
         
             
                return output
         
     | 
| 
         | 
|
| 72 | 
         | 
| 73 | 
         
             
            prompt = gr.Textbox(label="Prompt", info="Describe the image you want", placeholder="A cat...")
         
     | 
| 74 | 
         | 
| 75 | 
         
            +
            enhance_prompt = gr.Checkbox(label="Prompt Enhancement", info="Enhance your prompt with SuperPrompt-v1", value=True)
         
     | 
| 76 | 
         
            +
             
     | 
| 77 | 
         
             
            negative_prompt = gr.Textbox(label="Negative Prompt", info="Describe what you don't want in the image", placeholder="Ugly, bad anatomy...")
         
     | 
| 78 | 
         | 
| 79 | 
         
             
            num_inference_steps = gr.Number(label="Number of Inference Steps", precision=0, value=25)
         
     | 
| 
         | 
|
| 84 | 
         | 
| 85 | 
         
             
            guidance_scale = gr.Number(minimum=0.1, value=7.5, label="Guidance Scale", info="The number of denoising steps of the image. More denoising steps usually lead to a higher quality image at the cost of slower inference")
         
     | 
| 86 | 
         | 
| 87 | 
         
            +
            seed = gr.Slider(value=42, minimum=0, maximum=2**32-1, step=1, label="Seed", info="A starting point to initiate the generation process, put 0 for a random one")
         
     | 
| 88 | 
         
            +
             
     | 
| 89 | 
         
             
            num_images_per_prompt = gr.Slider(label="Number of Images to generate with the settings",minimum=1, maximum=4, step=1, value=1)
         
     | 
| 90 | 
         | 
| 91 | 
         
             
            interface = gr.Interface(
         
     | 
| 92 | 
         
             
                fn=generate_image,
         
     | 
| 93 | 
         
            +
                inputs=[prompt, enhance_prompt, negative_prompt, num_inference_steps, height, width, guidance_scale, seed, num_images_per_prompt],
         
     | 
| 94 | 
         
             
                outputs=gr.Gallery(label="Generated AI Images", elem_id="gallery", show_label=False),
         
     | 
| 95 | 
         
             
                title="Stable Diffusion 3 Medium",
         
     | 
| 96 | 
         
             
                description="Made by <a href='https://linktr.ee/Nick088' target='_blank'>Nick088</a> \n Join https://discord.gg/osai to talk about Open Source AI"
         
     |