Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- README.md +12 -12
- app.py +32 -0
- requirements.txt +1 -0
- t2i_space.py +67 -0
README.md
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Gradio Text-to-Image Demo Space creation helper
|
| 3 |
+
emoji: 🐶
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: red
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.38.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from t2i_space import get_t2i_space_contents
|
| 3 |
+
|
| 4 |
+
css = """"""
|
| 5 |
+
|
| 6 |
+
with gr.Blocks(theme="NoCrypt/miku@>=1.2.2", css=css) as demo:
|
| 7 |
+
gr.Markdown("# Gradio Text-to-Image Demo Space creation helper")
|
| 8 |
+
gr.Markdown(
|
| 9 |
+
f"""
|
| 10 |
+
**The steps are the following**:
|
| 11 |
+
- Input text-to-image model Repo ID which you want to use. e.g. 'user/model'.
|
| 12 |
+
- Click "Submit" and download generated README.md and app.py.
|
| 13 |
+
- [Create your new Gradio space](https://huggingface.co/new-space) with blank.
|
| 14 |
+
- Upload README.md and app.py to your space.
|
| 15 |
+
- If you got 500 error, it happens often, just restart space.
|
| 16 |
+
- If it does not work no matter how many times you try, there is often a problem with the settings of the original repo itself, or there is no generation function.
|
| 17 |
+
"""
|
| 18 |
+
)
|
| 19 |
+
with gr.Column():
|
| 20 |
+
repo_id = gr.Textbox(label="Model repo ID", placeholder="username/modelname", value="", max_lines=1)
|
| 21 |
+
run_button = gr.Button(value="Submit")
|
| 22 |
+
space_file = gr.Files(label="Output", interactive=False)
|
| 23 |
+
|
| 24 |
+
gr.on(
|
| 25 |
+
triggers=[repo_id.submit, run_button.click],
|
| 26 |
+
fn=get_t2i_space_contents,
|
| 27 |
+
inputs=[repo_id, space_file],
|
| 28 |
+
outputs=[space_file],
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
demo.queue()
|
| 32 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub
|
t2i_space.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def is_repo_name(s):
|
| 5 |
+
import re
|
| 6 |
+
return re.fullmatch(r'^[^/\.,\s]+?/[^/\.,\s]+?$', s)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def is_repo_exists(repo_id):
|
| 10 |
+
from huggingface_hub import HfApi
|
| 11 |
+
api = HfApi()
|
| 12 |
+
try:
|
| 13 |
+
if api.repo_exists(repo_id=repo_id, repo_type="model"): return True
|
| 14 |
+
else: return False
|
| 15 |
+
except Exception as e:
|
| 16 |
+
print(f"Error: Failed to connect {repo_id}. ")
|
| 17 |
+
return True # for safe
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def is_repo_t2i(repo_id):
|
| 21 |
+
from huggingface_hub import HfApi
|
| 22 |
+
api = HfApi()
|
| 23 |
+
try:
|
| 24 |
+
model_info = api.repo_info(repo_id=repo_id, repo_type="model")
|
| 25 |
+
if model_info.pipeline_tag == "text-to-image": return True
|
| 26 |
+
else: return False
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Error: Failed to connect {repo_id}. ")
|
| 29 |
+
return True # for safe
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def save_space_contents(repo_id: str, dir: str):
|
| 33 |
+
if not is_repo_name(repo_id) or not is_repo_exists(repo_id) or not is_repo_t2i(repo_id):
|
| 34 |
+
print(f"Error: Invalid repo ID: {repo_id}. ")
|
| 35 |
+
return []
|
| 36 |
+
model_name = repo_id.split("/")[-1]
|
| 37 |
+
app_py = f"""
|
| 38 |
+
import gradio as gr
|
| 39 |
+
demo = gr.load("{repo_id}", src="models").launch()
|
| 40 |
+
demo.launch()
|
| 41 |
+
"""
|
| 42 |
+
readme_md = f"""---
|
| 43 |
+
title: {model_name} Text-to-Image
|
| 44 |
+
emoji: 🖼
|
| 45 |
+
colorFrom: purple
|
| 46 |
+
colorTo: red
|
| 47 |
+
sdk: gradio
|
| 48 |
+
sdk_version: 4.38.1
|
| 49 |
+
app_file: app.py
|
| 50 |
+
pinned: false
|
| 51 |
+
short_description: {repo_id} | Text-to-Image
|
| 52 |
+
---
|
| 53 |
+
|
| 54 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
app_path = str(Path(dir, "app.py"))
|
| 58 |
+
with open(app_path, mode='w', encoding="utf-8") as f:
|
| 59 |
+
f.write(app_py)
|
| 60 |
+
readme_path = str(Path(dir, "README.md"))
|
| 61 |
+
with open(readme_path, mode='w', encoding="utf-8") as f:
|
| 62 |
+
f.write(readme_md)
|
| 63 |
+
return [app_path, readme_path]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def get_t2i_space_contents(repo_id: str):
|
| 67 |
+
return save_space_contents(repo_id, "./temp/")
|