scooter7 commited on
Commit
03791e3
·
verified ·
1 Parent(s): a06825a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -92
app.py CHANGED
@@ -122,7 +122,7 @@ def generate(
122
  inference_steps: int = 4,
123
  randomize_seed: bool = False,
124
  use_resolution_binning: bool = True,
125
- **kwargs
126
  ):
127
  seed = int(randomize_seed_fn(seed, randomize_seed))
128
  generator = torch.Generator().manual_seed(seed)
@@ -130,6 +130,7 @@ def generate(
130
  if not use_negative_prompt:
131
  negative_prompt = None
132
 
 
133
  color_selections = {
134
  color: {
135
  "selected": kwargs.get(f"{color.lower()}_selected", False),
@@ -157,7 +158,6 @@ def generate(
157
  return [], seed
158
 
159
  image_paths = [save_image(img) for img in images]
160
- print(image_paths)
161
  return image_paths, seed
162
 
163
  # Example prompts
@@ -175,78 +175,37 @@ color_checkboxes = {}
175
  color_sliders = {}
176
 
177
  # Set up the Gradio interface
 
 
 
 
178
  with gr.Blocks() as demo:
179
  gr.Markdown(DESCRIPTION)
180
- with gr.Row(equal_height=False):
181
- with gr.Group():
182
- with gr.Row():
183
- prompt = gr.Text(
184
- label="Prompt",
185
- show_label=False,
186
- max_lines=1,
187
- placeholder="Enter your prompt",
188
- container=False,
189
- )
190
- run_button = gr.Button("Run", scale=0)
191
- result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT, show_label=False)
192
-
193
- with gr.Accordion("Color Influences", open=False):
194
- with gr.Group():
195
- for color in color_attributes:
196
- with gr.Row():
197
- color_checkboxes[color] = gr.Checkbox(label=f"{color} Selected", value=False)
198
- color_sliders[color] = gr.Slider(label=f"{color} Influence Ratio", minimum=0, maximum=1, step=0.01, value=0)
199
 
200
- with gr.Accordion("Advanced options", open=False):
201
- with gr.Group():
 
 
 
 
 
 
202
  with gr.Row():
203
- use_negative_prompt = gr.Checkbox(label="Use negative prompt", value=False, visible=True)
204
- negative_prompt = gr.Text(
205
- label="Negative prompt",
206
- max_lines=1,
207
- placeholder="Enter a negative prompt",
208
- visible=True,
209
- )
210
- style_selection = gr.Radio(
211
- show_label=True,
212
- container=True,
213
- interactive=True,
214
- choices=STYLE_NAMES,
215
- value=DEFAULT_STYLE_NAME,
216
- label="Image Style",
217
- )
218
- seed = gr.Slider(
219
- label="Seed",
220
- minimum=0,
221
- maximum=MAX_SEED,
222
- step=1,
223
- value=0,
224
- )
225
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
226
- with gr.Row(visible=True):
227
- width = gr.Slider(
228
- label="Width",
229
- minimum=256,
230
- maximum=MAX_IMAGE_SIZE,
231
- step=32,
232
- value=1024,
233
- )
234
- height = gr.Slider(
235
- label="Height",
236
- minimum=256,
237
- maximum=MAX_IMAGE_SIZE,
238
- step=32,
239
- value=1024,
240
- )
241
- with gr.Row():
242
- inference_steps = gr.Slider(
243
- label="Steps",
244
- minimum=4,
245
- maximum=20,
246
- step=1,
247
- value=4,
248
- )
249
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  gr.Examples(
251
  examples=examples,
252
  inputs=prompt,
@@ -256,34 +215,31 @@ with gr.Blocks() as demo:
256
  )
257
 
258
  use_negative_prompt.change(
259
- fn=lambda x: gr.update(visible=x),
260
  inputs=use_negative_prompt,
261
  outputs=negative_prompt,
262
- api_name=False,
263
  )
264
 
265
- gr.on(
266
- triggers=[
267
- prompt.submit,
268
- negative_prompt.submit,
269
- run_button.click,
270
- ],
 
 
 
 
 
 
 
 
 
 
 
271
  fn=generate,
272
- inputs=[
273
- prompt,
274
- negative_prompt,
275
- style_selection,
276
- use_negative_prompt,
277
- seed,
278
- width,
279
- height,
280
- inference_steps,
281
- randomize_seed,
282
- *[color_checkboxes[color] for color in color_attributes],
283
- *[color_sliders[color] for color in color_attributes],
284
- ],
285
- outputs=[result, seed],
286
- api_name="run",
287
  )
288
 
289
  if __name__ == "__main__":
 
122
  inference_steps: int = 4,
123
  randomize_seed: bool = False,
124
  use_resolution_binning: bool = True,
125
+ **kwargs # This will capture all the dynamic inputs like color selections and ratios
126
  ):
127
  seed = int(randomize_seed_fn(seed, randomize_seed))
128
  generator = torch.Generator().manual_seed(seed)
 
130
  if not use_negative_prompt:
131
  negative_prompt = None
132
 
133
+ # Extract color selections and ratios from kwargs
134
  color_selections = {
135
  color: {
136
  "selected": kwargs.get(f"{color.lower()}_selected", False),
 
158
  return [], seed
159
 
160
  image_paths = [save_image(img) for img in images]
 
161
  return image_paths, seed
162
 
163
  # Example prompts
 
175
  color_sliders = {}
176
 
177
  # Set up the Gradio interface
178
+ # Initialize color checkboxes and sliders before using them
179
+ color_checkboxes = {color: gr.Checkbox(label=f"{color} Selected", value=False) for color in color_attributes}
180
+ color_sliders = {color: gr.Slider(label=f"{color} Influence Ratio", minimum=0, maximum=1, step=0.01, value=0) for color in color_attributes}
181
+
182
  with gr.Blocks() as demo:
183
  gr.Markdown(DESCRIPTION)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
+ with gr.Row():
186
+ prompt = gr.Text(label="Prompt", placeholder="Enter your prompt")
187
+ run_button = gr.Button("Run")
188
+
189
+ result = gr.Gallery(label="Result", columns=NUM_IMAGES_PER_PROMPT)
190
+
191
+ with gr.Accordion("Color Influences", open=False):
192
+ for color in color_attributes:
193
  with gr.Row():
194
+ color_checkboxes[color]
195
+ color_sliders[color]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
 
197
+ with gr.Accordion("Advanced options", open=False):
198
+ with gr.Column():
199
+ negative_prompt = gr.Text(label="Negative Prompt", placeholder="Enter a negative prompt")
200
+ use_negative_prompt = gr.Checkbox(label="Use Negative Prompt", value=False)
201
+ style_selection = gr.Radio(choices=STYLE_NAMES, label="Style", value=DEFAULT_STYLE_NAME)
202
+ seed = gr.Number(label="Seed", value=0)
203
+ width = gr.Number(label="Width", value=1024)
204
+ height = gr.Number(label="Height", value=1024)
205
+ inference_steps = gr.Slider(minimum=4, maximum=20, label="Inference Steps", value=4)
206
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
207
+
208
+ # Set examples for predefined prompts
209
  gr.Examples(
210
  examples=examples,
211
  inputs=prompt,
 
215
  )
216
 
217
  use_negative_prompt.change(
218
+ fn=lambda visible: gr.update(visible=visible),
219
  inputs=use_negative_prompt,
220
  outputs=negative_prompt,
 
221
  )
222
 
223
+ # Define the inputs for the generate function using a dictionary for clear mapping
224
+ generate_inputs = {
225
+ "prompt": prompt,
226
+ "negative_prompt": negative_prompt,
227
+ "style": style_selection,
228
+ "use_negative_prompt": use_negative_prompt,
229
+ "seed": seed,
230
+ "width": width,
231
+ "height": height,
232
+ "inference_steps": inference_steps,
233
+ "randomize_seed": randomize_seed,
234
+ **{f"{color.lower()}_selected": color_checkboxes[color] for color in color_attributes},
235
+ **{f"{color.lower()}_ratio": color_sliders[color] for color in color_attributes}
236
+ }
237
+
238
+ # Connect the UI with the generate function
239
+ run_button.click(
240
  fn=generate,
241
+ inputs=generate_inputs,
242
+ outputs=[result, seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  )
244
 
245
  if __name__ == "__main__":