Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from fastapi import FastAPI, Request, HTTPException, Depends, status
|
|
| 4 |
from fastapi.responses import Response # 导入Response
|
| 5 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials # 导入HTTPBearer和HTTPAuthorizationCredentials
|
| 6 |
import httpx # 使用httpx替代requests,因为requests是同步的,而FastAPI是异步的
|
| 7 |
-
import os
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from enum import Enum
|
| 10 |
|
|
@@ -12,7 +12,7 @@ from enum import Enum
|
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
# 从环境变量获取代理自身的API密钥
|
| 15 |
-
|
| 16 |
|
| 17 |
# 定义HTTPBearer安全方案
|
| 18 |
security = HTTPBearer()
|
|
@@ -24,7 +24,7 @@ class ProtocolType(str, Enum):
|
|
| 24 |
|
| 25 |
# 依赖函数:验证代理的API密钥
|
| 26 |
def verify_proxy_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 27 |
-
if not
|
| 28 |
raise HTTPException(
|
| 29 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 30 |
detail="Invalid or missing proxy API key",
|
|
@@ -43,27 +43,24 @@ def greet_json():
|
|
| 43 |
async def health_check():
|
| 44 |
return {"status": "ok", "message": "Proxy service is running."}
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path: str, proxy_api_key: str = Depends(verify_proxy_api_key)): # 添加代理认证依赖
|
| 55 |
-
# 拼接Gemini真实请求URL
|
| 56 |
-
gemini_url = f"{GEMINI_BASE_URL}/{path}"
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
print('gemini_url', gemini_url)
|
| 60 |
-
|
| 61 |
-
print('protocol.value', protocol.value)
|
| 62 |
-
print('host', host)
|
| 63 |
-
print('path', path)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
real_url = f"{protocol.value}://{host}/{path}"
|
| 66 |
-
print('real_url', real_url)
|
| 67 |
|
| 68 |
# 提取客户端请求的headers和body,透传给Gemini
|
| 69 |
client_headers = dict(request.headers)
|
|
@@ -75,8 +72,16 @@ async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path:
|
|
| 75 |
client_headers["User-Agent"] = "curl/8.7.1"
|
| 76 |
|
| 77 |
# 添加Authorization头,用于Gemini的OpenAI兼容API
|
| 78 |
-
if
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
try:
|
| 82 |
# 读取客户端请求体
|
|
@@ -86,7 +91,6 @@ async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path:
|
|
| 86 |
async with httpx.AsyncClient() as client:
|
| 87 |
response = await client.request(
|
| 88 |
method=request.method,
|
| 89 |
-
# url=gemini_url,
|
| 90 |
url=real_url,
|
| 91 |
headers=client_headers,
|
| 92 |
content=client_body, # httpx使用content参数传递请求体
|
|
|
|
| 4 |
from fastapi.responses import Response # 导入Response
|
| 5 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials # 导入HTTPBearer和HTTPAuthorizationCredentials
|
| 6 |
import httpx # 使用httpx替代requests,因为requests是同步的,而FastAPI是异步的
|
| 7 |
+
import os, json
|
| 8 |
from dotenv import load_dotenv
|
| 9 |
from enum import Enum
|
| 10 |
|
|
|
|
| 12 |
load_dotenv()
|
| 13 |
|
| 14 |
# 从环境变量获取代理自身的API密钥
|
| 15 |
+
proxy_api_key = os.getenv("PROXY_API_KEY")
|
| 16 |
|
| 17 |
# 定义HTTPBearer安全方案
|
| 18 |
security = HTTPBearer()
|
|
|
|
| 24 |
|
| 25 |
# 依赖函数:验证代理的API密钥
|
| 26 |
def verify_proxy_api_key(credentials: HTTPAuthorizationCredentials = Depends(security)):
|
| 27 |
+
if not proxy_api_key or credentials.credentials != proxy_api_key:
|
| 28 |
raise HTTPException(
|
| 29 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 30 |
detail="Invalid or missing proxy API key",
|
|
|
|
| 43 |
async def health_check():
|
| 44 |
return {"status": "ok", "message": "Proxy service is running."}
|
| 45 |
|
| 46 |
+
gemini_api_keys_str = os.getenv("GEMINI_API_KEYS_STR")
|
| 47 |
+
try:
|
| 48 |
+
gemini_api_keys_arr = json.loads(gemini_api_keys_str)
|
| 49 |
+
except json.JSONDecodeError as e:
|
| 50 |
+
print(f"JSON 解析错误:{e}")
|
| 51 |
+
gemini_api_keys_arr = [] # 解析失败时设置默认值
|
| 52 |
+
except Exception as e:
|
| 53 |
+
print(f"其他错误:{e}")
|
| 54 |
+
gemini_api_keys_arr = []
|
| 55 |
|
| 56 |
+
key_index = 0
|
| 57 |
+
keys_count = len(gemini_api_keys_arr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# @app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
|
| 60 |
+
@app.api_route("/v1/{protocol}/{host}/{path:path}", methods=["POST"])
|
| 61 |
+
async def proxy_gemini(request: Request, protocol: ProtocolType, host:str, path: str, proxy_api_key: str = Depends(verify_proxy_api_key)): # 添加代理认证依赖
|
| 62 |
+
global key_index # 声明使用全局变量
|
| 63 |
real_url = f"{protocol.value}://{host}/{path}"
|
|
|
|
| 64 |
|
| 65 |
# 提取客户端请求的headers和body,透传给Gemini
|
| 66 |
client_headers = dict(request.headers)
|
|
|
|
| 72 |
client_headers["User-Agent"] = "curl/8.7.1"
|
| 73 |
|
| 74 |
# 添加Authorization头,用于Gemini的OpenAI兼容API
|
| 75 |
+
if gemini_api_keys_arr: # 确保有可用的API密钥
|
| 76 |
+
print(f"key_index: {key_index}")
|
| 77 |
+
gemini_api_key = gemini_api_keys_arr[key_index]
|
| 78 |
+
key_index = (key_index + 1) % keys_count
|
| 79 |
+
print(f"next_key_index: {key_index}")
|
| 80 |
+
client_headers["Authorization"] = f"Bearer {gemini_api_key}"
|
| 81 |
+
elif gemini_api_key: # 如果没有API密钥列表,使用单个API密钥
|
| 82 |
+
client_headers["Authorization"] = f"Bearer {gemini_api_key}"
|
| 83 |
+
else:
|
| 84 |
+
raise HTTPException(status_code=500, detail="No Gemini API keys configured.")
|
| 85 |
|
| 86 |
try:
|
| 87 |
# 读取客户端请求体
|
|
|
|
| 91 |
async with httpx.AsyncClient() as client:
|
| 92 |
response = await client.request(
|
| 93 |
method=request.method,
|
|
|
|
| 94 |
url=real_url,
|
| 95 |
headers=client_headers,
|
| 96 |
content=client_body, # httpx使用content参数传递请求体
|