|
import gradio as gr |
|
from huggingface_hub import ModelCard |
|
|
|
from config import Config |
|
|
|
|
|
def selected_lora_from_gallery(evt: gr.SelectData): |
|
return ( |
|
gr.update( |
|
value=evt.index |
|
) |
|
) |
|
|
|
|
|
def update_selected_lora(custom_lora): |
|
link = custom_lora.split("/") |
|
|
|
if len(link) == 2: |
|
model_card = ModelCard.load(custom_lora) |
|
trigger_word = model_card.data.get("instance_prompt", "") |
|
image_url = f"""https://huggingface.co/{custom_lora}/resolve/main/{model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)}""" |
|
|
|
custom_lora_info_css = """ |
|
<style> |
|
.custom-lora-info { |
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; |
|
background: linear-gradient(135deg, #4a90e2, #7b61ff); |
|
color: white; |
|
padding: 16px; |
|
border-radius: 8px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
margin: 16px 0; |
|
} |
|
.custom-lora-header { |
|
font-size: 18px; |
|
font-weight: 600; |
|
margin-bottom: 12px; |
|
} |
|
.custom-lora-content { |
|
display: flex; |
|
align-items: center; |
|
background-color: rgba(255, 255, 255, 0.1); |
|
border-radius: 6px; |
|
padding: 12px; |
|
} |
|
.custom-lora-image { |
|
width: 80px; |
|
height: 80px; |
|
object-fit: cover; |
|
border-radius: 6px; |
|
margin-right: 16px; |
|
} |
|
.custom-lora-text h3 { |
|
margin: 0 0 8px 0; |
|
font-size: 16px; |
|
font-weight: 600; |
|
} |
|
.custom-lora-text small { |
|
font-size: 14px; |
|
opacity: 0.9; |
|
} |
|
.custom-trigger-word { |
|
background-color: rgba(255, 255, 255, 0.2); |
|
padding: 2px 6px; |
|
border-radius: 4px; |
|
font-weight: 600; |
|
} |
|
</style> |
|
""" |
|
|
|
custom_lora_info_html = f""" |
|
<div class="custom-lora-info"> |
|
<div class="custom-lora-header">Custom LoRA: {custom_lora}</div> |
|
<div class="custom-lora-content"> |
|
<img class="custom-lora-image" src="{image_url}" alt="LoRA preview"> |
|
<div class="custom-lora-text"> |
|
<h3>{link[1].replace("-", " ").replace("_", " ")}</h3> |
|
<small>{"Using: <span class='custom-trigger-word'>"+trigger_word+"</span> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}</small> |
|
</div> |
|
</div> |
|
</div> |
|
""" |
|
|
|
custom_lora_info_html = f"{custom_lora_info_css}{custom_lora_info_html}" |
|
|
|
return ( |
|
gr.update( |
|
value=custom_lora, |
|
), |
|
gr.update( |
|
value=custom_lora_info_html, |
|
visible=True |
|
) |
|
) |
|
|
|
else: |
|
return ( |
|
gr.update( |
|
value=custom_lora, |
|
), |
|
gr.update( |
|
value=custom_lora_info_html if len(link) == 0 else "", |
|
visible=False |
|
) |
|
) |
|
|
|
|
|
def update_lora_sliders(enabled_loras): |
|
sliders = [] |
|
remove_buttons = [] |
|
|
|
for lora in enabled_loras: |
|
sliders.append( |
|
gr.update( |
|
label=lora.get("repo_id", ""), |
|
info=f"Trigger Word: {lora.get('trigger_word', '')}", |
|
visible=True, |
|
interactive=True |
|
) |
|
) |
|
remove_buttons.append( |
|
gr.update( |
|
visible=True, |
|
interactive=True |
|
) |
|
) |
|
|
|
if len(sliders) < 6: |
|
for i in range(len(sliders), 6): |
|
sliders.append( |
|
gr.update( |
|
visible=False |
|
) |
|
) |
|
remove_buttons.append( |
|
gr.update( |
|
visible=False |
|
) |
|
) |
|
|
|
return *sliders, *remove_buttons |
|
|
|
|
|
def remove_from_enabled_loras(enabled_loras, index): |
|
enabled_loras.pop(index) |
|
return ( |
|
gr.update( |
|
value=enabled_loras |
|
) |
|
) |
|
|
|
|
|
def update_custom_embedding(custom_embedding): |
|
link = custom_embedding.split("/") |
|
|
|
if len(link) == 2: |
|
model_card = ModelCard.load(custom_embedding) |
|
trigger_word = model_card.data.get("instance_prompt", "") |
|
image_url = f"""https://huggingface.co/{custom_embedding}/resolve/main/{model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)}""" |
|
|
|
custom_embedding_info_css = """ |
|
<style> |
|
.custom-embedding-info { |
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; |
|
background: linear-gradient(135deg, #4a90e2, #7b61ff); |
|
color: white; |
|
padding: 16px; |
|
border-radius: 8px; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
margin: 16px 0; |
|
} |
|
.custom-embedding-header { |
|
font-size: 18px; |
|
font-weight: 600; |
|
margin-bottom: 12px; |
|
} |
|
.custom-embedding-content { |
|
display: flex; |
|
align-items: center; |
|
background-color: rgba(255, 255, 255, 0.1); |
|
border-radius: 6px; |
|
padding: 12px; |
|
} |
|
.custom-embedding-image { |
|
width: 80px; |
|
height: 80px; |
|
object-fit: cover; |
|
border-radius: 6px; |
|
margin-right: 16px; |
|
} |
|
.custom-embedding-text h3 { |
|
margin: 0 0 8px 0; |
|
font-size: 16px; |
|
font-weight: 600; |
|
} |
|
.custom-embedding-text small { |
|
font-size: 14px; |
|
opacity: 0.9; |
|
} |
|
.custom-trigger-word { |
|
background-color: rgba(255, 255, 255, 0.2); |
|
padding: 2px 6px; |
|
border-radius: 4px; |
|
font-weight: 600; |
|
} |
|
</style> |
|
""" |
|
|
|
custom_embedding_info_html = f""" |
|
<div class="custom-embedding-info"> |
|
<div class="custom-embedding-header">Custom Embed Model: {custom_embedding}</div> |
|
<div class="custom-embedding-content"> |
|
<img class="custom-embedding-image" src="{image_url}" alt="Embedding model preview"> |
|
<div class="custom-embedding-text"> |
|
<h3>{link[1].replace("-", " ").replace("_", " ")}</h3> |
|
<small>{"Using: <span class='custom-trigger-word'>"+trigger_word+"</span> as the trigger word" if trigger_word else "No trigger word found. If there's a trigger word, include it in your prompt"}</small> |
|
</div> |
|
</div> |
|
</div> |
|
""" |
|
|
|
custom_embedding_info_html = f"{custom_embedding_info_css}{custom_embedding_info_html}" |
|
|
|
return gr.update( |
|
value=custom_embedding_info_html, |
|
visible=True |
|
) |
|
|
|
|
|
def add_to_embeddings(custom_embedding, embeddings): |
|
link = custom_embedding.split("/") |
|
|
|
if len(link) == 2: |
|
model_card = ModelCard.load(custom_embedding) |
|
trigger_word = model_card.data.get("instance_prompt", "") |
|
image_url = f"""https://huggingface.co/{custom_embedding}/resolve/main/{model_card.data.get("widget", [{}])[0].get("output", {}).get("url", None)}""" |
|
|
|
embeddings.append({ |
|
"repo_id": custom_embedding, |
|
"trigger_word": trigger_word |
|
}) |
|
|
|
return ( |
|
gr.update( |
|
value="" |
|
), |
|
gr.update( |
|
value="", |
|
visible=False |
|
), |
|
gr.update( |
|
value=embeddings |
|
) |
|
) |
|
|
|
|
|
def remove_from_embeddings(embeddings, index): |
|
embeddings.pop(index) |
|
return ( |
|
gr.update( |
|
value=embeddings |
|
) |
|
) |
|
|
|
|