azr43l commited on
Commit
1a0c56b
1 Parent(s): 1cd9521

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -17
app.py CHANGED
@@ -2,6 +2,7 @@
2
  # https://username-spaceName.hf.space/proxy/anthropic/v1
3
  # Streaming is not supported
4
 
 
5
  import logging
6
  import json
7
  from flask import Flask, request, Response
@@ -18,24 +19,37 @@ def relay_request(path):
18
 
19
  relay_url = 'https://' + path
20
 
21
- headers_ = {}
22
 
23
- # Copy headers from the client request
24
- for key, value in request.headers.items():
25
- if key.lower() != 'host':
26
- headers_[key] = value
27
-
28
- # Proxy the request
29
- response = requests.request(
30
- method=request.method,
31
- url=relay_url,
32
- headers=headers_,
33
- data=request.stream,
34
- stream=True # Stream the response back to the client
35
- )
36
-
37
- # Stream the response content back to the client
38
- return Response(response.iter_content(chunk_size=1024), status=response.status_code, headers=response.headers.items())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  if __name__ == '__main__':
41
  app.run(host='0.0.0.0', port=7860)
 
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
 
19
 
20
  relay_url = 'https://' + path
21
 
 
22
 
23
+ headers_ = dict()
24
+
25
+
26
+ if 'Content-Type' in request.headers:
27
+ headers_['Content-Type'] = request.headers['Content-Type']
28
+ if 'x-api-key' in request.headers:
29
+ headers_['Authorization'] = "Bearer " + request.headers['x-api-key']
30
+ if 'Authorization' in request.headers:
31
+ headers_['Authorization'] = request.headers['Authorization']
32
+
33
+ if request.method == 'POST':
34
+ response = requests.request(
35
+ method='POST',
36
+ url=relay_url,
37
+ headers=headers_,
38
+ json=json.loads(request.get_data()),
39
+ allow_redirects=False
40
+ )
41
+ else:
42
+ response = requests.request(
43
+ method=request.method,
44
+ url=relay_url,
45
+ headers=headers_,
46
+ allow_redirects=False
47
+ )
48
+
49
+
50
+
51
+ headers = [(name, value) for name, value in response.headers.items()]
52
+ return Response(response.content, response.status_code, headers)
53
 
54
  if __name__ == '__main__':
55
  app.run(host='0.0.0.0', port=7860)