rr1 commited on
Commit
1a1d105
·
verified ·
1 Parent(s): 02d8684

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, Response, stream_with_context
2
+ import requests
3
+ import os
4
+ import json
5
+
6
+ app = Flask(__name__)
7
+
8
+ # Target API base URL from environment variable
9
+ TARGET_API = os.getenv("TARGET_API", "https://huggingface.co")
10
+
11
+ # Path mappings from environment variable
12
+ # Expected format: {"path1": "mapped_path1", "path2": "mapped_path2"}
13
+ def get_path_mappings():
14
+ mappings_str = os.getenv("PATH_MAPPINGS", '{"/": "/"}')
15
+ try:
16
+ return json.loads(mappings_str)
17
+ except json.JSONDecodeError:
18
+ # Fallback to default mappings if JSON is invalid
19
+ return {
20
+ "/": "/",
21
+ }
22
+
23
+ PATH_MAPPINGS = get_path_mappings()
24
+
25
+
26
+ @app.route('/<path:path>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
27
+ def proxy(path):
28
+ # Construct the full path
29
+ full_path = f"/{path}"
30
+
31
+ # Apply path mapping if matches
32
+ for original_path, new_path in PATH_MAPPINGS.items():
33
+ if full_path.startswith(original_path):
34
+ full_path = full_path.replace(original_path, new_path, 1)
35
+ break
36
+
37
+ # Construct target URL
38
+ target_url = f"{TARGET_API}{full_path}"
39
+
40
+ # Forward the request to the target API
41
+ headers = {key: value for key, value in request.headers if key != 'Host'}
42
+
43
+ # Handle streaming response
44
+ if request.method == 'POST':
45
+ response = requests.post(
46
+ target_url,
47
+ headers=headers,
48
+ json=request.get_json(silent=True),
49
+ params=request.args,
50
+ stream=True
51
+ )
52
+ elif request.method == 'GET':
53
+ response = requests.get(
54
+ target_url,
55
+ headers=headers,
56
+ params=request.args,
57
+ stream=True
58
+ )
59
+ elif request.method == 'PUT':
60
+ response = requests.put(
61
+ target_url,
62
+ headers=headers,
63
+ json=request.get_json(silent=True),
64
+ params=request.args,
65
+ stream=True
66
+ )
67
+ elif request.method == 'DELETE':
68
+ response = requests.delete(
69
+ target_url,
70
+ headers=headers,
71
+ params=request.args,
72
+ stream=True
73
+ )
74
+ elif request.method == 'PATCH':
75
+ response = requests.patch(
76
+ target_url,
77
+ headers=headers,
78
+ json=request.get_json(silent=True),
79
+ params=request.args,
80
+ stream=True
81
+ )
82
+
83
+ # Create a response with the same status code, headers, and streaming content
84
+ def generate():
85
+ for chunk in response.iter_content(chunk_size=8192):
86
+ yield chunk
87
+
88
+ # Create flask response
89
+ proxy_response = Response(
90
+ stream_with_context(generate()),
91
+ status=response.status_code
92
+ )
93
+
94
+ # Forward response headers
95
+ for key, value in response.headers.items():
96
+ if key.lower() not in ('content-length', 'transfer-encoding', 'connection'):
97
+ proxy_response.headers[key] = value
98
+
99
+ return proxy_response
100
+
101
+
102
+ @app.route('/', methods=['GET'])
103
+ def index():
104
+ return "service running."
105
+
106
+
107
+ if __name__ == '__main__':
108
+ app.run(host='0.0.0.0', port=7860, debug=False)