File size: 1,499 Bytes
5376cf0
847481c
 
 
0ab8f18
 
847481c
 
 
5376cf0
0ab8f18
847481c
 
 
 
 
 
 
 
0ab8f18
847481c
 
 
 
0ab8f18
847481c
 
64ab7c4
847481c
 
 
64ab7c4
847481c
 
 
 
 
 
 
 
 
 
 
b26c8cb
847481c
6dcee6c
847481c
 
 
 
6dcee6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import spaces
from diffusers import StableDiffusionXLPipeline
from pydantic import BaseModel
from PIL import Image
import gradio as gr
import torch
import uuid
import io
import os


# Load your model
pipe = StableDiffusionXLPipeline.from_pretrained(
    "segmind/SSD-1B", 
    torch_dtype=torch.float16, 
    use_safetensors=True, 
    variant="fp16"
)
pipe.to("cuda:0")

@spaces.GPU  # Apply the GPU decorator
def generate_and_save_image(prompt, negative_prompt=''):
    # Generate image using the provided prompts
    image = pipe(prompt=prompt, negative_prompt=negative_prompt).images[0]

    # Generate a unique UUID for the filename
    unique_id = str(uuid.uuid4())
    image_path = f"generated_images/{unique_id}.jpeg"

    # Save generated image locally
    os.makedirs('generated_images', exist_ok=True)
    image.save(image_path, format='JPEG')

    # Return the path of the saved image to display in Gradio interface
    return image_path

# Define the Gradio interface
iface = gr.Interface(
    fn=generate_and_save_image,
    inputs=[
        gr.Textbox(label="Enter prompt", placeholder="Type your prompt here..."),
        gr.Textbox(label="Enter negative prompt (optional)", placeholder="Type your negative prompt here...")
    ],
    outputs=gr.Image(type="filepath", label="Generated Image"),
    title="Image Generation with Stable Diffusion XL",
    description="Enter a prompt and (optionally) a negative prompt to generate an image."
)

# Launch the Gradio app
iface.launch()