thankfulcarp commited on
Commit
d58eafe
·
1 Parent(s): 7037b66

Lora preset handling.

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py CHANGED
@@ -13,6 +13,7 @@ from huggingface_hub import hf_hub_download
13
  import numpy as np
14
  from PIL import Image
15
  import gradio as gr
 
16
  import random
17
 
18
  # --- I2V (Image-to-Video) Configuration ---
@@ -107,6 +108,59 @@ MAX_FRAMES_MODEL = 81
107
  default_prompt_i2v = "Cinematic motion, smooth animation, detailed textures, dynamic lighting, professional cinematography"
108
  default_negative_prompt = "Static image, no motion, blurred details, overexposed, underexposed, low quality, worst quality, JPEG artifacts, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, watermark, text, signature, three legs, many people in the background, walking backwards"
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  # --- Helper Functions ---
111
  def sanitize_prompt_for_filename(prompt: str, max_len: int = 60) -> str:
112
  """Sanitizes a prompt string to be used as a valid filename."""
@@ -301,6 +355,11 @@ with gr.Blocks() as demo:
301
 
302
  # --- Event Handlers ---
303
  # I2V Handlers
 
 
 
 
 
304
  i2v_input_image.upload(
305
  fn=handle_image_upload_for_dims_wan,
306
  inputs=[i2v_input_image],
 
13
  import numpy as np
14
  from PIL import Image
15
  import gradio as gr
16
+ import json
17
  import random
18
 
19
  # --- I2V (Image-to-Video) Configuration ---
 
108
  default_prompt_i2v = "Cinematic motion, smooth animation, detailed textures, dynamic lighting, professional cinematography"
109
  default_negative_prompt = "Static image, no motion, blurred details, overexposed, underexposed, low quality, worst quality, JPEG artifacts, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, watermark, text, signature, three legs, many people in the background, walking backwards"
110
 
111
+ # --- LoRA Preset Helper Functions ---
112
+ def parse_lset_prompt(lset_prompt):
113
+ """Parses a .lset prompt, resolving variables with their defaults."""
114
+ # Find all variable declarations like ! {Subject}="woman"
115
+ variables = dict(re.findall(r'! \{(\w+)\}="([^"]+)"', lset_prompt))
116
+
117
+ # Remove the declaration lines to get the clean prompt template
118
+ prompt_template = re.sub(r'! \{\w+\}="[^"]+"\n?', '', lset_prompt).strip()
119
+
120
+ # Replace placeholders with their default values
121
+ resolved_prompt = prompt_template
122
+ for key, value in variables.items():
123
+ resolved_prompt = resolved_prompt.replace(f"{{{key}}}", value)
124
+
125
+ return resolved_prompt
126
+
127
+ def handle_lora_selection_change(lora_name, current_prompt):
128
+ """
129
+ When a LoRA is selected, this function tries to find a corresponding .lset file,
130
+ parses it, and appends the generated prompt to the current prompt.
131
+ """
132
+ if not lora_name or lora_name == "None":
133
+ return gr.update() # No LoRA selected, do not change the prompt.
134
+
135
+ try:
136
+ # Construct the .lset filename from the .safetensors filename
137
+ lset_filename = os.path.splitext(lora_name)[0] + ".lset"
138
+
139
+ # Download the .lset file from the same subfolder as the LoRA
140
+ lset_path = hf_hub_download(
141
+ repo_id=I2V_LORA_REPO_ID,
142
+ filename=lset_filename,
143
+ subfolder=I2V_LORA_SUBFOLDER,
144
+ repo_type='model'
145
+ )
146
+
147
+ with open(lset_path, 'r', encoding='utf-8') as f:
148
+ lset_data = json.load(f)
149
+
150
+ lset_prompt_raw = lset_data.get("prompt")
151
+ if not lset_prompt_raw:
152
+ return gr.update()
153
+
154
+ resolved_prompt = parse_lset_prompt(lset_prompt_raw)
155
+ new_prompt = f"{current_prompt} {resolved_prompt}".strip()
156
+ gr.Info(f"✅ Appended prompt from '{lset_filename}'")
157
+ return gr.update(value=new_prompt)
158
+ except Exception as e:
159
+ # This is expected if a .lset file doesn't exist for the selected LoRA.
160
+ print(f"Info: Could not process .lset for '{lora_name}'. Reason: {e}")
161
+ gr.Info(f"ℹ️ No prompt preset found for '{lora_name}'.")
162
+ return gr.update()
163
+
164
  # --- Helper Functions ---
165
  def sanitize_prompt_for_filename(prompt: str, max_len: int = 60) -> str:
166
  """Sanitizes a prompt string to be used as a valid filename."""
 
355
 
356
  # --- Event Handlers ---
357
  # I2V Handlers
358
+ i2v_lora_name.change(
359
+ fn=handle_lora_selection_change,
360
+ inputs=[i2v_lora_name, i2v_prompt],
361
+ outputs=[i2v_prompt]
362
+ )
363
  i2v_input_image.upload(
364
  fn=handle_image_upload_for_dims_wan,
365
  inputs=[i2v_input_image],