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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -66
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
- 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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()