Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import subprocess
|
3 |
+
from huggingface_hub import HfApi, upload_folder
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
from huggingface_hub import whoami, list_models
|
7 |
+
|
8 |
+
|
9 |
+
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 |
+
api = HfApi(token=token)
|
20 |
+
model_names = []
|
21 |
+
for model_info in model_infos:
|
22 |
+
model_id = model_info.modelId
|
23 |
+
if any([f.endswith("diffusion_pytorch_model.bin") for f in api.list_repo_files(repo_id=model_id)]):
|
24 |
+
model_names.append(model_id)
|
25 |
+
|
26 |
+
if not model_names:
|
27 |
+
return [], Exception("No diffusion models found in your account.")
|
28 |
+
|
29 |
+
return model_names, None
|
30 |
+
|
31 |
+
except Exception as e:
|
32 |
+
return [], e
|
33 |
+
|
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(visible=bool(model_names)), gr.update(choices=model_names), gr.update(visible=bool(model_names)), gr.update(value=error_str(error))
|
38 |
+
|
39 |
+
def create_and_push(model_id, title, description, prefix, update, token):
|
40 |
+
|
41 |
+
try:
|
42 |
+
|
43 |
+
# 1. Create the new space
|
44 |
+
api = HfApi(token=token)
|
45 |
+
repo_url = api.create_repo(
|
46 |
+
repo_id=model_id,
|
47 |
+
exist_ok=update,
|
48 |
+
repo_type="space",
|
49 |
+
space_sdk="gradio",
|
50 |
+
)
|
51 |
+
|
52 |
+
# 2. Replace the name, title, and description in the template
|
53 |
+
with open("template/app.py", "r") as f:
|
54 |
+
app = f.read()
|
55 |
+
app = app.replace("$model_id", model_id)
|
56 |
+
app = app.replace("$title", title)
|
57 |
+
app = app.replace("$description", description)
|
58 |
+
app = app.replace("$prefix", prefix)
|
59 |
+
|
60 |
+
# 3. save the new app.py file
|
61 |
+
with open("app.py", "w") as f:
|
62 |
+
f.write(app)
|
63 |
+
|
64 |
+
# 4. Upload the new app.py to the space
|
65 |
+
api.upload_file(
|
66 |
+
path_or_fileobj="app.py",
|
67 |
+
path_in_repo="app.py",
|
68 |
+
repo_id=model_id,
|
69 |
+
token=token,
|
70 |
+
repo_type="space",
|
71 |
+
)
|
72 |
+
|
73 |
+
# 5. Upload template/requirements.txt to the space
|
74 |
+
api.upload_file(
|
75 |
+
path_or_fileobj="template/requirements.txt",
|
76 |
+
path_in_repo="requirements.txt",
|
77 |
+
repo_id=model_id,
|
78 |
+
token=token,
|
79 |
+
repo_type="space",
|
80 |
+
)
|
81 |
+
|
82 |
+
# 5. Delete the app.py file
|
83 |
+
os.remove("app.py")
|
84 |
+
|
85 |
+
return f"""Successfully created space at: <a href="{repo_url}" target="_blank">{repo_url}</a>"""
|
86 |
+
|
87 |
+
except Exception as e:
|
88 |
+
return error_str(e)
|
89 |
+
|
90 |
+
def on_model_change(radio_model_names):
|
91 |
+
|
92 |
+
name = radio_model_names
|
93 |
+
title = " ".join([w.capitalize() for w in name.split("/")[-1].replace("-", " ").replace("_", " ").split(" ")])
|
94 |
+
|
95 |
+
description = f"""Demo for <a href="https://huggingface.co/{radio_model_names}">{title}</a> Stable Diffusion model.<br>
|
96 |
+
Add the following tokens to your prompts for the model to work properly: <b>$prefix</b>."""
|
97 |
+
|
98 |
+
return gr.update(value=name), gr.update(value=title), gr.update(value=description)
|
99 |
+
|
100 |
+
|
101 |
+
DESCRIPTION = """### Create a gradio space for your Diffusers🧨 model
|
102 |
+
With this space, you can easily create a gradio demo for your Diffusers model and share it with the community.
|
103 |
+
"""
|
104 |
+
|
105 |
+
with gr.Blocks() as demo:
|
106 |
+
|
107 |
+
gr.Markdown(DESCRIPTION)
|
108 |
+
with gr.Row():
|
109 |
+
|
110 |
+
with gr.Column(scale=11):
|
111 |
+
with gr.Column():
|
112 |
+
gr.Markdown("#### 1. Choose a model")
|
113 |
+
input_token = gr.Textbox(
|
114 |
+
max_lines=1,
|
115 |
+
label="Enter your Hugging Face token",
|
116 |
+
placeholder="WRITE permission is required!",
|
117 |
+
)
|
118 |
+
gr.Markdown("You can get a token [here](https://huggingface.co/settings/tokens)")
|
119 |
+
with gr.Group(visible=False) as group_model:
|
120 |
+
radio_model_names = gr.Radio(label="Choose a model")
|
121 |
+
|
122 |
+
with gr.Column(scale=10):
|
123 |
+
with gr.Column(visible=False) as group_create:
|
124 |
+
gr.Markdown("#### 2. Enter details and create the space")
|
125 |
+
name = gr.Textbox(label="Name", placeholder="e.g. diffusers-demo")
|
126 |
+
title = gr.Textbox(label="Title", placeholder="e.g. Diffusers Demo")
|
127 |
+
description = gr.Textbox(label="Description", placeholder="e.g. Demo for my awesome Diffusers model")
|
128 |
+
prefix = gr.Textbox(label="Prefix tokens", placeholder="Tokens that are required to be present in the prompt, e.g. `rick and morty style`")
|
129 |
+
update = gr.Checkbox(label="Update the space if it already exists?")
|
130 |
+
brn_create = gr.Button("Create the space")
|
131 |
+
|
132 |
+
error_output = gr.Markdown(label="Output")
|
133 |
+
|
134 |
+
|
135 |
+
input_token.change(
|
136 |
+
fn=on_token_change,
|
137 |
+
inputs=input_token,
|
138 |
+
outputs=[group_model, group_create, radio_model_names, error_output],
|
139 |
+
queue=False,
|
140 |
+
scroll_to_output=True)
|
141 |
+
|
142 |
+
radio_model_names.change(
|
143 |
+
fn=on_model_change,
|
144 |
+
inputs=radio_model_names,
|
145 |
+
outputs=[name, title, description],
|
146 |
+
queue=False,
|
147 |
+
scroll_to_output=True)
|
148 |
+
|
149 |
+
brn_create.click(
|
150 |
+
fn=create_and_push,
|
151 |
+
inputs=[radio_model_names, title, description, prefix, update, input_token],
|
152 |
+
outputs=[error_output],
|
153 |
+
scroll_to_output=True
|
154 |
+
)
|
155 |
+
|
156 |
+
# gr.Markdown("""<img src="https://raw.githubusercontent.com/huggingface/diffusers/main/docs/source/imgs/diffusers_library.jpg" width="150"/>""")
|
157 |
+
gr.HTML("""
|
158 |
+
<div style="border-top: 1px solid #303030;">
|
159 |
+
<br>
|
160 |
+
<p>Space by: <a href="https://twitter.com/hahahahohohe"><img src="https://img.shields.io/twitter/follow/hahahahohohe?label=%40anzorq&style=social" alt="Twitter Follow"></a></p><br>
|
161 |
+
<a href="https://www.buymeacoffee.com/anzorq" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 45px !important;width: 162px !important;" ></a><br><br>
|
162 |
+
<p><img src="https://visitor-badge.glitch.me/badge?page_id=anzorq.sd-space-creator" alt="visitors"></p>
|
163 |
+
</div>
|
164 |
+
""")
|
165 |
+
|
166 |
+
demo.queue()
|
167 |
+
demo.launch(debug=True)
|