Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 3 |
+
import httpx
|
| 4 |
+
import random
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
TELEGRAPH_URL = 'https://generativelanguage.googleapis.com/v1beta'
|
| 9 |
+
|
| 10 |
+
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS", "HEAD", "PATCH"])
|
| 11 |
+
async def proxy(path: str, request: Request):
|
| 12 |
+
try:
|
| 13 |
+
url = f"{TELEGRAPH_URL}/{path}"
|
| 14 |
+
url += f"?{request.query_params}" if request.query_params else ""
|
| 15 |
+
|
| 16 |
+
provided_api_keys = request.query_params.get('key')
|
| 17 |
+
|
| 18 |
+
if not provided_api_keys:
|
| 19 |
+
raise HTTPException(status_code=400, detail='API key is missing.')
|
| 20 |
+
|
| 21 |
+
api_key_array = [key.strip() for key in provided_api_keys.split(';') if key.strip()]
|
| 22 |
+
if not api_key_array:
|
| 23 |
+
raise HTTPException(status_code=400, detail='Valid API key is missing.')
|
| 24 |
+
|
| 25 |
+
selected_api_key = random.choice(api_key_array)
|
| 26 |
+
url = url.replace(provided_api_keys, selected_api_key)
|
| 27 |
+
|
| 28 |
+
headers = {key: value for key, value in request.headers.items() if key.lower() != 'host'}
|
| 29 |
+
headers['content-type'] = 'application/json'
|
| 30 |
+
|
| 31 |
+
async with httpx.AsyncClient() as client:
|
| 32 |
+
content = await request.body()
|
| 33 |
+
|
| 34 |
+
if content:
|
| 35 |
+
try:
|
| 36 |
+
original_body = await request.json()
|
| 37 |
+
except:
|
| 38 |
+
original_body = {}
|
| 39 |
+
new_body = {**original_body}
|
| 40 |
+
else:
|
| 41 |
+
new_body = {}
|
| 42 |
+
|
| 43 |
+
new_body["safetySettings"] = [
|
| 44 |
+
{
|
| 45 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 46 |
+
"threshold": "BLOCK_NONE"
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 50 |
+
"threshold": "BLOCK_NONE"
|
| 51 |
+
},
|
| 52 |
+
{
|
| 53 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 54 |
+
"threshold": "BLOCK_NONE"
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 58 |
+
"threshold": "BLOCK_NONE"
|
| 59 |
+
},
|
| 60 |
+
{
|
| 61 |
+
"category": "HARM_CATEGORY_CIVIC_INTEGRITY",
|
| 62 |
+
"threshold": "BLOCK_NONE"
|
| 63 |
+
}
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# 使用 httpx 发起代理请求
|
| 67 |
+
r = await client.request(
|
| 68 |
+
method=request.method,
|
| 69 |
+
url=url,
|
| 70 |
+
headers=headers,
|
| 71 |
+
content=content if not new_body else JSONResponse(content=new_body).body, # 将 new_body 转换为 JSON 字符串
|
| 72 |
+
timeout=60.0,
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
# 流式传输响应
|
| 76 |
+
return StreamingResponse(
|
| 77 |
+
content=r.aiter_bytes(),
|
| 78 |
+
status_code=r.status_code,
|
| 79 |
+
headers=r.headers,
|
| 80 |
+
media_type=r.headers.get("content-type")
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
except HTTPException as e:
|
| 84 |
+
return JSONResponse(content={"detail": e.detail}, status_code=e.status_code)
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return JSONResponse(content={"detail": "An error occurred: " + str(e)}, status_code=500)
|