Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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 |
+
# Копируем заголовки (особенно важен Authorization или x-goog-api-key)
|
| 21 |
+
headers = dict(request.headers)
|
| 22 |
+
# Удаляем хост, чтобы Google не ругался
|
| 23 |
+
headers.pop("host", None)
|
| 24 |
+
|
| 25 |
+
# Делаем запрос к Google
|
| 26 |
+
resp = await client.request(
|
| 27 |
+
method=request.method,
|
| 28 |
+
url=url,
|
| 29 |
+
content=body,
|
| 30 |
+
headers=headers,
|
| 31 |
+
timeout=60.0
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
return Response(
|
| 35 |
+
content=resp.content,
|
| 36 |
+
status_code=resp.status_code,
|
| 37 |
+
headers=dict(resp.headers)
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
import uvicorn
|
| 42 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|