45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
#!/usr/bin/env python3
|
|
from flask import Flask, request, Response
|
|
import requests
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
GROQ_BASE = "https://api.proxyapi.ru/openai"
|
|
|
|
CORS = {
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
|
"Access-Control-Allow-Headers": "Authorization, Content-Type",
|
|
}
|
|
|
|
# Импорт pronounce после создания app
|
|
import sys, os
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from pronounce import register_routes
|
|
register_routes(app)
|
|
|
|
@app.route("/<path:path>", methods=["OPTIONS"])
|
|
def options(path):
|
|
return Response(status=204, headers=CORS)
|
|
|
|
@app.route("/<path:subpath>", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
|
def proxy(subpath):
|
|
url = f"{GROQ_BASE}/{subpath}"
|
|
hdrs = {}
|
|
if request.headers.get("Authorization"):
|
|
hdrs["Authorization"] = request.headers["Authorization"]
|
|
if request.headers.get("Content-Type"):
|
|
hdrs["Content-Type"] = request.headers["Content-Type"]
|
|
body = request.get_data()
|
|
print(f"[PROXY] {request.method} {subpath} body={len(body)}b ct={hdrs.get('Content-Type','')[:50]}", flush=True)
|
|
r = requests.request(request.method, url, headers=hdrs,
|
|
data=body, timeout=60, stream=True)
|
|
out = dict(CORS)
|
|
out["Content-Type"] = r.headers.get("Content-Type", "application/json")
|
|
raw = r.content
|
|
print(f"[PROXY] ← {r.status_code} {raw[:200]}", flush=True)
|
|
return Response(raw, status=r.status_code, headers=out)
|
|
|
|
if __name__ == "__main__":
|
|
app.run(host="127.0.0.1", port=8765)
|