CultriX commited on
Commit
3d57404
1 Parent(s): 38ad555

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -33
app.py CHANGED
@@ -12,6 +12,8 @@ from huggingface_hub import hf_hub_download, HfFileSystem, ModelCard, snapshot_d
12
  import copy
13
  import random
14
  import time
 
 
15
 
16
  # Load LoRAs from JSON file
17
  with open('loras.json', 'r') as f:
@@ -113,7 +115,18 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
113
  joint_attention_kwargs={"scale": lora_scale},
114
  output_type="pil",
115
  ).images[0]
116
- return final_image
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  @spaces.GPU(duration=30)
119
  def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
@@ -154,9 +167,8 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
154
  seed = random.randint(0, MAX_SEED)
155
 
156
  if(image_input is not None):
157
-
158
- final_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed)
159
- yield final_image, seed, gr.update(visible=False)
160
  else:
161
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
162
 
@@ -167,9 +179,15 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
167
  step_counter+=1
168
  final_image = image
169
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
170
- yield image, seed, gr.update(value=progress_bar, visible=True)
171
 
172
- yield final_image, seed, gr.update(value=progress_bar, visible=False)
 
 
 
 
 
 
173
 
174
  def get_huggingface_safetensors(link):
175
  split_link = link.split("/")
@@ -218,7 +236,7 @@ def add_custom_lora(custom_lora):
218
  <img src="{image}" />
219
  <div>
220
  <h3>{title}</h3>
221
- <small>{"Using: <code><b>"+trigger_word+"</code></b> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}<br></small>
222
  </div>
223
  </div>
224
  </div>
@@ -295,6 +313,8 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
295
  with gr.Column():
296
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
297
  result = gr.Image(label="Generated Image")
 
 
298
 
299
  with gr.Row():
300
  with gr.Accordion("Advanced Settings", open=False):
@@ -312,29 +332,4 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
312
 
313
  with gr.Row():
314
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
315
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0, randomize=True)
316
- lora_scale = gr.Slider(label="LoRA Scale", minimum=0, maximum=3, step=0.01, value=0.95)
317
-
318
- gallery.select(
319
- update_selection,
320
- inputs=[width, height],
321
- outputs=[prompt, selected_info, selected_index, width, height]
322
- )
323
- custom_lora.input(
324
- add_custom_lora,
325
- inputs=[custom_lora],
326
- outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt]
327
- )
328
- custom_lora_button.click(
329
- remove_custom_lora,
330
- outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora]
331
- )
332
- gr.on(
333
- triggers=[generate_button.click, prompt.submit],
334
- fn=run_lora,
335
- inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale],
336
- outputs=[result, seed, progress_bar]
337
- )
338
-
339
- app.queue()
340
- app.launch()
 
12
  import copy
13
  import random
14
  import time
15
+ import base64
16
+ import tempfile
17
 
18
  # Load LoRAs from JSON file
19
  with open('loras.json', 'r') as f:
 
115
  joint_attention_kwargs={"scale": lora_scale},
116
  output_type="pil",
117
  ).images[0]
118
+
119
+ # Save the image as a downloadable PNG file
120
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
121
+ final_image.save(temp_file.name, "PNG")
122
+
123
+ # Convert the image to a base64 string
124
+ buffered = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
125
+ final_image.save(buffered.name, format="PNG")
126
+ with open(buffered.name, "rb") as f:
127
+ img_base64 = base64.b64encode(f.read()).decode("utf-8")
128
+
129
+ return final_image, temp_file.name, f"data:image/png;base64,{img_base64}"
130
 
131
  @spaces.GPU(duration=30)
132
  def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, progress=gr.Progress(track_tqdm=True)):
 
167
  seed = random.randint(0, MAX_SEED)
168
 
169
  if(image_input is not None):
170
+ final_image, file_path, base64_str = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed)
171
+ yield final_image, seed, file_path, base64_str, gr.update(visible=False)
 
172
  else:
173
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
174
 
 
179
  step_counter+=1
180
  final_image = image
181
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
182
+ yield image, seed, None, None, gr.update(value=progress_bar, visible=True)
183
 
184
+ # Save the final image and encode to Base64
185
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
186
+ final_image.save(temp_file.name, "PNG")
187
+ with open(temp_file.name, "rb") as f:
188
+ img_base64 = base64.b64encode(f.read()).decode("utf-8")
189
+
190
+ yield final_image, seed, temp_file.name, f"data:image/png;base64,{img_base64}", gr.update(value=progress_bar, visible=False)
191
 
192
  def get_huggingface_safetensors(link):
193
  split_link = link.split("/")
 
236
  <img src="{image}" />
237
  <div>
238
  <h3>{title}</h3>
239
+ <small>{"Using: <code><b"+trigger_word+"</code></b> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}<br></small>
240
  </div>
241
  </div>
242
  </div>
 
313
  with gr.Column():
314
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
315
  result = gr.Image(label="Generated Image")
316
+ download_link = gr.File(label="Download Image")
317
+ base64_output = gr.Textbox(label="Base64 Encoded Image")
318
 
319
  with gr.Row():
320
  with gr.Accordion("Advanced Settings", open=False):
 
332
 
333
  with gr.Row():
334
  randomize_seed = gr.Checkbox(True, label="Randomize seed")
335
+ seed = gr.Slider(label="Seed", minimum=0, maximum