Update app.py
Browse files
app.py
CHANGED
@@ -364,63 +364,47 @@ def select_lora(selected_indices, loras_state):
|
|
364 |
|
365 |
|
366 |
def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
367 |
-
"""
|
368 |
-
Fügt eine benutzerdefinierte LoRA hinzu und aktualisiert die Galerie.
|
369 |
-
"""
|
370 |
if custom_lora:
|
371 |
try:
|
372 |
-
# Überprüfen und Informationen zur LoRA abrufen
|
373 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
380 |
-
|
381 |
-
# Prüfen, ob die LoRA bereits existiert
|
382 |
existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
|
383 |
if existing_item_index is None:
|
|
|
|
|
384 |
new_item = {
|
385 |
-
"image": image,
|
386 |
"title": title,
|
387 |
"repo": repo,
|
388 |
"weights": path,
|
389 |
"trigger_word": trigger_word
|
390 |
}
|
|
|
|
|
391 |
current_loras.append(new_item)
|
392 |
-
existing_item_index = len(current_loras) - 1
|
393 |
-
|
394 |
-
# Galerie-Elemente aktualisieren
|
395 |
-
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
396 |
|
397 |
-
#
|
|
|
|
|
398 |
if len(selected_indices) < 2:
|
399 |
selected_indices.append(existing_item_index)
|
400 |
else:
|
401 |
-
gr.Warning("You can select up to 2 LoRAs
|
402 |
-
|
403 |
-
|
404 |
-
selected_info_1 = f"### LoRA 1 Selected: {current_loras[selected_indices[0]]['title']}" if len(selected_indices) >= 1 else "Select a LoRA 1"
|
405 |
-
selected_info_2 = f"### LoRA 2 Selected: {current_loras[selected_indices[1]]['title']}" if len(selected_indices) >= 2 else "Select a LoRA 2"
|
406 |
-
|
407 |
-
return (
|
408 |
-
current_loras,
|
409 |
-
gr.update(value=gallery_items),
|
410 |
-
selected_info_1,
|
411 |
-
selected_info_2,
|
412 |
-
selected_indices,
|
413 |
-
gr.update(value=1.15),
|
414 |
-
gr.update(value=1.15),
|
415 |
-
current_loras[selected_indices[0]]['image'] if len(selected_indices) >= 1 else None,
|
416 |
-
current_loras[selected_indices[1]]['image'] if len(selected_indices) >= 2 else None
|
417 |
-
)
|
418 |
except Exception as e:
|
419 |
-
print(
|
420 |
gr.Warning(str(e))
|
421 |
-
return current_loras, gr.update()
|
422 |
else:
|
423 |
-
return current_loras, gr.update()
|
|
|
424 |
|
425 |
|
426 |
|
@@ -453,7 +437,20 @@ def update_gallery_with_loras(selected_indices, loras_state, gallery):
|
|
453 |
)
|
454 |
|
455 |
|
456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
457 |
|
458 |
|
459 |
|
|
|
364 |
|
365 |
|
366 |
def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
|
|
|
|
|
|
367 |
if custom_lora:
|
368 |
try:
|
|
|
369 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
370 |
+
if image is not None and "http" in image and not is_repo_public(repo) and repo in image:
|
371 |
+
try:
|
372 |
+
image = download_file_mod(image)
|
373 |
+
except Exception as e:
|
374 |
+
print(e)
|
375 |
+
image = None
|
376 |
+
print(f"Loaded custom LoRA: {repo}")
|
|
|
377 |
existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
|
378 |
if existing_item_index is None:
|
379 |
+
if repo.endswith(".safetensors") and repo.startswith("http"):
|
380 |
+
repo = download_file_mod(repo)
|
381 |
new_item = {
|
382 |
+
"image": image if image else get_custom_image(), # Integration der neuen Funktion
|
383 |
"title": title,
|
384 |
"repo": repo,
|
385 |
"weights": path,
|
386 |
"trigger_word": trigger_word
|
387 |
}
|
388 |
+
print(f"New LoRA: {new_item}")
|
389 |
+
existing_item_index = len(current_loras)
|
390 |
current_loras.append(new_item)
|
|
|
|
|
|
|
|
|
391 |
|
392 |
+
# Update gallery
|
393 |
+
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
394 |
+
# Update selected_indices if there's room
|
395 |
if len(selected_indices) < 2:
|
396 |
selected_indices.append(existing_item_index)
|
397 |
else:
|
398 |
+
gr.Warning("You can select up to 2 LoRAs, remove one to select a new one.")
|
399 |
+
|
400 |
+
return current_loras, gr.update(value=gallery_items)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
401 |
except Exception as e:
|
402 |
+
print(e)
|
403 |
gr.Warning(str(e))
|
404 |
+
return current_loras, gr.update()
|
405 |
else:
|
406 |
+
return current_loras, gr.update()
|
407 |
+
|
408 |
|
409 |
|
410 |
|
|
|
437 |
)
|
438 |
|
439 |
|
440 |
+
def get_custom_image():
|
441 |
+
"""
|
442 |
+
Liefert ein Bild für den Fallback.
|
443 |
+
Prüft zuerst das Repository, dann die URL, und setzt sonst ein Platzhalterbild.
|
444 |
+
"""
|
445 |
+
try:
|
446 |
+
# Prüfen, ob ein benutzerdefiniertes Bild hochgeladen wurde
|
447 |
+
# Platzhalter: "/path/to/default-placeholder.png"
|
448 |
+
placeholder = "/path/to/default-placeholder.png"
|
449 |
+
# Logik für benutzerdefiniertes Bild hinzufügen
|
450 |
+
return placeholder
|
451 |
+
except Exception as e:
|
452 |
+
print(f"Error fetching custom image: {e}")
|
453 |
+
return "/path/to/default-placeholder.png" # Sicherer Fallback
|
454 |
|
455 |
|
456 |
|