Spaces:
Runtime error
Runtime error
macadeliccc
commited on
Commit
β’
847481c
1
Parent(s):
0ab8f18
init
Browse files
app.py
CHANGED
@@ -1,13 +1,51 @@
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
import spaces
|
3 |
import torch
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
@spaces.GPU
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from diffusers import StableDiffusionXLPipeline
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from PIL import Image
|
5 |
import gradio as gr
|
|
|
6 |
import torch
|
7 |
+
import uuid
|
8 |
+
import io
|
9 |
+
import os
|
10 |
+
import spaces # Assuming spaces is a valid module with GPU decorator
|
11 |
|
12 |
+
# Load your model
|
13 |
+
pipe = StableDiffusionXLPipeline.from_pretrained(
|
14 |
+
"segmind/SSD-1B",
|
15 |
+
torch_dtype=torch.float16,
|
16 |
+
use_safetensors=True,
|
17 |
+
variant="fp16"
|
18 |
+
)
|
19 |
+
pipe.to("cuda:0")
|
20 |
|
21 |
+
@spaces.GPU # Apply the GPU decorator
|
22 |
+
def generate_and_save_image(prompt, negative_prompt=''):
|
23 |
+
# Generate image using the provided prompts
|
24 |
+
image = pipe(prompt=prompt, negative_prompt=negative_prompt).images[0]
|
25 |
|
26 |
+
# Generate a unique UUID for the filename
|
27 |
+
unique_id = str(uuid.uuid4())
|
28 |
+
image_path = f"generated_images/{unique_id}.png"
|
29 |
+
|
30 |
+
# Save generated image locally
|
31 |
+
os.makedirs('generated_images', exist_ok=True)
|
32 |
+
image.save(image_path, format='PNG')
|
33 |
+
|
34 |
+
# Return the path of the saved image to display in Gradio interface
|
35 |
+
return image_path
|
36 |
+
|
37 |
+
# Define the Gradio interface
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=generate_and_save_image,
|
40 |
+
inputs=[
|
41 |
+
gr.Textbox(label="Enter prompt", placeholder="Type your prompt here..."),
|
42 |
+
gr.Textbox(label="Enter negative prompt (optional)", placeholder="Type your negative prompt here...")
|
43 |
+
],
|
44 |
+
outputs=gr.Image(type="file", label="Generated Image"),
|
45 |
+
title="Image Generation with Stable Diffusion XL",
|
46 |
+
description="Enter a prompt and (optionally) a negative prompt to generate an image.",
|
47 |
+
layout="vertical"
|
48 |
+
)
|
49 |
+
|
50 |
+
# Launch the Gradio app
|
51 |
+
iface.launch()
|