Spaces:
Running
on
Zero
Running
on
Zero
Alexander Bagus
commited on
Commit
·
f2125e1
1
Parent(s):
8cf29be
22
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ vram_config_disk_offload = {
|
|
| 31 |
}
|
| 32 |
|
| 33 |
# Load models
|
| 34 |
-
|
| 35 |
torch_dtype=torch.bfloat16,
|
| 36 |
device="cuda",
|
| 37 |
model_configs=[
|
|
@@ -58,6 +58,11 @@ pipe = QwenImagePipeline.from_pretrained(
|
|
| 58 |
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
@spaces.GPU
|
| 63 |
def generate_lora(
|
|
@@ -86,8 +91,8 @@ def generate_lora(
|
|
| 86 |
|
| 87 |
# Model inference
|
| 88 |
with torch.no_grad():
|
| 89 |
-
embs = QwenImageUnit_Image2LoRAEncode().process(
|
| 90 |
-
lora = QwenImageUnit_Image2LoRADecode().process(
|
| 91 |
|
| 92 |
lora_name = f"{ulid}.safetensors"
|
| 93 |
lora_path = f"loras/{lora_name}"
|
|
@@ -109,7 +114,27 @@ def generate_image(
|
|
| 109 |
num_inference_steps=8,
|
| 110 |
progress=gr.Progress(track_tqdm=True),
|
| 111 |
):
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
return True
|
| 114 |
|
| 115 |
|
|
@@ -156,7 +181,7 @@ with gr.Blocks() as demo:
|
|
| 156 |
with gr.Column():
|
| 157 |
lora_name = gr.Textbox(label="Generated LoRA path",lines=2, interactive=False)
|
| 158 |
lora_download = gr.DownloadButton(label=f"Download LoRA", visible=False)
|
| 159 |
-
with gr.Column(visible=False,
|
| 160 |
gr.Markdown("### After your LoRA is ready, you can try generate image here.")
|
| 161 |
with gr.Row():
|
| 162 |
with gr.Column():
|
|
@@ -183,7 +208,7 @@ with gr.Blocks() as demo:
|
|
| 183 |
minimum=1,
|
| 184 |
maximum=50,
|
| 185 |
step=1,
|
| 186 |
-
value=
|
| 187 |
)
|
| 188 |
with gr.Row():
|
| 189 |
width = gr.Slider(
|
|
@@ -244,7 +269,7 @@ with gr.Blocks() as demo:
|
|
| 244 |
guidance_scale,
|
| 245 |
num_inference_steps,
|
| 246 |
],
|
| 247 |
-
outputs=[
|
| 248 |
)
|
| 249 |
|
| 250 |
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
# Load models
|
| 34 |
+
pipe_lora = QwenImagePipeline.from_pretrained(
|
| 35 |
torch_dtype=torch.bfloat16,
|
| 36 |
device="cuda",
|
| 37 |
model_configs=[
|
|
|
|
| 58 |
vram_limit=torch.cuda.mem_get_info("cuda")[1] / (1024 ** 3) - 0.5,
|
| 59 |
)
|
| 60 |
|
| 61 |
+
pipe_imagen = QwenImagePipeline.from_pretrained(
|
| 62 |
+
"Qwen/Qwen-Image",
|
| 63 |
+
torch_dtype=DTYPE
|
| 64 |
+
).to("cuda")
|
| 65 |
+
|
| 66 |
|
| 67 |
@spaces.GPU
|
| 68 |
def generate_lora(
|
|
|
|
| 91 |
|
| 92 |
# Model inference
|
| 93 |
with torch.no_grad():
|
| 94 |
+
embs = QwenImageUnit_Image2LoRAEncode().process(pipe_lora, image2lora_images=input_images)
|
| 95 |
+
lora = QwenImageUnit_Image2LoRADecode().process(pipe_lora, **embs)["lora"]
|
| 96 |
|
| 97 |
lora_name = f"{ulid}.safetensors"
|
| 98 |
lora_path = f"loras/{lora_name}"
|
|
|
|
| 114 |
num_inference_steps=8,
|
| 115 |
progress=gr.Progress(track_tqdm=True),
|
| 116 |
):
|
| 117 |
+
lora_path = f"loras/{lora_name}"
|
| 118 |
+
pipe_imagen.load_lora(pipe_imagen.dit, lora_path)
|
| 119 |
+
|
| 120 |
+
if randomize_seed:
|
| 121 |
+
seed = random.randint(0, MAX_SEED)
|
| 122 |
+
|
| 123 |
+
generator = torch.Generator().manual_seed(seed)
|
| 124 |
+
|
| 125 |
+
image = pipe_imagen(
|
| 126 |
+
prompt=prompt,
|
| 127 |
+
negative_prompt=negative_prompt,
|
| 128 |
+
num_inference_steps=num_inference_steps,
|
| 129 |
+
width=width,
|
| 130 |
+
height=height,
|
| 131 |
+
generator=generator,
|
| 132 |
+
true_cfg_scale=guidance_scale,
|
| 133 |
+
guidance_scale=1.0 # Use a fixed default for distilled guidance
|
| 134 |
+
).images[0]
|
| 135 |
+
|
| 136 |
+
return image, seed
|
| 137 |
+
|
| 138 |
return True
|
| 139 |
|
| 140 |
|
|
|
|
| 181 |
with gr.Column():
|
| 182 |
lora_name = gr.Textbox(label="Generated LoRA path",lines=2, interactive=False)
|
| 183 |
lora_download = gr.DownloadButton(label=f"Download LoRA", visible=False)
|
| 184 |
+
with gr.Column(visible=False, elem_id='imagen-container') as imagen_container:
|
| 185 |
gr.Markdown("### After your LoRA is ready, you can try generate image here.")
|
| 186 |
with gr.Row():
|
| 187 |
with gr.Column():
|
|
|
|
| 208 |
minimum=1,
|
| 209 |
maximum=50,
|
| 210 |
step=1,
|
| 211 |
+
value=25,
|
| 212 |
)
|
| 213 |
with gr.Row():
|
| 214 |
width = gr.Slider(
|
|
|
|
| 269 |
guidance_scale,
|
| 270 |
num_inference_steps,
|
| 271 |
],
|
| 272 |
+
outputs=[output_image, seed],
|
| 273 |
)
|
| 274 |
|
| 275 |
|