macgaga commited on
Commit
7543b84
·
verified ·
1 Parent(s): 436f733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -28
app.py CHANGED
@@ -283,48 +283,46 @@ def randomize_loras(selected_indices, loras_state):
283
  return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
284
 
285
  def download_loras_images(loras_json_orig: list[dict]):
286
- api = HfApi(token=HF_TOKEN)
 
 
 
287
  loras_json = []
288
 
289
  for lora in loras_json_orig:
290
  repo = lora.get("repo", None)
291
 
292
- # Standardwerte setzen
293
  lora["title"] = lora.get("title", "Unknown LoRA")
294
  lora["trigger_word"] = lora.get("trigger_word", "")
295
- lora["image"] = lora.get("image", "/home/user/app/custom.png") # Fallback
296
 
297
- if repo and api.repo_exists(repo_id=repo, token=HF_TOKEN):
298
- # Wenn das Repo existiert, prüfen, ob das Bild von dort geladen werden kann
 
299
  try:
300
- repo_image_url = f"https://huggingface.co/{repo}/resolve/main/{lora['image']}"
301
- response = requests.head(repo_image_url)
302
- if response.status_code == 200:
303
- # URL innerhalb des REPOs funktioniert
304
  lora["image"] = repo_image_url
305
  else:
306
- print(f"Repo image not found: {repo_image_url}")
307
  except Exception as e:
308
- print(f"Error checking repo image: {e}")
309
-
310
- # Prüfen auf externe URL (falls angegeben und gültig)
311
- image = lora.get("image", None)
312
- try:
313
- if image and "http" in image:
314
- # Bild-URL testen, falls extern
315
- response = requests.head(image)
316
- if response.status_code == 200:
317
- lora["image"] = image
318
  else:
319
  raise ValueError("Invalid external image URL.")
320
- else:
321
- raise ValueError("Invalid or missing image URL.")
322
- except Exception as e:
323
- # Fehler beim Bild-Download -> Standardbild verwenden
324
- print(f"Failed to download image for LoRA '{repo}': {e}")
325
  lora["image"] = "/home/user/app/custom.png"
326
 
327
- # Fügen Sie das LoRA in die Liste ein
328
  loras_json.append(lora)
329
 
330
  return loras_json
@@ -334,6 +332,7 @@ def download_loras_images(loras_json_orig: list[dict]):
334
 
335
 
336
 
 
337
  def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
338
  if custom_lora:
339
  try:
@@ -873,8 +872,15 @@ with gr.Blocks(theme='NoCrypt/miku@>=1.2.2', fill_width=True, css=css, delete_ca
873
  with gr.Row():
874
  with gr.Column():
875
  selected_info = gr.Markdown("")
876
- gallery = gr.Gallery([(item["image"], item["title"]) for item in loras], label="LoRA Gallery", allow_preview=False,
877
- columns=4, elem_id="gallery", show_share_button=False, interactive=False)
 
 
 
 
 
 
 
878
  with gr.Group():
879
  with gr.Row(elem_id="custom_lora_structure"):
880
  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)
 
283
  return selected_info_1, selected_info_2, selected_indices, lora_scale_1, lora_scale_2, lora_image_1, lora_image_2, random_prompt
284
 
285
  def download_loras_images(loras_json_orig: list[dict]):
286
+ """
287
+ Überarbeitete Funktion zur Handhabung von Vorschaubildern.
288
+ Prüft REPO-URLs zuerst, dann externe URLs, und verwendet das Platzhalterbild als Fallback.
289
+ """
290
  loras_json = []
291
 
292
  for lora in loras_json_orig:
293
  repo = lora.get("repo", None)
294
 
295
+ # Standardwerte und Fallback setzen
296
  lora["title"] = lora.get("title", "Unknown LoRA")
297
  lora["trigger_word"] = lora.get("trigger_word", "")
298
+ image_url = lora.get("image", None)
299
 
300
+ # Prüfen, ob die Bild-URL korrekt ist (vorzugsweise aus dem REPO)
301
+ if repo:
302
+ repo_image_url = f"https://huggingface.co/{repo}/resolve/main/{image_url}" if image_url else None
303
  try:
304
+ if repo_image_url and requests.head(repo_image_url).status_code == 200:
305
+ # REPO-URL verwenden, falls verfügbar
 
 
306
  lora["image"] = repo_image_url
307
  else:
308
+ raise ValueError(f"Image not found in repo: {repo_image_url}")
309
  except Exception as e:
310
+ print(f"Error loading REPO image for '{repo}': {e}")
311
+
312
+ # Fallback zu externer URL (falls angegeben und gültig)
313
+ if "http" in image_url and lora["image"] != repo_image_url:
314
+ try:
315
+ if requests.head(image_url).status_code == 200:
316
+ lora["image"] = image_url
 
 
 
317
  else:
318
  raise ValueError("Invalid external image URL.")
319
+ except Exception as e:
320
+ print(f"Failed to use external image URL for '{repo}': {e}")
321
+
322
+ # Wenn keine gültige URL gefunden -> Platzhalter
323
+ if not lora.get("image"):
324
  lora["image"] = "/home/user/app/custom.png"
325
 
 
326
  loras_json.append(lora)
327
 
328
  return loras_json
 
332
 
333
 
334
 
335
+
336
  def add_custom_lora(custom_lora, selected_indices, current_loras, gallery):
337
  if custom_lora:
338
  try:
 
872
  with gr.Row():
873
  with gr.Column():
874
  selected_info = gr.Markdown("")
875
+ gallery = gr.Gallery(
876
+ label="LoRA Gallery",
877
+ elements=[
878
+ (lora["image"], lora["title"]) for lora in loras # `loras` enthält die Bild-URLs und Titel
879
+ ],
880
+ columns=4,
881
+ interactive=False
882
+ )
883
+
884
  with gr.Group():
885
  with gr.Row(elem_id="custom_lora_structure"):
886
  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)