openfree commited on
Commit
9297033
ยท
verified ยท
1 Parent(s): fe395b2

Delete repo_to_space_auto.py

Browse files
Files changed (1) hide show
  1. repo_to_space_auto.py +0 -185
repo_to_space_auto.py DELETED
@@ -1,185 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
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
-
19
- import os, sys, json, argparse, subprocess, tempfile, textwrap, requests, shutil
20
- from pathlib import Path
21
- import git # GitPython
22
- from huggingface_hub import HfApi, login # HF Hub SDK
23
-
24
- # ---------- Brave Search ํ—ฌํผ ---------- #
25
- def brave_search_repo(repo_url: str, count: int = 5) -> list[dict]:
26
- api_key = os.getenv("BAPI_TOKEN")
27
- if not api_key:
28
- raise RuntimeError("ํ™˜๊ฒฝ๋ณ€์ˆ˜ BAPI_TOKEN์ด ์„ค์ •๋ผ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
29
- headers = {"X-Subscription-Token": api_key, "Accept": "application/json"}
30
- params = {"q": f'site:github.com "{repo_url}"', "count": count, "search_lang": "en"}
31
- resp = requests.get("https://api.search.brave.com/res/v1/web/search",
32
- headers=headers, params=params, timeout=10)
33
- resp.raise_for_status()
34
- return resp.json().get("web", {}).get("results", [])
35
-
36
- # ---------- Friendli LLM ํ—ฌํผ ---------- #
37
- def friendli_generate_scaffold(context: str) -> dict:
38
- token = os.getenv("FRIENDLI_TOKEN")
39
- if not token:
40
- raise RuntimeError("ํ™˜๊ฒฝ๋ณ€์ˆ˜ FRIENDLI_TOKEN์ด ์„ค์ •๋ผ ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
41
- payload = {
42
- "model": "dep89a2fld32mcm",
43
- "messages": [
44
- {"role": "system",
45
- "content": ("You are an expert Hugging Face Space architect. "
46
- "Given repository context, output JSON with keys "
47
- "`app_py`, `requirements_txt`, `need_docker` (bool), "
48
- "`dockerfile` (if needed) and `summary`.")},
49
- {"role": "user", "content": context}
50
- ],
51
- "max_tokens": 16384,
52
- "top_p": 0.8,
53
- "stream": False
54
- }
55
- headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
56
- r = requests.post("https://api.friendli.ai/dedicated/v1/chat/completions",
57
- json=payload, headers=headers, timeout=120)
58
- r.raise_for_status()
59
- return json.loads(r.json()["choices"][0]["message"]["content"])
60
-
61
- # ---------- ๋ฉ”์ธ ๋ฐฐํฌ ๋กœ์ง ---------- #
62
- def deploy(repo_url: str, hf_token: str, private=False, hardware=None) -> str:
63
- """๋ ˆํฌ๋ฅผ Gradio Space๋กœ ๋ฐฐํฌํ•˜๊ณ  Space URL ๋ฐ˜ํ™˜."""
64
- login(hf_token)
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 ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
88
- brave_meta = brave_search_repo(repo_url)
89
-
90
- # 2) ์›๋ณธ ๋ ˆํฌ ํด๋ก 
91
- src = Path(work) / "src"
92
- git.Repo.clone_from(repo_url, src)
93
-
94
- readme = ""
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
- """)
109
-
110
- # 3) Friendli ์Šค์บํด๋“œ ์ƒ์„ฑ
111
- scaffold = friendli_generate_scaffold(context)
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
-
163
- # ---------- CLI ์—”ํŠธ๋ฆฌ ---------- #
164
- def main():
165
- parser = argparse.ArgumentParser(description="Git ๋ ˆํฌ๋ฅผ Hugging Face Gradio Space๋กœ ์ž๋™ ๋ฐฐํฌ")
166
- parser.add_argument("--repo_url", default=os.getenv("REPO_URL"),
167
- help="์›๋ณธ Git ๋ ˆํฌ์ง€ํ† ๋ฆฌ URL (or env REPO_URL)")
168
- parser.add_argument("--hf_token", default=os.getenv("HF_TOKEN"),
169
- help="์“ฐ๊ธฐ ๊ถŒํ•œ HF ํ† ํฐ (or env HF_TOKEN)")
170
- parser.add_argument("--private", action="store_true", help="๋น„๊ณต๊ฐœ Space ์ƒ์„ฑ")
171
- parser.add_argument("--hardware", default=None, help="์˜ˆ: 't4-medium'")
172
- args = parser.parse_args()
173
-
174
- if not args.repo_url or not args.hf_token:
175
- parser.error("--repo_url ๋ฐ --hf_token (๋˜๋Š” ๋™๋ช… ํ™˜๊ฒฝ๋ณ€์ˆ˜) ๋‘˜ ๋‹ค ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
176
-
177
- try:
178
- url = deploy(args.repo_url, args.hf_token, args.private, args.hardware)
179
- print(f"โœ… ๋ฐฐํฌ ์„ฑ๊ณต: {url}")
180
- except Exception as e:
181
- print(f"โŒ ๋ฐฐํฌ ์‹คํŒจ: {e}", file=sys.stderr)
182
- sys.exit(1)
183
-
184
- if __name__ == "__main__":
185
- main()