Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,10 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from diffusers import DiffusionPipeline, AutoencoderKL
|
| 4 |
|
|
@@ -10,11 +16,12 @@ pipe = DiffusionPipeline.from_pretrained(
|
|
| 10 |
use_safetensors=True
|
| 11 |
)
|
| 12 |
|
|
|
|
| 13 |
|
| 14 |
def load_model(custom_model):
|
| 15 |
# This is where you load your trained weights
|
| 16 |
-
pipe.load_lora_weights(custom_model)
|
| 17 |
-
pipe.to(
|
| 18 |
return "Model loaded!"
|
| 19 |
|
| 20 |
def infer (prompt, inf_steps, guidance_scale, seed, lora_weigth, progress=gr.Progress(track_tqdm=True)):
|
|
@@ -28,14 +35,54 @@ def infer (prompt, inf_steps, guidance_scale, seed, lora_weigth, progress=gr.Pro
|
|
| 28 |
).images[0]
|
| 29 |
return image
|
| 30 |
|
| 31 |
-
css
|
| 32 |
-
#col-container
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
"""
|
| 34 |
|
| 35 |
with gr.Blocks(css=css) as demo:
|
| 36 |
with gr.Column(elem_id="col-container"):
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
""")
|
| 40 |
with gr.Row():
|
| 41 |
with gr.Column():
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import login
|
| 3 |
+
import os
|
| 4 |
+
is_shared_ui = True if "fffiloni/sd-xl-custom-model" in os.environ['SPACE_ID'] else False
|
| 5 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 6 |
+
login(token=hf_token)
|
| 7 |
+
|
| 8 |
import torch
|
| 9 |
from diffusers import DiffusionPipeline, AutoencoderKL
|
| 10 |
|
|
|
|
| 16 |
use_safetensors=True
|
| 17 |
)
|
| 18 |
|
| 19 |
+
device="cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
|
| 21 |
def load_model(custom_model):
|
| 22 |
# This is where you load your trained weights
|
| 23 |
+
pipe.load_lora_weights(custom_model, use_auth_token=True)
|
| 24 |
+
pipe.to(device)
|
| 25 |
return "Model loaded!"
|
| 26 |
|
| 27 |
def infer (prompt, inf_steps, guidance_scale, seed, lora_weigth, progress=gr.Progress(track_tqdm=True)):
|
|
|
|
| 35 |
).images[0]
|
| 36 |
return image
|
| 37 |
|
| 38 |
+
css="""
|
| 39 |
+
#col-container{
|
| 40 |
+
margin: 0 auto;
|
| 41 |
+
max-width: 680px;
|
| 42 |
+
text-align: left;
|
| 43 |
+
}
|
| 44 |
+
div#warning-duplicate {
|
| 45 |
+
background-color: #ebf5ff;
|
| 46 |
+
padding: 0 10px 5px;
|
| 47 |
+
margin: 20px 0;
|
| 48 |
+
}
|
| 49 |
+
div#warning-duplicate > .gr-prose > h2, div#warning-duplicate > .gr-prose > p {
|
| 50 |
+
color: #0f4592!important;
|
| 51 |
+
}
|
| 52 |
+
div#warning-duplicate strong {
|
| 53 |
+
color: #0f4592;
|
| 54 |
+
}
|
| 55 |
+
p.actions {
|
| 56 |
+
display: flex;
|
| 57 |
+
align-items: center;
|
| 58 |
+
margin: 20px 0;
|
| 59 |
+
}
|
| 60 |
+
div#warning-duplicate .actions a {
|
| 61 |
+
display: inline-block;
|
| 62 |
+
margin-right: 10px;
|
| 63 |
+
}
|
| 64 |
"""
|
| 65 |
|
| 66 |
with gr.Blocks(css=css) as demo:
|
| 67 |
with gr.Column(elem_id="col-container"):
|
| 68 |
+
if is_shared_ui:
|
| 69 |
+
top_description = gr.HTML(f'''
|
| 70 |
+
<div class="gr-prose">
|
| 71 |
+
<h2><svg xmlns="http://www.w3.org/2000/svg" width="18px" height="18px" style="margin-right: 0px;display: inline-block;"fill="none"><path fill="#fff" d="M7 13.2a6.3 6.3 0 0 0 4.4-10.7A6.3 6.3 0 0 0 .6 6.9 6.3 6.3 0 0 0 7 13.2Z"/><path fill="#fff" fill-rule="evenodd" d="M7 0a6.9 6.9 0 0 1 4.8 11.8A6.9 6.9 0 0 1 0 7 6.9 6.9 0 0 1 7 0Zm0 0v.7V0ZM0 7h.6H0Zm7 6.8v-.6.6ZM13.7 7h-.6.6ZM9.1 1.7c-.7-.3-1.4-.4-2.2-.4a5.6 5.6 0 0 0-4 1.6 5.6 5.6 0 0 0-1.6 4 5.6 5.6 0 0 0 1.6 4 5.6 5.6 0 0 0 4 1.7 5.6 5.6 0 0 0 4-1.7 5.6 5.6 0 0 0 1.7-4 5.6 5.6 0 0 0-1.7-4c-.5-.5-1.1-.9-1.8-1.2Z" clip-rule="evenodd"/><path fill="#000" fill-rule="evenodd" d="M7 2.9a.8.8 0 1 1 0 1.5A.8.8 0 0 1 7 3ZM5.8 5.7c0-.4.3-.6.6-.6h.7c.3 0 .6.2.6.6v3.7h.5a.6.6 0 0 1 0 1.3H6a.6.6 0 0 1 0-1.3h.4v-3a.6.6 0 0 1-.6-.7Z" clip-rule="evenodd"/></svg>
|
| 72 |
+
Note: you might want to use a private custom LoRa model</h2>
|
| 73 |
+
<p class="main-message">
|
| 74 |
+
To do so, <strong>duplicate the Space</strong> and run it on your own profile using <strong>your own access token</strong> and eventually a GPU (T4-small or A10G-small) for faster inference without waiting in the queue.<br />
|
| 75 |
+
</p>
|
| 76 |
+
<p class="actions">
|
| 77 |
+
<a href="https://huggingface.co/spaces/{os.environ['SPACE_ID']}?duplicate=true">
|
| 78 |
+
<img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-lg-dark.svg" alt="Duplicate this Space" />
|
| 79 |
+
</a>
|
| 80 |
+
to start using private models and skip the queue
|
| 81 |
+
</p>
|
| 82 |
+
</div>
|
| 83 |
+
''', elem_id="warning-duplicate")
|
| 84 |
+
gr.HTML("""
|
| 85 |
+
<h2 style="text-align: center;">SD-XL Custom Model Inference</h2>
|
| 86 |
""")
|
| 87 |
with gr.Row():
|
| 88 |
with gr.Column():
|