Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import os
|
2 |
-
import subprocess
|
3 |
from huggingface_hub import HfApi, upload_folder
|
4 |
import gradio as gr
|
5 |
|
@@ -10,17 +9,24 @@ def error_str(error, title="Error"):
|
|
10 |
return f"""#### {title}
|
11 |
{error}""" if error else ""
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
def get_my_model_names(token):
|
14 |
|
15 |
try:
|
16 |
author = whoami(token=token)
|
17 |
model_infos = list_models(author=author["name"], use_auth_token=token)
|
18 |
|
19 |
-
|
20 |
model_names = []
|
21 |
for model_info in model_infos:
|
22 |
model_id = model_info.modelId
|
23 |
-
if
|
24 |
model_names.append(model_id)
|
25 |
|
26 |
if not model_names:
|
@@ -34,9 +40,33 @@ def get_my_model_names(token):
|
|
34 |
def on_token_change(token):
|
35 |
model_names, error = get_my_model_names(token)
|
36 |
|
37 |
-
return gr.update(visible=bool(model_names)), gr.update(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
try:
|
42 |
|
@@ -52,7 +82,7 @@ def create_and_push(space_type, model_id, title, description, prefix, update, to
|
|
52 |
# 2. Replace the name, title, and description in the template
|
53 |
with open("template/app_simple.py" if space_type == "Simple" else "template/app_advanced.py", "r") as f:
|
54 |
app = f.read()
|
55 |
-
app = app.replace("$model_id",
|
56 |
app = app.replace("$title", title)
|
57 |
app = app.replace("$description", description)
|
58 |
app = app.replace("$prefix", prefix)
|
@@ -88,16 +118,6 @@ def create_and_push(space_type, model_id, title, description, prefix, update, to
|
|
88 |
except Exception as e:
|
89 |
return error_str(e)
|
90 |
|
91 |
-
def on_model_change(radio_model_names):
|
92 |
-
|
93 |
-
name = radio_model_names
|
94 |
-
title = " ".join([w.capitalize() for w in name.split("/")[-1].replace("-", " ").replace("_", " ").split(" ")])
|
95 |
-
|
96 |
-
description = f"""Demo for <a href="https://huggingface.co/{radio_model_names}">{title}</a> Stable Diffusion model.<br>
|
97 |
-
Add the following tokens to your prompts for the model to work properly: <b>$prefix</b>."""
|
98 |
-
|
99 |
-
return gr.update(value=name), gr.update(value=title), gr.update(value=description)
|
100 |
-
|
101 |
|
102 |
DESCRIPTION = """### Create a gradio space for your Diffusers🧨 model
|
103 |
With this space, you can easily create a gradio demo for your Diffusers model and share it with the community.
|
@@ -118,14 +138,16 @@ with gr.Blocks() as demo:
|
|
118 |
)
|
119 |
gr.Markdown("You can get a token [here](https://huggingface.co/settings/tokens)")
|
120 |
with gr.Group(visible=False) as group_model:
|
121 |
-
radio_model_names = gr.Radio(label="
|
|
|
|
|
122 |
|
123 |
with gr.Column(scale=10):
|
124 |
with gr.Column(visible=False) as group_create:
|
125 |
gr.Markdown("#### 2. Enter details and create the space")
|
126 |
name = gr.Textbox(label="Name", placeholder="e.g. diffusers-demo")
|
127 |
title = gr.Textbox(label="Title", placeholder="e.g. Diffusers Demo")
|
128 |
-
description = gr.Textbox(label="Description", placeholder="e.g. Demo for my awesome Diffusers model")
|
129 |
prefix = gr.Textbox(label="Prefix tokens", placeholder="Tokens that are required to be present in the prompt, e.g. `rick and morty style`")
|
130 |
gr.Markdown("""#### Choose space type
|
131 |
- **Simple** - Runs on GPU using Hugging Face inference API, but you cannot control image generation parameters.
|
@@ -142,20 +164,20 @@ with gr.Blocks() as demo:
|
|
142 |
input_token.change(
|
143 |
fn=on_token_change,
|
144 |
inputs=input_token,
|
145 |
-
outputs=[group_model,
|
146 |
queue=False,
|
147 |
scroll_to_output=True)
|
148 |
|
149 |
-
|
150 |
-
fn=
|
151 |
-
inputs=radio_model_names,
|
152 |
-
outputs=[name, title, description],
|
153 |
queue=False,
|
154 |
scroll_to_output=True)
|
155 |
|
156 |
brn_create.click(
|
157 |
fn=create_and_push,
|
158 |
-
inputs=[space_type, radio_model_names, title, description, prefix, update, input_token],
|
159 |
outputs=[error_output],
|
160 |
scroll_to_output=True
|
161 |
)
|
|
|
1 |
import os
|
|
|
2 |
from huggingface_hub import HfApi, upload_folder
|
3 |
import gradio as gr
|
4 |
|
|
|
9 |
return f"""#### {title}
|
10 |
{error}""" if error else ""
|
11 |
|
12 |
+
def url_to_model_id(model_id_str):
|
13 |
+
return model_id_str.split("/")[-2] + "/" + model_id_str.split("/")[-1] if model_id_str.startswith("https://huggingface.co/") else model_id_str
|
14 |
+
|
15 |
+
def has_diffusion_model(model_id, token):
|
16 |
+
api = HfApi(token=token)
|
17 |
+
return any([f.endswith("diffusion_pytorch_model.bin") for f in api.list_repo_files(repo_id=model_id)])
|
18 |
+
|
19 |
def get_my_model_names(token):
|
20 |
|
21 |
try:
|
22 |
author = whoami(token=token)
|
23 |
model_infos = list_models(author=author["name"], use_auth_token=token)
|
24 |
|
25 |
+
|
26 |
model_names = []
|
27 |
for model_info in model_infos:
|
28 |
model_id = model_info.modelId
|
29 |
+
if has_diffusion_model(model_id, token):
|
30 |
model_names.append(model_id)
|
31 |
|
32 |
if not model_names:
|
|
|
40 |
def on_token_change(token):
|
41 |
model_names, error = get_my_model_names(token)
|
42 |
|
43 |
+
return gr.update(visible=bool(model_names)), gr.update(choices=model_names), gr.update(value=error_str(error))
|
44 |
+
|
45 |
+
def on_load_model(user_model_id, other_model_id, token):
|
46 |
+
|
47 |
+
if not user_model_id and not other_model_id:
|
48 |
+
return None, None, None, None, gr.update(value=error_str("Please enter a model ID."))
|
49 |
+
|
50 |
+
try:
|
51 |
+
model_id = url_to_model_id(other_model_id) if other_model_id else user_model_id
|
52 |
+
original_model_id = model_id
|
53 |
+
|
54 |
+
if not has_diffusion_model(model_id, token):
|
55 |
+
return None, None, None, None, gr.update(value=error_str("There are no diffusion weights in the model you selected."))
|
56 |
|
57 |
+
user = whoami(token=token)
|
58 |
+
model_id = user["name"] + "/" + model_id.split("/")[-1]
|
59 |
+
title = " ".join([w.capitalize() for w in model_id.split("/")[-1].replace("-", " ").replace("_", " ").split(" ")])
|
60 |
+
|
61 |
+
description = f"""Demo for <a href="https://huggingface.co/{original_model_id}">{title}</a> Stable Diffusion model.<br>
|
62 |
+
Add the following tokens to your prompts for the model to work properly: <b>$prefix</b>."""
|
63 |
+
|
64 |
+
return gr.update(visible=True), gr.update(value=model_id), gr.update(value=title), gr.update(value=description), None
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
return None, None, None, None, gr.update(value=error_str(e))
|
68 |
+
|
69 |
+
def create_and_push(space_type, other_model_name, radio_model_names, model_id, title, description, prefix, update, token):
|
70 |
|
71 |
try:
|
72 |
|
|
|
82 |
# 2. Replace the name, title, and description in the template
|
83 |
with open("template/app_simple.py" if space_type == "Simple" else "template/app_advanced.py", "r") as f:
|
84 |
app = f.read()
|
85 |
+
app = app.replace("$model_id", url_to_model_id(other_model_name) if other_model_name else radio_model_names)
|
86 |
app = app.replace("$title", title)
|
87 |
app = app.replace("$description", description)
|
88 |
app = app.replace("$prefix", prefix)
|
|
|
118 |
except Exception as e:
|
119 |
return error_str(e)
|
120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
DESCRIPTION = """### Create a gradio space for your Diffusers🧨 model
|
123 |
With this space, you can easily create a gradio demo for your Diffusers model and share it with the community.
|
|
|
138 |
)
|
139 |
gr.Markdown("You can get a token [here](https://huggingface.co/settings/tokens)")
|
140 |
with gr.Group(visible=False) as group_model:
|
141 |
+
radio_model_names = gr.Radio(label="Your models:")
|
142 |
+
other_model_name = gr.Textbox(label="Other model:", placeholder="URL or model id, e.g. username/model_name")
|
143 |
+
btn_load = gr.Button(value="Load model")
|
144 |
|
145 |
with gr.Column(scale=10):
|
146 |
with gr.Column(visible=False) as group_create:
|
147 |
gr.Markdown("#### 2. Enter details and create the space")
|
148 |
name = gr.Textbox(label="Name", placeholder="e.g. diffusers-demo")
|
149 |
title = gr.Textbox(label="Title", placeholder="e.g. Diffusers Demo")
|
150 |
+
description = gr.Textbox(label="Description", placeholder="e.g. Demo for my awesome Diffusers model", lines=5)
|
151 |
prefix = gr.Textbox(label="Prefix tokens", placeholder="Tokens that are required to be present in the prompt, e.g. `rick and morty style`")
|
152 |
gr.Markdown("""#### Choose space type
|
153 |
- **Simple** - Runs on GPU using Hugging Face inference API, but you cannot control image generation parameters.
|
|
|
164 |
input_token.change(
|
165 |
fn=on_token_change,
|
166 |
inputs=input_token,
|
167 |
+
outputs=[group_model, radio_model_names, error_output],
|
168 |
queue=False,
|
169 |
scroll_to_output=True)
|
170 |
|
171 |
+
btn_load.click(
|
172 |
+
fn=on_load_model,
|
173 |
+
inputs=[radio_model_names, other_model_name, input_token],
|
174 |
+
outputs=[group_create, name, title, description, error_output],
|
175 |
queue=False,
|
176 |
scroll_to_output=True)
|
177 |
|
178 |
brn_create.click(
|
179 |
fn=create_and_push,
|
180 |
+
inputs=[space_type, other_model_name, radio_model_names, name, title, description, prefix, update, input_token],
|
181 |
outputs=[error_output],
|
182 |
scroll_to_output=True
|
183 |
)
|