Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import HfApi, create_repo
|
3 |
import os
|
4 |
-
from
|
|
|
|
|
5 |
|
6 |
-
#
|
7 |
translations = {
|
8 |
"en": {
|
9 |
"title": "Hugging Face Space Creation Form",
|
@@ -37,123 +39,132 @@ translations = {
|
|
37 |
}
|
38 |
}
|
39 |
|
40 |
-
# Hugging Face
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def create_huggingface_space(space_name, github_repo_url, github_token, visibility, sdk, description, license_type, port, name, emoji, pinned):
|
42 |
try:
|
43 |
-
# GitHub
|
44 |
repo = get_github_repo_info(github_repo_url, github_token)
|
45 |
|
46 |
-
# Hugging Faceスペースを作成
|
47 |
-
create_repo(
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
hf_api.upload_file(
|
55 |
-
path_or_fileobj=readme_content,
|
56 |
path_in_repo="README.md",
|
57 |
-
repo_id=space_name
|
|
|
58 |
)
|
59 |
|
60 |
-
#
|
61 |
-
hf_api.
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
return f"スペース {space_name} が作成されました!"
|
69 |
except Exception as e:
|
70 |
-
return f"エラーが発生しました: {str(e)}"
|
71 |
-
|
|
|
72 |
def gradio_ui():
|
73 |
with gr.Blocks() as demo:
|
74 |
-
#
|
75 |
js_code = """
|
76 |
<script>
|
77 |
-
// ブラウザの言語を取得
|
78 |
const userLang = navigator.language || navigator.userLanguage;
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
"visibility": "Visibility (Public or Private)",
|
89 |
-
"sdk": "SDK",
|
90 |
-
"description": "Description",
|
91 |
-
"license": "License",
|
92 |
-
"port": "Port",
|
93 |
-
"name": "Name",
|
94 |
-
"emoji": "Emoji",
|
95 |
-
"pinned": "Pin this space",
|
96 |
-
"submit": "Create Space"
|
97 |
-
},
|
98 |
-
"ja": {
|
99 |
-
"title": "Hugging Face スペース作成フォーム",
|
100 |
-
"space_name": "新規作成するスペース名",
|
101 |
-
"repo_url": "GitHubリポジトリのURL",
|
102 |
-
"github_token": "GitHubトークン(プライベートリポジトリの場合のみ)",
|
103 |
-
"visibility": "公開/非公開",
|
104 |
-
"sdk": "SDK",
|
105 |
-
"description": "説明",
|
106 |
-
"license": "ライセンス",
|
107 |
-
"port": "ポート",
|
108 |
-
"name": "名前",
|
109 |
-
"emoji": "絵文字",
|
110 |
-
"pinned": "固定するかどうか",
|
111 |
-
"submit": "スペースを作成"
|
112 |
-
}
|
113 |
-
};
|
114 |
-
|
115 |
-
// 言語に基づいてUIのテキストを変更
|
116 |
-
const translateUI = (lang) => {
|
117 |
-
document.querySelectorAll('[data-translate]').forEach((element) => {
|
118 |
-
const key = element.getAttribute('data-translate');
|
119 |
-
element.innerText = translations[lang][key] || translations["en"][key];
|
120 |
});
|
121 |
-
}
|
122 |
-
|
123 |
-
// 言語をセットしUIを更新
|
124 |
-
translateUI(lang);
|
125 |
</script>
|
126 |
"""
|
127 |
-
|
128 |
-
gr.HTML(js_code) # JavaScriptコードを埋め込む
|
129 |
|
130 |
-
# UIコンポーネント
|
|
|
|
|
131 |
with gr.Row():
|
132 |
-
gr.
|
133 |
-
|
|
|
134 |
with gr.Row():
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
149 |
submit_btn.click(
|
150 |
create_huggingface_space,
|
151 |
inputs=[space_name, github_repo_url, github_token, visibility, sdk, description, license_type, port, name, emoji, pinned],
|
152 |
-
outputs=
|
153 |
)
|
154 |
-
|
155 |
return demo
|
156 |
|
157 |
-
#
|
158 |
if __name__ == "__main__":
|
159 |
-
gradio_ui()
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import HfApi, create_repo
|
3 |
import os
|
4 |
+
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",
|
|
|
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': description
|
72 |
+
})
|
73 |
+
except git.exc.GitCommandError as e:
|
74 |
+
raise Exception(f"GitHub リポジトリの取得に失敗: {str(e)}")
|
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() # "gradio", "streamlit", etc.
|
88 |
+
)
|
89 |
+
|
90 |
+
# README.md の内容を生成
|
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",
|
97 |
+
repo_id=space_name,
|
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"✅ スペース '{space_name}' が正常に作成されました!"
|
|
|
109 |
except Exception as e:
|
110 |
+
return f"❌ エラーが発生しました: {str(e)}"
|
111 |
+
|
112 |
+
# Gradio UI の構築
|
113 |
def gradio_ui():
|
114 |
with gr.Blocks() as demo:
|
115 |
+
# ブラウザの言語に基づいてUIを翻訳
|
116 |
js_code = """
|
117 |
<script>
|
|
|
118 |
const userLang = navigator.language || navigator.userLanguage;
|
119 |
+
const lang = userLang.startsWith('ja') ? 'ja' : 'en';
|
120 |
+
const translations = """ + str(translations) + """;
|
121 |
+
|
122 |
+
function translateUI() {
|
123 |
+
document.querySelectorAll('[data-translate]').forEach(el => {
|
124 |
+
const key = el.getAttribute('data-translate');
|
125 |
+
if (translations[lang] && translations[lang][key]) {
|
126 |
+
el.textContent = translations[lang][key];
|
127 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
});
|
129 |
+
}
|
130 |
+
translateUI();
|
|
|
|
|
131 |
</script>
|
132 |
"""
|
133 |
+
gr.HTML(js_code)
|
|
|
134 |
|
135 |
+
# UI コンポーネント
|
136 |
+
gr.Markdown("## <span data-translate='title'>Hugging Face スペース作成フォーム</span>")
|
137 |
+
|
138 |
with gr.Row():
|
139 |
+
space_name = gr.Textbox(label="Space Name", data-translate="space_name")
|
140 |
+
github_repo_url = gr.Textbox(label="GitHub Repository URL", data-translate="repo_url")
|
141 |
+
|
142 |
with gr.Row():
|
143 |
+
github_token = gr.Textbox(label="GitHub Token", type="password", data-translate="github_token")
|
144 |
+
visibility = gr.Dropdown(choices=["Public", "Private"], value="Public", data-translate="visibility")
|
145 |
+
sdk = gr.Dropdown(choices=["Gradio", "Streamlit", "Static", "Docker"], value="Gradio", data-translate="sdk")
|
146 |
+
|
147 |
+
description = gr.Textbox(label="Description", lines=3, data-translate="description")
|
148 |
+
license_type = gr.Textbox(label="License", value="MIT", data-translate="license")
|
149 |
+
|
150 |
+
with gr.Row():
|
151 |
+
port = gr.Number(label="Port", value=7860, data-translate="port")
|
152 |
+
name = gr.Textbox(label="Name", data-translate="name")
|
153 |
+
emoji = gr.Textbox(label="Emoji", placeholder="🚀", data-translate="emoji")
|
154 |
+
pinned = gr.Checkbox(label="Pin this space", data-translate="pinned")
|
155 |
+
|
156 |
+
submit_btn = gr.Button("Create Space", data-translate="submit")
|
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],
|
162 |
+
outputs=output
|
163 |
)
|
164 |
+
|
165 |
return demo
|
166 |
|
167 |
+
# アプリ起動
|
168 |
if __name__ == "__main__":
|
169 |
+
app = gradio_ui()
|
170 |
+
app.launch()
|