soiz1 commited on
Commit
d1f5c23
·
verified ·
1 Parent(s): ff5706a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -6,6 +6,7 @@ import uuid
6
  import os
7
  import re
8
 
 
9
  def extract_from_readme(readme_path):
10
  title = None
11
  description = None
@@ -24,23 +25,25 @@ def extract_from_readme(readme_path):
24
  port = match_port.group(1).strip()
25
  return title, description, port
26
 
 
27
  def clone_and_create(profile: gr.OAuthProfile, token: gr.OAuthToken,
28
  repo_url, repo_type, private, space_sdk, sdk_version,
29
  space_id, space_title, space_description):
30
- folder = str(uuid.uuid4())
31
- Repo.clone_from(repo_url, folder)
32
 
33
  hf_api = HfApi(token=token.token)
34
 
35
- # 自動補完
36
  title_from_readme, desc_from_readme, port_from_readme = extract_from_readme(os.path.join(folder, "README.md"))
37
 
 
38
  final_id = slugify(space_id) if space_id else slugify(repo_url.split("/")[-1])
39
  final_title = space_title or title_from_readme or final_id
40
  final_description = space_description or desc_from_readme
41
  repo_visibility = "private" if private else "public"
42
 
43
- # スペースの場合
44
  if repo_type == "space":
45
  hf_api.create_repo(
46
  repo_id=f"{profile.username}/{final_id}",
@@ -56,7 +59,7 @@ def clone_and_create(profile: gr.OAuthProfile, token: gr.OAuthToken,
56
  private=private
57
  )
58
 
59
- # README.md に補完情報追加(必要なら)
60
  readme_path = os.path.join(folder, "README.md")
61
  with open(readme_path, "a", encoding="utf-8") as f:
62
  if space_title and not title_from_readme:
@@ -66,34 +69,36 @@ def clone_and_create(profile: gr.OAuthProfile, token: gr.OAuthToken,
66
  if port_from_readme:
67
  f.write(f"\napp_port: {port_from_readme}")
68
 
69
- # アップロード
70
  hf_api.upload_folder(
71
  folder_path=folder,
72
  repo_id=f"{profile.username}/{final_id}",
73
  repo_type=repo_type
74
  )
75
 
76
- # URL
77
  return f"https://huggingface.co/{repo_type + 's' if repo_type != 'model' else 'models'}/{profile.username}/{final_id}"
78
 
 
79
  with gr.Blocks() as demo:
80
- gr.Markdown("# 🚀 Gitリポジトリ → Hugging Face クローンツール")
81
  gr.LoginButton()
82
 
83
  with gr.Row():
84
  with gr.Column():
85
- repo_url = gr.Textbox(label="GitリポジトリURLGitHub等)")
86
- repo_type = gr.Dropdown(choices=["space", "dataset", "model"], value="space", label="Hugging Faceの種類")
87
- private = gr.Checkbox(label="非公開にする", value=False)
88
- space_sdk = gr.Dropdown(choices=["gradio", "streamlit", "docker", "static"], label="スペースSDK", visible=True)
89
- sdk_version = gr.Textbox(label="SDKバージョン(任意)", visible=True)
90
- space_id = gr.Textbox(label="Hugging Face ID(空欄なら自動)")
91
- space_title = gr.Textbox(label="スペースのタイトル(空欄ならREADMEIDから)")
92
- space_description = gr.Textbox(label="スペースの説明(空欄ならREADMEの内容を使用)")
93
 
94
  with gr.Column():
95
- output = gr.Textbox(label="作成されたHugging Face URL")
96
 
 
97
  def toggle_space_fields(repo_type_val):
98
  visible = repo_type_val == "space"
99
  return {
@@ -106,7 +111,7 @@ with gr.Blocks() as demo:
106
 
107
  repo_type.change(fn=toggle_space_fields, inputs=repo_type, outputs=[space_sdk, sdk_version, space_id, space_title, space_description])
108
 
109
- btn = gr.Button("クローンして作成!")
110
  btn.click(
111
  fn=clone_and_create,
112
  inputs=[repo_url, repo_type, private, space_sdk, sdk_version, space_id, space_title, space_description],
 
6
  import os
7
  import re
8
 
9
+ # Function to extract metadata from README.md file
10
  def extract_from_readme(readme_path):
11
  title = None
12
  description = None
 
25
  port = match_port.group(1).strip()
26
  return title, description, port
27
 
28
+ # Main function to clone a Git repo and create a Hugging Face repo
29
  def clone_and_create(profile: gr.OAuthProfile, token: gr.OAuthToken,
30
  repo_url, repo_type, private, space_sdk, sdk_version,
31
  space_id, space_title, space_description):
32
+ folder = str(uuid.uuid4()) # Create a unique folder name
33
+ Repo.clone_from(repo_url, folder) # Clone the git repository
34
 
35
  hf_api = HfApi(token=token.token)
36
 
37
+ # Auto-fill info by extracting from README.md
38
  title_from_readme, desc_from_readme, port_from_readme = extract_from_readme(os.path.join(folder, "README.md"))
39
 
40
+ # Determine final repo ID, title, description
41
  final_id = slugify(space_id) if space_id else slugify(repo_url.split("/")[-1])
42
  final_title = space_title or title_from_readme or final_id
43
  final_description = space_description or desc_from_readme
44
  repo_visibility = "private" if private else "public"
45
 
46
+ # Create Hugging Face repo
47
  if repo_type == "space":
48
  hf_api.create_repo(
49
  repo_id=f"{profile.username}/{final_id}",
 
59
  private=private
60
  )
61
 
62
+ # Append missing info to README.md if needed
63
  readme_path = os.path.join(folder, "README.md")
64
  with open(readme_path, "a", encoding="utf-8") as f:
65
  if space_title and not title_from_readme:
 
69
  if port_from_readme:
70
  f.write(f"\napp_port: {port_from_readme}")
71
 
72
+ # Upload folder contents to the Hugging Face repo
73
  hf_api.upload_folder(
74
  folder_path=folder,
75
  repo_id=f"{profile.username}/{final_id}",
76
  repo_type=repo_type
77
  )
78
 
79
+ # Return URL to created Hugging Face repo
80
  return f"https://huggingface.co/{repo_type + 's' if repo_type != 'model' else 'models'}/{profile.username}/{final_id}"
81
 
82
+ # Define Gradio UI
83
  with gr.Blocks() as demo:
84
+ gr.Markdown("# 🚀 Git Repository → Hugging Face Clone Tool")
85
  gr.LoginButton()
86
 
87
  with gr.Row():
88
  with gr.Column():
89
+ repo_url = gr.Textbox(label="Git Repository URL (e.g., GitHub)")
90
+ repo_type = gr.Dropdown(choices=["space", "dataset", "model"], value="space", label="Hugging Face repo type")
91
+ private = gr.Checkbox(label="Make Private", value=False)
92
+ space_sdk = gr.Dropdown(choices=["gradio", "streamlit", "docker", "static"], label="Space SDK", visible=True)
93
+ sdk_version = gr.Textbox(label="SDK Version (optional)", visible=True)
94
+ space_id = gr.Textbox(label="Hugging Face ID (auto if blank)")
95
+ space_title = gr.Textbox(label="Space Title (from README or ID if blank)")
96
+ space_description = gr.Textbox(label="Space Description (from README if blank)")
97
 
98
  with gr.Column():
99
+ output = gr.Textbox(label="Created Hugging Face URL")
100
 
101
+ # Show/hide some inputs depending on repo_type
102
  def toggle_space_fields(repo_type_val):
103
  visible = repo_type_val == "space"
104
  return {
 
111
 
112
  repo_type.change(fn=toggle_space_fields, inputs=repo_type, outputs=[space_sdk, sdk_version, space_id, space_title, space_description])
113
 
114
+ btn = gr.Button("Clone and Create!")
115
  btn.click(
116
  fn=clone_and_create,
117
  inputs=[repo_url, repo_type, private, space_sdk, sdk_version, space_id, space_title, space_description],