Spaces:
Runtime error
Runtime error
macadeliccc
commited on
Commit
•
600e217
1
Parent(s):
64ab7c4
test
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import spaces
|
2 |
from diffusers import StableDiffusionXLPipeline
|
|
|
3 |
from pydantic import BaseModel
|
4 |
from PIL import Image
|
5 |
import gradio as gr
|
@@ -8,6 +9,14 @@ import uuid
|
|
8 |
import io
|
9 |
import os
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Load your model
|
13 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
@@ -18,6 +27,17 @@ pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
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
|
@@ -34,18 +54,62 @@ def generate_and_save_image(prompt, negative_prompt=''):
|
|
34 |
# Return the path of the saved image to display in Gradio interface
|
35 |
return image_path
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
# Launch the Gradio app
|
50 |
-
|
51 |
|
|
|
1 |
import spaces
|
2 |
from diffusers import StableDiffusionXLPipeline
|
3 |
+
from diffusers import DiffusionPipeline
|
4 |
from pydantic import BaseModel
|
5 |
from PIL import Image
|
6 |
import gradio as gr
|
|
|
9 |
import io
|
10 |
import os
|
11 |
|
12 |
+
# Load the base & refiner pipelines
|
13 |
+
base = DiffusionPipeline.from_pretrained(
|
14 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
15 |
+
torch_dtype=torch.float16,
|
16 |
+
variant="fp16",
|
17 |
+
use_safetensors=True
|
18 |
+
)
|
19 |
+
base.to("cuda:0")
|
20 |
|
21 |
# Load your model
|
22 |
pipe = StableDiffusionXLPipeline.from_pretrained(
|
|
|
27 |
)
|
28 |
pipe.to("cuda:0")
|
29 |
|
30 |
+
refiner = DiffusionPipeline.from_pretrained(
|
31 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
32 |
+
text_encoder_2=base.text_encoder_2,
|
33 |
+
vae=base.vae,
|
34 |
+
torch_dtype=torch.float16,
|
35 |
+
use_safetensors=True,
|
36 |
+
variant="fp16",
|
37 |
+
)
|
38 |
+
refiner.to("cuda:0")
|
39 |
+
refiner.unet = torch.compile(refiner.unet, mode="reduce-overhead", fullgraph=True)
|
40 |
+
|
41 |
@spaces.GPU # Apply the GPU decorator
|
42 |
def generate_and_save_image(prompt, negative_prompt=''):
|
43 |
# Generate image using the provided prompts
|
|
|
54 |
# Return the path of the saved image to display in Gradio interface
|
55 |
return image_path
|
56 |
|
57 |
+
def generate_image_with_refinement(prompt):
|
58 |
+
n_steps = 40
|
59 |
+
high_noise_frac = 0.8
|
60 |
+
|
61 |
+
# run both experts
|
62 |
+
image = base(
|
63 |
+
prompt=prompt,
|
64 |
+
num_inference_steps=n_steps,
|
65 |
+
denoising_end=high_noise_frac,
|
66 |
+
output_type="latent",
|
67 |
+
).images
|
68 |
+
image = refiner(
|
69 |
+
prompt=prompt,
|
70 |
+
num_inference_steps=n_steps,
|
71 |
+
denoising_start=high_noise_frac,
|
72 |
+
image=image,
|
73 |
+
).images[0]
|
74 |
+
|
75 |
+
# Save the image as before
|
76 |
+
unique_id = str(uuid.uuid4())
|
77 |
+
image_path = f"generated_images_refined/{unique_id}.jpeg"
|
78 |
+
os.makedirs('generated_images_refined', exist_ok=True)
|
79 |
+
image.save(image_path, format='JPEG')
|
80 |
+
|
81 |
+
return image_path
|
82 |
+
|
83 |
+
# Start of the Gradio Blocks interface
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
gr.Markdown("# Image Generation with SSD-1B")
|
86 |
+
gr.Markdown("Enter a prompt and (optionally) a negative prompt to generate an image.")
|
87 |
+
with gr.Row():
|
88 |
+
prompt1 = gr.Textbox(label="Enter prompt")
|
89 |
+
negative_prompt = gr.Textbox(label="Enter negative prompt (optional)", visible=False)
|
90 |
+
with gr.Row():
|
91 |
+
generate_button1 = gr.Button("Generate Image")
|
92 |
+
output_image1 = gr.Image(type="filepath", label="Generated Image")
|
93 |
+
|
94 |
+
generate_button1.click(
|
95 |
+
generate_and_save_image,
|
96 |
+
inputs=[prompt1, negative_prompt],
|
97 |
+
outputs=output_image1
|
98 |
+
)
|
99 |
+
|
100 |
+
gr.Markdown("## Refined Image Generation")
|
101 |
+
gr.Markdown("Enter a prompt to generate a refined image.")
|
102 |
+
with gr.Row():
|
103 |
+
prompt2 = gr.Textbox(label="Enter prompt for refined generation")
|
104 |
+
generate_button2 = gr.Button("Generate Refined Image")
|
105 |
+
output_image2 = gr.Image(type="filepath", label="Generated Refined Image")
|
106 |
+
|
107 |
+
generate_button2.click(
|
108 |
+
generate_image_with_refinement,
|
109 |
+
inputs=[prompt2],
|
110 |
+
outputs=output_image2
|
111 |
+
)
|
112 |
|
113 |
+
# Launch the combined Gradio app
|
114 |
+
demo.launch()
|
115 |
|