Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,121 +1,3 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import numpy as np
|
3 |
-
import random
|
4 |
-
import spaces
|
5 |
-
from diffusers import DiffusionPipeline
|
6 |
-
import torch
|
7 |
-
|
8 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
9 |
-
model_repo_id = "stabilityai/stable-diffusion-3.5-large"
|
10 |
-
|
11 |
-
if torch.cuda.is_available():
|
12 |
-
torch_dtype = torch.bfloat16
|
13 |
-
else:
|
14 |
-
torch_dtype = torch.float32
|
15 |
-
|
16 |
-
pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
|
17 |
-
pipe = pipe.to(device)
|
18 |
-
|
19 |
-
MAX_SEED = np.iinfo(np.int32).max
|
20 |
-
MAX_IMAGE_SIZE = 1024
|
21 |
-
|
22 |
-
@spaces.GPU(duration=45)
|
23 |
-
def infer(
|
24 |
-
prompt,
|
25 |
-
negative_prompt="",
|
26 |
-
seed=42,
|
27 |
-
randomize_seed=False,
|
28 |
-
width=768,
|
29 |
-
height=768,
|
30 |
-
guidance_scale=4.5,
|
31 |
-
num_inference_steps=20,
|
32 |
-
progress=gr.Progress(track_tqdm=True),
|
33 |
-
):
|
34 |
-
if randomize_seed:
|
35 |
-
seed = random.randint(0, MAX_SEED)
|
36 |
-
|
37 |
-
generator = torch.Generator().manual_seed(seed)
|
38 |
-
|
39 |
-
image = pipe(
|
40 |
-
prompt=prompt,
|
41 |
-
negative_prompt=negative_prompt,
|
42 |
-
guidance_scale=guidance_scale,
|
43 |
-
num_inference_steps=num_inference_steps,
|
44 |
-
width=width,
|
45 |
-
height=height,
|
46 |
-
generator=generator,
|
47 |
-
).images[0]
|
48 |
-
|
49 |
-
return image, seed
|
50 |
-
|
51 |
-
examples = [
|
52 |
-
"A border collie lying in some Fall leaves as the forest trees change colors",
|
53 |
-
]
|
54 |
-
|
55 |
-
# Define the custom theme with input styles
|
56 |
-
class CustomTheme(gr.themes.Base):
|
57 |
-
def __init__(self):
|
58 |
-
super().__init__()
|
59 |
-
self.primary_hue = "#5271FF"
|
60 |
-
self.background_fill_primary = "#17181B"
|
61 |
-
self.background_fill_secondary = "#17181B"
|
62 |
-
self.background_fill_tertiary = "#17181B"
|
63 |
-
self.text_color_primary = "#AEB3B8"
|
64 |
-
self.text_color_secondary = "#AEB3B8"
|
65 |
-
self.text_color_tertiary = "#AEB3B8"
|
66 |
-
self.input_background_fill = "#17181B" # Set input background color
|
67 |
-
self.input_text_color = "#AEB3B8" # Set input text color
|
68 |
-
|
69 |
-
# Custom CSS to hide the footer, set fonts, and adjust font weights
|
70 |
-
css = """
|
71 |
-
/* Hide the footer */
|
72 |
-
footer {
|
73 |
-
visibility: hidden;
|
74 |
-
height: 0;
|
75 |
-
margin: 0;
|
76 |
-
padding: 0;
|
77 |
-
overflow: hidden;
|
78 |
-
}
|
79 |
-
|
80 |
-
/* Import Google Fonts */
|
81 |
-
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&family=Montserrat:wght@500;700&display=swap');
|
82 |
-
|
83 |
-
/* Apply fonts to different elements */
|
84 |
-
body, input, button, textarea, select, .gr-button {
|
85 |
-
font-family: 'Roboto', sans-serif;
|
86 |
-
}
|
87 |
-
|
88 |
-
/* Make button text normal weight */
|
89 |
-
.generate-button, .generate-button .gr-button {
|
90 |
-
font-weight: normal !important;
|
91 |
-
}
|
92 |
-
|
93 |
-
/* Ensure headings use Montserrat */
|
94 |
-
h1, h2, h3, h4, h5, h6 {
|
95 |
-
font-family: 'Montserrat', sans-serif;
|
96 |
-
font-weight: 700;
|
97 |
-
}
|
98 |
-
|
99 |
-
/* Additional styling for sliders and checkboxes if needed */
|
100 |
-
input[type="range"]::-webkit-slider-thumb {
|
101 |
-
background: #5271FF;
|
102 |
-
}
|
103 |
-
input[type="range"]::-moz-range-thumb {
|
104 |
-
background: #5271FF;
|
105 |
-
}
|
106 |
-
input[type="range"]::-ms-thumb {
|
107 |
-
background: #5271FF;
|
108 |
-
}
|
109 |
-
input[type="checkbox"]:checked {
|
110 |
-
background-color: #5271FF;
|
111 |
-
}
|
112 |
-
|
113 |
-
/* Make Prompt text bold */
|
114 |
-
.prompt-text {
|
115 |
-
font-weight: bold;
|
116 |
-
}
|
117 |
-
"""
|
118 |
-
|
119 |
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
120 |
with gr.Column(elem_id="col-container"):
|
121 |
# Make "Prompt" bold using Markdown syntax and assign a class
|
@@ -139,62 +21,12 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
|
139 |
|
140 |
result = gr.Image(label="Result", show_label=False)
|
141 |
|
142 |
-
|
143 |
-
negative_prompt = gr.Text(
|
144 |
-
label="Negative Prompt",
|
145 |
-
max_lines=1,
|
146 |
-
placeholder="Enter a negative prompt",
|
147 |
-
visible=False,
|
148 |
-
)
|
149 |
-
|
150 |
-
seed = gr.Slider(
|
151 |
-
label="Seed",
|
152 |
-
minimum=0,
|
153 |
-
maximum=MAX_SEED,
|
154 |
-
step=1,
|
155 |
-
value=0,
|
156 |
-
)
|
157 |
-
|
158 |
-
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
159 |
-
|
160 |
-
with gr.Row():
|
161 |
-
width = gr.Slider(
|
162 |
-
label="Width",
|
163 |
-
minimum=512,
|
164 |
-
maximum=MAX_IMAGE_SIZE,
|
165 |
-
step=32,
|
166 |
-
value=1024,
|
167 |
-
)
|
168 |
-
|
169 |
-
height = gr.Slider(
|
170 |
-
label="Height",
|
171 |
-
minimum=512,
|
172 |
-
maximum=MAX_IMAGE_SIZE,
|
173 |
-
step=32,
|
174 |
-
value=1024,
|
175 |
-
)
|
176 |
-
|
177 |
-
with gr.Row():
|
178 |
-
guidance_scale = gr.Slider(
|
179 |
-
label="Guidance Scale",
|
180 |
-
minimum=0.0,
|
181 |
-
maximum=7.5,
|
182 |
-
step=0.1,
|
183 |
-
value=4.5,
|
184 |
-
)
|
185 |
-
|
186 |
-
num_inference_steps = gr.Slider(
|
187 |
-
label="Number of Inference Steps",
|
188 |
-
minimum=1,
|
189 |
-
maximum=50,
|
190 |
-
step=1,
|
191 |
-
value=40,
|
192 |
-
)
|
193 |
|
194 |
gr.Examples(
|
195 |
examples=examples,
|
196 |
inputs=[prompt],
|
197 |
-
outputs=[result
|
198 |
fn=infer,
|
199 |
cache_examples=True,
|
200 |
cache_mode="lazy"
|
@@ -203,31 +35,13 @@ with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
|
203 |
# Use click method for the button and submit for the text field
|
204 |
run_button.click(
|
205 |
fn=infer,
|
206 |
-
inputs=[
|
207 |
-
|
208 |
-
negative_prompt,
|
209 |
-
seed,
|
210 |
-
randomize_seed,
|
211 |
-
width,
|
212 |
-
height,
|
213 |
-
guidance_scale,
|
214 |
-
num_inference_steps,
|
215 |
-
],
|
216 |
-
outputs=[result, seed],
|
217 |
)
|
218 |
prompt.submit(
|
219 |
fn=infer,
|
220 |
-
inputs=[
|
221 |
-
|
222 |
-
negative_prompt,
|
223 |
-
seed,
|
224 |
-
randomize_seed,
|
225 |
-
width,
|
226 |
-
height,
|
227 |
-
guidance_scale,
|
228 |
-
num_inference_steps,
|
229 |
-
],
|
230 |
-
outputs=[result, seed],
|
231 |
)
|
232 |
|
233 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
with gr.Blocks(theme=CustomTheme(), css=css) as demo:
|
2 |
with gr.Column(elem_id="col-container"):
|
3 |
# Make "Prompt" bold using Markdown syntax and assign a class
|
|
|
21 |
|
22 |
result = gr.Image(label="Result", show_label=False)
|
23 |
|
24 |
+
# Removed Advanced Settings block
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
gr.Examples(
|
27 |
examples=examples,
|
28 |
inputs=[prompt],
|
29 |
+
outputs=[result],
|
30 |
fn=infer,
|
31 |
cache_examples=True,
|
32 |
cache_mode="lazy"
|
|
|
35 |
# Use click method for the button and submit for the text field
|
36 |
run_button.click(
|
37 |
fn=infer,
|
38 |
+
inputs=[prompt], # Only the prompt as input
|
39 |
+
outputs=[result], # Only result as output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
)
|
41 |
prompt.submit(
|
42 |
fn=infer,
|
43 |
+
inputs=[prompt], # Only the prompt as input
|
44 |
+
outputs=[result], # Only result as output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
)
|
46 |
|
47 |
if __name__ == "__main__":
|