Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@
|
|
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,37 +18,24 @@ def relay_request(path):
|
|
19 |
|
20 |
relay_url = 'https://' + path
|
21 |
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
response
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
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)
|
|
|
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 |
|
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)
|