Spaces:
Runtime error
Runtime error
File size: 2,361 Bytes
b851128 7eb71d9 e7adbbd 7eb71d9 e7adbbd b851128 e7adbbd b851128 7eb71d9 a85e57f 7eb71d9 b851128 e774c25 84b4094 e7adbbd b599634 e7adbbd b599634 e7adbbd b599634 e7adbbd 84b4094 b851128 e774c25 b851128 7eb71d9 b851128 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | import requests
from flask import Flask, request, Response, jsonify
import os
app = Flask(__name__)
cookie = os.environ.get('COOKIE')
# Authentication headers (this could be made dynamic too)
headers = {
'accept': 'application/json',
'content-type': 'application/json',
'cookie': cookie
}
# Base URL for proxying the requests
base_url = 'https://openui.fly.dev'
@app.route('/')
def hello():
return 'Hello, World!'
# /v1/models endpoint
@app.route('/v1/models', methods=['GET','POST'])
def get_models():
models_data = {
"data": [
{
"id": "gpt-3.5-turbo",
"object": "model",
"created": 1,
"token_coefficient": 0.5,
"max_tokens": 16385
},
{
"id": "gpt-4-turbo",
"object": "model",
"created": 1,
"vision": True,
"token_coefficient": 1,
"max_tokens": 128000
},
{
"id": "gpt-4o",
"object": "model",
"created": 1,
"vision": True,
"token_coefficient": 0.9,
"max_tokens": 128000
},
{
"id": "gpt-4o-mini",
"object": "model",
"created": 1,
"vision": True,
"token_coefficient": 0.2,
"max_tokens": 128000
}
]
}
return jsonify(models_data)
# /v1/chat/completions endpoint with streaming
@app.route('/v1/chat/completions', methods=['POST','GET'])
def chat_completions():
# Extract request data (JSON payload)
payload = request.json
# Forward the POST request to the target API with streaming responses
url = f'{base_url}/v1/chat/completions'
response = requests.post(url, headers=headers, json=payload, stream=True)
# Streaming response back to the client
def stream_response():
for chunk in response.iter_content(chunk_size=1024):
if chunk:
yield chunk.decode('utf-8')
return Response(stream_response(), content_type='application/json')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0') # To run it in docker container
|