Spaces:
Running on Zero
Running on Zero
Update app_lora.py
Browse files- app_lora.py +84 -21
app_lora.py
CHANGED
|
@@ -687,7 +687,73 @@ def generate_image_all_latents(prompt, height, width, steps, seed, guidance_scal
|
|
| 687 |
yield placeholder, latent_gallery, LOGS
|
| 688 |
|
| 689 |
@spaces.GPU
|
| 690 |
-
def generate_image(prompt, height, width, steps, seed, guidance_scale=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
LOGS = []
|
| 692 |
device = "cuda"
|
| 693 |
cpu_device = "cpu"
|
|
@@ -975,12 +1041,12 @@ loaded_loras = {}
|
|
| 975 |
with gr.Blocks(title="Z-Image-Turbo") as demo:
|
| 976 |
gr.Markdown("# π¨ Z-Image-Turbo (LoRA-enabled UI)")
|
| 977 |
|
| 978 |
-
#
|
| 979 |
-
#
|
| 980 |
-
#
|
| 981 |
with gr.Tabs():
|
| 982 |
|
| 983 |
-
# -------- Image
|
| 984 |
with gr.TabItem("Image & Latents"):
|
| 985 |
with gr.Row():
|
| 986 |
with gr.Column(scale=1):
|
|
@@ -995,17 +1061,17 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 995 |
final_image = gr.Image(label="Final Image")
|
| 996 |
latent_gallery = gr.Gallery(label="Latent Steps", columns=4, height=256, preview=True)
|
| 997 |
|
| 998 |
-
# -------- Logs
|
| 999 |
with gr.TabItem("Logs"):
|
| 1000 |
logs_box = gr.Textbox(label="Logs", lines=25, interactive=False)
|
| 1001 |
|
| 1002 |
-
#
|
| 1003 |
-
# LoRA
|
| 1004 |
-
#
|
| 1005 |
gr.Markdown("## π§© LoRA Controls")
|
| 1006 |
|
| 1007 |
with gr.Row():
|
| 1008 |
-
lora_repo = gr.Textbox(label="LoRA Repo (HF)", value="rahul7star/ZImageLora"
|
| 1009 |
lora_file = gr.Dropdown(label="LoRA file (.safetensors)", choices=[])
|
| 1010 |
lora_strength = gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="LoRA strength")
|
| 1011 |
|
|
@@ -1014,9 +1080,9 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1014 |
apply_lora_btn = gr.Button("β
Apply LoRA")
|
| 1015 |
clear_lora_btn = gr.Button("β Clear LoRA")
|
| 1016 |
|
| 1017 |
-
#
|
| 1018 |
-
#
|
| 1019 |
-
#
|
| 1020 |
def refresh_lora_list(repo_name):
|
| 1021 |
files = list_loras_from_repo(repo_name)
|
| 1022 |
if not files:
|
|
@@ -1034,17 +1100,13 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1034 |
if not lora_filename:
|
| 1035 |
return "β οΈ No LoRA file selected"
|
| 1036 |
|
| 1037 |
-
|
| 1038 |
-
adapter_name = f"ui_lora_{lora_filename.replace('/', '_').replace('.safetensors', '')}"
|
| 1039 |
-
|
| 1040 |
try:
|
| 1041 |
-
# Only load if not already loaded
|
| 1042 |
if adapter_name not in loaded_loras:
|
| 1043 |
pipe.load_lora_weights(repo_name, weight_name=lora_filename, adapter_name=adapter_name)
|
| 1044 |
loaded_loras[adapter_name] = lora_filename
|
| 1045 |
log(f"π₯ Loaded LoRA: {lora_filename}")
|
| 1046 |
|
| 1047 |
-
# Activate adapter with given strength
|
| 1048 |
pipe.set_adapters([adapter_name], [strength])
|
| 1049 |
log(f"β
Applied LoRA adapter: {adapter_name} (strength={strength})")
|
| 1050 |
return f"LoRA applied: {lora_filename}"
|
|
@@ -1069,9 +1131,9 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1069 |
|
| 1070 |
clear_lora_btn.click(clear_lora, outputs=[logs_box])
|
| 1071 |
|
| 1072 |
-
#
|
| 1073 |
-
#
|
| 1074 |
-
#
|
| 1075 |
run_btn.click(
|
| 1076 |
generate_image,
|
| 1077 |
inputs=[prompt, height, width, steps, seed],
|
|
@@ -1079,4 +1141,5 @@ with gr.Blocks(title="Z-Image-Turbo") as demo:
|
|
| 1079 |
)
|
| 1080 |
|
| 1081 |
|
|
|
|
| 1082 |
demo.launch()
|
|
|
|
| 687 |
yield placeholder, latent_gallery, LOGS
|
| 688 |
|
| 689 |
@spaces.GPU
|
| 690 |
+
def generate_image(prompt, height, width, steps, seed, guidance_scale=7.5):
|
| 691 |
+
LOGS = []
|
| 692 |
+
device = "cuda"
|
| 693 |
+
generator = torch.Generator(device).manual_seed(int(seed))
|
| 694 |
+
|
| 695 |
+
placeholder = Image.new("RGB", (width, height), color=(255, 255, 255))
|
| 696 |
+
latent_gallery = []
|
| 697 |
+
|
| 698 |
+
try:
|
| 699 |
+
# -------------------
|
| 700 |
+
# Prepare latents
|
| 701 |
+
# -------------------
|
| 702 |
+
batch_size = 1
|
| 703 |
+
if hasattr(pipe, "vae") and hasattr(pipe.vae, "config"):
|
| 704 |
+
num_channels = pipe.vae.config.latent_channels
|
| 705 |
+
else:
|
| 706 |
+
num_channels = 4
|
| 707 |
+
|
| 708 |
+
latents = torch.randn(
|
| 709 |
+
(batch_size, num_channels, height // 8, width // 8),
|
| 710 |
+
generator=generator,
|
| 711 |
+
device=device,
|
| 712 |
+
)
|
| 713 |
+
|
| 714 |
+
# -------------------
|
| 715 |
+
# Encode prompt
|
| 716 |
+
# -------------------
|
| 717 |
+
text_embeddings = pipe._encode_prompt(prompt)
|
| 718 |
+
|
| 719 |
+
# -------------------
|
| 720 |
+
# Scheduler loop
|
| 721 |
+
# -------------------
|
| 722 |
+
num_previews = min(10, steps)
|
| 723 |
+
preview_indices = torch.linspace(0, steps - 1, num_previews).long()
|
| 724 |
+
|
| 725 |
+
for t_idx in range(steps):
|
| 726 |
+
t = pipe.scheduler.timesteps[t_idx]
|
| 727 |
+
|
| 728 |
+
with torch.no_grad():
|
| 729 |
+
latents = pipe.unet(latents, t, encoder_hidden_states=text_embeddings).sample
|
| 730 |
+
latents = pipe.scheduler.step(latents, t, latents).prev_sample
|
| 731 |
+
|
| 732 |
+
if t_idx in preview_indices:
|
| 733 |
+
try:
|
| 734 |
+
decoded = pipe.decode_latents(latents)
|
| 735 |
+
latent_gallery.append(decoded)
|
| 736 |
+
except Exception as e:
|
| 737 |
+
LOGS.append(f"β οΈ Preview decode failed: {e}")
|
| 738 |
+
latent_gallery.append(placeholder)
|
| 739 |
+
|
| 740 |
+
yield None, latent_gallery[-5:], LOGS
|
| 741 |
+
|
| 742 |
+
# -------------------
|
| 743 |
+
# Final image
|
| 744 |
+
# -------------------
|
| 745 |
+
final_img = pipe.decode_latents(latents)
|
| 746 |
+
latent_gallery.append(final_img)
|
| 747 |
+
LOGS.append("β
Pipeline generation completed successfully.")
|
| 748 |
+
yield final_img, latent_gallery[-5:] + [final_img], LOGS
|
| 749 |
+
|
| 750 |
+
except Exception as e:
|
| 751 |
+
LOGS.append(f"β Generation failed: {e}")
|
| 752 |
+
latent_gallery.append(placeholder)
|
| 753 |
+
yield placeholder, latent_gallery[-5:] + [placeholder], LOGS
|
| 754 |
+
|
| 755 |
+
|
| 756 |
+
def generate_image1(prompt, height, width, steps, seed, guidance_scale=0.0):
|
| 757 |
LOGS = []
|
| 758 |
device = "cuda"
|
| 759 |
cpu_device = "cpu"
|
|
|
|
| 1041 |
with gr.Blocks(title="Z-Image-Turbo") as demo:
|
| 1042 |
gr.Markdown("# π¨ Z-Image-Turbo (LoRA-enabled UI)")
|
| 1043 |
|
| 1044 |
+
# -------------------------
|
| 1045 |
+
# Tabs
|
| 1046 |
+
# -------------------------
|
| 1047 |
with gr.Tabs():
|
| 1048 |
|
| 1049 |
+
# -------- Image & Latents --------
|
| 1050 |
with gr.TabItem("Image & Latents"):
|
| 1051 |
with gr.Row():
|
| 1052 |
with gr.Column(scale=1):
|
|
|
|
| 1061 |
final_image = gr.Image(label="Final Image")
|
| 1062 |
latent_gallery = gr.Gallery(label="Latent Steps", columns=4, height=256, preview=True)
|
| 1063 |
|
| 1064 |
+
# -------- Logs --------
|
| 1065 |
with gr.TabItem("Logs"):
|
| 1066 |
logs_box = gr.Textbox(label="Logs", lines=25, interactive=False)
|
| 1067 |
|
| 1068 |
+
# -------------------------
|
| 1069 |
+
# LoRA Controls
|
| 1070 |
+
# -------------------------
|
| 1071 |
gr.Markdown("## π§© LoRA Controls")
|
| 1072 |
|
| 1073 |
with gr.Row():
|
| 1074 |
+
lora_repo = gr.Textbox(label="LoRA Repo (HF)", value="rahul7star/ZImageLora")
|
| 1075 |
lora_file = gr.Dropdown(label="LoRA file (.safetensors)", choices=[])
|
| 1076 |
lora_strength = gr.Slider(0.0, 2.0, value=1.0, step=0.05, label="LoRA strength")
|
| 1077 |
|
|
|
|
| 1080 |
apply_lora_btn = gr.Button("β
Apply LoRA")
|
| 1081 |
clear_lora_btn = gr.Button("β Clear LoRA")
|
| 1082 |
|
| 1083 |
+
# -------------------------
|
| 1084 |
+
# Callbacks
|
| 1085 |
+
# -------------------------
|
| 1086 |
def refresh_lora_list(repo_name):
|
| 1087 |
files = list_loras_from_repo(repo_name)
|
| 1088 |
if not files:
|
|
|
|
| 1100 |
if not lora_filename:
|
| 1101 |
return "β οΈ No LoRA file selected"
|
| 1102 |
|
| 1103 |
+
adapter_name = f"ui_lora_{lora_filename.replace('/', '_').replace('.', '_')}"
|
|
|
|
|
|
|
| 1104 |
try:
|
|
|
|
| 1105 |
if adapter_name not in loaded_loras:
|
| 1106 |
pipe.load_lora_weights(repo_name, weight_name=lora_filename, adapter_name=adapter_name)
|
| 1107 |
loaded_loras[adapter_name] = lora_filename
|
| 1108 |
log(f"π₯ Loaded LoRA: {lora_filename}")
|
| 1109 |
|
|
|
|
| 1110 |
pipe.set_adapters([adapter_name], [strength])
|
| 1111 |
log(f"β
Applied LoRA adapter: {adapter_name} (strength={strength})")
|
| 1112 |
return f"LoRA applied: {lora_filename}"
|
|
|
|
| 1131 |
|
| 1132 |
clear_lora_btn.click(clear_lora, outputs=[logs_box])
|
| 1133 |
|
| 1134 |
+
# -------------------------
|
| 1135 |
+
# Run Generation
|
| 1136 |
+
# -------------------------
|
| 1137 |
run_btn.click(
|
| 1138 |
generate_image,
|
| 1139 |
inputs=[prompt, height, width, steps, seed],
|
|
|
|
| 1141 |
)
|
| 1142 |
|
| 1143 |
|
| 1144 |
+
|
| 1145 |
demo.launch()
|