LPX55 commited on
Commit
89ce9dc
·
verified ·
1 Parent(s): 91627f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -59
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=2000,
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.Column():
150
- input_image = gr.Image(label="Input Image", type="pil")
151
- prompt = gr.Text(label="Edit Instruction", placeholder="e.g. Add a dog to the right side.")
152
- run_button = gr.Button("Edit", variant="primary")
153
- result = gr.Gallery(label="Output Images", show_label=False)
154
-
155
- with gr.Accordion("Advanced Settings", open=False):
156
- seed = gr.Slider(
157
- label="Seed",
158
- minimum=0,
159
- maximum=MAX_SEED,
160
- step=1,
161
- value=0
162
- )
163
- randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
164
-
165
- with gr.Row():
166
- true_guidance_scale = gr.Slider(
167
- label="True Guidance Scale",
168
- minimum=1.0,
169
- maximum=5.0,
170
- step=0.1,
171
- value=4.0
172
- )
173
- num_inference_steps = gr.Slider(
174
- label="Inference Steps (Fast 8-step mode)",
175
- minimum=4,
176
- maximum=8,
177
- step=1,
178
- value=8
179
- )
180
- num_images_per_prompt = gr.Slider(
181
- label="Images per Prompt",
182
- minimum=1,
183
- maximum=4,
184
- step=1,
185
- value=1
186
- )
187
-
188
- with gr.Group():
189
- rewrite_toggle = gr.Checkbox(label="Use Prompt Rewriter (Requires HF Token)", value=False, interactive=True)
190
- hf_token_input = gr.Textbox(
191
- label="Your Hugging Face Token",
192
- type="password",
193
- placeholder="hf_xxxxxxxxxxxxxxxx",
194
- visible=False,
195
- info="Required for prompt rewriting - get yours from [Hugging Face settings](https://huggingface.co/settings/tokens)"
196
- )
197
-
198
- def toggle_token_visibility(checked):
199
- return gr.update(visible=checked)
 
 
 
 
 
200
 
201
- rewrite_toggle.change(
202
- toggle_token_visibility,
203
- inputs=[rewrite_toggle],
204
- outputs=[hf_token_input]
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],