soiz1 commited on
Commit
e717773
·
verified ·
1 Parent(s): f2c4213

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -166
app.py CHANGED
@@ -1,176 +1,54 @@
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",
12
- "space_name": "Space Name",
13
- "repo_url": "GitHub Repository URL",
14
- "github_token": "GitHub Token (Only for private repos)",
15
- "visibility": "Visibility (Public or Private)",
16
- "sdk": "SDK",
17
- "description": "Description",
18
- "license": "License",
19
- "port": "Port",
20
- "name": "Name",
21
- "emoji": "Emoji",
22
- "pinned": "Pin this space",
23
- "submit": "Create Space"
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": "説明",
33
- "license": "ライセンス",
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",
84
- repo_id=space_name,
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],
169
- outputs=output
170
  )
171
-
172
- return demo
 
173
 
174
  if __name__ == "__main__":
175
- app = gradio_ui()
176
- app.launch()
 
1
+ from flask import Flask, render_template, request, redirect, jsonify
2
+ import requests
3
  import os
 
 
 
4
 
5
+ app = Flask(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ @app.route("/", methods=["GET", "POST"])
8
+ def index():
9
+ if request.method == "POST":
10
+ token = request.form["token"]
11
+ space_name = request.form["space_name"]
12
+ github_url = request.form["github_url"]
13
+ github_token = request.form.get("github_token", None)
14
+ private = request.form.get("private") == "on"
15
+ sdk = request.form["sdk"]
16
+ description = request.form["description"]
17
+ license_ = request.form["license"]
18
+ port = request.form["port"]
19
+ title = request.form["title"]
20
+ emoji = request.form["emoji"]
21
+ pinned = request.form.get("pinned") == "on"
22
 
23
+ # GitHubリポジトリURLを整形
24
+ if not github_url.startswith("https://"):
25
+ github_url = f"https://github.com/{github_url}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
+ headers = {"Authorization": f"Bearer {token}"}
28
+ data = {
29
+ "name": space_name,
30
+ "private": private,
31
+ "sdk": sdk,
32
+ "git": github_url,
33
+ "title": title,
34
+ "emoji": emoji,
35
+ "pinned": pinned,
36
+ "license": license_,
37
+ "app_port": port,
38
+ "description": description
39
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
+ if github_token:
42
+ data["secrets"] = {"GITHUB_TOKEN": github_token}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
 
44
+ response = requests.post(
45
+ "https://huggingface.co/api/spaces",
46
+ headers=headers,
47
+ json=data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  )
49
+
50
+ return jsonify(response.json())
51
+ return render_template("index.html")
52
 
53
  if __name__ == "__main__":
54
+ app.run(debug=True)