Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,10 @@ import random
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
from PIL import Image
|
|
|
|
| 7 |
from diffusers import QwenImageEditPipeline
|
|
|
|
|
|
|
| 8 |
import os
|
| 9 |
import base64
|
| 10 |
import json
|
|
@@ -41,7 +44,7 @@ def polish_prompt(original_prompt, system_prompt, hf_token):
|
|
| 41 |
completion = client.chat.completions.create(
|
| 42 |
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 43 |
messages=messages,
|
| 44 |
-
max_tokens=
|
| 45 |
)
|
| 46 |
polished_prompt = completion.choices[0].message.content
|
| 47 |
polished_prompt = polished_prompt.strip().replace("\n", " ")
|
|
@@ -92,6 +95,10 @@ pipe.load_lora_weights(
|
|
| 92 |
)
|
| 93 |
pipe.fuse_lora()
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
@spaces.GPU(duration=60)
|
| 97 |
def infer(
|
|
@@ -143,66 +150,71 @@ examples = [
|
|
| 143 |
]
|
| 144 |
|
| 145 |
with gr.Blocks() as demo:
|
| 146 |
-
gr.Markdown("# Qwen-Image-Edit with Prompt Enhancement")
|
|
|
|
| 147 |
gr.Markdown("⚠️ **Prompt rewriting requires your own [Hugging Face token](https://huggingface.co/settings/tokens)**")
|
| 148 |
-
|
| 149 |
-
with gr.
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
)
|
| 206 |
|
| 207 |
gr.on(
|
| 208 |
triggers=[run_button.click, prompt.submit],
|
|
|
|
| 4 |
import torch
|
| 5 |
import spaces
|
| 6 |
from PIL import Image
|
| 7 |
+
|
| 8 |
from diffusers import QwenImageEditPipeline
|
| 9 |
+
from diffusers.utils import is_xformers_available
|
| 10 |
+
|
| 11 |
import os
|
| 12 |
import base64
|
| 13 |
import json
|
|
|
|
| 44 |
completion = client.chat.completions.create(
|
| 45 |
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 46 |
messages=messages,
|
| 47 |
+
max_tokens=512,
|
| 48 |
)
|
| 49 |
polished_prompt = completion.choices[0].message.content
|
| 50 |
polished_prompt = polished_prompt.strip().replace("\n", " ")
|
|
|
|
| 95 |
)
|
| 96 |
pipe.fuse_lora()
|
| 97 |
|
| 98 |
+
if is_xformers_available():
|
| 99 |
+
pipe.enable_xformers_memory_efficient_attention()
|
| 100 |
+
else:
|
| 101 |
+
print("xformers not available or failed to load.")
|
| 102 |
|
| 103 |
@spaces.GPU(duration=60)
|
| 104 |
def infer(
|
|
|
|
| 150 |
]
|
| 151 |
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
+
gr.Markdown("# Qwen-Image-Edit [FAST] with HF Inference Prompt Enhancement")
|
| 154 |
+
gr.Markdown("✨ **8-step lightning inferencing with lightx2v's LoRA.**")
|
| 155 |
gr.Markdown("⚠️ **Prompt rewriting requires your own [Hugging Face token](https://huggingface.co/settings/tokens)**")
|
| 156 |
+
|
| 157 |
+
with gr.Row():
|
| 158 |
+
with gr.Column():
|
| 159 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
| 160 |
+
prompt = gr.Text(label="Edit Instruction", placeholder="e.g. Add a dog to the right side.")
|
| 161 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 162 |
+
seed = gr.Slider(
|
| 163 |
+
label="Seed",
|
| 164 |
+
minimum=0,
|
| 165 |
+
maximum=MAX_SEED,
|
| 166 |
+
step=1,
|
| 167 |
+
value=0
|
| 168 |
+
)
|
| 169 |
+
randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
|
| 170 |
+
|
| 171 |
+
with gr.Row():
|
| 172 |
+
true_guidance_scale = gr.Slider(
|
| 173 |
+
label="True Guidance Scale",
|
| 174 |
+
minimum=1.0,
|
| 175 |
+
maximum=5.0,
|
| 176 |
+
step=0.1,
|
| 177 |
+
value=4.0
|
| 178 |
+
)
|
| 179 |
+
num_inference_steps = gr.Slider(
|
| 180 |
+
label="Inference Steps (Fast 8-step mode)",
|
| 181 |
+
minimum=4,
|
| 182 |
+
maximum=16,
|
| 183 |
+
step=1,
|
| 184 |
+
value=8
|
| 185 |
+
)
|
| 186 |
+
num_images_per_prompt = gr.Slider(
|
| 187 |
+
label="Images per Prompt",
|
| 188 |
+
minimum=1,
|
| 189 |
+
maximum=4,
|
| 190 |
+
step=1,
|
| 191 |
+
value=1
|
| 192 |
+
)
|
| 193 |
+
with gr.Group():
|
| 194 |
+
rewrite_toggle = gr.Checkbox(label="Use Prompt Rewriter (Requires HF Token)", value=False, interactive=True)
|
| 195 |
+
hf_token_input = gr.Textbox(
|
| 196 |
+
label="Your Hugging Face Token",
|
| 197 |
+
type="password",
|
| 198 |
+
placeholder="hf_xxxxxxxxxxxxxxxx",
|
| 199 |
+
visible=False,
|
| 200 |
+
info="Required for prompt rewriting - get yours from [Hugging Face settings](https://huggingface.co/settings/tokens)"
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
def toggle_token_visibility(checked):
|
| 204 |
+
return gr.update(visible=checked)
|
| 205 |
+
|
| 206 |
+
rewrite_toggle.change(
|
| 207 |
+
toggle_token_visibility,
|
| 208 |
+
inputs=[rewrite_toggle],
|
| 209 |
+
outputs=[hf_token_input]
|
| 210 |
+
)
|
| 211 |
+
|
| 212 |
+
run_button = gr.Button("Edit", variant="primary")
|
| 213 |
|
| 214 |
+
with gr.Column():
|
| 215 |
+
result = gr.Gallery(label="Output Images", show_label=False)
|
| 216 |
+
|
| 217 |
+
|
|
|
|
| 218 |
|
| 219 |
gr.on(
|
| 220 |
triggers=[run_button.click, prompt.submit],
|