File size: 1,515 Bytes
851c519
 
 
 
1a0c56b
851c519
 
 
 
 
 
 
 
 
 
 
 
 
 
194abaf
851c519
194abaf
1a0c56b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
851c519
 
194abaf
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
# 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)