duck2api1616 / duck2api_fwd.py
ffreemt
Update SERVER_PORT
c5fa067
r"""
Refer to 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}"
# --data '{"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "stream": false}'
curl https://mikeee-duck2api.hf.space/hf/v1/chat/completions -H "Content-Type: application/json" --data "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"Say this is a test!\"}], \"stream\": false}"
"""
import os
from threading import Thread
import requests
from dotenv import load_dotenv, dotenv_values
from flask import Flask, jsonify, request
from python_run_cmd import run_cmd
from ycecream import y
def run_duck2api():
try:
run_cmd("./duck2api")
except Exception as exc:
y(exc)
Thread(target=run_duck2api).start()
y.configure(sln=1)
load_dotenv()
y(dotenv_values())
# ./duck2api starts at 8088 specified in .env (default 8080)
PORT = os.getenv("SERVER_PORT")
if not PORT:
PORT = 8080
y(PORT)
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def landing():
y(request.url)
y(request.path)
if request.path.startswith("/hf"):
url = f"http://127.0.0.1:{PORT}{request.path[3:]}"
else:
url = f"http://127.0.0.1:{PORT}{request.path}"
response = requests.get(url)
return response.text
@app.route("/hf/v1/chat/completions", methods=["POST"])
def my_endpoint():
# url = "https://api.anakin.ai/v1/apps/1344/chatbot/chat"
# url = "http://acone:{PORT}/v1/chat/completions"
# url = f"http://127.0.0.1:{PORT}/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
y(request.url)
y(request.path)
if request.path.startswith("/hf"):
url = f"http://127.0.0.1:{PORT}{request.path[3:]}"
else:
url = f"http://127.0.0.1:{PORT}{request.path}"
y(url)
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__":
y(" -- ")
app.run(port=5000, debug=True)
y(" -end- ")