anbucur commited on
Commit
25e8e9c
·
1 Parent(s): e502d15

Refactor prompt generation logic in app.py to enhance validation and user experience

Browse files

- Introduced a helper function to validate input values, ensuring that 'None' and 'Keep Existing' selections are excluded from the prompt generation.
- Improved the construction of prompt parts for room, floor, wall, and accessories by checking for valid values before appending to the prompt.
- Streamlined the logic for adding accessories and custom text, ensuring only valid entries are included in the final prompt.
- Enhanced overall clarity and maintainability of the code by reducing redundancy and improving the structure of the prompt generation process.

Files changed (1) hide show
  1. app.py +45 -28
app.py CHANGED
@@ -383,6 +383,10 @@ def create_ui(model: DesignModel):
383
  with gr.Row(elem_classes="button-row"):
384
  submit = gr.Button("✨ Create My Design", variant="primary", size="lg")
385
 
 
 
 
 
386
  def update_prompt(room, style, colors, floor_t, floor_c, floor_p,
387
  wall_t, wall_c, wall_f, custom_text,
388
  art_en, art_col, art_size,
@@ -390,59 +394,72 @@ def create_ui(model: DesignModel):
390
  sconce_en, sconce_col, sconce_style,
391
  shelf_en, shelf_col, shelf_size,
392
  plants_en, plants_type, plants_size):
393
- # Start with basic room, style, and color scheme (these should always be included as they have valid defaults)
394
- prompt_parts = [f"Design a {style} {room.lower()} with a {colors} color scheme"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
395
 
396
- # Add floor details only if not "Keep Existing" or "None"
397
- if floor_t not in ["Keep Existing", "None"]:
398
- floor_desc = floor_t
399
- if floor_c not in ["Keep Existing", "None"]:
400
- floor_desc += f" in {floor_c}"
401
- if floor_p not in ["Keep Existing", "None"]:
402
- floor_desc += f" with {floor_p} pattern"
403
- prompt_parts.append(f"featuring {floor_desc} flooring")
404
 
405
- # Add wall details only if not "Keep Existing" or "None"
406
- if wall_t not in ["Keep Existing", "None"]:
407
- wall_desc = wall_t
408
- if wall_c not in ["Keep Existing", "None"]:
409
- wall_desc += f" in {wall_c}"
410
- if wall_f not in ["Keep Existing", "None"]:
411
- wall_desc += f" with {wall_f} finish"
412
- prompt_parts.append(f"with {wall_desc} walls")
413
 
414
- # Add accessories only if enabled AND properties are not "Keep Existing" or "None"
415
  accessories = []
416
 
417
  # Art Print
418
- if art_en and art_col not in ["Keep Existing", "None"] and art_size not in ["Keep Existing", "None"]:
419
  accessories.append(f"{art_size} {art_col} Art Print")
420
 
421
  # Mirror
422
- if mirror_en and mirror_fr not in ["Keep Existing", "None"] and mirror_size not in ["Keep Existing", "None"]:
423
  accessories.append(f"{mirror_size} Mirror with {mirror_fr} frame")
424
 
425
  # Wall Sconce
426
- if sconce_en and sconce_col not in ["Keep Existing", "None"] and sconce_style not in ["Keep Existing", "None"]:
427
  accessories.append(f"{sconce_style} {sconce_col} Wall Sconce")
428
 
429
  # Floating Shelves
430
- if shelf_en and shelf_col not in ["Keep Existing", "None"] and shelf_size not in ["Keep Existing", "None"]:
431
  accessories.append(f"{shelf_size} {shelf_col} Floating Shelves")
432
 
433
  # Wall Plants
434
- if plants_en and plants_type not in ["Keep Existing", "None"] and plants_size not in ["Keep Existing", "None"]:
435
  accessories.append(f"{plants_size} {plants_type}")
436
 
437
- # Only add accessories section if there are any accessories
438
  if accessories:
439
  prompt_parts.append("decorated with " + ", ".join(accessories))
440
 
441
- # Add custom text only if provided and non-empty
442
- if custom_text and custom_text.strip():
443
  prompt_parts.append(custom_text.strip())
444
 
445
- return ", ".join(prompt_parts)
446
 
447
  def on_submit(image, room, style, colors, floor_t, floor_c, floor_p,
448
  wall_t, wall_c, wall_f, custom_text,
 
383
  with gr.Row(elem_classes="button-row"):
384
  submit = gr.Button("✨ Create My Design", variant="primary", size="lg")
385
 
386
+ def is_valid_value(*values):
387
+ """Helper function to check if values are valid (not None or Keep Existing)"""
388
+ return all(v not in ["None", "Keep Existing"] for v in values)
389
+
390
  def update_prompt(room, style, colors, floor_t, floor_c, floor_p,
391
  wall_t, wall_c, wall_f, custom_text,
392
  art_en, art_col, art_size,
 
394
  sconce_en, sconce_col, sconce_style,
395
  shelf_en, shelf_col, shelf_size,
396
  plants_en, plants_type, plants_size):
397
+ """Generate a prompt for the design, skipping any None or Keep Existing values."""
398
+
399
+ invalid_values = {"None", "Keep Existing", None, ""}
400
+ prompt_parts = []
401
+
402
+ # Basic room settings - each part separate for maximum flexibility
403
+ if all(x not in invalid_values for x in [style, room, colors]):
404
+ base_parts = []
405
+ if style not in invalid_values:
406
+ base_parts.append(style)
407
+ if room not in invalid_values:
408
+ base_parts.append(room.lower())
409
+ if colors not in invalid_values:
410
+ base_parts.append(f"with a {colors} color scheme")
411
+ if base_parts:
412
+ prompt_parts.append("Design a " + " ".join(base_parts))
413
 
414
+ # Floor details - build up piece by piece
415
+ if floor_t not in invalid_values:
416
+ floor_parts = [floor_t]
417
+ if floor_c not in invalid_values:
418
+ floor_parts.append(f"in {floor_c}")
419
+ if floor_p not in invalid_values:
420
+ floor_parts.append(f"with {floor_p} pattern")
421
+ prompt_parts.append(f"featuring {' '.join(floor_parts)} flooring")
422
 
423
+ # Wall details - build up piece by piece
424
+ if wall_t not in invalid_values:
425
+ wall_parts = [wall_t]
426
+ if wall_c not in invalid_values:
427
+ wall_parts.append(f"in {wall_c}")
428
+ if wall_f not in invalid_values:
429
+ wall_parts.append(f"with {wall_f} finish")
430
+ prompt_parts.append(f"with {' '.join(wall_parts)} walls")
431
 
432
+ # Accessories - only add valid combinations
433
  accessories = []
434
 
435
  # Art Print
436
+ if art_en and art_col not in invalid_values and art_size not in invalid_values:
437
  accessories.append(f"{art_size} {art_col} Art Print")
438
 
439
  # Mirror
440
+ if mirror_en and mirror_fr not in invalid_values and mirror_size not in invalid_values:
441
  accessories.append(f"{mirror_size} Mirror with {mirror_fr} frame")
442
 
443
  # Wall Sconce
444
+ if sconce_en and sconce_col not in invalid_values and sconce_style not in invalid_values:
445
  accessories.append(f"{sconce_style} {sconce_col} Wall Sconce")
446
 
447
  # Floating Shelves
448
+ if shelf_en and shelf_col not in invalid_values and shelf_size not in invalid_values:
449
  accessories.append(f"{shelf_size} {shelf_col} Floating Shelves")
450
 
451
  # Wall Plants
452
+ if plants_en and plants_type not in invalid_values and plants_size not in invalid_values:
453
  accessories.append(f"{plants_size} {plants_type}")
454
 
 
455
  if accessories:
456
  prompt_parts.append("decorated with " + ", ".join(accessories))
457
 
458
+ # Add custom text if present
459
+ if custom_text and custom_text.strip() and custom_text not in invalid_values:
460
  prompt_parts.append(custom_text.strip())
461
 
462
+ return ", ".join(prompt_parts) if prompt_parts else "Please select room type, style, and color scheme"
463
 
464
  def on_submit(image, room, style, colors, floor_t, floor_c, floor_p,
465
  wall_t, wall_c, wall_f, custom_text,