Update app.py
Browse files
app.py
CHANGED
@@ -284,7 +284,7 @@ def randomize_loras(selected_indices, loras_state):
|
|
284 |
|
285 |
def download_loras_images(loras_json_orig: list[dict]):
|
286 |
"""
|
287 |
-
Optimierte Funktion zur Handhabung von Bild-URLs aus Repositories.
|
288 |
"""
|
289 |
loras_json = []
|
290 |
|
@@ -292,73 +292,88 @@ def download_loras_images(loras_json_orig: list[dict]):
|
|
292 |
repo = lora.get("repo", None)
|
293 |
image_url = lora.get("image", None)
|
294 |
|
295 |
-
# Standardwerte
|
296 |
lora["title"] = lora.get("title", "Unknown LoRA")
|
297 |
lora["trigger_word"] = lora.get("trigger_word", "")
|
298 |
-
|
299 |
-
# Prüfen und
|
300 |
if repo and image_url:
|
301 |
if not image_url.startswith("http"):
|
302 |
-
#
|
303 |
image_url = f"https://huggingface.co/{repo}/resolve/main/{image_url}"
|
304 |
-
|
305 |
-
# Validieren
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
#
|
314 |
lora["image"] = image_url if image_url else "/path/to/default-placeholder.png"
|
315 |
loras_json.append(lora)
|
316 |
|
317 |
return loras_json
|
318 |
|
319 |
|
|
|
|
|
320 |
def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
|
|
|
|
|
|
321 |
if custom_lora:
|
322 |
try:
|
|
|
323 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
|
|
324 |
if image is not None and "http" in image and not is_repo_public(repo) and repo in image:
|
325 |
try:
|
|
|
326 |
image = download_file_mod(image)
|
327 |
except Exception as e:
|
328 |
-
print(e)
|
329 |
image = None
|
330 |
-
|
|
|
|
|
|
|
331 |
existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
|
332 |
if existing_item_index is None:
|
|
|
333 |
if repo.endswith(".safetensors") and repo.startswith("http"):
|
334 |
-
#repo = download_file(repo)
|
335 |
repo = download_file_mod(repo)
|
|
|
|
|
336 |
new_item = {
|
337 |
-
"image": image if image else "/
|
338 |
"title": title,
|
339 |
"repo": repo,
|
340 |
"weights": path,
|
341 |
"trigger_word": trigger_word
|
342 |
}
|
343 |
-
print(f"
|
344 |
existing_item_index = len(current_loras)
|
345 |
current_loras.append(new_item)
|
346 |
-
|
347 |
-
#
|
348 |
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
349 |
-
|
|
|
350 |
if len(selected_indices) < 2:
|
351 |
selected_indices.append(existing_item_index)
|
352 |
else:
|
353 |
-
gr.Warning("
|
354 |
|
355 |
-
#
|
356 |
selected_info_1 = "Select a LoRA 1"
|
357 |
selected_info_2 = "Select a LoRA 2"
|
358 |
lora_scale_1 = 1.15
|
359 |
lora_scale_2 = 1.15
|
360 |
lora_image_1 = None
|
361 |
lora_image_2 = None
|
|
|
362 |
if len(selected_indices) >= 1:
|
363 |
lora1 = current_loras[selected_indices[0]]
|
364 |
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
@@ -367,24 +382,16 @@ def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
|
367 |
lora2 = current_loras[selected_indices[1]]
|
368 |
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
369 |
lora_image_2 = lora2['image'] if lora2['image'] else None
|
370 |
-
|
|
|
371 |
return (
|
372 |
current_loras,
|
373 |
gr.update(value=gallery_items),
|
374 |
-
selected_info_1,
|
375 |
selected_info_2,
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
lora_image_1,
|
380 |
-
lora_image_2
|
381 |
-
)
|
382 |
-
except Exception as e:
|
383 |
-
print(e)
|
384 |
-
gr.Warning(str(e))
|
385 |
-
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
386 |
-
else:
|
387 |
-
return current_loras, gr.update(), gr.update(), gr.update(), selected_indices, gr.update(), gr.update(), gr.update(), gr.update()
|
388 |
|
389 |
def remove_custom_lora(selected_indices, current_loras, gallery):
|
390 |
if current_loras:
|
|
|
284 |
|
285 |
def download_loras_images(loras_json_orig: list[dict]):
|
286 |
"""
|
287 |
+
Optimierte Funktion zur Handhabung und Korrektur von Bild-URLs aus Repositories.
|
288 |
"""
|
289 |
loras_json = []
|
290 |
|
|
|
292 |
repo = lora.get("repo", None)
|
293 |
image_url = lora.get("image", None)
|
294 |
|
295 |
+
# Standardwerte setzen
|
296 |
lora["title"] = lora.get("title", "Unknown LoRA")
|
297 |
lora["trigger_word"] = lora.get("trigger_word", "")
|
298 |
+
|
299 |
+
# Prüfen und korrigieren der Bild-URL
|
300 |
if repo and image_url:
|
301 |
if not image_url.startswith("http"):
|
302 |
+
# Korrekte URL aus Repo und Bildnamen erstellen
|
303 |
image_url = f"https://huggingface.co/{repo}/resolve/main/{image_url}"
|
304 |
+
try:
|
305 |
+
# Validieren, ob die URL erreichbar ist
|
306 |
+
response = requests.head(image_url)
|
307 |
+
if response.status_code != 200:
|
308 |
+
raise ValueError(f"Image URL not reachable: {image_url}")
|
309 |
+
except Exception as e:
|
310 |
+
print(f"Error validating image URL: {e}")
|
311 |
+
image_url = None # Fallback bei ungültiger URL
|
312 |
+
|
313 |
+
# Platzhalter verwenden, falls kein gültiges Bild verfügbar ist
|
314 |
lora["image"] = image_url if image_url else "/path/to/default-placeholder.png"
|
315 |
loras_json.append(lora)
|
316 |
|
317 |
return loras_json
|
318 |
|
319 |
|
320 |
+
|
321 |
+
|
322 |
def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
|
323 |
+
"""
|
324 |
+
Funktion zum Hinzufügen eines benutzerdefinierten LoRA (Low-Rank Adaptation).
|
325 |
+
"""
|
326 |
if custom_lora:
|
327 |
try:
|
328 |
+
# Überprüfung und Aufbereitung der Eingaben
|
329 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
330 |
+
|
331 |
if image is not None and "http" in image and not is_repo_public(repo) and repo in image:
|
332 |
try:
|
333 |
+
# Lade das Bild herunter, wenn es eine URL ist
|
334 |
image = download_file_mod(image)
|
335 |
except Exception as e:
|
336 |
+
print(f"Fehler beim Herunterladen des Bildes: {e}")
|
337 |
image = None
|
338 |
+
|
339 |
+
print(f"Benutzerdefinierte LoRA geladen: {repo}")
|
340 |
+
|
341 |
+
# Prüfen, ob die LoRA bereits vorhanden ist
|
342 |
existing_item_index = next((index for (index, item) in enumerate(current_loras) if item['repo'] == repo), None)
|
343 |
if existing_item_index is None:
|
344 |
+
# Wenn es sich um eine *.safetensors-Datei handelt und die URL korrekt ist
|
345 |
if repo.endswith(".safetensors") and repo.startswith("http"):
|
|
|
346 |
repo = download_file_mod(repo)
|
347 |
+
|
348 |
+
# Neues LoRA-Objekt erstellen
|
349 |
new_item = {
|
350 |
+
"image": image if image else "/path/to/default-placeholder.png",
|
351 |
"title": title,
|
352 |
"repo": repo,
|
353 |
"weights": path,
|
354 |
"trigger_word": trigger_word
|
355 |
}
|
356 |
+
print(f"Neue LoRA hinzugefügt: {new_item}")
|
357 |
existing_item_index = len(current_loras)
|
358 |
current_loras.append(new_item)
|
359 |
+
|
360 |
+
# Galerie-Elemente aktualisieren
|
361 |
gallery_items = [(item["image"], item["title"]) for item in current_loras]
|
362 |
+
|
363 |
+
# Auswahl-Indizes aktualisieren
|
364 |
if len(selected_indices) < 2:
|
365 |
selected_indices.append(existing_item_index)
|
366 |
else:
|
367 |
+
gr.Warning("Du kannst maximal 2 LoRAs auswählen. Entferne eine, um eine neue hinzuzufügen.")
|
368 |
|
369 |
+
# Ausgewählte Informationen und Bilder aktualisieren
|
370 |
selected_info_1 = "Select a LoRA 1"
|
371 |
selected_info_2 = "Select a LoRA 2"
|
372 |
lora_scale_1 = 1.15
|
373 |
lora_scale_2 = 1.15
|
374 |
lora_image_1 = None
|
375 |
lora_image_2 = None
|
376 |
+
|
377 |
if len(selected_indices) >= 1:
|
378 |
lora1 = current_loras[selected_indices[0]]
|
379 |
selected_info_1 = f"### LoRA 1 Selected: {lora1['title']} ✨"
|
|
|
382 |
lora2 = current_loras[selected_indices[1]]
|
383 |
selected_info_2 = f"### LoRA 2 Selected: {lora2['title']} ✨"
|
384 |
lora_image_2 = lora2['image'] if lora2['image'] else None
|
385 |
+
|
386 |
+
print("Benutzerdefinierte LoRA erfolgreich hinzugefügt.")
|
387 |
return (
|
388 |
current_loras,
|
389 |
gr.update(value=gallery_items),
|
390 |
+
selected_info_1,
|
391 |
selected_info_2,
|
392 |
+
sele
|
393 |
+
|
394 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
395 |
|
396 |
def remove_custom_lora(selected_indices, current_loras, gallery):
|
397 |
if current_loras:
|