Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -234,70 +234,111 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
|
|
234 |
if randomize_seed:
|
235 |
seed = random.randint(0, MAX_SEED)
|
236 |
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
return final_image, file_path, base64_str
|
241 |
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
#
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
with gr.
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
234 |
if randomize_seed:
|
235 |
seed = random.randint(0, MAX_SEED)
|
236 |
|
237 |
+
if image_input is not None:
|
238 |
+
final_image, file_path, base64_str = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed)
|
239 |
+
return final_image, file_path, base64_str
|
|
|
240 |
|
241 |
+
else:
|
242 |
+
image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
|
243 |
+
final_image = None
|
244 |
+
for image in image_generator:
|
245 |
+
final_image = image
|
246 |
+
|
247 |
+
# Save and encode final image
|
248 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
249 |
+
final_image.save(temp_file.name, "PNG")
|
250 |
+
with open(temp_file.name, "rb") as f:
|
251 |
+
base64_str = base64.b64encode(f.read()).decode("utf-8")
|
252 |
+
|
253 |
+
return final_image, temp_file.name, base64_str
|
254 |
+
|
255 |
+
css = '''
|
256 |
+
#gen_btn{height: 100%}
|
257 |
+
#gen_column{align-self: stretch}
|
258 |
+
#title{text-align: center}
|
259 |
+
#title h1{font-size: 3em; display:inline-flex; align-items:center}
|
260 |
+
#title img{width: 100px; margin-right: 0.5em}
|
261 |
+
#gallery .grid-wrap{height: 10vh}
|
262 |
+
#lora_list{background: var(--block-background-fill);padding: 0 1em .3em; font-size: 90%}
|
263 |
+
.card_internal{display: flex;height: 100px;margin-top: .5em}
|
264 |
+
.card_internal img{margin-right: 1em}
|
265 |
+
.styler{--form-gap-width: 0px !important}
|
266 |
+
#progress{height:30px}
|
267 |
+
#progress .generating{display:none}
|
268 |
+
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
269 |
+
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
270 |
+
'''
|
271 |
+
font=[gr.themes.GoogleFont("Source Sans Pro"), "Arial", "sans-serif"]
|
272 |
+
with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60)) as app:
|
273 |
+
title = gr.HTML(
|
274 |
+
"""<h1><img src=\"https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png\" alt=\"LoRA\"> FLUX LoRA the Explorer</h1>""",
|
275 |
+
elem_id="title",
|
276 |
+
)
|
277 |
+
selected_index = gr.State(None)
|
278 |
+
with gr.Row():
|
279 |
+
with gr.Column(scale=3):
|
280 |
+
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
|
281 |
+
with gr.Column(scale=1, elem_id="gen_column"):
|
282 |
+
generate_button = gr.Button("Generate", variant="primary", elem_id="gen_btn")
|
283 |
+
with gr.Row():
|
284 |
+
with gr.Column():
|
285 |
+
selected_info = gr.Markdown("")
|
286 |
+
gallery = gr.Gallery(
|
287 |
+
[(item["image"], item["title"]) for item in loras],
|
288 |
+
label="LoRA Gallery",
|
289 |
+
allow_preview=False,
|
290 |
+
columns=3,
|
291 |
+
elem_id="gallery",
|
292 |
+
show_share_button=False
|
293 |
+
)
|
294 |
+
with gr.Group():
|
295 |
+
custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="multimodalart/vintage-ads-flux")
|
296 |
+
gr.Markdown("[Check the list of FLUX LoRas](https://huggingface.co/models?other=base_model:adapter:black-forest-labs/FLUX.1-dev)", elem_id="lora_list")
|
297 |
+
custom_lora_info = gr.HTML(visible=False)
|
298 |
+
custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
|
299 |
+
with gr.Column():
|
300 |
+
progress_bar = gr.Markdown(elem_id="progress",visible=False)
|
301 |
+
result = gr.Image(label="Generated Image")
|
302 |
+
download_link = gr.File(label="Download Image")
|
303 |
+
base64_output = gr.Textbox(label="Base64 Encoded Image")
|
304 |
+
|
305 |
+
with gr.Row():
|
306 |
+
with gr.Accordion("Advanced Settings", open=False):
|
307 |
+
with gr.Row():
|
308 |
+
input_image = gr.Image(label="Input image", type="filepath")
|
309 |
+
image_strength = gr.Slider(label="Denoise Strength", info="Lower means more image influence", minimum=0.1, maximum=1.0, step=0.01, value=0.75)
|
310 |
+
with gr.Column():
|
311 |
+
with gr.Row():
|
312 |
+
cfg_scale = gr.Slider(label="CFG Scale", minimum=1, maximum=20, step=0.5, value=3.5)
|
313 |
+
steps = gr.Slider(label="Steps", minimum=1, maximum=50, step=1, value=28)
|
314 |
+
|
315 |
+
with gr.Row():
|
316 |
+
width = gr.Slider(label="Width", minimum=256, maximum=1536, step=64, value=1024)
|
317 |
+
height = gr.Slider(label="Height", minimum=256, maximum=1536, step=64, value=1024)
|
318 |
+
|
319 |
+
with gr.Row():
|
320 |
+
randomize_seed = gr.Checkbox(True, label="Randomize seed")
|
321 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
|
322 |
+
lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
|
323 |
+
|
324 |
+
gallery.select(
|
325 |
+
update_selection,
|
326 |
+
inputs=[width, height],
|
327 |
+
outputs=[prompt, selected_info, selected_index, width, height]
|
328 |
+
)
|
329 |
+
custom_lora.input(
|
330 |
+
add_custom_lora,
|
331 |
+
inputs=[custom_lora],
|
332 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt]
|
333 |
+
)
|
334 |
+
custom_lora_button.click(
|
335 |
+
remove_custom_lora,
|
336 |
+
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
|
337 |
+
)
|
338 |
+
generate_button.click(
|
339 |
+
run_lora,
|
340 |
+
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
|
341 |
+
outputs=[result, download_link, base64_output]
|
342 |
+
)
|
343 |
+
app.queue()
|
344 |
+
app.launch()
|