Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -122,24 +122,21 @@ def generate(
|
|
122 |
inference_steps: int = 4,
|
123 |
randomize_seed: bool = False,
|
124 |
use_resolution_binning: bool = True,
|
125 |
-
**
|
126 |
):
|
127 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
128 |
generator = torch.Generator().manual_seed(seed)
|
129 |
|
130 |
if not use_negative_prompt:
|
131 |
-
negative_prompt = None
|
132 |
|
133 |
-
#
|
134 |
-
color_selections = {
|
135 |
-
color: {
|
136 |
-
"selected": kwargs.get(f"{color.lower()}_selected", False),
|
137 |
-
"ratio": kwargs.get(f"{color.lower()}_ratio", 0)
|
138 |
-
} for color in color_attributes
|
139 |
-
}
|
140 |
|
|
|
141 |
prompt, negative_prompt = apply_style(style, prompt, color_selections)
|
142 |
|
|
|
143 |
try:
|
144 |
images = pipe(
|
145 |
prompt=prompt,
|
@@ -158,6 +155,7 @@ def generate(
|
|
158 |
return [], seed
|
159 |
|
160 |
image_paths = [save_image(img) for img in images]
|
|
|
161 |
return image_paths, seed
|
162 |
|
163 |
# Example prompts
|
@@ -170,42 +168,81 @@ examples = [
|
|
170 |
"an astronaut sitting in a diner, eating fries, cinematic, analog film",
|
171 |
]
|
172 |
|
173 |
-
# Initialize dictionaries for dynamic UI components
|
174 |
-
color_checkboxes = {}
|
175 |
-
color_sliders = {}
|
176 |
-
|
177 |
# Set up the Gradio interface
|
178 |
-
# Assuming the generate function and other necessary imports and initializations are already defined
|
179 |
-
|
180 |
-
# Initialize dynamic UI components for color influences
|
181 |
-
color_checkboxes = {color: gr.Checkbox(label=f"{color} Selected", value=False) for color in color_attributes}
|
182 |
-
color_sliders = {color: gr.Slider(label=f"{color} Influence Ratio", minimum=0, maximum=1, step=0.01, value=0) for color in color_attributes}
|
183 |
-
|
184 |
with gr.Blocks() as demo:
|
185 |
gr.Markdown(DESCRIPTION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
-
|
188 |
-
prompt = gr.Text(label="Prompt", placeholder="Enter your prompt")
|
189 |
-
run_button = gr.Button("Run")
|
190 |
-
|
191 |
-
result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT)
|
192 |
-
|
193 |
with gr.Accordion("Color Influences", open=False):
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
|
|
|
|
|
|
199 |
with gr.Accordion("Advanced options", open=False):
|
200 |
-
with gr.
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
|
210 |
gr.Examples(
|
211 |
examples=examples,
|
@@ -215,33 +252,40 @@ with gr.Blocks() as demo:
|
|
215 |
cache_examples=CACHE_EXAMPLES,
|
216 |
)
|
217 |
|
|
|
218 |
use_negative_prompt.change(
|
219 |
-
fn=lambda
|
220 |
inputs=use_negative_prompt,
|
221 |
outputs=negative_prompt,
|
|
|
222 |
)
|
223 |
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
seed,
|
231 |
-
width,
|
232 |
-
height,
|
233 |
-
inference_steps,
|
234 |
-
randomize_seed,
|
235 |
-
*[color_checkboxes[color] for color in color_attributes],
|
236 |
-
*[color_sliders[color] for color in color_attributes]
|
237 |
-
]
|
238 |
-
|
239 |
-
# Connect the UI with the generate function using the run_button click
|
240 |
-
run_button.click(
|
241 |
fn=generate,
|
242 |
-
inputs=
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
)
|
245 |
|
|
|
246 |
if __name__ == "__main__":
|
247 |
-
demo.launch()
|
|
|
|
|
|
122 |
inference_steps: int = 4,
|
123 |
randomize_seed: bool = False,
|
124 |
use_resolution_binning: bool = True,
|
125 |
+
**color_ratios # Collect color ratios dynamically
|
126 |
):
|
127 |
seed = int(randomize_seed_fn(seed, randomize_seed))
|
128 |
generator = torch.Generator().manual_seed(seed)
|
129 |
|
130 |
if not use_negative_prompt:
|
131 |
+
negative_prompt = None # type: ignore
|
132 |
|
133 |
+
# Process color selections and their ratios
|
134 |
+
color_selections = {color: {"selected": color_ratios.get(f"{color.lower()}_selected", False), "ratio": color_ratios.get(f"{color.lower()}_ratio", 0)} for color in color_attributes}
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
+
# Apply style and modify prompt based on color selections
|
137 |
prompt, negative_prompt = apply_style(style, prompt, color_selections)
|
138 |
|
139 |
+
# Generate images
|
140 |
try:
|
141 |
images = pipe(
|
142 |
prompt=prompt,
|
|
|
155 |
return [], seed
|
156 |
|
157 |
image_paths = [save_image(img) for img in images]
|
158 |
+
print(image_paths)
|
159 |
return image_paths, seed
|
160 |
|
161 |
# Example prompts
|
|
|
168 |
"an astronaut sitting in a diner, eating fries, cinematic, analog film",
|
169 |
]
|
170 |
|
|
|
|
|
|
|
|
|
171 |
# Set up the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
with gr.Blocks() as demo:
|
173 |
gr.Markdown(DESCRIPTION)
|
174 |
+
with gr.Row(equal_height=False):
|
175 |
+
with gr.Group():
|
176 |
+
with gr.Row():
|
177 |
+
prompt = gr.Text(
|
178 |
+
label="Prompt",
|
179 |
+
show_label=False,
|
180 |
+
max_lines=1,
|
181 |
+
placeholder="Enter your prompt",
|
182 |
+
container=False,
|
183 |
+
)
|
184 |
+
run_button = gr.Button("Run", scale=0)
|
185 |
+
result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT, show_label=False)
|
186 |
|
187 |
+
# Color selection and ratio configuration in the UI
|
|
|
|
|
|
|
|
|
|
|
188 |
with gr.Accordion("Color Influences", open=False):
|
189 |
+
with gr.Group():
|
190 |
+
color_checkboxes = {}
|
191 |
+
color_sliders = {}
|
192 |
+
for color in color_attributes:
|
193 |
+
with gr.Row():
|
194 |
+
color_checkboxes[color] = gr.Checkbox(label=f"{color} Selected", value=False)
|
195 |
+
color_sliders[color] = gr.Slider(label=f"{color} Influence Ratio", minimum=0, maximum=1, step=0.01, value=0.0)
|
196 |
+
|
197 |
with gr.Accordion("Advanced options", open=False):
|
198 |
+
with gr.Group():
|
199 |
+
with gr.Row():
|
200 |
+
use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False, visible=True)
|
201 |
+
negative_prompt = gr.Text(
|
202 |
+
label="Negative prompt",
|
203 |
+
max_lines=1,
|
204 |
+
placeholder="Enter a negative prompt",
|
205 |
+
visible=True,
|
206 |
+
)
|
207 |
+
style_selection = gr.Radio(
|
208 |
+
choices=STYLE_NAMES,
|
209 |
+
value=DEFAULT_STYLE_NAME,
|
210 |
+
label="Image Style",
|
211 |
+
show_label=True,
|
212 |
+
container=True,
|
213 |
+
interactive=True,
|
214 |
+
)
|
215 |
+
seed = gr.Slider(
|
216 |
+
label="Seed",
|
217 |
+
minimum=0,
|
218 |
+
maximum=MAX_SEED,
|
219 |
+
step=1,
|
220 |
+
value=0,
|
221 |
+
)
|
222 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
223 |
+
with gr.Row(visible=True):
|
224 |
+
width = gr.Slider(
|
225 |
+
label="Width",
|
226 |
+
minimum=256,
|
227 |
+
maximum=MAX_IMAGE_SIZE,
|
228 |
+
step=32,
|
229 |
+
value=1024,
|
230 |
+
)
|
231 |
+
height = gr.Slider(
|
232 |
+
label="Height",
|
233 |
+
minimum=256,
|
234 |
+
maximum=MAX_IMAGE_SIZE,
|
235 |
+
step=32,
|
236 |
+
value=1024,
|
237 |
+
)
|
238 |
+
with gr.Row():
|
239 |
+
inference_steps = gr.Slider(
|
240 |
+
label="Steps",
|
241 |
+
minimum=4,
|
242 |
+
maximum=20,
|
243 |
+
step=1,
|
244 |
+
value=4,
|
245 |
+
)
|
246 |
|
247 |
gr.Examples(
|
248 |
examples=examples,
|
|
|
252 |
cache_examples=CACHE_EXAMPLES,
|
253 |
)
|
254 |
|
255 |
+
# Dynamic updates based on user interactions
|
256 |
use_negative_prompt.change(
|
257 |
+
fn=lambda x: gr.update(visible=x),
|
258 |
inputs=use_negative_prompt,
|
259 |
outputs=negative_prompt,
|
260 |
+
api_name=False,
|
261 |
)
|
262 |
|
263 |
+
gr.on(
|
264 |
+
triggers=[
|
265 |
+
prompt.submit,
|
266 |
+
negative_prompt.submit,
|
267 |
+
run_button.click,
|
268 |
+
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
fn=generate,
|
270 |
+
inputs=[
|
271 |
+
prompt,
|
272 |
+
negative_prompt,
|
273 |
+
style_selection,
|
274 |
+
use_negative_prompt,
|
275 |
+
seed,
|
276 |
+
width,
|
277 |
+
height,
|
278 |
+
inference_steps,
|
279 |
+
randomize_seed,
|
280 |
+
*[color_checkboxes[color] for color in color_attributes],
|
281 |
+
*[color_sliders[color] for color in color_attributes]
|
282 |
+
],
|
283 |
+
outputs=[result, seed],
|
284 |
+
api_name="run",
|
285 |
)
|
286 |
|
287 |
+
# Launch the Gradio app
|
288 |
if __name__ == "__main__":
|
289 |
+
demo.queue(max_size=20).launch()
|
290 |
+
# Uncomment the next line to launch the server with specific options
|
291 |
+
# demo.queue(max_size=20).launch(server_name="0.0.0.0", server_port=11900, debug=True)
|