soiz1 commited on
Commit
3a23bd6
·
verified ·
1 Parent(s): 842f7e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -84
app.py CHANGED
@@ -1,104 +1,67 @@
1
  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, 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
 
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()