Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from flask import Flask, request, Response
|
| 3 |
+
|
| 4 |
+
app = Flask(__name__)
|
| 5 |
+
|
| 6 |
+
GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/v1beta"
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
@app.route("/", methods=["GET"])
|
| 10 |
+
def home():
|
| 11 |
+
return {"status": "ok", "message": "Gemini proxy работает :3"}
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.route("/<path:endpoint>", methods=["GET", "POST", "PUT", "PATCH", "DELETE"])
|
| 15 |
+
def proxy(endpoint):
|
| 16 |
+
url = f"{GEMINI_BASE_URL}/{endpoint}"
|
| 17 |
+
|
| 18 |
+
# Копируем все заголовки, кроме host и content-length
|
| 19 |
+
headers = {k: v for k, v in request.headers if k.lower() not in ["host", "content-length"]}
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
resp = requests.request(
|
| 23 |
+
method=request.method,
|
| 24 |
+
url=url,
|
| 25 |
+
headers=headers,
|
| 26 |
+
params=request.args,
|
| 27 |
+
data=request.get_data(),
|
| 28 |
+
allow_redirects=False,
|
| 29 |
+
stream=True,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
excluded_headers = ["content-encoding", "transfer-encoding", "connection"]
|
| 33 |
+
response_headers = [(k, v) for k, v in resp.raw.headers.items() if k.lower() not in excluded_headers]
|
| 34 |
+
|
| 35 |
+
return Response(resp.content, resp.status_code, response_headers)
|
| 36 |
+
|
| 37 |
+
except Exception as e:
|
| 38 |
+
return {"error": str(e)}, 500
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
if __name__ == "__main__":
|
| 42 |
+
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))
|