macgaga commited on
Commit
27d5614
·
verified ·
1 Parent(s): fa58599

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -20
app.py CHANGED
@@ -284,8 +284,9 @@ def randomize_loras(selected_indices, loras_state):
284
 
285
  def download_loras_images(loras_json_orig: list[dict]):
286
  """
287
- Überarbeitete Funktion zur Verarbeitung von Bild-URLs aus JSON-Daten und Repositories.
288
  """
 
289
  loras_json = []
290
 
291
  for lora in loras_json_orig:
@@ -295,30 +296,72 @@ def download_loras_images(loras_json_orig: list[dict]):
295
  # Standardwerte und Fallbacks
296
  lora["title"] = lora.get("title", "Unknown LoRA")
297
  lora["trigger_word"] = lora.get("trigger_word", "")
 
298
 
299
- # URL-Validierung und Korrektur
300
- if repo and image_url:
301
- if not image_url.startswith("http"):
302
- # URL für Bilder aus Repos korrekt erstellen
303
- image_url = f"https://huggingface.co/{repo}/resolve/main/{image_url}"
304
  try:
305
- response = requests.head(image_url)
306
- if response.status_code != 200:
307
- raise ValueError(f"Bild-URL nicht erreichbar: {image_url}")
308
  except Exception as e:
309
- print(f"Fehler beim Laden des Bildes: {e}")
310
- image_url = None
311
- else:
312
- # Falls keine Bild-URL oder Repo angegeben, Platzhalter verwenden
313
- image_url = "/path/to/default-placeholder.png"
 
 
 
 
314
 
315
- # Finales Bild in das JSON-Objekt einfügen
316
- lora["image"] = image_url
317
  loras_json.append(lora)
318
 
319
  return loras_json
320
 
321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
322
 
323
 
324
 
@@ -908,15 +951,49 @@ with gr.Blocks(theme='NoCrypt/miku@>=1.2.2', fill_width=True, css=css, delete_ca
908
  with gr.Row():
909
  with gr.Column():
910
  selected_info = gr.Markdown("")
 
911
  gallery = gr.Gallery(
912
  label="LoRA Gallery",
913
- value=[
914
- (lora["image"], lora["title"]) for lora in loras
915
- ],
916
  columns=4,
917
- interactive=False
 
 
 
 
 
 
 
 
 
 
 
 
 
918
  )
919
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
920
  with gr.Group():
921
  with gr.Row(elem_id="custom_lora_structure"):
922
  custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path or *.safetensors public URL", placeholder="multimodalart/vintage-ads-flux", scale=3, min_width=150)
 
284
 
285
  def download_loras_images(loras_json_orig: list[dict]):
286
  """
287
+ Optimierte Funktion zur Handhabung von Bild-URLs aus Repositories mit Fallback-Logik.
288
  """
289
+ default_placeholder = "/path/to/default-placeholder.png" # Platzhalterbild für fehlende Bilder
290
  loras_json = []
291
 
292
  for lora in loras_json_orig:
 
296
  # Standardwerte und Fallbacks
297
  lora["title"] = lora.get("title", "Unknown LoRA")
298
  lora["trigger_word"] = lora.get("trigger_word", "")
299
+ resolved_image_url = None
300
 
301
+ # 1. Prüfen und Laden des Repository-Bildes
302
+ if repo:
303
+ repo_image_url = f"https://huggingface.co/{repo}/resolve/main/{image_url}" if image_url else None
 
 
304
  try:
305
+ if repo_image_url and requests.head(repo_image_url).status_code == 200:
306
+ resolved_image_url = repo_image_url
 
307
  except Exception as e:
308
+ print(f"Fehler beim Laden des Repo-Bildes: {repo_image_url}: {e}")
309
+
310
+ # 2. Fallback: Laden des Bildes aus der JSON-URL (Hotlink)
311
+ if not resolved_image_url and image_url:
312
+ try:
313
+ if requests.head(image_url).status_code == 200:
314
+ resolved_image_url = image_url
315
+ except Exception as e:
316
+ print(f"Fehler beim Laden des Hotlink-Bildes: {image_url}: {e}")
317
 
318
+ # 3. Fallback: Platzhalterbild verwenden
319
+ lora["image"] = resolved_image_url if resolved_image_url else default_placeholder
320
  loras_json.append(lora)
321
 
322
  return loras_json
323
 
324
 
325
+ def handle_gallery_click(selected_index, loras_state):
326
+ """
327
+ Handhabt den Klick auf ein Bild in der Galerie.
328
+ """
329
+ if selected_index is None:
330
+ # Kein Bild ausgewählt, zurück zur Galerie-Ansicht
331
+ return (
332
+ gr.update(visible=True), # Galerie sichtbar
333
+ gr.update(visible=False), # Großes Bild unsichtbar
334
+ None, # Keine LoRA ausgewählt
335
+ None # Keine LoRA-Skalierung
336
+ )
337
+
338
+ # LoRA-Daten für das ausgewählte Bild
339
+ selected_lora = loras_state[selected_index]
340
+ large_view_image = selected_lora["image"]
341
+ lora_title = selected_lora["title"]
342
+
343
+ return (
344
+ gr.update(visible=False), # Galerie unsichtbar
345
+ gr.update(visible=True, value=large_view_image), # Großes Bild sichtbar
346
+ selected_index, # Ausgewählter Index
347
+ lora_title # Titel des LoRA
348
+ )
349
+
350
+
351
+ def toggle_large_view(selected_index, loras_state):
352
+ """
353
+ Schaltet zwischen Galerie-Ansicht und Großansicht um.
354
+ """
355
+ if selected_index is not None:
356
+ # Wechsel zur Galerie-Ansicht
357
+ return gr.update(visible=True), gr.update(visible=False)
358
+ else:
359
+ # Wechsel zur Großansicht
360
+ selected_lora = loras_state[selected_index]
361
+ large_view_image = selected_lora["image"]
362
+ return gr.update(visible=False), gr.update(visible=True, value=large_view_image)
363
+
364
+
365
 
366
 
367
 
 
951
  with gr.Row():
952
  with gr.Column():
953
  selected_info = gr.Markdown("")
954
+ # Galerie-Komponente
955
  gallery = gr.Gallery(
956
  label="LoRA Gallery",
957
+ value=[(lora["image"], lora["title"]) for lora in loras], # Loras initial
 
 
958
  columns=4,
959
+ interactive=True # Galerie interaktiv
960
+ )
961
+
962
+ # Großansicht für das ausgewählte Bild
963
+ large_view = gr.Image(
964
+ label="Selected Image",
965
+ visible=False, # Standardmäßig nicht sichtbar
966
+ interactive=False # Keine Interaktivität
967
+ )
968
+
969
+ # Select-Button, um das ausgewählte Bild zu übernehmen
970
+ select_button = gr.Button(
971
+ "Select",
972
+ visible=False # Nur sichtbar, wenn ein Bild ausgewählt ist
973
  )
974
 
975
+ # Event-Handler: Klick auf ein Galerie-Bild
976
+ gallery.select(
977
+ handle_gallery_click, # Funktion zum Verarbeiten des Galerie-Klicks
978
+ inputs=[gallery, loras_state], # Eingabe: Galerie-Index und State
979
+ outputs=[gallery, large_view, select_button] # Ausgabe: Galerie, Großansicht, Button
980
+ )
981
+
982
+ # Event-Handler: Klick auf den Select-Button
983
+ select_button.click(
984
+ add_custom_lora, # Vorhandene Funktion zur LoRA-Auswahl
985
+ inputs=[selected_indices, loras_state],
986
+ outputs=[gallery, large_view, select_button]
987
+ )
988
+
989
+ # Event-Handler: Klick auf das große Bild zum Zurückwechseln
990
+ large_view.click(
991
+ toggle_large_view, # Funktion zum Umschalten der Ansicht
992
+ inputs=[selected_indices, loras_state],
993
+ outputs=[gallery, large_view]
994
+ )
995
+
996
+
997
  with gr.Group():
998
  with gr.Row(elem_id="custom_lora_structure"):
999
  custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path or *.safetensors public URL", placeholder="multimodalart/vintage-ads-flux", scale=3, min_width=150)