Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,117 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
from huggingface_hub import HfApi, whoami
|
4 |
from git import Repo
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
with gr.Blocks() as demo:
|
42 |
-
gr.Markdown("
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import HfApi
|
|
|
3 |
from git import Repo
|
4 |
+
from slugify import slugify
|
5 |
+
import uuid
|
6 |
+
import os
|
7 |
+
import re
|
8 |
|
9 |
+
def extract_from_readme(readme_path):
|
10 |
+
title = None
|
11 |
+
description = None
|
12 |
+
port = None
|
13 |
+
if os.path.exists(readme_path):
|
14 |
+
with open(readme_path, encoding="utf-8") as f:
|
15 |
+
content = f.read()
|
16 |
+
match_title = re.search(r'title:\s*(.*)', content)
|
17 |
+
match_desc = re.search(r'short_description:\s*(.*)', content)
|
18 |
+
match_port = re.search(r'app_port:\s*(\d+)', content)
|
19 |
+
if match_title:
|
20 |
+
title = match_title.group(1).strip()
|
21 |
+
if match_desc:
|
22 |
+
description = match_desc.group(1).strip()
|
23 |
+
if match_port:
|
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}",
|
47 |
+
repo_type="space",
|
48 |
+
space_sdk=space_sdk,
|
49 |
+
space_sdk_version=sdk_version if sdk_version else None,
|
50 |
+
private=private
|
51 |
+
)
|
52 |
+
else:
|
53 |
+
hf_api.create_repo(
|
54 |
+
repo_id=f"{profile.username}/{final_id}",
|
55 |
+
repo_type=repo_type,
|
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:
|
63 |
+
f.write(f"\ntitle: {space_title}")
|
64 |
+
if space_description and not desc_from_readme:
|
65 |
+
f.write(f"\nshort_description: {space_description}")
|
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リポジトリURL(GitHub等)")
|
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="スペースのタイトル(空欄ならREADMEやIDから)")
|
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 {
|
100 |
+
space_sdk: gr.update(visible=visible),
|
101 |
+
sdk_version: gr.update(visible=visible),
|
102 |
+
space_id: gr.update(visible=True),
|
103 |
+
space_title: gr.update(visible=True),
|
104 |
+
space_description: gr.update(visible=True),
|
105 |
+
}
|
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],
|
113 |
+
outputs=output
|
114 |
+
)
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
demo.launch()
|