Spaces:
Running
on
Zero
Running
on
Zero
Update app_lora.py
Browse files- app_lora.py +16 -12
app_lora.py
CHANGED
|
@@ -973,6 +973,15 @@ import os
|
|
| 973 |
# -------------------------
|
| 974 |
# Helper: Recursive LoRA listing
|
| 975 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 976 |
def list_loras_from_repo(repo_id: str):
|
| 977 |
"""
|
| 978 |
List all .safetensors files in a Hugging Face repo, including subfolders.
|
|
@@ -986,9 +995,9 @@ def list_loras_from_repo(repo_id: str):
|
|
| 986 |
log(f"β Failed to list repo files: {e}")
|
| 987 |
return []
|
| 988 |
|
| 989 |
-
#
|
| 990 |
-
#
|
| 991 |
-
#
|
| 992 |
with gr.Blocks(title="Z-Image-Turbo") as demo:
|
| 993 |
gr.Markdown("# π¨ Z-Image-Turbo (LoRA-enabled UI)")
|
| 994 |
|
|
@@ -996,8 +1005,6 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 996 |
# MAIN TABS
|
| 997 |
# =========================
|
| 998 |
with gr.Tabs():
|
| 999 |
-
|
| 1000 |
-
# -------- Image Tab --------
|
| 1001 |
with gr.TabItem("Image & Latents"):
|
| 1002 |
with gr.Row():
|
| 1003 |
with gr.Column(scale=1):
|
|
@@ -1012,7 +1019,6 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1012 |
final_image = gr.Image(label="Final Image")
|
| 1013 |
latent_gallery = gr.Gallery(label="Latent Steps", columns=4, height=256, preview=True)
|
| 1014 |
|
| 1015 |
-
# -------- Logs Tab --------
|
| 1016 |
with gr.TabItem("Logs"):
|
| 1017 |
logs_box = gr.Textbox(label="Logs", lines=25, interactive=False)
|
| 1018 |
|
|
@@ -1020,7 +1026,6 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1020 |
# LoRA CONTROLS
|
| 1021 |
# =========================
|
| 1022 |
gr.Markdown("## π§© LoRA Controls")
|
| 1023 |
-
|
| 1024 |
with gr.Row():
|
| 1025 |
lora_repo = gr.Textbox(label="LoRA Repo (HF)", value="rahul7star/ZImageLora", placeholder="username/repo")
|
| 1026 |
lora_file = gr.Dropdown(label="LoRA file (.safetensors)", choices=[])
|
|
@@ -1034,9 +1039,6 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1034 |
# =========================
|
| 1035 |
# CALLBACKS
|
| 1036 |
# =========================
|
| 1037 |
-
------------------------
|
| 1038 |
-
# CALLBACKS
|
| 1039 |
-
# ------------------------
|
| 1040 |
def refresh_lora_list(repo_name):
|
| 1041 |
files = list_loras_from_repo(repo_name)
|
| 1042 |
if not files:
|
|
@@ -1047,6 +1049,8 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1047 |
|
| 1048 |
refresh_lora_btn.click(refresh_lora_list, inputs=[lora_repo], outputs=[lora_file])
|
| 1049 |
|
|
|
|
|
|
|
| 1050 |
def apply_lora(repo_name, lora_filename, strength):
|
| 1051 |
global pipe, loaded_loras
|
| 1052 |
if pipe is None:
|
|
@@ -1069,6 +1073,8 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1069 |
log(f"β Failed to apply LoRA: {e}")
|
| 1070 |
return f"Failed: {e}"
|
| 1071 |
|
|
|
|
|
|
|
| 1072 |
def clear_lora():
|
| 1073 |
global pipe
|
| 1074 |
if pipe is None:
|
|
@@ -1093,6 +1099,4 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1093 |
)
|
| 1094 |
|
| 1095 |
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
demo.launch()
|
|
|
|
| 973 |
# -------------------------
|
| 974 |
# Helper: Recursive LoRA listing
|
| 975 |
# -------------------------
|
| 976 |
+
from huggingface_hub import list_repo_files
|
| 977 |
+
import gradio as gr
|
| 978 |
+
from PIL import Image
|
| 979 |
+
|
| 980 |
+
|
| 981 |
+
|
| 982 |
+
# ----------------------------
|
| 983 |
+
# LIST LoRA FILES HELPER
|
| 984 |
+
# ----------------------------
|
| 985 |
def list_loras_from_repo(repo_id: str):
|
| 986 |
"""
|
| 987 |
List all .safetensors files in a Hugging Face repo, including subfolders.
|
|
|
|
| 995 |
log(f"β Failed to list repo files: {e}")
|
| 996 |
return []
|
| 997 |
|
| 998 |
+
# ----------------------------
|
| 999 |
+
# GRADIO UI
|
| 1000 |
+
# ----------------------------
|
| 1001 |
with gr.Blocks(title="Z-Image-Turbo") as demo:
|
| 1002 |
gr.Markdown("# π¨ Z-Image-Turbo (LoRA-enabled UI)")
|
| 1003 |
|
|
|
|
| 1005 |
# MAIN TABS
|
| 1006 |
# =========================
|
| 1007 |
with gr.Tabs():
|
|
|
|
|
|
|
| 1008 |
with gr.TabItem("Image & Latents"):
|
| 1009 |
with gr.Row():
|
| 1010 |
with gr.Column(scale=1):
|
|
|
|
| 1019 |
final_image = gr.Image(label="Final Image")
|
| 1020 |
latent_gallery = gr.Gallery(label="Latent Steps", columns=4, height=256, preview=True)
|
| 1021 |
|
|
|
|
| 1022 |
with gr.TabItem("Logs"):
|
| 1023 |
logs_box = gr.Textbox(label="Logs", lines=25, interactive=False)
|
| 1024 |
|
|
|
|
| 1026 |
# LoRA CONTROLS
|
| 1027 |
# =========================
|
| 1028 |
gr.Markdown("## π§© LoRA Controls")
|
|
|
|
| 1029 |
with gr.Row():
|
| 1030 |
lora_repo = gr.Textbox(label="LoRA Repo (HF)", value="rahul7star/ZImageLora", placeholder="username/repo")
|
| 1031 |
lora_file = gr.Dropdown(label="LoRA file (.safetensors)", choices=[])
|
|
|
|
| 1039 |
# =========================
|
| 1040 |
# CALLBACKS
|
| 1041 |
# =========================
|
|
|
|
|
|
|
|
|
|
| 1042 |
def refresh_lora_list(repo_name):
|
| 1043 |
files = list_loras_from_repo(repo_name)
|
| 1044 |
if not files:
|
|
|
|
| 1049 |
|
| 1050 |
refresh_lora_btn.click(refresh_lora_list, inputs=[lora_repo], outputs=[lora_file])
|
| 1051 |
|
| 1052 |
+
loaded_loras = {} # keep track of loaded adapters
|
| 1053 |
+
|
| 1054 |
def apply_lora(repo_name, lora_filename, strength):
|
| 1055 |
global pipe, loaded_loras
|
| 1056 |
if pipe is None:
|
|
|
|
| 1073 |
log(f"β Failed to apply LoRA: {e}")
|
| 1074 |
return f"Failed: {e}"
|
| 1075 |
|
| 1076 |
+
apply_lora_btn.click(apply_lora, inputs=[lora_repo, lora_file, lora_strength], outputs=[logs_box])
|
| 1077 |
+
|
| 1078 |
def clear_lora():
|
| 1079 |
global pipe
|
| 1080 |
if pipe is None:
|
|
|
|
| 1099 |
)
|
| 1100 |
|
| 1101 |
|
|
|
|
|
|
|
| 1102 |
demo.launch()
|