Spaces:
Running
Running
i have base code
Browse filesmaybe this kinda help, lol
- t2i_space.py +77 -70
t2i_space.py
CHANGED
@@ -1,70 +1,77 @@
|
|
1 |
-
from pathlib import Path
|
2 |
-
import os
|
3 |
-
import gradio as gr
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
def is_repo_exists(repo_id):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
""
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pathlib import Path
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
from huggingface_hub import HfApi, HfFolder, CommitOperationAdd, create_repo, Repository
|
5 |
+
|
6 |
+
def is_repo_name(s):
|
7 |
+
import re
|
8 |
+
return re.fullmatch(r'^[^/,\s]+?/[^/,\s]+?$', s)
|
9 |
+
|
10 |
+
def is_repo_exists(repo_id):
|
11 |
+
api = HfApi()
|
12 |
+
try:
|
13 |
+
return api.repo_exists(repo_id=repo_id, repo_type="space")
|
14 |
+
except Exception as e:
|
15 |
+
print(f"Error: Failed to connect {repo_id}.")
|
16 |
+
return True # for safe handling
|
17 |
+
|
18 |
+
def save_space_contents(repo_id: str, gradio_version: str, private_ok: bool, dir: str):
|
19 |
+
if not is_repo_name(repo_id):
|
20 |
+
gr.Info(f"Error: Invalid repo ID: {repo_id}.")
|
21 |
+
return []
|
22 |
+
if not private_ok and not is_repo_exists(repo_id):
|
23 |
+
gr.Info(f"Error: Repo doesn't exist: {repo_id}.")
|
24 |
+
return []
|
25 |
+
os.makedirs(dir, exist_ok=True)
|
26 |
+
model_name = repo_id.split("/")[-1]
|
27 |
+
app_py = f"""import gradio as gr
|
28 |
+
import os
|
29 |
+
demo = gr.load("{repo_id}", src="models", hf_token=os.environ.get("HF_TOKEN")).launch()
|
30 |
+
"""
|
31 |
+
readme_md = f"""---
|
32 |
+
title: {model_name} Demo
|
33 |
+
emoji: 🖼
|
34 |
+
colorFrom: purple
|
35 |
+
colorTo: red
|
36 |
+
sdk: gradio
|
37 |
+
sdk_version: {gradio_version}
|
38 |
+
app_file: app.py
|
39 |
+
pinned: false
|
40 |
+
---
|
41 |
+
|
42 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
43 |
+
"""
|
44 |
+
app_path = str(Path(dir, "app.py"))
|
45 |
+
with open(app_path, mode='w', encoding="utf-8") as f:
|
46 |
+
f.write(app_py)
|
47 |
+
readme_path = str(Path(dir, "README.md"))
|
48 |
+
with open(readme_path, mode='w', encoding="utf-8") as f:
|
49 |
+
f.write(readme_md)
|
50 |
+
return [app_path, readme_path]
|
51 |
+
|
52 |
+
def upload_to_space(repo_id, app_path, readme_path):
|
53 |
+
api = HfApi()
|
54 |
+
token = HfFolder.get_token()
|
55 |
+
|
56 |
+
# Create the repo if it does not exist
|
57 |
+
if not is_repo_exists(repo_id):
|
58 |
+
create_repo(repo_id, repo_type="space", token=token, private=False)
|
59 |
+
|
60 |
+
# Upload files to the repository
|
61 |
+
operations = [
|
62 |
+
CommitOperationAdd(path_in_repo="app.py", path_or_fileobj=app_path),
|
63 |
+
CommitOperationAdd(path_in_repo="README.md", path_or_fileobj=readme_path),
|
64 |
+
]
|
65 |
+
api.create_commit(
|
66 |
+
repo_id=repo_id,
|
67 |
+
repo_type="space",
|
68 |
+
operations=operations,
|
69 |
+
commit_message="Add Gradio app and README",
|
70 |
+
token=token,
|
71 |
+
)
|
72 |
+
print(f"Files uploaded successfully to {repo_id}.")
|
73 |
+
|
74 |
+
def get_t2i_space_contents(repo_id: str, gradio_version: str, private_ok: bool):
|
75 |
+
paths = save_space_contents(repo_id, gradio_version, private_ok, "./temp/")
|
76 |
+
if paths:
|
77 |
+
upload_to_space(repo_id, *paths)
|