CultriX commited on
Commit
c1fbb43
1 Parent(s): bf25225

Update app.py

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