azur303 commited on
Commit
a16b74a
1 Parent(s): f5c7772

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Your url for ST will be something like this:
2
+ # https://username-spaceName.hf.space/proxy/anthropic/v1
3
+ # Streaming is not supported
4
+
5
+
6
+ import logging
7
+ import json
8
+ from flask import Flask, request, Response
9
+ import requests
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Configure the logging level
14
+ app.logger.setLevel(logging.INFO)
15
+
16
+ @app.route('/', defaults={'path': ''})
17
+ @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])
18
+ def relay_request(path):
19
+ # Extract the base URL to relay requests
20
+ base_url = 'https://username84-g.hf.space' # Replace with the URL you want to relay requests to
21
+
22
+ # Prepare the relay URL by appending the path from the original request
23
+ relay_url = base_url + '/' + path
24
+
25
+ # Apparently the original headers contain you IP, so I'm getting only the necessary info
26
+ headers_ = dict()
27
+
28
+ # So far I only saw ST sending the API through the headers, let me know if you find something else.
29
+ if 'Content-Type' in request.headers:
30
+ headers_['Content-Type'] = request.headers['Content-Type']
31
+ if 'x-api-key' in request.headers:
32
+ headers_['x-api-key'] = request.headers['x-api-key']
33
+
34
+ # app.logger.info(headers_)
35
+
36
+ # If post includes the json data
37
+ if request.method == 'POST':
38
+ response = requests.request(
39
+ method='POST',
40
+ url=relay_url,
41
+ headers=headers_,
42
+ json=json.loads(request.get_data()),
43
+ allow_redirects=False # Idk if this is needed, GPT added it and it works, so whatever
44
+ )
45
+ else:
46
+ response = requests.request(
47
+ method=request.method,
48
+ url=relay_url,
49
+ headers=headers_,
50
+ allow_redirects=False # Idk if this is needed, GPT added it and it works, so whatever
51
+ )
52
+
53
+
54
+ # Prepare the response to return to the original API call
55
+ headers = [(name, value) for name, value in response.headers.items()]
56
+ return Response(response.content, response.status_code, headers)
57
+
58
+ if __name__ == '__main__':
59
+ app.run(host='0.0.0.0', port=7860)