Spaces:
Runtime error
Runtime error
import json | |
import requests | |
from flask import Flask, render_template, request, jsonify | |
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") | |
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のIDだけの入力にも対応 | |
if not github_url.startswith("https://"): | |
github_url = f"https://github.com/{github_url}" | |
# Hugging Face ユーザー名を取得(または入力フォームから取得してもOK) | |
headers = {"Authorization": f"Bearer {token}"} | |
user_info = requests.get("https://huggingface.co/api/whoami-v2", headers=headers).json() | |
username = user_info["name"] | |
api_url = f"https://huggingface.co/api/spaces/{username}/{space_name}" | |
payload = { | |
"sdk": sdk, | |
"private": private, | |
"title": title, | |
"emoji": emoji, | |
"pinned": pinned, | |
"license": license_, | |
"app_port": port, | |
"description": description, | |
"git": github_url | |
} | |
if github_token: | |
payload["secrets"] = {"GITHUB_TOKEN": github_token} | |
json_data = json.dumps(payload, ensure_ascii=False).encode("utf-8") | |
response = requests.put( | |
api_url, | |
headers={ | |
"Authorization": f"Bearer {token}", | |
"Content-Type": "application/json" | |
}, | |
data=json_data | |
) | |
try: | |
return jsonify(response.json()) | |
except requests.exceptions.JSONDecodeError: | |
return f"エラー内容(JSON形式でない可能性): {response.text}", response.status_code | |
return render_template("index.html") | |