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