relay / app.py
azr43l's picture
Update app.py
1a0c56b verified
# Your url for ST will be something like this:
# https://username-spaceName.hf.space/proxy/anthropic/v1
# Streaming is not supported
import logging
import json
from flask import Flask, request, Response
import requests
app = Flask(__name__)
# Configure the logging level
app.logger.setLevel(logging.INFO)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])
def relay_request(path):
relay_url = 'https://' + path
headers_ = dict()
if 'Content-Type' in request.headers:
headers_['Content-Type'] = request.headers['Content-Type']
if 'x-api-key' in request.headers:
headers_['Authorization'] = "Bearer " + request.headers['x-api-key']
if 'Authorization' in request.headers:
headers_['Authorization'] = request.headers['Authorization']
if request.method == 'POST':
response = requests.request(
method='POST',
url=relay_url,
headers=headers_,
json=json.loads(request.get_data()),
allow_redirects=False
)
else:
response = requests.request(
method=request.method,
url=relay_url,
headers=headers_,
allow_redirects=False
)
headers = [(name, value) for name, value in response.headers.items()]
return Response(response.content, response.status_code, headers)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)