lichorosario commited on
Commit
42f684a
·
verified ·
1 Parent(s): 6744e1f

Update Gradio app with multiple files

Browse files
Files changed (1) hide show
  1. app.py +60 -1
app.py CHANGED
@@ -172,6 +172,37 @@ def update_selection(evt: gr.SelectData, aspect_ratio):
172
  lora_repo = selected_lora["repo"]
173
  updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
174
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  # Update aspect ratio if specified in LoRA config
176
  if "aspect" in selected_lora:
177
  if selected_lora["aspect"] == "portrait":
@@ -185,7 +216,7 @@ def update_selection(evt: gr.SelectData, aspect_ratio):
185
 
186
  return (
187
  gr.update(placeholder=new_placeholder),
188
- updated_text,
189
  evt.index,
190
  aspect_ratio,
191
  )
@@ -396,6 +427,33 @@ def add_custom_lora(custom_lora):
396
  try:
397
  title, repo, path, trigger_word, image = check_custom_model(custom_lora)
398
  print(f"Loaded custom LoRA: {repo}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  card = f'''
400
  <div class="custom_lora_card">
401
  <span>Loaded custom LoRA:</span>
@@ -406,6 +464,7 @@ def add_custom_lora(custom_lora):
406
  <small>{"Using: <code><b>"+trigger_word+"</code></b> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}<br></small>
407
  </div>
408
  </div>
 
409
  </div>
410
  '''
411
  existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
 
172
  lora_repo = selected_lora["repo"]
173
  updated_text = f"### Selected: [{lora_repo}](https://huggingface.co/{lora_repo}) ✨"
174
 
175
+ # Get model card information
176
+ model_card_html = ""
177
+ try:
178
+ model_card = ModelCard.load(lora_repo)
179
+ widget_data = model_card.data.get("widget", [])
180
+ if widget_data and len(widget_data) > 0:
181
+ # Get first 4 examples from widget data
182
+ examples_html = '<div style="margin-top: 10px;">'
183
+ examples_html += '<h4 style="margin-bottom: 8px; font-size: 0.9em;">Sample Images:</h4>'
184
+ examples_html += '<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;">'
185
+
186
+ for i, example in enumerate(widget_data[:4]):
187
+ if "output" in example and "url" in example["output"]:
188
+ image_url = f"https://huggingface.co/{lora_repo}/resolve/main/{example['output']['url']}"
189
+ caption = example.get("text", f"Example {i+1}")
190
+ examples_html += f'''
191
+ <div style="text-align: center;">
192
+ <img src="{image_url}" style="width: 100%; height: auto; border-radius: 4px;" />
193
+ <p style="font-size: 0.7em; margin: 2px 0;">{caption[:30]}{'...' if len(caption) > 30 else ''}</p>
194
+ </div>
195
+ '''
196
+
197
+ examples_html += '</div></div>'
198
+ model_card_html = examples_html
199
+ except Exception as e:
200
+ print(f"Could not load model card for {lora_repo}: {e}")
201
+ model_card_html = ""
202
+
203
+ # Combine selected info with model card examples
204
+ full_info = updated_text + model_card_html
205
+
206
  # Update aspect ratio if specified in LoRA config
207
  if "aspect" in selected_lora:
208
  if selected_lora["aspect"] == "portrait":
 
216
 
217
  return (
218
  gr.update(placeholder=new_placeholder),
219
+ full_info,
220
  evt.index,
221
  aspect_ratio,
222
  )
 
427
  try:
428
  title, repo, path, trigger_word, image = check_custom_model(custom_lora)
429
  print(f"Loaded custom LoRA: {repo}")
430
+
431
+ # Get model card examples for custom LoRA
432
+ model_card_examples = ""
433
+ try:
434
+ model_card = ModelCard.load(repo)
435
+ widget_data = model_card.data.get("widget", [])
436
+ if widget_data and len(widget_data) > 0:
437
+ examples_html = '<div style="margin-top: 10px;">'
438
+ examples_html += '<h4 style="margin-bottom: 8px; font-size: 0.9em;">Sample Images:</h4>'
439
+ examples_html += '<div style="display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px;">'
440
+
441
+ for i, example in enumerate(widget_data[:4]):
442
+ if "output" in example and "url" in example["output"]:
443
+ image_url = f"https://huggingface.co/{repo}/resolve/main/{example['output']['url']}"
444
+ caption = example.get("text", f"Example {i+1}")
445
+ examples_html += f'''
446
+ <div style="text-align: center;">
447
+ <img src="{image_url}" style="width: 100%; height: auto; border-radius: 4px;" />
448
+ <p style="font-size: 0.7em; margin: 2px 0;">{caption[:30]}{'...' if len(caption) > 30 else ''}</p>
449
+ </div>
450
+ '''
451
+
452
+ examples_html += '</div></div>'
453
+ model_card_examples = examples_html
454
+ except Exception as e:
455
+ print(f"Could not load model card examples for custom LoRA: {e}")
456
+
457
  card = f'''
458
  <div class="custom_lora_card">
459
  <span>Loaded custom LoRA:</span>
 
464
  <small>{"Using: <code><b>"+trigger_word+"</code></b> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}<br></small>
465
  </div>
466
  </div>
467
+ {model_card_examples}
468
  </div>
469
  '''
470
  existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)