Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,37 @@ from fastapi import FastAPI, Request, Response
|
|
| 2 |
import httpx
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
-
|
| 6 |
-
# Базовый URL Google AI Studio
|
| 7 |
GOOGLE_API_URL = "https://generativelanguage.googleapis.com"
|
| 8 |
|
| 9 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 10 |
async def proxy(path: str, request: Request):
|
| 11 |
async with httpx.AsyncClient() as client:
|
| 12 |
-
# Собираем URL
|
| 13 |
url = f"{GOOGLE_API_URL}/{path}"
|
| 14 |
if request.query_params:
|
| 15 |
url += f"?{request.query_params}"
|
| 16 |
|
| 17 |
-
# Читаем тело запроса
|
| 18 |
body = await request.body()
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
headers.pop("host", None)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 2 |
import httpx
|
| 3 |
|
| 4 |
app = FastAPI()
|
|
|
|
|
|
|
| 5 |
GOOGLE_API_URL = "https://generativelanguage.googleapis.com"
|
| 6 |
|
| 7 |
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 8 |
async def proxy(path: str, request: Request):
|
| 9 |
async with httpx.AsyncClient() as client:
|
|
|
|
| 10 |
url = f"{GOOGLE_API_URL}/{path}"
|
| 11 |
if request.query_params:
|
| 12 |
url += f"?{request.query_params}"
|
| 13 |
|
|
|
|
| 14 |
body = await request.body()
|
| 15 |
|
| 16 |
+
# Фильтруем заголовки, оставляем только нужные для API
|
| 17 |
+
allowed_headers = ['content-type', 'x-goog-api-key', 'authorization', 'x-goog-api-client']
|
| 18 |
+
headers = {k: v for k, v in request.headers.items() if k.lower() in allowed_headers}
|
|
|
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
resp = await client.request(
|
| 22 |
+
method=request.method,
|
| 23 |
+
url=url,
|
| 24 |
+
content=body,
|
| 25 |
+
headers=headers,
|
| 26 |
+
timeout=60.0
|
| 27 |
+
)
|
| 28 |
+
return Response(
|
| 29 |
+
content=resp.content,
|
| 30 |
+
status_code=resp.status_code,
|
| 31 |
+
headers={"Content-Type": resp.headers.get("Content-Type", "application/json")}
|
| 32 |
+
)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return Response(content=str(e), status_code=500)
|
| 35 |
|
| 36 |
+
@app.get("/")
|
| 37 |
+
async def health():
|
| 38 |
+
return {"status": "proxy is running"}
|