Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,91 +2,102 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import shutil
|
4 |
import tempfile
|
5 |
-
from huggingface_hub import HfApi
|
6 |
from git import Repo
|
7 |
|
8 |
api = HfApi()
|
9 |
|
10 |
-
def clone_and_create(hf_token,
|
11 |
-
if not
|
12 |
-
return "
|
13 |
-
|
14 |
temp_dir = tempfile.mkdtemp()
|
15 |
-
|
16 |
try:
|
17 |
-
#
|
18 |
-
Repo.clone_from(
|
19 |
|
20 |
-
#
|
21 |
if not space_id.strip():
|
22 |
-
space_id =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# README.md
|
25 |
-
readme_path = os.path.join(temp_dir, "README.md")
|
26 |
if repo_type == "space":
|
27 |
-
|
|
|
|
|
28 |
if title.strip():
|
29 |
-
|
30 |
if description.strip():
|
31 |
-
|
32 |
if port.strip():
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
42 |
repo_type=repo_type,
|
43 |
-
|
44 |
-
exist_ok=False
|
45 |
)
|
46 |
|
47 |
-
|
48 |
-
local_repo = Repo(temp_dir)
|
49 |
-
local_repo.create_remote("hf", repo_url)
|
50 |
-
local_repo.git.push("hf", "HEAD:main")
|
51 |
-
|
52 |
-
return f"{repo_type} を作成しました:\n{repo_url}"
|
53 |
|
54 |
except Exception as e:
|
55 |
-
return f"エラー: {str(e)}"
|
|
|
56 |
finally:
|
57 |
shutil.rmtree(temp_dir)
|
58 |
|
59 |
-
with gr.Blocks(title="Hugging Face リポジトリ作成") as demo:
|
60 |
-
gr.Markdown("## Hugging Face に新しい Space / Dataset / Model を作成")
|
61 |
|
62 |
-
|
63 |
-
|
|
|
|
|
64 |
|
65 |
with gr.Column():
|
66 |
-
token = gr.Textbox(label="Hugging Face
|
67 |
-
|
68 |
repo_type = gr.Radio(label="作成タイプ", choices=["space", "dataset", "model"], value="space")
|
69 |
private = gr.Checkbox(label="非公開にする", value=False)
|
70 |
|
71 |
-
with gr.Group(visible=
|
72 |
-
space_id = gr.Textbox(label="Space ID(空欄で自動生成)", placeholder="例: my-
|
73 |
-
port = gr.Textbox(label="起動ポート(README
|
74 |
-
title = gr.Textbox(label="スペース名(README
|
75 |
-
description = gr.Textbox(label="
|
76 |
|
77 |
-
repo_type.change(lambda
|
78 |
|
79 |
submit_btn = gr.Button("作成")
|
80 |
-
output = gr.Textbox(label="
|
81 |
|
82 |
-
|
|
|
83 |
return login_data["access_token"]
|
84 |
|
85 |
-
login.login(
|
86 |
|
|
|
87 |
submit_btn.click(
|
88 |
-
clone_and_create,
|
89 |
-
inputs=[token,
|
90 |
outputs=output
|
91 |
)
|
92 |
|
|
|
2 |
import os
|
3 |
import shutil
|
4 |
import tempfile
|
5 |
+
from huggingface_hub import HfApi
|
6 |
from git import Repo
|
7 |
|
8 |
api = HfApi()
|
9 |
|
10 |
+
def clone_and_create(hf_token, git_url, repo_type, private, space_id, port, title, description):
|
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 |
+
user_info = api.whoami(token=hf_token)
|
25 |
+
repo_id = f"{user_info['name']}/{space_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 |
+
readme_path = os.path.join(temp_dir, "README.md")
|
39 |
+
readme_lines = []
|
40 |
+
|
41 |
if title.strip():
|
42 |
+
readme_lines.append(f"# {title.strip()}")
|
43 |
if description.strip():
|
44 |
+
readme_lines.append(f"\n{description.strip()}")
|
45 |
if port.strip():
|
46 |
+
readme_lines.append(f"\napp_port: {port.strip()}")
|
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"❌ エラー: {str(e)}"
|
64 |
+
|
65 |
finally:
|
66 |
shutil.rmtree(temp_dir)
|
67 |
|
|
|
|
|
68 |
|
69 |
+
with gr.Blocks(title="Hugging Face リポジトリ作成ツール") as demo:
|
70 |
+
gr.Markdown("## 🛠️ Hugging Face Space / Dataset / Model 作成ツール")
|
71 |
+
|
72 |
+
login = gr.LoginButton()
|
73 |
|
74 |
with gr.Column():
|
75 |
+
token = gr.Textbox(label="Hugging Face トークン", type="password")
|
76 |
+
git_url = gr.Textbox(label="Git リポジトリのURL(GitHub / GitLab など)", placeholder="https://...")
|
77 |
repo_type = gr.Radio(label="作成タイプ", choices=["space", "dataset", "model"], value="space")
|
78 |
private = gr.Checkbox(label="非公開にする", value=False)
|
79 |
|
80 |
+
with gr.Group(visible=True) as space_options:
|
81 |
+
space_id = gr.Textbox(label="Space ID(空欄で自動生成)", placeholder="例: my-space")
|
82 |
+
port = gr.Textbox(label="起動ポート(READMEに記載)", placeholder="例: 7860")
|
83 |
+
title = gr.Textbox(label="スペース名(READMEタイトル)", placeholder="例: My App")
|
84 |
+
description = gr.Textbox(label="説明文(READMEに記載)", placeholder="例: これは〇〇なスペースです")
|
85 |
|
86 |
+
repo_type.change(lambda t: gr.update(visible=(t == "space")), inputs=repo_type, outputs=space_options)
|
87 |
|
88 |
submit_btn = gr.Button("作成")
|
89 |
+
output = gr.Textbox(label="出力", lines=5)
|
90 |
|
91 |
+
# ログイン成功時にトークンを自動取得
|
92 |
+
def on_login(login_data):
|
93 |
return login_data["access_token"]
|
94 |
|
95 |
+
login.login(on_login, outputs=token)
|
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 |
|