Spaces:
Running
Running
from flask import Flask, render_template, request, redirect, jsonify | |
import requests | |
import os | |
app = Flask(__name__) | |
def index(): | |
if request.method == "POST": | |
token = request.form["token"] | |
space_name = request.form["space_name"] | |
github_url = request.form["github_url"] | |
github_token = request.form.get("github_token", None) | |
private = request.form.get("private") == "on" | |
sdk = request.form["sdk"] | |
description = request.form["description"] | |
license_ = request.form["license"] | |
port = request.form["port"] | |
title = request.form["title"] | |
emoji = request.form["emoji"] | |
pinned = request.form.get("pinned") == "on" | |
# GitHubリポジトリURLを整形 | |
if not github_url.startswith("https://"): | |
github_url = f"https://github.com/{github_url}" | |
headers = {"Authorization": f"Bearer {token}"} | |
data = { | |
"name": space_name, | |
"private": private, | |
"sdk": sdk, | |
"git": github_url, | |
"title": title, | |
"emoji": emoji, | |
"pinned": pinned, | |
"license": license_, | |
"app_port": port, | |
"description": description | |
} | |
if github_token: | |
data["secrets"] = {"GITHUB_TOKEN": github_token} | |
response = requests.post( | |
"https://huggingface.co/api/spaces", | |
headers=headers, | |
json=data | |
) | |
return jsonify(response.json()) | |
return render_template("index.html") | |
if __name__ == "__main__": | |
app.run(debug=True) | |