Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,104 +1,67 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
-
import
|
4 |
-
import tempfile
|
5 |
-
from huggingface_hub import HfApi
|
6 |
from git import Repo
|
7 |
|
8 |
api = HfApi()
|
9 |
|
10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
11 |
if not git_url.strip():
|
12 |
-
return "Git リポジトリのURLを入力してください。"
|
13 |
-
|
14 |
temp_dir = tempfile.mkdtemp()
|
15 |
-
|
16 |
try:
|
17 |
-
# リポジトリをクローン(github 以外も可)
|
18 |
Repo.clone_from(git_url, temp_dir)
|
19 |
-
|
20 |
-
# 空欄ならIDをリポジトリ名から自動生成
|
21 |
if not space_id.strip():
|
22 |
-
space_id = git_url.rstrip("/").split("/")[-1].replace(".git",
|
23 |
-
|
24 |
-
|
25 |
-
repo_id =
|
26 |
-
|
27 |
-
# Hugging Face 上でリポジトリ作成
|
28 |
-
api.create_repo(
|
29 |
-
repo_id=repo_id,
|
30 |
-
repo_type=repo_type,
|
31 |
-
private=private,
|
32 |
-
token=hf_token,
|
33 |
-
exist_ok=False
|
34 |
-
)
|
35 |
-
|
36 |
-
# README.md の作成(Space のときだけ)
|
37 |
if repo_type == "space":
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
if
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
if readme_lines:
|
49 |
-
with open(readme_path, "w", encoding="utf-8") as f:
|
50 |
-
f.write("\n".join(readme_lines))
|
51 |
-
|
52 |
-
# クローンしたファイルをアップロード
|
53 |
-
api.upload_folder(
|
54 |
-
folder_path=temp_dir,
|
55 |
-
repo_id=repo_id,
|
56 |
-
repo_type=repo_type,
|
57 |
-
token=hf_token
|
58 |
-
)
|
59 |
-
|
60 |
-
return f"✅ {repo_type} を作成しました!\nhttps://huggingface.co/{repo_type}s/{space_id}"
|
61 |
-
|
62 |
except Exception as e:
|
63 |
-
return f"❌ エラー: {
|
64 |
-
|
65 |
finally:
|
66 |
shutil.rmtree(temp_dir)
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
gr.Markdown("## 🛠️ Hugging Face Space / Dataset / Model 作成ツール")
|
71 |
-
|
72 |
login = gr.LoginButton()
|
73 |
-
|
74 |
with gr.Column():
|
75 |
-
|
76 |
-
|
77 |
-
repo_type = gr.Radio(label="作成タイプ", choices=["space", "dataset", "model"], value="space")
|
78 |
private = gr.Checkbox(label="非公開にする", value=False)
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
# 実行
|
98 |
-
submit_btn.click(
|
99 |
-
fn=clone_and_create,
|
100 |
-
inputs=[token, git_url, repo_type, private, space_id, port, title, description],
|
101 |
-
outputs=output
|
102 |
-
)
|
103 |
|
104 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import os, shutil, tempfile
|
3 |
+
from huggingface_hub import HfApi, whoami
|
|
|
|
|
4 |
from git import Repo
|
5 |
|
6 |
api = HfApi()
|
7 |
|
8 |
+
def create_from_repo(oauth_token: gr.OAuthToken | None,
|
9 |
+
git_url, repo_type, private,
|
10 |
+
space_id, port, title, description):
|
11 |
+
if oauth_token is None:
|
12 |
+
return "⚠️ Hugging Face にログインしてください。"
|
13 |
+
token = oauth_token.token
|
14 |
if not git_url.strip():
|
15 |
+
return "Git リポジトリの URL を入力してください。"
|
|
|
16 |
temp_dir = tempfile.mkdtemp()
|
|
|
17 |
try:
|
|
|
18 |
Repo.clone_from(git_url, temp_dir)
|
|
|
|
|
19 |
if not space_id.strip():
|
20 |
+
space_id = git_url.rstrip("/").split("/")[-1].replace(".git","")
|
21 |
+
user = whoami(token=token)
|
22 |
+
repo_id = f"{user['name']}/{space_id}"
|
23 |
+
api.create_repo(repo_id=repo_id, repo_type=repo_type,
|
24 |
+
private=private, token=token, exist_ok=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
if repo_type == "space":
|
26 |
+
readme = []
|
27 |
+
if title.strip(): readme.append(f"# {title.strip()}")
|
28 |
+
if description.strip(): readme.append(f"\n{description.strip()}")
|
29 |
+
if port.strip(): readme.append(f"\napp_port: {port.strip()}")
|
30 |
+
if readme:
|
31 |
+
with open(os.path.join(temp_dir,"README.md"),"w",encoding="utf‑8") as f:
|
32 |
+
f.write("\n".join(readme))
|
33 |
+
api.upload_folder(folder_path=temp_dir, repo_id=repo_id,
|
34 |
+
repo_type=repo_type, token=token)
|
35 |
+
return f"✅ {repo_type} を作成しました: https://huggingface.co/{repo_id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
+
return f"❌ エラー: {e}"
|
|
|
38 |
finally:
|
39 |
shutil.rmtree(temp_dir)
|
40 |
|
41 |
+
with gr.Blocks() as demo:
|
42 |
+
gr.Markdown("## Hugging Face に Space / Dataset / Model を作成")
|
|
|
|
|
43 |
login = gr.LoginButton()
|
|
|
44 |
with gr.Column():
|
45 |
+
git_url = gr.Textbox(label="Git リポジトリ URL", placeholder="https://...")
|
46 |
+
repo_type = gr.Radio(["space","dataset","model"], label="タイプ", value="space")
|
|
|
47 |
private = gr.Checkbox(label="非公開にする", value=False)
|
48 |
+
space_id = gr.Textbox(label="Space ID(空欄で自動生成)", visible=True)
|
49 |
+
port = gr.Textbox(label="起動ポート(README)", visible=True)
|
50 |
+
title = gr.Textbox(label="スペース名", visible=True)
|
51 |
+
description = gr.Textbox(label="説明文", visible=True)
|
52 |
+
output = gr.Textbox(label="出力", lines=4)
|
53 |
+
def toggle_spaceopts(rtype):
|
54 |
+
vis = (rtype=="space")
|
55 |
+
return {space_id: gr.update(visible=vis),
|
56 |
+
port: gr.update(visible=vis),
|
57 |
+
title: gr.update(visible=vis),
|
58 |
+
description: gr.update(visible=vis)}
|
59 |
+
repo_type.change(toggle_spaceopts, inputs=repo_type,
|
60 |
+
outputs=[space_id, port, title, description])
|
61 |
+
|
62 |
+
login.click(create_from_repo,
|
63 |
+
inputs=[gr.OAuthToken(), git_url, repo_type, private,
|
64 |
+
space_id, port, title, description],
|
65 |
+
outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
demo.launch()
|