macgaga commited on
Commit
2120e5d
·
verified ·
1 Parent(s): 2d917e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -54
app.py CHANGED
@@ -321,17 +321,22 @@ def download_loras_images(loras_json_orig: list[dict]):
321
 
322
  return loras_json
323
 
 
324
  def handle_gallery_click(evt: gr.SelectData, loras_state):
325
  """Behandelt Klicks auf Galerie-Elemente."""
326
  selected_index = evt.index
327
  selected_lora = loras_state[selected_index]
328
 
329
- # Großansicht vorbereiten
330
- large_image = selected_lora["image"]
331
- title = selected_lora["title"]
332
 
333
  # Galerie ausblenden, Großansicht und Select-Button einblenden
334
- return gr.update(visible=False), gr.update(value=large_image, visible=True), gr.update(visible=True)
 
 
 
 
335
 
336
  def toggle_large_view(selected_indices, loras_state):
337
  """Kehrt von der Großansicht zur Galerie-Ansicht zurück."""
@@ -356,90 +361,68 @@ def toggle_large_view(selected_index, loras_state):
356
 
357
 
358
 
359
- def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
360
  """
361
- Funktion zum Hinzufügen eines benutzerdefinierten LoRA (Low-Rank Adaptation).
362
  """
363
  if custom_lora:
364
  try:
365
- # Überprüfung und Aufbereitung der Eingaben
366
  title, repo, path, trigger_word, image = check_custom_model(custom_lora)
367
-
368
- if image is not None and "http" in image and not is_repo_public(repo) and repo in image:
369
- try:
370
- # Lade das Bild herunter, wenn es eine URL ist
371
- image = download_file_mod(image)
372
- except Exception as e:
373
- print(f"Fehler beim Herunterladen des Bildes: {e}")
374
- image = None
375
-
376
- print(f"Benutzerdefinierte LoRA geladen: {repo}")
377
 
378
- # Prüfen, ob die LoRA bereits vorhanden ist
 
 
 
 
 
 
379
  existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
380
  if existing_item_index is None:
381
- # Wenn es sich um eine *.safetensors-Datei handelt und die URL korrekt ist
382
- if repo.endswith(".safetensors") and repo.startswith("http"):
383
- repo = download_file_mod(repo)
384
-
385
- # Neues LoRA-Objekt erstellen
386
  new_item = {
387
- "image": image if image else "/path/to/default-placeholder.png",
388
  "title": title,
389
  "repo": repo,
390
  "weights": path,
391
  "trigger_word": trigger_word
392
  }
393
- print(f"Neue LoRA hinzugefügt: {new_item}")
394
- existing_item_index = len(current_loras)
395
  current_loras.append(new_item)
 
396
 
397
  # Galerie-Elemente aktualisieren
398
- gallery_items = update_gallery_with_loras(current_loras)
399
-
400
- # Auswahl-Indizes aktualisieren
401
  if len(selected_indices) < 2:
402
  selected_indices.append(existing_item_index)
403
  else:
404
- gr.Warning("Du kannst maximal 2 LoRAs auswählen. Entferne eine, um eine neue hinzuzufügen.")
405
-
406
- # Ausgewählte Informationen und Bilder aktualisieren
407
- selected_info_1 = "Select a LoRA 1"
408
- selected_info_2 = "Select a LoRA 2"
409
- lora_scale_1 = 1.15
410
- lora_scale_2 = 1.15
411
- lora_image_1 = None
412
- lora_image_2 = None
413
-
414
- if len(selected_indices) >= 1:
415
- lora1 = current_loras[selected_indices[0]]
416
- selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
417
- lora_image_1 = lora1['image'] if lora1['image'] else None
418
- if len(selected_indices) >= 2:
419
- lora2 = current_loras[selected_indices[1]]
420
- selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
421
- lora_image_2 = lora2['image'] if lora2['image'] else None
422
-
423
- print("Benutzerdefinierte LoRA erfolgreich hinzugefügt.")
424
  return (
425
  current_loras,
426
  gr.update(value=gallery_items),
427
  selected_info_1,
428
  selected_info_2,
429
  selected_indices,
430
- lora_scale_1,
431
- lora_scale_2,
432
- lora_image_1,
433
- lora_image_2
434
  )
435
  except Exception as e:
436
- print(f"Fehler beim Hinzufügen der benutzerdefinierten LoRA: {e}")
 
437
  return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
438
  else:
439
  return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
440
 
441
 
442
 
 
443
  def update_gallery_with_loras(selected_indices, loras_state, gallery):
444
  """
445
  Aktualisiert die Galerie basierend auf der Auswahl. Implementiert die Vorschau-Logik.
 
321
 
322
  return loras_json
323
 
324
+
325
  def handle_gallery_click(evt: gr.SelectData, loras_state):
326
  """Behandelt Klicks auf Galerie-Elemente."""
327
  selected_index = evt.index
328
  selected_lora = loras_state[selected_index]
329
 
330
+ # Daten des ausgewählten LoRA-Elements abrufen
331
+ large_image = selected_lora.get("image", "/path/to/default-placeholder.png")
332
+ title = selected_lora.get("title", "Unknown LoRA")
333
 
334
  # Galerie ausblenden, Großansicht und Select-Button einblenden
335
+ return (
336
+ gr.update(visible=False), # Galerie ausblenden
337
+ gr.update(value=large_image, visible=True), # Großansicht anzeigen
338
+ gr.update(visible=True) # Select-Button anzeigen
339
+ )
340
 
341
  def toggle_large_view(selected_indices, loras_state):
342
  """Kehrt von der Großansicht zur Galerie-Ansicht zurück."""
 
361
 
362
 
363
 
364
+ def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
365
  """
366
+ Fügt eine benutzerdefinierte LoRA hinzu und aktualisiert die Galerie.
367
  """
368
  if custom_lora:
369
  try:
370
+ # Überprüfen und Informationen zur LoRA abrufen
371
  title, repo, path, trigger_word, image = check_custom_model(custom_lora)
 
 
 
 
 
 
 
 
 
 
372
 
373
+ # URL-Validierung und Fallback-Logik anwenden
374
+ if image and "http" in image:
375
+ image = validate_and_fetch_image_url(repo, image)
376
+ elif repo:
377
+ image = f"https://huggingface.co/{repo}/resolve/main/{image}" if image else "/path/to/default-placeholder.png"
378
+
379
+ # Prüfen, ob die LoRA bereits existiert
380
  existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
381
  if existing_item_index is None:
 
 
 
 
 
382
  new_item = {
383
+ "image": image,
384
  "title": title,
385
  "repo": repo,
386
  "weights": path,
387
  "trigger_word": trigger_word
388
  }
 
 
389
  current_loras.append(new_item)
390
+ existing_item_index = len(current_loras) - 1
391
 
392
  # Galerie-Elemente aktualisieren
393
+ gallery_items = [(item["image"], item["title"]) for item in current_loras]
394
+
395
+ # Indizes aktualisieren
396
  if len(selected_indices) < 2:
397
  selected_indices.append(existing_item_index)
398
  else:
399
+ gr.Warning("You can select up to 2 LoRAs. Remove one to select a new one.")
400
+
401
+ # Aktuelle Auswahl aktualisieren
402
+ selected_info_1 = f"### LoRA 1 Selected: {current_loras[selected_indices[0]]['title']}" if len(selected_indices) >= 1 else "Select a LoRA 1"
403
+ selected_info_2 = f"### LoRA 2 Selected: {current_loras[selected_indices[1]]['title']}" if len(selected_indices) >= 2 else "Select a LoRA 2"
404
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
405
  return (
406
  current_loras,
407
  gr.update(value=gallery_items),
408
  selected_info_1,
409
  selected_info_2,
410
  selected_indices,
411
+ gr.update(value=1.15),
412
+ gr.update(value=1.15),
413
+ current_loras[selected_indices[0]]['image'] if len(selected_indices) >= 1 else None,
414
+ current_loras[selected_indices[1]]['image'] if len(selected_indices) >= 2 else None
415
  )
416
  except Exception as e:
417
+ print(f"Error adding custom LoRA: {e}")
418
+ gr.Warning(str(e))
419
  return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
420
  else:
421
  return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
422
 
423
 
424
 
425
+
426
  def update_gallery_with_loras(selected_indices, loras_state, gallery):
427
  """
428
  Aktualisiert die Galerie basierend auf der Auswahl. Implementiert die Vorschau-Logik.