Spaces:
Sleeping
Sleeping
# https://linux.do/t/topic/120588/5 | |
# flask 糊接口 | |
# curl -sS --location http://127.0.0.1:5000/hf/v1/chat/completions -H "Content-Type: application/json" --data "{\"model\": \"gpt-.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"Say this is a test!\"}], \"stream\": false}" | |
from threading import Thread | |
import requests | |
from flask import Flask, jsonify, request | |
from python_run_cmd import run_cmd | |
from ycecream import y | |
def run_duck2api(): | |
run_cmd("./duck2api") | |
Thread(target=run_duck2api).start() | |
y.configure(sln=1) | |
app = Flask(__name__) | |
def my_endpoint(): | |
url = "https://api.anakin.ai/v1/apps/1344/chatbot/chat" | |
url = "http://127.0.0.1:8080/v1/chat/completions" | |
url = "http://acone:8080/v1/chat/completions" | |
# y(request) | |
# y(request.host) | |
# y(request.host_url) | |
# y(request.path) | |
# y(request.full_path) | |
# y(request.json) | |
# y(dir(request)) | |
headers = { | |
"Content-Type": "application/json", | |
# 'Authorization': 'Bearer ANAKINAI_API_ACCESS_TOKEN' | |
} | |
# remove prefix /hf | |
url = f"http://127.0.0.1:8080{request.path[3:]}" | |
try: | |
response = requests.post(url, headers=headers, json=request.json) | |
except Exception as exc: | |
y(exc) | |
err_msg = str(exc) | |
# raise | |
y(response.json()) | |
if response.status_code == 200: | |
return jsonify(response.json()) | |
return jsonify({"error": {"message": f"An error occurred: {err_msg}", "type": "server_error", "code": 500}}) | |
if __name__ == "__main__": | |
app.run(port=5000) | |