Spaces:
Runtime error
Runtime error
erwold
commited on
Commit
·
00a1ccb
1
Parent(s):
af7a5be
Initial Commit
Browse files
app.py
CHANGED
@@ -87,25 +87,9 @@ class FluxInterface:
|
|
87 |
vae=vae,
|
88 |
text_encoder=text_encoder,
|
89 |
tokenizer=tokenizer,
|
90 |
-
)
|
91 |
-
|
92 |
-
def resize_image(self, img, max_pixels=1050000):
|
93 |
-
if not isinstance(img, Image.Image):
|
94 |
-
img = Image.fromarray(img)
|
95 |
-
|
96 |
-
width, height = img.size
|
97 |
-
num_pixels = width * height
|
98 |
-
|
99 |
-
if num_pixels > max_pixels:
|
100 |
-
scale = math.sqrt(max_pixels / num_pixels)
|
101 |
-
new_width = int(width * scale)
|
102 |
-
new_height = int(height * scale)
|
103 |
-
new_width = new_width - (new_width % 8)
|
104 |
-
new_height = new_height - (new_height % 8)
|
105 |
-
img = img.resize((new_width, new_height), Image.LANCZOS)
|
106 |
-
|
107 |
-
return img
|
108 |
|
|
|
109 |
def process_image(self, image):
|
110 |
message = [
|
111 |
{
|
@@ -125,8 +109,8 @@ class FluxInterface:
|
|
125 |
image_hidden_state = self.models['connector'](image_hidden_state)
|
126 |
|
127 |
return image_hidden_state, image_grid_thw
|
128 |
-
|
129 |
-
def
|
130 |
"""Compute T5 embeddings for text prompt"""
|
131 |
if prompt == "":
|
132 |
return None
|
@@ -145,24 +129,6 @@ class FluxInterface:
|
|
145 |
|
146 |
return prompt_embeds
|
147 |
|
148 |
-
def compute_text_embeddings(self, prompt=""):
|
149 |
-
with torch.no_grad():
|
150 |
-
text_inputs = self.models['tokenizer'](
|
151 |
-
prompt,
|
152 |
-
padding="max_length",
|
153 |
-
max_length=77,
|
154 |
-
truncation=True,
|
155 |
-
return_tensors="pt"
|
156 |
-
).to(self.device)
|
157 |
-
|
158 |
-
prompt_embeds = self.models['text_encoder'](
|
159 |
-
text_inputs.input_ids,
|
160 |
-
output_hidden_states=False
|
161 |
-
)
|
162 |
-
pooled_prompt_embeds = prompt_embeds.pooler_output.to(self.dtype)
|
163 |
-
|
164 |
-
return pooled_prompt_embeds
|
165 |
-
|
166 |
def generate(self, input_image, prompt="", guidance_scale=3.5, num_inference_steps=28, num_images=2, seed=None):
|
167 |
try:
|
168 |
if seed is not None:
|
@@ -197,7 +163,7 @@ class FluxInterface:
|
|
197 |
interface = FluxInterface()
|
198 |
|
199 |
# Create Gradio interface
|
200 |
-
with gr.Blocks(
|
201 |
gr.Markdown("""
|
202 |
# 🎨 Qwen2vl-Flux Image Variation Demo
|
203 |
Upload an image and get AI-generated variations. You can optionally add a text prompt to guide the generation.
|
@@ -208,80 +174,50 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
208 |
input_image = gr.Image(
|
209 |
label="Upload Image",
|
210 |
type="pil",
|
211 |
-
height=384
|
212 |
-
width=384,
|
213 |
-
tool="select"
|
214 |
)
|
215 |
prompt = gr.Textbox(
|
216 |
-
label="Optional Text Prompt",
|
217 |
placeholder="Enter text prompt here (optional)",
|
218 |
lines=2
|
219 |
)
|
220 |
|
221 |
with gr.Group():
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
value=2,
|
248 |
-
step=1,
|
249 |
-
label="Number of Images",
|
250 |
-
info="Generate multiple variations"
|
251 |
-
)
|
252 |
-
with gr.Column(scale=1):
|
253 |
-
seed = gr.Number(
|
254 |
-
label="Random Seed",
|
255 |
-
value=None,
|
256 |
-
precision=0,
|
257 |
-
info="Optional, for reproducibility"
|
258 |
-
)
|
259 |
|
260 |
-
submit_btn = gr.Button(
|
261 |
-
"Generate Variations",
|
262 |
-
variant="primary",
|
263 |
-
scale=1
|
264 |
-
)
|
265 |
|
266 |
with gr.Column(scale=1):
|
267 |
output_gallery = gr.Gallery(
|
268 |
label="Generated Variations",
|
269 |
columns=2,
|
270 |
-
rows=2,
|
271 |
-
height=768,
|
272 |
-
object_fit="contain",
|
273 |
show_label=True
|
274 |
)
|
275 |
|
276 |
-
gr.Markdown("""
|
277 |
-
### Tips:
|
278 |
-
- Upload any image to get started
|
279 |
-
- Add a text prompt to guide the generation in a specific direction
|
280 |
-
- Adjust guidance scale to control how closely the output follows the prompt
|
281 |
-
- Increase steps for higher quality (but slower) generation
|
282 |
-
- Use the same seed to reproduce results
|
283 |
-
""")
|
284 |
-
|
285 |
# Set up the generation function
|
286 |
submit_btn.click(
|
287 |
fn=interface.generate,
|
@@ -296,6 +232,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
296 |
outputs=output_gallery
|
297 |
)
|
298 |
|
299 |
-
# Launch the app
|
300 |
if __name__ == "__main__":
|
301 |
demo.launch()
|
|
|
87 |
vae=vae,
|
88 |
text_encoder=text_encoder,
|
89 |
tokenizer=tokenizer,
|
90 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
+
# [Previous methods remain unchanged...]
|
93 |
def process_image(self, image):
|
94 |
message = [
|
95 |
{
|
|
|
109 |
image_hidden_state = self.models['connector'](image_hidden_state)
|
110 |
|
111 |
return image_hidden_state, image_grid_thw
|
112 |
+
|
113 |
+
def compute_text_embeddings(self, prompt):
|
114 |
"""Compute T5 embeddings for text prompt"""
|
115 |
if prompt == "":
|
116 |
return None
|
|
|
129 |
|
130 |
return prompt_embeds
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
def generate(self, input_image, prompt="", guidance_scale=3.5, num_inference_steps=28, num_images=2, seed=None):
|
133 |
try:
|
134 |
if seed is not None:
|
|
|
163 |
interface = FluxInterface()
|
164 |
|
165 |
# Create Gradio interface
|
166 |
+
with gr.Blocks() as demo:
|
167 |
gr.Markdown("""
|
168 |
# 🎨 Qwen2vl-Flux Image Variation Demo
|
169 |
Upload an image and get AI-generated variations. You can optionally add a text prompt to guide the generation.
|
|
|
174 |
input_image = gr.Image(
|
175 |
label="Upload Image",
|
176 |
type="pil",
|
177 |
+
height=384
|
|
|
|
|
178 |
)
|
179 |
prompt = gr.Textbox(
|
180 |
+
label="Optional Text Prompt, As Long As Possible",
|
181 |
placeholder="Enter text prompt here (optional)",
|
182 |
lines=2
|
183 |
)
|
184 |
|
185 |
with gr.Group():
|
186 |
+
guidance = gr.Slider(
|
187 |
+
minimum=1,
|
188 |
+
maximum=10,
|
189 |
+
value=3.5,
|
190 |
+
step=0.5,
|
191 |
+
label="Guidance Scale"
|
192 |
+
)
|
193 |
+
steps = gr.Slider(
|
194 |
+
minimum=1,
|
195 |
+
maximum=50,
|
196 |
+
value=28,
|
197 |
+
step=1,
|
198 |
+
label="Number of Steps"
|
199 |
+
)
|
200 |
+
num_images = gr.Slider(
|
201 |
+
minimum=1,
|
202 |
+
maximum=4,
|
203 |
+
value=2,
|
204 |
+
step=1,
|
205 |
+
label="Number of Images"
|
206 |
+
)
|
207 |
+
seed = gr.Number(
|
208 |
+
label="Random Seed (optional)",
|
209 |
+
precision=0
|
210 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
+
submit_btn = gr.Button("Generate Variations", variant="primary")
|
|
|
|
|
|
|
|
|
213 |
|
214 |
with gr.Column(scale=1):
|
215 |
output_gallery = gr.Gallery(
|
216 |
label="Generated Variations",
|
217 |
columns=2,
|
|
|
|
|
|
|
218 |
show_label=True
|
219 |
)
|
220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
# Set up the generation function
|
222 |
submit_btn.click(
|
223 |
fn=interface.generate,
|
|
|
232 |
outputs=output_gallery
|
233 |
)
|
234 |
|
|
|
235 |
if __name__ == "__main__":
|
236 |
demo.launch()
|