nftnik commited on
Commit
90233e4
·
verified ·
1 Parent(s): dc35706

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -81
app.py CHANGED
@@ -171,77 +171,77 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
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("/")
176
- if(len(split_link) == 2):
177
- model_card = ModelCard.load(link)
178
- base_model = model_card.data.get("base_model")
179
- print(base_model)
180
- if((base_model != "black-forest-labs/FLUX.1-dev") and (base_model != "black-forest-labs/FLUX.1-schnell")):
181
- raise Exception("Not a FLUX LoRA!")
182
- image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
183
- trigger_word = model_card.data.get("instance_prompt", "")
184
- image_url = f"https://huggingface.co/{link}/resolve/main/{image_path}" if image_path else None
185
- fs = HfFileSystem()
186
- try:
187
- list_of_files = fs.ls(link, detail=False)
188
- for file in list_of_files:
189
- if(file.endswith(".safetensors")):
190
- safetensors_name = file.split("/")[-1]
191
- if (not image_url and file.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))):
192
- image_elements = file.split("/")
193
- image_url = f"https://huggingface.co/{link}/resolve/main/{image_elements[-1]}"
194
- except Exception as e:
195
- print(e)
196
- gr.Warning(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
197
- raise Exception(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
198
- return split_link[1], link, safetensors_name, trigger_word, image_url
199
 
200
- def check_custom_model(link):
201
- if(link.startswith("https://")):
202
- if(link.startswith("https://huggingface.co") or link.startswith("https://www.huggingface.co")):
203
- link_split = link.split("huggingface.co/")
204
- return get_huggingface_safetensors(link_split[1])
205
- else:
206
- return get_huggingface_safetensors(link)
207
 
208
- def add_custom_lora(custom_lora):
209
- global loras
210
- if(custom_lora):
211
- try:
212
- title, repo, path, trigger_word, image = check_custom_model(custom_lora)
213
- print(f"Loaded custom LoRA: {repo}")
214
- card = f'''
215
- <div class="custom_lora_card">
216
- <span>Loaded custom LoRA:</span>
217
- <div class="card_internal">
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>
225
- '''
226
- existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
227
- if(not existing_item_index):
228
- new_item = {
229
- "image": image,
230
- "title": title,
231
- "repo": repo,
232
- "weights": path,
233
- "trigger_word": trigger_word
234
- }
235
- print(new_item)
236
- existing_item_index = len(loras)
237
- loras.append(new_item)
238
-
239
- return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word
240
- except Exception as e:
241
- gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-FLUX LoRA")
242
- return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-FLUX LoRA"), gr.update(visible=True), gr.update(), "", None, ""
243
- else:
244
- return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
245
 
246
  def remove_custom_lora():
247
  return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
@@ -287,11 +287,11 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
287
  elem_id="gallery",
288
  show_share_button=False
289
  )
290
- with gr.Group():
291
- custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="nftnik/NFTNIK-FLUX.1-dev-LoRA")
292
- gr.Markdown("", elem_id="lora_list")
293
- custom_lora_info = gr.HTML(visible=False)
294
- custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
295
  with gr.Column():
296
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
297
  result = gr.Image(label="Generated Image")
@@ -320,14 +320,14 @@ with gr.Blocks(theme=gr.themes.Soft(font=font), css=css, delete_cache=(60, 60))
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],
 
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("/")
176
+ # if(len(split_link) == 2):
177
+ # model_card = ModelCard.load(link)
178
+ # base_model = model_card.data.get("base_model")
179
+ # print(base_model)
180
+ # if((base_model != "black-forest-labs/FLUX.1-dev") and (base_model != "black-forest-labs/FLUX.1-schnell")):
181
+ # raise Exception("Not a FLUX LoRA!")
182
+ # image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
183
+ # trigger_word = model_card.data.get("instance_prompt", "")
184
+ # image_url = f"https://huggingface.co/{link}/resolve/main/{image_path}" if image_path else None
185
+ # fs = HfFileSystem()
186
+ # try:
187
+ # list_of_files = fs.ls(link, detail=False)
188
+ # for file in list_of_files:
189
+ # if(file.endswith(".safetensors")):
190
+ # safetensors_name = file.split("/")[-1]
191
+ # if (not image_url and file.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))):
192
+ # image_elements = file.split("/")
193
+ # image_url = f"https://huggingface.co/{link}/resolve/main/{image_elements[-1]}"
194
+ # except Exception as e:
195
+ # print(e)
196
+ # gr.Warning(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
197
+ # raise Exception(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
198
+ # return split_link[1], link, safetensors_name, trigger_word, image_url
199
 
200
+ #def check_custom_model(link):
201
+ # if(link.startswith("https://")):
202
+ # if(link.startswith("https://huggingface.co") or link.startswith("https://www.huggingface.co")):
203
+ # link_split = link.split("huggingface.co/")
204
+ # return get_huggingface_safetensors(link_split[1])
205
+ # else:
206
+ # return get_huggingface_safetensors(link)
207
 
208
+ #def add_custom_lora(custom_lora):
209
+ # global loras
210
+ # if(custom_lora):
211
+ # try:
212
+ # title, repo, path, trigger_word, image = check_custom_model(custom_lora)
213
+ # print(f"Loaded custom LoRA: {repo}")
214
+ # card = f'''
215
+ # <div class="custom_lora_card">
216
+ # <span>Loaded custom LoRA:</span>
217
+ # <div class="card_internal">
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>
225
+ # '''
226
+ # existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
227
+ # if(not existing_item_index):
228
+ # new_item = {
229
+ # "image": image,
230
+ # "title": title,
231
+ # "repo": repo,
232
+ # "weights": path,
233
+ # "trigger_word": trigger_word
234
+ # }
235
+ # print(new_item)
236
+ # existing_item_index = len(loras)
237
+ # loras.append(new_item)
238
+ #
239
+ # return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word
240
+ # except Exception as e:
241
+ # gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-FLUX LoRA")
242
+ # return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-FLUX LoRA"), gr.update(visible=True), gr.update(), "", None, ""
243
+ # else:
244
+ # return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
245
 
246
  def remove_custom_lora():
247
  return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
 
287
  elem_id="gallery",
288
  show_share_button=False
289
  )
290
+ # with gr.Group():
291
+ # custom_lora = gr.Textbox(label="Custom LoRA", info="LoRA Hugging Face path", placeholder="nftnik/NFTNIK-FLUX.1-dev-LoRA")
292
+ # gr.Markdown("", elem_id="lora_list")
293
+ # custom_lora_info = gr.HTML(visible=False)
294
+ # custom_lora_button = gr.Button("Remove custom LoRA", visible=False)
295
  with gr.Column():
296
  progress_bar = gr.Markdown(elem_id="progress",visible=False)
297
  result = gr.Image(label="Generated Image")
 
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],