Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,50 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
app = Flask(__name__)
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
@app.route('/')
|
| 7 |
def hello():
|
| 8 |
return 'Hello, World!'
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
if __name__ == '__main__':
|
| 12 |
-
app.run(debug=True) #
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from flask import Flask, request, Response, jsonify
|
| 3 |
|
| 4 |
app = Flask(__name__)
|
| 5 |
|
| 6 |
+
# Authentication headers (this could be made dynamic too)
|
| 7 |
+
headers = {
|
| 8 |
+
'accept': 'application/json',
|
| 9 |
+
'content-type': 'application/json',
|
| 10 |
+
'cookie': 'session=eyJzZXNzaW9uX2lkIjogIjgyMGM2OTJmLTRhMzItNGVhMi05MTNhLTBjNzk1YTYwN2MyMSIsICJ1c2VyX2lkIjogIjA2ODkxMzVhLWRhZTUtNDk0OS05NTE3LWQ3MTJlMmZkZDU4YiJ9.ZykspA.CLUCSuyHoG8S1uruiVKNkHRjw5Q'
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
# Base URL for proxying the requests
|
| 14 |
+
base_url = 'https://openui.fly.dev'
|
| 15 |
+
|
| 16 |
@app.route('/')
|
| 17 |
def hello():
|
| 18 |
return 'Hello, World!'
|
| 19 |
|
| 20 |
+
# /v1/models endpoint
|
| 21 |
+
@app.route('/v1/models', methods=['GET'])
|
| 22 |
+
def get_models():
|
| 23 |
+
url = f'{base_url}/v1/models'
|
| 24 |
+
# Forward the GET request to the target API
|
| 25 |
+
response = requests.get(url, headers=headers)
|
| 26 |
+
|
| 27 |
+
# Return the JSON response from the target API
|
| 28 |
+
return jsonify(response.json())
|
| 29 |
+
|
| 30 |
+
# /v1/chat/completions endpoint with streaming
|
| 31 |
+
@app.route('/v1/chat/completions', methods=['POST'])
|
| 32 |
+
def chat_completions():
|
| 33 |
+
# Extract request data (JSON payload)
|
| 34 |
+
payload = request.json
|
| 35 |
+
|
| 36 |
+
# Forward the POST request to the target API with streaming responses
|
| 37 |
+
url = f'{base_url}/v1/chat/completions'
|
| 38 |
+
response = requests.post(url, headers=headers, json=payload, stream=True)
|
| 39 |
+
|
| 40 |
+
# Streaming response back to the client
|
| 41 |
+
def stream_response():
|
| 42 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 43 |
+
if chunk:
|
| 44 |
+
yield chunk.decode('utf-8')
|
| 45 |
+
|
| 46 |
+
return Response(stream_response(), content_type='application/json')
|
| 47 |
|
| 48 |
if __name__ == '__main__':
|
| 49 |
+
app.run(debug=True, host='0.0.0.0') # To run it in docker container
|
| 50 |
+
|