Spaces:
Sleeping
Sleeping
Update repo_to_space_auto.py
Browse files- repo_to_space_auto.py +58 -14
repo_to_space_auto.py
CHANGED
|
@@ -3,19 +3,16 @@
|
|
| 3 |
repo_to_space_auto.py
|
| 4 |
---------------------
|
| 5 |
๊ณต๊ฐ Git ๋ ํฌ๋ฅผ Hugging Face Gradio Space๋ก ์๋ ๋ฐฐํฌํฉ๋๋ค.
|
| 6 |
-
|
| 7 |
ํ์ ํ๊ฒฝ๋ณ์
|
| 8 |
-------------
|
| 9 |
BAPI_TOKEN : Brave Search API Key
|
| 10 |
FRIENDLI_TOKEN : Friendli Dedicated Endpoint Token
|
| 11 |
-
|
| 12 |
CLI ์ธ์
|
| 13 |
--------
|
| 14 |
--repo_url <URL> : ์๋ณธ Git ๋ ํฌ URL (ํ์*ยน)
|
| 15 |
--hf_token <TOK> : ์ฐ๊ธฐ ๊ถํ Hugging Face Access Token (ํ์*ยน)
|
| 16 |
--private : ๋น๊ณต๊ฐ Space๋ก ์์ฑ
|
| 17 |
--hardware <tier> : ์) 't4-medium' GPU ์ธ์คํด์ค ์ง์
|
| 18 |
-
|
| 19 |
*ยน ์ธ์๋ฅผ ์๋ตํ๋ฉด ๋๋ช
ํ๊ฒฝ๋ณ์(REPO_URL, HF_TOKEN)๊ฐ ๋์ ์ฌ์ฉ๋ฉ๋๋ค.
|
| 20 |
"""
|
| 21 |
|
|
@@ -68,8 +65,23 @@ def deploy(repo_url: str, hf_token: str, private=False, hardware=None) -> str:
|
|
| 68 |
api = HfApi(token=hf_token)
|
| 69 |
user = api.whoami()["name"]
|
| 70 |
space = f"{user}/{Path(repo_url).stem.lower().replace('.', '-')}"
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
with tempfile.TemporaryDirectory() as work:
|
| 75 |
# 1) Brave ๋ฉํ๋ฐ์ดํฐ
|
|
@@ -83,16 +95,14 @@ def deploy(repo_url: str, hf_token: str, private=False, hardware=None) -> str:
|
|
| 83 |
if (src / "README.md").exists():
|
| 84 |
readme = (src / "README.md").read_text(encoding="utf-8", errors="ignore")[:4000]
|
| 85 |
|
| 86 |
-
tree_out = subprocess.run(["bash", "-lc", f"tree -L 2 {src} | head -n 40"],
|
| 87 |
text=True, capture_output=True).stdout
|
| 88 |
|
| 89 |
context = textwrap.dedent(f"""
|
| 90 |
## Brave meta
|
| 91 |
{json.dumps(brave_meta, ensure_ascii=False, indent=2)}
|
| 92 |
-
|
| 93 |
## Repository tree (depth 2)
|
| 94 |
{tree_out}
|
| 95 |
-
|
| 96 |
## README (first 4 kB)
|
| 97 |
{readme}
|
| 98 |
""")
|
|
@@ -102,17 +112,51 @@ def deploy(repo_url: str, hf_token: str, private=False, hardware=None) -> str:
|
|
| 102 |
|
| 103 |
# 4) Space ๋ ํฌ ํด๋ก & ํ์ผ ์ฐ๊ธฐ
|
| 104 |
dst = Path(work) / "space"
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
(dst / "app.py").write_text(scaffold["app_py"], encoding="utf-8")
|
| 108 |
(dst / "requirements.txt").write_text(scaffold["requirements_txt"], encoding="utf-8")
|
| 109 |
if scaffold.get("need_docker"):
|
| 110 |
(dst / "Dockerfile").write_text(scaffold["dockerfile"], encoding="utf-8")
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
return f"https://huggingface.co/spaces/{space}"
|
| 118 |
|
|
@@ -138,4 +182,4 @@ def main():
|
|
| 138 |
sys.exit(1)
|
| 139 |
|
| 140 |
if __name__ == "__main__":
|
| 141 |
-
main()
|
|
|
|
| 3 |
repo_to_space_auto.py
|
| 4 |
---------------------
|
| 5 |
๊ณต๊ฐ Git ๋ ํฌ๋ฅผ Hugging Face Gradio Space๋ก ์๋ ๋ฐฐํฌํฉ๋๋ค.
|
|
|
|
| 6 |
ํ์ ํ๊ฒฝ๋ณ์
|
| 7 |
-------------
|
| 8 |
BAPI_TOKEN : Brave Search API Key
|
| 9 |
FRIENDLI_TOKEN : Friendli Dedicated Endpoint Token
|
|
|
|
| 10 |
CLI ์ธ์
|
| 11 |
--------
|
| 12 |
--repo_url <URL> : ์๋ณธ Git ๋ ํฌ URL (ํ์*ยน)
|
| 13 |
--hf_token <TOK> : ์ฐ๊ธฐ ๊ถํ Hugging Face Access Token (ํ์*ยน)
|
| 14 |
--private : ๋น๊ณต๊ฐ Space๋ก ์์ฑ
|
| 15 |
--hardware <tier> : ์) 't4-medium' GPU ์ธ์คํด์ค ์ง์
|
|
|
|
| 16 |
*ยน ์ธ์๋ฅผ ์๋ตํ๋ฉด ๋๋ช
ํ๊ฒฝ๋ณ์(REPO_URL, HF_TOKEN)๊ฐ ๋์ ์ฌ์ฉ๋ฉ๋๋ค.
|
| 17 |
"""
|
| 18 |
|
|
|
|
| 65 |
api = HfApi(token=hf_token)
|
| 66 |
user = api.whoami()["name"]
|
| 67 |
space = f"{user}/{Path(repo_url).stem.lower().replace('.', '-')}"
|
| 68 |
+
|
| 69 |
+
# create_repo ํธ์ถ (hardware ํ๋ผ๋ฏธํฐ ์ ๊ฑฐ)
|
| 70 |
+
api.create_repo(
|
| 71 |
+
repo_id=space,
|
| 72 |
+
repo_type="space",
|
| 73 |
+
space_sdk="gradio",
|
| 74 |
+
private=private,
|
| 75 |
+
exist_ok=True
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# hardware ์ค์ ์ด ํ์ํ ๊ฒฝ์ฐ ๋ณ๋๋ก update_repo_settings ํธ์ถ
|
| 79 |
+
if hardware:
|
| 80 |
+
try:
|
| 81 |
+
# Space ์ค์ ์
๋ฐ์ดํธ (hardware)
|
| 82 |
+
api.request_space_hardware(repo_id=space, hardware=hardware)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"โ ๏ธ Hardware ์ค์ ์คํจ (Space๋ ์์ฑ๋จ): {e}")
|
| 85 |
|
| 86 |
with tempfile.TemporaryDirectory() as work:
|
| 87 |
# 1) Brave ๋ฉํ๋ฐ์ดํฐ
|
|
|
|
| 95 |
if (src / "README.md").exists():
|
| 96 |
readme = (src / "README.md").read_text(encoding="utf-8", errors="ignore")[:4000]
|
| 97 |
|
| 98 |
+
tree_out = subprocess.run(["bash", "-lc", f"tree -L 2 {src} 2>/dev/null || find {src} -maxdepth 2 -type f | head -n 40"],
|
| 99 |
text=True, capture_output=True).stdout
|
| 100 |
|
| 101 |
context = textwrap.dedent(f"""
|
| 102 |
## Brave meta
|
| 103 |
{json.dumps(brave_meta, ensure_ascii=False, indent=2)}
|
|
|
|
| 104 |
## Repository tree (depth 2)
|
| 105 |
{tree_out}
|
|
|
|
| 106 |
## README (first 4 kB)
|
| 107 |
{readme}
|
| 108 |
""")
|
|
|
|
| 112 |
|
| 113 |
# 4) Space ๋ ํฌ ํด๋ก & ํ์ผ ์ฐ๊ธฐ
|
| 114 |
dst = Path(work) / "space"
|
| 115 |
+
space_repo = git.Repo.clone_from(
|
| 116 |
+
f"https://huggingface.co/spaces/{space}",
|
| 117 |
+
dst,
|
| 118 |
+
env={"HF_TOKEN": hf_token}
|
| 119 |
+
)
|
| 120 |
|
| 121 |
(dst / "app.py").write_text(scaffold["app_py"], encoding="utf-8")
|
| 122 |
(dst / "requirements.txt").write_text(scaffold["requirements_txt"], encoding="utf-8")
|
| 123 |
if scaffold.get("need_docker"):
|
| 124 |
(dst / "Dockerfile").write_text(scaffold["dockerfile"], encoding="utf-8")
|
| 125 |
+
|
| 126 |
+
# README.md์ ์๋ณธ ๋ ํฌ ์ ๋ณด ์ถ๊ฐ
|
| 127 |
+
readme_content = f"""---
|
| 128 |
+
title: {Path(repo_url).stem}
|
| 129 |
+
emoji: ๐
|
| 130 |
+
colorFrom: blue
|
| 131 |
+
colorTo: green
|
| 132 |
+
sdk: gradio
|
| 133 |
+
sdk_version: 4.44.1
|
| 134 |
+
app_file: app.py
|
| 135 |
+
pinned: false
|
| 136 |
+
---
|
| 137 |
+
|
| 138 |
+
# {Path(repo_url).stem}
|
| 139 |
+
|
| 140 |
+
Automatically deployed from: {repo_url}
|
| 141 |
+
|
| 142 |
+
## Summary
|
| 143 |
+
{scaffold["summary"]}
|
| 144 |
+
|
| 145 |
+
---
|
| 146 |
+
*Created by HF Space Auto-Deployer*
|
| 147 |
+
"""
|
| 148 |
+
(dst / "README.md").write_text(readme_content, encoding="utf-8")
|
| 149 |
|
| 150 |
+
# Git ์ปค๋ฐ ๋ฐ ํธ์
|
| 151 |
+
space_repo.index.add(["app.py", "requirements.txt", "README.md"])
|
| 152 |
+
if scaffold.get("need_docker"):
|
| 153 |
+
space_repo.index.add(["Dockerfile"])
|
| 154 |
+
|
| 155 |
+
space_repo.index.commit("Initial auto-deploy")
|
| 156 |
+
|
| 157 |
+
# Push with authentication
|
| 158 |
+
origin = space_repo.remote("origin")
|
| 159 |
+
origin.push(env={"HF_TOKEN": hf_token})
|
| 160 |
|
| 161 |
return f"https://huggingface.co/spaces/{space}"
|
| 162 |
|
|
|
|
| 182 |
sys.exit(1)
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
+
main()
|