prithivMLmods commited on
Commit
44ace3b
·
verified ·
1 Parent(s): f5d20f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -42
app.py CHANGED
@@ -151,29 +151,23 @@ def process_gallery_images(images):
151
 
152
  return pil_images
153
 
154
- def get_style_config(name):
155
- """Retrieve style configuration by title."""
156
  for style in LORA_STYLES:
157
  if style["title"] == name:
158
  return style
159
- return LORA_STYLES[0] # Default to None if not found
160
 
161
  def update_style_selection(evt: gr.SelectData):
162
- """Update selected style based on gallery click"""
163
  selected_style = LORA_STYLES[evt.index]
164
-
165
- # Get default prompt if available
166
  default_prompt = selected_style.get("default_prompt", None)
167
-
168
- # Return name (for State) and prompt
169
  return selected_style["title"], default_prompt if default_prompt else gr.update()
170
 
171
- def update_info_text(style_name):
172
- """Updates the info text when the style name changes (via Gallery or Example)"""
173
- if style_name == "None" or not style_name:
174
- return "### Selected: None (FLUX.2-klein-9B) ✅"
175
- else:
176
- return f"### Selected: {style_name} ✅"
177
 
178
  def get_image_count_info(images):
179
  """Return info about uploaded images"""
@@ -192,7 +186,7 @@ def get_image_count_info(images):
192
  def infer(
193
  input_images,
194
  prompt,
195
- selected_style_name,
196
  seed=42,
197
  randomize_seed=True,
198
  guidance_scale=1.0,
@@ -211,11 +205,8 @@ def infer(
211
  if not pil_images:
212
  raise gr.Error("Could not process uploaded images.")
213
 
214
- # Resolve Style Config from Name
215
- if selected_style_name is None:
216
- selected_style_name = "None"
217
-
218
- selected_style = get_style_config(selected_style_name)
219
 
220
  # Check if Face Swap is selected and validate image count
221
  if selected_style["adapter_name"] == "face-swap":
@@ -284,21 +275,17 @@ def infer(
284
 
285
  @spaces.GPU
286
  def infer_example(input_images, prompt, style_name):
287
- """Wrapper for examples to handle paths and names"""
288
  if not input_images:
289
  return None, 0
290
 
 
291
  if isinstance(input_images, str):
292
  input_images = [input_images]
293
- elif isinstance(input_images, list) and len(input_images) > 0:
294
- pass
295
- else:
296
- return None, 0
297
-
298
  image, seed = infer(
299
  input_images=input_images,
300
  prompt=prompt,
301
- selected_style_name=style_name,
302
  seed=0,
303
  randomize_seed=True,
304
  guidance_scale=1.0,
@@ -318,7 +305,8 @@ with gr.Blocks() as demo:
318
  gr.Markdown("# **FLUX.2-Klein-LoRA-Studio**", elem_id="main-title")
319
  gr.Markdown("Perform diverse image edits using specialized [LoRAs](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.2-klein-9B) adapters for the [FLUX.2-Klein-Distilled](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B) model.")
320
 
321
- selected_style_name = gr.State("None")
 
322
 
323
  with gr.Row(equal_height=True):
324
  with gr.Column():
@@ -364,17 +352,26 @@ with gr.Blocks() as demo:
364
 
365
  gr.Examples(
366
  examples=[
367
- # Example 1: None
368
- [["examples/2.jpg"], "Relight the image to remove all existing lighting conditions and replace them with neutral, uniform illumination. Apply soft, evenly distributed lighting with no directional shadows, no harsh highlights, and no dramatic contrast. Maintain the original identity of all subjects exactly—preserve facial structure, skin tone, proportions, expressions, hair, clothing, and textures. Do not alter pose, camera angle, background geometry, or image composition. Lighting should appear balanced, and studio-neutral, similar to diffuse overcast or a soft lightbox setup. Ensure consistent exposure across the entire image with realistic depth and subtle shading only where necessary for form.", "None"],
369
-
370
- # Example 2: Klein-Delight-Style
371
- [["examples/1.jpg"], "cinematic polaroid with soft grain subtle vignette gentle lighting white frame handwritten photographed by prithivMLmods preserving realistic texture and details", "Klein-Delight-Style"],
372
-
373
- # Example 3: Klein-Delight-Style (Multiple images scenario)
374
- [["examples/1.jpg", "examples/2.jpg"], "Apply the style from image 2 to image 1, preserving the subject's identity.", "Klein-Delight-Style"],
375
-
376
- # Example 4: Best-Face-Swap
377
- [["examples/face1.jpg", "examples/face2.jpg"], FACE_SWAP_PROMPT, "Best-Face-Swap"],
 
 
 
 
 
 
 
 
 
378
  ],
379
  inputs=[input_images, prompt, selected_style_name],
380
  outputs=[output_image, used_seed],
@@ -385,18 +382,21 @@ with gr.Blocks() as demo:
385
 
386
  gr.Markdown("[*](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B)This is still an experimental Space for FLUX.2-Klein-9B. More adapters will be added soon.")
387
 
 
388
  input_images.change(
389
  fn=get_image_count_info,
390
  inputs=[input_images],
391
  )
392
 
 
393
  style_gallery.select(
394
- update_style_selection,
395
  outputs=[selected_style_name, prompt]
396
  )
397
-
 
398
  selected_style_name.change(
399
- fn=update_info_text,
400
  inputs=[selected_style_name],
401
  outputs=[selected_style_info]
402
  )
 
151
 
152
  return pil_images
153
 
154
+ def get_style_by_name(name):
155
+ """Retrieve the style dictionary by its title."""
156
  for style in LORA_STYLES:
157
  if style["title"] == name:
158
  return style
159
+ return LORA_STYLES[0] # Default to None
160
 
161
  def update_style_selection(evt: gr.SelectData):
162
+ """Update selected style based on gallery click."""
163
  selected_style = LORA_STYLES[evt.index]
 
 
164
  default_prompt = selected_style.get("default_prompt", None)
165
+ # Return the title string and optional prompt update
 
166
  return selected_style["title"], default_prompt if default_prompt else gr.update()
167
 
168
+ def update_style_info(style_name):
169
+ """Update the info text based on the selected style name."""
170
+ return f"### Selected: {style_name} ✅"
 
 
 
171
 
172
  def get_image_count_info(images):
173
  """Return info about uploaded images"""
 
186
  def infer(
187
  input_images,
188
  prompt,
189
+ style_name,
190
  seed=42,
191
  randomize_seed=True,
192
  guidance_scale=1.0,
 
205
  if not pil_images:
206
  raise gr.Error("Could not process uploaded images.")
207
 
208
+ # Find the selected style configuration
209
+ selected_style = get_style_by_name(style_name)
 
 
 
210
 
211
  # Check if Face Swap is selected and validate image count
212
  if selected_style["adapter_name"] == "face-swap":
 
275
 
276
  @spaces.GPU
277
  def infer_example(input_images, prompt, style_name):
 
278
  if not input_images:
279
  return None, 0
280
 
281
+ # Handle examples where inputs might be paths
282
  if isinstance(input_images, str):
283
  input_images = [input_images]
284
+
 
 
 
 
285
  image, seed = infer(
286
  input_images=input_images,
287
  prompt=prompt,
288
+ style_name=style_name,
289
  seed=0,
290
  randomize_seed=True,
291
  guidance_scale=1.0,
 
305
  gr.Markdown("# **FLUX.2-Klein-LoRA-Studio**", elem_id="main-title")
306
  gr.Markdown("Perform diverse image edits using specialized [LoRAs](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.2-klein-9B) adapters for the [FLUX.2-Klein-Distilled](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B) model.")
307
 
308
+ # Use a hidden textbox to store the actual selected Style Name
309
+ selected_style_name = gr.Textbox(value="None", visible=False, label="Selected Style Name")
310
 
311
  with gr.Row(equal_height=True):
312
  with gr.Column():
 
352
 
353
  gr.Examples(
354
  examples=[
355
+ [
356
+ ["examples/2.jpg"],
357
+ "Relight the image to remove all existing lighting conditions and replace them with neutral, uniform illumination. Apply soft, evenly distributed lighting with no directional shadows, no harsh highlights, and no dramatic contrast. Maintain the original identity of all subjects exactly—preserve facial structure, skin tone, proportions, expressions, hair, clothing, and textures. Do not alter pose, camera angle, background geometry, or image composition. Lighting should appear balanced, and studio-neutral, similar to diffuse overcast or a soft lightbox setup. Ensure consistent exposure across the entire image with realistic depth and subtle shading only where necessary for form.",
358
+ "Klein-Delight-Style"
359
+ ],
360
+ [
361
+ ["examples/1.jpg"],
362
+ "cinematic polaroid with soft grain subtle vignette gentle lighting white frame handwritten photographed by prithivMLmods preserving realistic texture and details",
363
+ "Klein-Delight-Style"
364
+ ],
365
+ [
366
+ ["examples/1.jpg", "examples/2.jpg"],
367
+ "Apply the style from image 2 to image 1, preserving the subject's identity.",
368
+ "Klein-Delight-Style"
369
+ ],
370
+ [
371
+ ["examples/face1.jpg", "examples/face2.jpg"],
372
+ FACE_SWAP_PROMPT,
373
+ "Best-Face-Swap"
374
+ ],
375
  ],
376
  inputs=[input_images, prompt, selected_style_name],
377
  outputs=[output_image, used_seed],
 
382
 
383
  gr.Markdown("[*](https://huggingface.co/black-forest-labs/FLUX.2-klein-9B)This is still an experimental Space for FLUX.2-Klein-9B. More adapters will be added soon.")
384
 
385
+ # Update image count info when gallery changes
386
  input_images.change(
387
  fn=get_image_count_info,
388
  inputs=[input_images],
389
  )
390
 
391
+ # Update hidden selection state and prompt when gallery is clicked
392
  style_gallery.select(
393
+ fn=update_style_selection,
394
  outputs=[selected_style_name, prompt]
395
  )
396
+
397
+ # When hidden selection state changes (either via gallery click or Example click), update the Info Markdown
398
  selected_style_name.change(
399
+ fn=update_style_info,
400
  inputs=[selected_style_name],
401
  outputs=[selected_style_info]
402
  )