soiz1 commited on
Commit
d9adf77
·
verified ·
1 Parent(s): a483542

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -50
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, 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
 
 
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