Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ import time
|
|
| 15 |
|
| 16 |
# Load LoRAs from JSON file
|
| 17 |
with open('loras.json', 'r') as f:
|
| 18 |
-
|
| 19 |
|
| 20 |
# Initialize the base model
|
| 21 |
dtype = torch.bfloat16
|
|
@@ -55,7 +55,7 @@ class calculateDuration:
|
|
| 55 |
else:
|
| 56 |
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
| 57 |
|
| 58 |
-
def update_selection(evt: gr.SelectData, width, height
|
| 59 |
selected_lora = loras[evt.index]
|
| 60 |
new_placeholder = f"Type a prompt for {selected_lora['title']}"
|
| 61 |
lora_repo = selected_lora["repo"]
|
|
@@ -116,7 +116,7 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
|
|
| 116 |
).images[0]
|
| 117 |
return final_image
|
| 118 |
|
| 119 |
-
def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale,
|
| 120 |
if selected_index is None:
|
| 121 |
raise gr.Error("You must select a LoRA before proceeding.")
|
| 122 |
selected_lora = loras[selected_index]
|
|
@@ -174,30 +174,30 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
|
|
| 174 |
yield final_image, seed, gr.update(value=progress_bar, visible=False)
|
| 175 |
|
| 176 |
def get_huggingface_safetensors(link):
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
|
| 202 |
def check_custom_model(link):
|
| 203 |
if(link.startswith("https://")):
|
|
@@ -207,7 +207,8 @@ def check_custom_model(link):
|
|
| 207 |
else:
|
| 208 |
return get_huggingface_safetensors(link)
|
| 209 |
|
| 210 |
-
def add_custom_lora(custom_lora
|
|
|
|
| 211 |
if(custom_lora):
|
| 212 |
try:
|
| 213 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
|
@@ -225,7 +226,7 @@ def add_custom_lora(custom_lora, loras):
|
|
| 225 |
</div>
|
| 226 |
'''
|
| 227 |
existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
|
| 228 |
-
if existing_item_index
|
| 229 |
new_item = {
|
| 230 |
"image": image,
|
| 231 |
"title": title,
|
|
@@ -235,17 +236,17 @@ def add_custom_lora(custom_lora, loras):
|
|
| 235 |
}
|
| 236 |
print(new_item)
|
| 237 |
existing_item_index = len(loras)
|
| 238 |
-
loras
|
| 239 |
|
| 240 |
-
return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(
|
| 241 |
except Exception as e:
|
| 242 |
gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-FLUX LoRA")
|
| 243 |
-
return gr.update(visible=True, value=f"Invalid LoRA: either you entered an invalid link, a non-FLUX LoRA"), gr.update(visible=
|
| 244 |
else:
|
| 245 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
| 246 |
|
| 247 |
-
def remove_custom_lora(
|
| 248 |
-
return gr.update(visible=False), gr.update(visible=False), gr.
|
| 249 |
|
| 250 |
run_lora.zerogpu = True
|
| 251 |
|
|
@@ -265,16 +266,12 @@ css = '''
|
|
| 265 |
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
| 266 |
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
| 267 |
'''
|
| 268 |
-
|
| 269 |
with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
| 270 |
-
loras = gr.State(initial_loras)
|
| 271 |
-
selected_index = gr.State(None)
|
| 272 |
-
|
| 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 |
-
|
| 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")
|
|
@@ -284,7 +281,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
|
| 284 |
with gr.Column():
|
| 285 |
selected_info = gr.Markdown("")
|
| 286 |
gallery = gr.Gallery(
|
| 287 |
-
[(item["image"], item["title"]) for item in
|
| 288 |
label="LoRA Gallery",
|
| 289 |
allow_preview=False,
|
| 290 |
columns=3,
|
|
@@ -320,23 +317,22 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
|
| 320 |
|
| 321 |
gallery.select(
|
| 322 |
update_selection,
|
| 323 |
-
inputs=[width, height
|
| 324 |
outputs=[prompt, selected_info, selected_index, width, height]
|
| 325 |
)
|
| 326 |
custom_lora.input(
|
| 327 |
add_custom_lora,
|
| 328 |
-
inputs=[custom_lora
|
| 329 |
-
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, prompt
|
| 330 |
)
|
| 331 |
custom_lora_button.click(
|
| 332 |
remove_custom_lora,
|
| 333 |
-
|
| 334 |
-
outputs=[custom_lora_info, custom_lora_button, gallery, selected_info, selected_index, custom_lora, loras]
|
| 335 |
)
|
| 336 |
gr.on(
|
| 337 |
triggers=[generate_button.click, prompt.submit],
|
| 338 |
fn=run_lora,
|
| 339 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale
|
| 340 |
outputs=[result, seed, progress_bar]
|
| 341 |
)
|
| 342 |
|
|
|
|
| 15 |
|
| 16 |
# Load LoRAs from JSON file
|
| 17 |
with open('loras.json', 'r') as f:
|
| 18 |
+
loras = json.load(f)
|
| 19 |
|
| 20 |
# Initialize the base model
|
| 21 |
dtype = torch.bfloat16
|
|
|
|
| 55 |
else:
|
| 56 |
print(f"Elapsed time: {self.elapsed_time:.6f} seconds")
|
| 57 |
|
| 58 |
+
def update_selection(evt: gr.SelectData, width, height):
|
| 59 |
selected_lora = loras[evt.index]
|
| 60 |
new_placeholder = f"Type a prompt for {selected_lora['title']}"
|
| 61 |
lora_repo = selected_lora["repo"]
|
|
|
|
| 116 |
).images[0]
|
| 117 |
return final_image
|
| 118 |
|
| 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)):
|
| 120 |
if selected_index is None:
|
| 121 |
raise gr.Error("You must select a LoRA before proceeding.")
|
| 122 |
selected_lora = loras[selected_index]
|
|
|
|
| 174 |
yield final_image, seed, gr.update(value=progress_bar, visible=False)
|
| 175 |
|
| 176 |
def get_huggingface_safetensors(link):
|
| 177 |
+
split_link = link.split("/")
|
| 178 |
+
if(len(split_link) == 2):
|
| 179 |
+
model_card = ModelCard.load(link)
|
| 180 |
+
base_model = model_card.data.get("base_model")
|
| 181 |
+
print(base_model)
|
| 182 |
+
if((base_model != "black-forest-labs/FLUX.1-dev") and (base_model != "black-forest-labs/FLUX.1-schnell")):
|
| 183 |
+
raise Exception("Not a FLUX LoRA!")
|
| 184 |
+
image_path = model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)
|
| 185 |
+
trigger_word = model_card.data.get("instance_prompt", "")
|
| 186 |
+
image_url = f"https://huggingface.co/{link}/resolve/main/{image_path}" if image_path else None
|
| 187 |
+
fs = HfFileSystem()
|
| 188 |
+
try:
|
| 189 |
+
list_of_files = fs.ls(link, detail=False)
|
| 190 |
+
for file in list_of_files:
|
| 191 |
+
if(file.endswith(".safetensors")):
|
| 192 |
+
safetensors_name = file.split("/")[-1]
|
| 193 |
+
if (not image_url and file.lower().endswith((".jpg", ".jpeg", ".png", ".webp"))):
|
| 194 |
+
image_elements = file.split("/")
|
| 195 |
+
image_url = f"https://huggingface.co/{link}/resolve/main/{image_elements[-1]}"
|
| 196 |
+
except Exception as e:
|
| 197 |
+
print(e)
|
| 198 |
+
gr.Warning(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
|
| 199 |
+
raise Exception(f"You didn't include a link neither a valid Hugging Face repository with a *.safetensors LoRA")
|
| 200 |
+
return split_link[1], link, safetensors_name, trigger_word, image_url
|
| 201 |
|
| 202 |
def check_custom_model(link):
|
| 203 |
if(link.startswith("https://")):
|
|
|
|
| 207 |
else:
|
| 208 |
return get_huggingface_safetensors(link)
|
| 209 |
|
| 210 |
+
def add_custom_lora(custom_lora):
|
| 211 |
+
global loras
|
| 212 |
if(custom_lora):
|
| 213 |
try:
|
| 214 |
title, repo, path, trigger_word, image = check_custom_model(custom_lora)
|
|
|
|
| 226 |
</div>
|
| 227 |
'''
|
| 228 |
existing_item_index = next((index for (index, item) in enumerate(loras) if item['repo'] == repo), None)
|
| 229 |
+
if(not existing_item_index):
|
| 230 |
new_item = {
|
| 231 |
"image": image,
|
| 232 |
"title": title,
|
|
|
|
| 236 |
}
|
| 237 |
print(new_item)
|
| 238 |
existing_item_index = len(loras)
|
| 239 |
+
loras.append(new_item)
|
| 240 |
|
| 241 |
+
return gr.update(visible=True, value=card), gr.update(visible=True), gr.Gallery(selected_index=None), f"Custom: {path}", existing_item_index, trigger_word
|
| 242 |
except Exception as e:
|
| 243 |
gr.Warning(f"Invalid LoRA: either you entered an invalid link, or a non-FLUX LoRA")
|
| 244 |
+
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, ""
|
| 245 |
else:
|
| 246 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
| 247 |
|
| 248 |
+
def remove_custom_lora():
|
| 249 |
+
return gr.update(visible=False), gr.update(visible=False), gr.update(), "", None, ""
|
| 250 |
|
| 251 |
run_lora.zerogpu = True
|
| 252 |
|
|
|
|
| 266 |
.progress-container {width: 100%;height: 30px;background-color: #f0f0f0;border-radius: 15px;overflow: hidden;margin-bottom: 20px}
|
| 267 |
.progress-bar {height: 100%;background-color: #4f46e5;width: calc(var(--current) / var(--total) * 100%);transition: width 0.5s ease-in-out}
|
| 268 |
'''
|
|
|
|
| 269 |
with gr.Blocks(theme=gr.themes.Soft(), css=css, delete_cache=(60, 3600)) as app:
|
|
|
|
|
|
|
|
|
|
| 270 |
title = gr.HTML(
|
| 271 |
"""<h1><img src="https://huggingface.co/spaces/multimodalart/flux-lora-the-explorer/resolve/main/flux_lora.png" alt="LoRA"> FLUX LoRA the Explorer</h1>""",
|
| 272 |
elem_id="title",
|
| 273 |
)
|
| 274 |
+
selected_index = gr.State(None)
|
| 275 |
with gr.Row():
|
| 276 |
with gr.Column(scale=3):
|
| 277 |
prompt = gr.Textbox(label="Prompt", lines=1, placeholder="Type a prompt after selecting a LoRA")
|
|
|
|
| 281 |
with gr.Column():
|
| 282 |
selected_info = gr.Markdown("")
|
| 283 |
gallery = gr.Gallery(
|
| 284 |
+
[(item["image"], item["title"]) for item in loras],
|
| 285 |
label="LoRA Gallery",
|
| 286 |
allow_preview=False,
|
| 287 |
columns=3,
|
|
|
|
| 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 |
|