nevreal commited on
Commit
6a47005
1 Parent(s): 9d702c7

i have base code

Browse files

maybe this kinda help, lol

Files changed (1) hide show
  1. 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
- def is_repo_name(s):
6
- import re
7
- return re.fullmatch(r'^[^/,\s]+?/[^/,\s]+?$', s)
8
-
9
-
10
- def is_repo_exists(repo_id):
11
- from huggingface_hub import HfApi
12
- api = HfApi()
13
- try:
14
- if api.repo_exists(repo_id=repo_id, repo_type="model"): return True
15
- else: return False
16
- except Exception as e:
17
- print(f"Error: Failed to connect {repo_id}.")
18
- return True # for safe
19
-
20
-
21
- def is_repo_t2i(repo_id):
22
- from huggingface_hub import HfApi
23
- api = HfApi()
24
- try:
25
- model_info = api.repo_info(repo_id=repo_id, repo_type="model")
26
- if model_info.pipeline_tag == "text-to-image": return True
27
- else: return False
28
- except Exception as e:
29
- print(f"Error: Failed to connect {repo_id}.")
30
- return True # for safe
31
-
32
-
33
- def save_space_contents(repo_id: str, gradio_version: str, private_ok: bool, dir: str):
34
- if not is_repo_name(repo_id): # or not is_repo_t2i(repo_id)
35
- gr.Info(f"Error: Invalid repo ID: {repo_id}.")
36
- return []
37
- if not private_ok and not is_repo_exists(repo_id):
38
- gr.Info(f"Error: Repo doesn't exist: {repo_id}.")
39
- return []
40
- os.makedirs(dir, exist_ok=True)
41
- model_name = repo_id.split("/")[-1]
42
- app_py = f"""import gradio as gr
43
- import os
44
- demo = gr.load("{repo_id}", src="models", hf_token=os.environ.get("HF_TOKEN")).launch()
45
- """
46
- readme_md = f"""---
47
- title: {model_name} Demo
48
- emoji: 🖼
49
- colorFrom: purple
50
- colorTo: red
51
- sdk: gradio
52
- sdk_version: {gradio_version}
53
- app_file: app.py
54
- pinned: false
55
- ---
56
-
57
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
58
-
59
- """
60
- app_path = str(Path(dir, "app.py"))
61
- with open(app_path, mode='w', encoding="utf-8") as f:
62
- f.write(app_py)
63
- readme_path = str(Path(dir, "README.md"))
64
- with open(readme_path, mode='w', encoding="utf-8") as f:
65
- f.write(readme_md)
66
- return [app_path, readme_path]
67
-
68
-
69
- def get_t2i_space_contents(repo_id: str, gradio_version: str, private_ok: bool):
70
- return save_space_contents(repo_id, gradio_version, private_ok, "./temp/")
 
 
 
 
 
 
 
 
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)