scooter7 commited on
Commit
7fca79d
·
verified ·
1 Parent(s): fe1642d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -60
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
- **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)
129
 
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),
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
- with gr.Row():
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
- for color in color_attributes:
195
- with gr.Row():
196
- color_checkboxes[color]
197
- color_sliders[color]
198
-
 
 
 
199
  with gr.Accordion("Advanced options", open=False):
200
- with gr.Column():
201
- negative_prompt = gr.Text(label="Negative Prompt", placeholder="Enter a negative prompt")
202
- use_negative_prompt = gr.Checkbox(label="Use Negative Prompt", value=False)
203
- style_selection = gr.Radio(choices=STYLE_NAMES, label="Style", value=DEFAULT_STYLE_NAME)
204
- seed = gr.Number(label="Seed", value=0)
205
- width = gr.Number(label="Width", value=1024)
206
- height = gr.Number(label="Height", value=1024)
207
- inference_steps = gr.Slider(minimum=4, maximum=20, label="Inference Steps", value=4)
208
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 visible: gr.update(visible=visible),
220
  inputs=use_negative_prompt,
221
  outputs=negative_prompt,
 
222
  )
223
 
224
- # List of inputs for the generate function
225
- generate_inputs = [
226
- prompt,
227
- negative_prompt,
228
- style_selection,
229
- use_negative_prompt,
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=generate_inputs,
243
- outputs=[result, seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
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)