Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from git import Repo
|
|
5 |
import git
|
6 |
from urllib.parse import urlparse
|
7 |
|
8 |
-
#
|
9 |
translations = {
|
10 |
"en": {
|
11 |
"title": "Hugging Face Space Creation Form",
|
@@ -24,9 +24,9 @@ translations = {
|
|
24 |
},
|
25 |
"ja": {
|
26 |
"title": "Hugging Face スペース作成フォーム",
|
27 |
-
"space_name": "
|
28 |
-
"repo_url": "GitHub
|
29 |
-
"github_token": "GitHub
|
30 |
"visibility": "公開/非公開",
|
31 |
"sdk": "SDK",
|
32 |
"description": "説明",
|
@@ -34,63 +34,50 @@ translations = {
|
|
34 |
"port": "ポート",
|
35 |
"name": "名前",
|
36 |
"emoji": "絵文字",
|
37 |
-
"pinned": "
|
38 |
-
"submit": "
|
39 |
}
|
40 |
}
|
41 |
|
42 |
-
# Hugging Face API クライアント
|
43 |
hf_api = HfApi()
|
44 |
|
45 |
-
# GitHub リポジトリ情報を取得
|
46 |
def get_github_repo_info(github_repo_url, github_token=None):
|
47 |
try:
|
48 |
parsed_url = urlparse(github_repo_url)
|
49 |
repo_path = parsed_url.path.strip('/')
|
|
|
50 |
|
51 |
-
# 一時ディレクトリにクローンして情報を取得
|
52 |
-
temp_dir = "temp_repo_clone"
|
53 |
if os.path.exists(temp_dir):
|
54 |
os.system(f"rm -rf {temp_dir}")
|
55 |
-
|
56 |
if github_token:
|
57 |
-
# 認証付きURLでクローン
|
58 |
auth_url = f"https://{github_token}@github.com/{repo_path}.git"
|
59 |
repo = Repo.clone_from(auth_url, temp_dir)
|
60 |
else:
|
61 |
repo = Repo.clone_from(github_repo_url, temp_dir)
|
62 |
-
|
63 |
repo_name = os.path.basename(repo_path)
|
64 |
-
description = "GitHub リポジトリの説明" # 必要に応じて `repo.description` を使用
|
65 |
-
|
66 |
-
# 一時ディレクトリを削除
|
67 |
os.system(f"rm -rf {temp_dir}")
|
68 |
|
69 |
return type('obj', (object,), {
|
70 |
'name': repo_name,
|
71 |
-
'description':
|
72 |
})
|
73 |
except git.exc.GitCommandError as e:
|
74 |
-
raise Exception(f"GitHub
|
75 |
|
76 |
-
# Hugging Face スペースを作成
|
77 |
def create_huggingface_space(space_name, github_repo_url, github_token, visibility, sdk, description, license_type, port, name, emoji, pinned):
|
78 |
try:
|
79 |
-
# GitHub リポジトリ情報を取得
|
80 |
repo = get_github_repo_info(github_repo_url, github_token)
|
81 |
|
82 |
-
# Hugging Face スペースを作成
|
83 |
create_repo(
|
84 |
repo_id=space_name,
|
85 |
repo_type="space",
|
86 |
private=(visibility == 'Private'),
|
87 |
-
space_sdk=sdk.lower()
|
88 |
)
|
89 |
|
90 |
-
#
|
91 |
-
readme_content = f"""# {repo.name}\n\n{description}\n\n## GitHub Repository\n{github_repo_url}\n\n**License**: {license_type}\n**Port**: {port}\n"""
|
92 |
-
|
93 |
-
# README.md をアップロード
|
94 |
hf_api.upload_file(
|
95 |
path_or_fileobj=readme_content.encode(),
|
96 |
path_in_repo="README.md",
|
@@ -98,64 +85,84 @@ def create_huggingface_space(space_name, github_repo_url, github_token, visibili
|
|
98 |
repo_type="space"
|
99 |
)
|
100 |
|
101 |
-
# スペース設定を更新
|
102 |
hf_api.set_space_sdk(space_name, sdk.lower())
|
103 |
if emoji:
|
104 |
hf_api.set_space_emoji(space_name, emoji)
|
105 |
if pinned:
|
106 |
hf_api.set_space_pinned(space_name, pinned)
|
107 |
-
|
108 |
-
return f"✅
|
109 |
except Exception as e:
|
110 |
-
return f"❌
|
111 |
|
112 |
-
# Gradio UI の構築
|
113 |
def gradio_ui():
|
114 |
with gr.Blocks() as demo:
|
115 |
-
#
|
116 |
-
|
117 |
<script>
|
118 |
-
const
|
119 |
-
const lang =
|
120 |
-
const translations = """ + str(translations) + """;
|
121 |
|
122 |
-
function
|
123 |
-
document.querySelectorAll('[data-translate]').forEach(el => {
|
124 |
const key = el.getAttribute('data-translate');
|
125 |
-
if (translations[lang] && translations[lang][key]) {
|
126 |
-
el.
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
131 |
</script>
|
132 |
"""
|
133 |
-
gr.HTML(
|
134 |
|
135 |
-
# UI
|
136 |
-
gr.Markdown("## <span data-translate='title'>Hugging Face
|
137 |
|
138 |
with gr.Row():
|
139 |
-
space_name = gr.Textbox(label="
|
140 |
-
github_repo_url = gr.Textbox(label="
|
141 |
|
142 |
with gr.Row():
|
143 |
-
github_token = gr.Textbox(label="
|
144 |
-
visibility = gr.Dropdown(choices=["Public", "Private"], value="Public",
|
145 |
-
sdk = gr.Dropdown(choices=["Gradio", "Streamlit", "Static", "Docker"], value="Gradio",
|
146 |
|
147 |
-
description = gr.Textbox(label="
|
148 |
-
license_type = gr.Textbox(label="
|
149 |
|
150 |
with gr.Row():
|
151 |
-
port = gr.Number(label="
|
152 |
-
name = gr.Textbox(label="
|
153 |
-
emoji = gr.Textbox(label="
|
154 |
-
pinned = gr.Checkbox(label="
|
155 |
|
156 |
-
submit_btn = gr.Button("
|
157 |
output = gr.Textbox(label="Result")
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
submit_btn.click(
|
160 |
create_huggingface_space,
|
161 |
inputs=[space_name, github_repo_url, github_token, visibility, sdk, description, license_type, port, name, emoji, pinned],
|
@@ -164,7 +171,6 @@ def gradio_ui():
|
|
164 |
|
165 |
return demo
|
166 |
|
167 |
-
# アプリ起動
|
168 |
if __name__ == "__main__":
|
169 |
app = gradio_ui()
|
170 |
app.launch()
|
|
|
5 |
import git
|
6 |
from urllib.parse import urlparse
|
7 |
|
8 |
+
# 翻訳辞書
|
9 |
translations = {
|
10 |
"en": {
|
11 |
"title": "Hugging Face Space Creation Form",
|
|
|
24 |
},
|
25 |
"ja": {
|
26 |
"title": "Hugging Face スペース作成フォーム",
|
27 |
+
"space_name": "スペース名",
|
28 |
+
"repo_url": "GitHubリポジトリURL",
|
29 |
+
"github_token": "GitHubトークン(プライベートリポジトリの場合)",
|
30 |
"visibility": "公開/非公開",
|
31 |
"sdk": "SDK",
|
32 |
"description": "説明",
|
|
|
34 |
"port": "ポート",
|
35 |
"name": "名前",
|
36 |
"emoji": "絵文字",
|
37 |
+
"pinned": "スペースを固定",
|
38 |
+
"submit": "作成"
|
39 |
}
|
40 |
}
|
41 |
|
|
|
42 |
hf_api = HfApi()
|
43 |
|
|
|
44 |
def get_github_repo_info(github_repo_url, github_token=None):
|
45 |
try:
|
46 |
parsed_url = urlparse(github_repo_url)
|
47 |
repo_path = parsed_url.path.strip('/')
|
48 |
+
temp_dir = "temp_repo"
|
49 |
|
|
|
|
|
50 |
if os.path.exists(temp_dir):
|
51 |
os.system(f"rm -rf {temp_dir}")
|
52 |
+
|
53 |
if github_token:
|
|
|
54 |
auth_url = f"https://{github_token}@github.com/{repo_path}.git"
|
55 |
repo = Repo.clone_from(auth_url, temp_dir)
|
56 |
else:
|
57 |
repo = Repo.clone_from(github_repo_url, temp_dir)
|
58 |
+
|
59 |
repo_name = os.path.basename(repo_path)
|
|
|
|
|
|
|
60 |
os.system(f"rm -rf {temp_dir}")
|
61 |
|
62 |
return type('obj', (object,), {
|
63 |
'name': repo_name,
|
64 |
+
'description': f"Repository: {repo_name}"
|
65 |
})
|
66 |
except git.exc.GitCommandError as e:
|
67 |
+
raise Exception(f"GitHub error: {str(e)}")
|
68 |
|
|
|
69 |
def create_huggingface_space(space_name, github_repo_url, github_token, visibility, sdk, description, license_type, port, name, emoji, pinned):
|
70 |
try:
|
|
|
71 |
repo = get_github_repo_info(github_repo_url, github_token)
|
72 |
|
|
|
73 |
create_repo(
|
74 |
repo_id=space_name,
|
75 |
repo_type="space",
|
76 |
private=(visibility == 'Private'),
|
77 |
+
space_sdk=sdk.lower()
|
78 |
)
|
79 |
|
80 |
+
readme_content = f"""# {repo.name}\n\n{description}\n\n## GitHub\n{github_repo_url}\n\n**License**: {license_type}"""
|
|
|
|
|
|
|
81 |
hf_api.upload_file(
|
82 |
path_or_fileobj=readme_content.encode(),
|
83 |
path_in_repo="README.md",
|
|
|
85 |
repo_type="space"
|
86 |
)
|
87 |
|
|
|
88 |
hf_api.set_space_sdk(space_name, sdk.lower())
|
89 |
if emoji:
|
90 |
hf_api.set_space_emoji(space_name, emoji)
|
91 |
if pinned:
|
92 |
hf_api.set_space_pinned(space_name, pinned)
|
93 |
+
|
94 |
+
return f"✅ Successfully created space: {space_name}"
|
95 |
except Exception as e:
|
96 |
+
return f"❌ Error: {str(e)}"
|
97 |
|
|
|
98 |
def gradio_ui():
|
99 |
with gr.Blocks() as demo:
|
100 |
+
# JavaScript for translation
|
101 |
+
js = f"""
|
102 |
<script>
|
103 |
+
const translations = {translations};
|
104 |
+
const lang = navigator.language.startsWith('ja') ? 'ja' : 'en';
|
|
|
105 |
|
106 |
+
function translate() {{
|
107 |
+
document.querySelectorAll('[data-translate]').forEach(el => {{
|
108 |
const key = el.getAttribute('data-translate');
|
109 |
+
if (translations[lang] && translations[lang][key]) {{
|
110 |
+
if (el.tagName === 'INPUT' && el.type === 'submit') {{
|
111 |
+
el.value = translations[lang][key];
|
112 |
+
}} else {{
|
113 |
+
el.innerText = translations[lang][key];
|
114 |
+
}}
|
115 |
+
}}
|
116 |
+
}});
|
117 |
+
}}
|
118 |
+
translate();
|
119 |
</script>
|
120 |
"""
|
121 |
+
gr.HTML(js)
|
122 |
|
123 |
+
# UI Components
|
124 |
+
gr.Markdown("## <span data-translate='title'>Hugging Face Space Creator</span>")
|
125 |
|
126 |
with gr.Row():
|
127 |
+
space_name = gr.Textbox(label=translations["en"]["space_name"], elem_id="space_name")
|
128 |
+
github_repo_url = gr.Textbox(label=translations["en"]["repo_url"], elem_id="repo_url")
|
129 |
|
130 |
with gr.Row():
|
131 |
+
github_token = gr.Textbox(label=translations["en"]["github_token"], type="password", elem_id="github_token")
|
132 |
+
visibility = gr.Dropdown(choices=["Public", "Private"], value="Public", label=translations["en"]["visibility"], elem_id="visibility")
|
133 |
+
sdk = gr.Dropdown(choices=["Gradio", "Streamlit", "Static", "Docker"], value="Gradio", label=translations["en"]["sdk"], elem_id="sdk")
|
134 |
|
135 |
+
description = gr.Textbox(label=translations["en"]["description"], lines=3, elem_id="description")
|
136 |
+
license_type = gr.Textbox(label=translations["en"]["license"], value="MIT", elem_id="license")
|
137 |
|
138 |
with gr.Row():
|
139 |
+
port = gr.Number(label=translations["en"]["port"], value=7860, elem_id="port")
|
140 |
+
name = gr.Textbox(label=translations["en"]["name"], elem_id="name")
|
141 |
+
emoji = gr.Textbox(label=translations["en"]["emoji"], placeholder="🚀", elem_id="emoji")
|
142 |
+
pinned = gr.Checkbox(label=translations["en"]["pinned"], elem_id="pinned")
|
143 |
|
144 |
+
submit_btn = gr.Button(translations["en"]["submit"], elem_id="submit")
|
145 |
output = gr.Textbox(label="Result")
|
146 |
|
147 |
+
# Add data-translate attributes via JavaScript
|
148 |
+
js_init = """
|
149 |
+
<script>
|
150 |
+
document.getElementById('space_name').setAttribute('data-translate', 'space_name');
|
151 |
+
document.getElementById('repo_url').setAttribute('data-translate', 'repo_url');
|
152 |
+
document.getElementById('github_token').setAttribute('data-translate', 'github_token');
|
153 |
+
document.getElementById('visibility').setAttribute('data-translate', 'visibility');
|
154 |
+
document.getElementById('sdk').setAttribute('data-translate', 'sdk');
|
155 |
+
document.getElementById('description').setAttribute('data-translate', 'description');
|
156 |
+
document.getElementById('license').setAttribute('data-translate', 'license');
|
157 |
+
document.getElementById('port').setAttribute('data-translate', 'port');
|
158 |
+
document.getElementById('name').setAttribute('data-translate', 'name');
|
159 |
+
document.getElementById('emoji').setAttribute('data-translate', 'emoji');
|
160 |
+
document.getElementById('pinned').setAttribute('data-translate', 'pinned');
|
161 |
+
document.getElementById('submit').setAttribute('data-translate', 'submit');
|
162 |
+
</script>
|
163 |
+
"""
|
164 |
+
gr.HTML(js_init)
|
165 |
+
|
166 |
submit_btn.click(
|
167 |
create_huggingface_space,
|
168 |
inputs=[space_name, github_repo_url, github_token, visibility, sdk, description, license_type, port, name, emoji, pinned],
|
|
|
171 |
|
172 |
return demo
|
173 |
|
|
|
174 |
if __name__ == "__main__":
|
175 |
app = gradio_ui()
|
176 |
app.launch()
|