soiz1 commited on
Commit
19590d4
·
verified ·
1 Parent(s): a45703f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -95
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 github import Github
 
 
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(space_name, private=(visibility == 'Private'))
48
-
49
- # README.mdの作成
50
- readme_content = f"# {repo.name}\n\n{description}\n\n## GitHub Repository\n{github_repo_url}\n"
51
- readme_content += f"\n**License**: {license_type}\n**Port**: {port}\n**Name**: {name}\n**Emoji**: {emoji}"
52
-
53
- # README.mdファイルを作成しアップロード
 
 
 
 
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.set_repo_visibility(space_name, private=(visibility == 'Private'))
62
-
63
- # スペース設定の詳細(SDKやその他の設定など)
64
- hf_api.set_space_sdk(space_name, sdk)
65
- hf_api.set_space_emoji(space_name, emoji)
66
- hf_api.set_space_pinned(space_name, pinned)
67
-
68
- return f"スペース {space_name} が作成されました!"
69
  except Exception as e:
70
- return f"エラーが発生しました: {str(e)}"
71
- # Gradio UIの作成
 
72
  def gradio_ui():
73
  with gr.Blocks() as demo:
74
- # JavaScriptでブラウザ言語を取得し、UIを動的に翻訳
75
  js_code = """
76
  <script>
77
- // ブラウザの言語を取得
78
  const userLang = navigator.language || navigator.userLanguage;
79
- let lang = userLang.split('-')[0]; // ja or en
80
-
81
- // Gradioに必要な翻訳データを設定
82
- const translations = {
83
- "en": {
84
- "title": "Hugging Face Space Creation Form",
85
- "space_name": "Space Name",
86
- "repo_url": "GitHub Repository URL",
87
- "github_token": "GitHub Token (Only for private repos)",
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.Markdown("<h3 data-translate='title'>Hugging Face スペース作成フォーム</h3>")
133
-
 
134
  with gr.Row():
135
- space_name = gr.Textbox(label="新規作成するスペース名", placeholder="スペース名", elem_id="space_name", data_translate="space_name")
136
- github_repo_url = gr.Textbox(label="GitHubリポジトリのURL", placeholder="GitHubリポジトリURL", elem_id="repo_url", data_translate="repo_url")
137
- github_token = gr.Textbox(label="GitHubトークン", placeholder="GitHubトークン(プライベートリポジトリの場合のみ)", type="password", elem_id="github_token", data_translate="github_token")
138
- visibility = gr.Dropdown(label="公開/非公開", choices=["Public", "Private"], value="Public", elem_id="visibility", data_translate="visibility")
139
- sdk = gr.Dropdown(label="SDK", choices=["Gradio", "Static", "Streamlit", "Docker"], value="Gradio", elem_id="sdk", data_translate="sdk")
140
- description = gr.Textbox(label="説明", placeholder="スペースの説明", elem_id="description", data_translate="description")
141
- license_type = gr.Textbox(label="ライセンス", placeholder="ライセンス", elem_id="license", data_translate="license")
142
- port = gr.Number(label="ポート", value=7860, elem_id="port", data_translate="port")
143
- name = gr.Textbox(label="名前", placeholder="名前", elem_id="name", data_translate="name")
144
- emoji = gr.Textbox(label="絵文字", placeholder="絵文字", elem_id="emoji", data_translate="emoji")
145
- pinned = gr.Checkbox(label="固定するかどうか", value=False, elem_id="pinned", data_translate="pinned")
146
-
147
- submit_btn = gr.Button("スペースを作成", elem_id="submit", data_translate="submit")
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="text"
153
  )
154
-
155
  return demo
156
 
157
- # Gradio UIの起動
158
  if __name__ == "__main__":
159
- gradio_ui().launch()
 
 
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()