Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,11 @@ import random
|
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
|
10 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
11 |
pipe.enable_xformers_memory_efficient_attention()
|
12 |
pipe = pipe.to(device)
|
@@ -14,34 +16,37 @@ else:
|
|
14 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
15 |
pipe = pipe.to(device)
|
16 |
|
|
|
17 |
MAX_SEED = np.iinfo(np.int32).max
|
18 |
MAX_IMAGE_SIZE = 1024
|
19 |
|
|
|
20 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
21 |
-
|
22 |
if randomize_seed:
|
23 |
seed = random.randint(0, MAX_SEED)
|
24 |
|
25 |
-
generator = torch.Generator().manual_seed(seed)
|
26 |
|
27 |
image = pipe(
|
28 |
-
prompt
|
29 |
-
negative_prompt
|
30 |
-
guidance_scale
|
31 |
-
num_inference_steps
|
32 |
-
width
|
33 |
-
height
|
34 |
-
generator
|
35 |
).images[0]
|
36 |
|
37 |
return image
|
38 |
|
|
|
39 |
examples = [
|
40 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
41 |
"An astronaut riding a green horse",
|
42 |
"A delicious ceviche cheesecake slice",
|
43 |
]
|
44 |
|
|
|
45 |
css="""
|
46 |
#col-container {
|
47 |
margin: 0 auto;
|
@@ -49,9 +54,11 @@ css="""
|
|
49 |
}
|
50 |
"""
|
51 |
|
|
|
|
|
52 |
|
|
|
53 |
with gr.Blocks(css=css) as demo:
|
54 |
-
|
55 |
with gr.Column(elem_id="col-container"):
|
56 |
gr.Markdown(f"""
|
57 |
# Text-to-Image Gradio Template
|
@@ -59,7 +66,6 @@ with gr.Blocks(css=css) as demo:
|
|
59 |
""")
|
60 |
|
61 |
with gr.Row():
|
62 |
-
|
63 |
prompt = gr.Text(
|
64 |
label="Prompt",
|
65 |
show_label=False,
|
@@ -73,12 +79,11 @@ with gr.Blocks(css=css) as demo:
|
|
73 |
result = gr.Image(label="Result", show_label=False)
|
74 |
|
75 |
with gr.Accordion("Advanced Settings", open=False):
|
76 |
-
|
77 |
negative_prompt = gr.Text(
|
78 |
label="Negative prompt",
|
79 |
max_lines=1,
|
80 |
placeholder="Enter a negative prompt",
|
81 |
-
visible=
|
82 |
)
|
83 |
|
84 |
seed = gr.Slider(
|
@@ -92,7 +97,6 @@ with gr.Blocks(css=css) as demo:
|
|
92 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
93 |
|
94 |
with gr.Row():
|
95 |
-
|
96 |
width = gr.Slider(
|
97 |
label="Width",
|
98 |
minimum=256,
|
@@ -110,32 +114,31 @@ with gr.Blocks(css=css) as demo:
|
|
110 |
)
|
111 |
|
112 |
with gr.Row():
|
113 |
-
|
114 |
guidance_scale = gr.Slider(
|
115 |
label="Guidance scale",
|
116 |
minimum=0.0,
|
117 |
maximum=10.0,
|
118 |
step=0.1,
|
119 |
-
value=
|
120 |
)
|
121 |
|
122 |
num_inference_steps = gr.Slider(
|
123 |
label="Number of inference steps",
|
124 |
minimum=1,
|
125 |
-
maximum=
|
126 |
step=1,
|
127 |
-
value=
|
128 |
)
|
129 |
|
130 |
gr.Examples(
|
131 |
-
examples
|
132 |
-
inputs
|
133 |
)
|
134 |
|
135 |
run_button.click(
|
136 |
-
fn
|
137 |
-
inputs
|
138 |
-
outputs
|
139 |
)
|
140 |
|
141 |
-
demo.launch(
|
|
|
4 |
from diffusers import DiffusionPipeline
|
5 |
import torch
|
6 |
|
7 |
+
# Check for GPU availability and set the device accordingly
|
8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
|
10 |
+
# Load the diffusion pipeline based on the availability of GPU
|
11 |
+
if device == "cuda":
|
12 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", torch_dtype=torch.float16, variant="fp16", use_safetensors=True)
|
13 |
pipe.enable_xformers_memory_efficient_attention()
|
14 |
pipe = pipe.to(device)
|
|
|
16 |
pipe = DiffusionPipeline.from_pretrained("stabilityai/sdxl-turbo", use_safetensors=True)
|
17 |
pipe = pipe.to(device)
|
18 |
|
19 |
+
# Constants
|
20 |
MAX_SEED = np.iinfo(np.int32).max
|
21 |
MAX_IMAGE_SIZE = 1024
|
22 |
|
23 |
+
# Inference function
|
24 |
def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps):
|
|
|
25 |
if randomize_seed:
|
26 |
seed = random.randint(0, MAX_SEED)
|
27 |
|
28 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
29 |
|
30 |
image = pipe(
|
31 |
+
prompt=prompt,
|
32 |
+
negative_prompt=negative_prompt,
|
33 |
+
guidance_scale=guidance_scale,
|
34 |
+
num_inference_steps=num_inference_steps,
|
35 |
+
width=width,
|
36 |
+
height=height,
|
37 |
+
generator=generator
|
38 |
).images[0]
|
39 |
|
40 |
return image
|
41 |
|
42 |
+
# Examples for the Gradio UI
|
43 |
examples = [
|
44 |
"Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
|
45 |
"An astronaut riding a green horse",
|
46 |
"A delicious ceviche cheesecake slice",
|
47 |
]
|
48 |
|
49 |
+
# CSS styling for the Gradio UI
|
50 |
css="""
|
51 |
#col-container {
|
52 |
margin: 0 auto;
|
|
|
54 |
}
|
55 |
"""
|
56 |
|
57 |
+
# Determine the power device (GPU or CPU) for display purposes
|
58 |
+
power_device = "GPU" if device == "cuda" else "CPU"
|
59 |
|
60 |
+
# Gradio UI setup
|
61 |
with gr.Blocks(css=css) as demo:
|
|
|
62 |
with gr.Column(elem_id="col-container"):
|
63 |
gr.Markdown(f"""
|
64 |
# Text-to-Image Gradio Template
|
|
|
66 |
""")
|
67 |
|
68 |
with gr.Row():
|
|
|
69 |
prompt = gr.Text(
|
70 |
label="Prompt",
|
71 |
show_label=False,
|
|
|
79 |
result = gr.Image(label="Result", show_label=False)
|
80 |
|
81 |
with gr.Accordion("Advanced Settings", open=False):
|
|
|
82 |
negative_prompt = gr.Text(
|
83 |
label="Negative prompt",
|
84 |
max_lines=1,
|
85 |
placeholder="Enter a negative prompt",
|
86 |
+
visible=True,
|
87 |
)
|
88 |
|
89 |
seed = gr.Slider(
|
|
|
97 |
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
98 |
|
99 |
with gr.Row():
|
|
|
100 |
width = gr.Slider(
|
101 |
label="Width",
|
102 |
minimum=256,
|
|
|
114 |
)
|
115 |
|
116 |
with gr.Row():
|
|
|
117 |
guidance_scale = gr.Slider(
|
118 |
label="Guidance scale",
|
119 |
minimum=0.0,
|
120 |
maximum=10.0,
|
121 |
step=0.1,
|
122 |
+
value=7.5,
|
123 |
)
|
124 |
|
125 |
num_inference_steps = gr.Slider(
|
126 |
label="Number of inference steps",
|
127 |
minimum=1,
|
128 |
+
maximum=50,
|
129 |
step=1,
|
130 |
+
value=25,
|
131 |
)
|
132 |
|
133 |
gr.Examples(
|
134 |
+
examples=examples,
|
135 |
+
inputs=[prompt]
|
136 |
)
|
137 |
|
138 |
run_button.click(
|
139 |
+
fn=infer,
|
140 |
+
inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
|
141 |
+
outputs=[result]
|
142 |
)
|
143 |
|
144 |
+
demo.queue().launch()
|