flask1 / main.py
craftgamesnetwork's picture
Update main.py
5e6ce82 verified
import requests
from io import BytesIO
from flask import Flask, request, jsonify
from gradio_client import Client
from huggingface_hub import create_repo, upload_file
app = Flask(__name__)
@app.route('/upload_model', methods=['POST'])
def upload_model():
# Par芒metros
file_name= request.args.get('file_name', default='')
repo = request.args.get('repo', default='')
url = request.args.get('url', default='')
token = request.args.get('token', default='')
try:
# Crie o reposit贸rio
repo_id = repo
create_repo(repo_id=repo_id, token=token)
# Fa莽a o download do conte煤do da URL em mem贸ria
response = requests.get(url)
if response.status_code == 200:
# Obtenha o conte煤do do arquivo em bytes
file_content = response.content
# Crie um objeto de arquivo em mem贸ria
file_obj = BytesIO(file_content)
# Fa莽a o upload do arquivo
upload_file(
path_or_fileobj=file_obj,
path_in_repo=file_name,
repo_id=repo_id,
token=token
)
# Mensagem de sucesso
return jsonify({"message": "Sucess"})
else:
return jsonify({"error": "Failed"}), 500
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)