Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
import tempfile
|
5 |
+
from huggingface_hub import HfApi, create_repo, create_repo_readme
|
6 |
+
from git import Repo
|
7 |
+
|
8 |
+
api = HfApi()
|
9 |
+
|
10 |
+
def clone_and_create(hf_token, github_url, repo_type, private, space_id, port, title, description):
|
11 |
+
if not github_url.strip():
|
12 |
+
return "GitHub URLを入力してください。"
|
13 |
+
|
14 |
+
temp_dir = tempfile.mkdtemp()
|
15 |
+
|
16 |
+
try:
|
17 |
+
# GitHubからクローン
|
18 |
+
Repo.clone_from(github_url, temp_dir)
|
19 |
+
|
20 |
+
# 自動生成ID
|
21 |
+
if not space_id.strip():
|
22 |
+
space_id = github_url.strip().rstrip('/').split('/')[-1]
|
23 |
+
|
24 |
+
# README.mdの処理(Space用)
|
25 |
+
readme_path = os.path.join(temp_dir, "README.md")
|
26 |
+
if repo_type == "space":
|
27 |
+
lines = []
|
28 |
+
if title.strip():
|
29 |
+
lines.append(f"# {title.strip()}")
|
30 |
+
if description.strip():
|
31 |
+
lines.append(f"\n{description.strip()}")
|
32 |
+
if port.strip():
|
33 |
+
lines.append(f"\napp_port: {port.strip()}")
|
34 |
+
readme_text = "\n".join(lines)
|
35 |
+
with open(readme_path, "w", encoding="utf-8") as f:
|
36 |
+
f.write(readme_text)
|
37 |
+
|
38 |
+
# huggingface に作成
|
39 |
+
repo_url = create_repo(
|
40 |
+
name=space_id,
|
41 |
+
token=hf_token,
|
42 |
+
repo_type=repo_type,
|
43 |
+
private=private,
|
44 |
+
exist_ok=False
|
45 |
+
)
|
46 |
+
|
47 |
+
# リポジトリをpush
|
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 |
+
with gr.Row():
|
63 |
+
login = gr.LoginButton()
|
64 |
+
|
65 |
+
with gr.Column():
|
66 |
+
token = gr.Textbox(label="Hugging Face Token(ログイン後自動取得)", type="password")
|
67 |
+
github_url = gr.Textbox(label="GitHub リポジトリURL", placeholder="https://github.com/your/repo")
|
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=False) as space_options:
|
72 |
+
space_id = gr.Textbox(label="Space ID(空欄で自動生成)", placeholder="例: my-awesome-space")
|
73 |
+
port = gr.Textbox(label="起動ポート(README.mdに追加)", placeholder="例: 7860")
|
74 |
+
title = gr.Textbox(label="スペース名(READMEのタイトル)", placeholder="例: My Space")
|
75 |
+
description = gr.Textbox(label="説明(READMEのshort_description)", placeholder="例: これは○○なスペースです")
|
76 |
+
|
77 |
+
repo_type.change(lambda x: gr.update(visible=(x == "space")), inputs=repo_type, outputs=space_options)
|
78 |
+
|
79 |
+
submit_btn = gr.Button("作成")
|
80 |
+
output = gr.Textbox(label="出力メッセージ", lines=5)
|
81 |
+
|
82 |
+
def auto_token(login_data):
|
83 |
+
return login_data["access_token"]
|
84 |
+
|
85 |
+
login.login(auto_token, outputs=token)
|
86 |
+
|
87 |
+
submit_btn.click(
|
88 |
+
clone_and_create,
|
89 |
+
inputs=[token, github_url, repo_type, private, space_id, port, title, description],
|
90 |
+
outputs=output
|
91 |
+
)
|
92 |
+
|
93 |
+
demo.launch()
|