Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ import secrets
|
|
| 12 |
import urllib.parse
|
| 13 |
from typing import Optional, Dict, Any
|
| 14 |
from fastapi import FastAPI, Request, HTTPException, Depends, Header
|
| 15 |
-
from fastapi.responses import StreamingResponse, JSONResponse
|
| 16 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 17 |
from fake_useragent import UserAgent
|
| 18 |
# 修复 Pydantic 导入
|
|
@@ -35,7 +35,7 @@ class Settings(BaseSettings):
|
|
| 35 |
LOCAL_API_KEY: str = os.getenv("LOCAL_API_KEY", secrets.token_urlsafe(32))
|
| 36 |
|
| 37 |
# 其他配置
|
| 38 |
-
TOKEN_CACHE_TIME: int = int(os.getenv("TOKEN_CACHE_TIME",
|
| 39 |
FINGERPRINT_PREFIX: str = os.getenv("FINGERPRINT_PREFIX", "anon_")
|
| 40 |
|
| 41 |
class Config:
|
|
@@ -293,6 +293,135 @@ async def debug_token():
|
|
| 293 |
"expiry_time": token_expiry,
|
| 294 |
}
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
class SambaAuthAsync:
|
| 297 |
def __init__(self, email, password):
|
| 298 |
self.email = email
|
|
|
|
| 12 |
import urllib.parse
|
| 13 |
from typing import Optional, Dict, Any
|
| 14 |
from fastapi import FastAPI, Request, HTTPException, Depends, Header
|
| 15 |
+
from fastapi.responses import StreamingResponse, JSONResponse, HTMLResponse
|
| 16 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 17 |
from fake_useragent import UserAgent
|
| 18 |
# 修复 Pydantic 导入
|
|
|
|
| 35 |
LOCAL_API_KEY: str = os.getenv("LOCAL_API_KEY", secrets.token_urlsafe(32))
|
| 36 |
|
| 37 |
# 其他配置
|
| 38 |
+
TOKEN_CACHE_TIME: int = int(os.getenv("TOKEN_CACHE_TIME", 604800)) # 默认缓存7天 (7*24*60*60=604800秒)
|
| 39 |
FINGERPRINT_PREFIX: str = os.getenv("FINGERPRINT_PREFIX", "anon_")
|
| 40 |
|
| 41 |
class Config:
|
|
|
|
| 293 |
"expiry_time": token_expiry,
|
| 294 |
}
|
| 295 |
|
| 296 |
+
@app.get("/", response_class=HTMLResponse)
|
| 297 |
+
async def root():
|
| 298 |
+
"""根路由健康检查,返回HTML界面"""
|
| 299 |
+
current_time = time.time()
|
| 300 |
+
token_valid = access_token is not None and current_time < token_expiry
|
| 301 |
+
expires_in = max(0, int(token_expiry - current_time)) if access_token else 0
|
| 302 |
+
|
| 303 |
+
# 计算过期时间的可读格式
|
| 304 |
+
if expires_in > 0:
|
| 305 |
+
days = expires_in // 86400
|
| 306 |
+
hours = (expires_in % 86400) // 3600
|
| 307 |
+
minutes = (expires_in % 3600) // 60
|
| 308 |
+
expiry_readable = f"{days}天 {hours}小时 {minutes}分钟"
|
| 309 |
+
else:
|
| 310 |
+
expiry_readable = "已过期"
|
| 311 |
+
|
| 312 |
+
html_content = f"""
|
| 313 |
+
<!DOCTYPE html>
|
| 314 |
+
<html>
|
| 315 |
+
<head>
|
| 316 |
+
<title>SambaNova OpenAI 代理服务</title>
|
| 317 |
+
<meta charset="UTF-8">
|
| 318 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 319 |
+
<style>
|
| 320 |
+
body {{
|
| 321 |
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
| 322 |
+
line-height: 1.6;
|
| 323 |
+
color: #333;
|
| 324 |
+
max-width: 800px;
|
| 325 |
+
margin: 0 auto;
|
| 326 |
+
padding: 20px;
|
| 327 |
+
}}
|
| 328 |
+
h1 {{
|
| 329 |
+
color: #2c3e50;
|
| 330 |
+
border-bottom: 1px solid #eee;
|
| 331 |
+
padding-bottom: 10px;
|
| 332 |
+
}}
|
| 333 |
+
.status-card {{
|
| 334 |
+
background-color: #f8f9fa;
|
| 335 |
+
border-radius: 8px;
|
| 336 |
+
padding: 20px;
|
| 337 |
+
margin-bottom: 20px;
|
| 338 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 339 |
+
}}
|
| 340 |
+
.status-item {{
|
| 341 |
+
margin-bottom: 10px;
|
| 342 |
+
display: flex;
|
| 343 |
+
justify-content: space-between;
|
| 344 |
+
}}
|
| 345 |
+
.status-label {{
|
| 346 |
+
font-weight: bold;
|
| 347 |
+
color: #555;
|
| 348 |
+
}}
|
| 349 |
+
.status-value {{
|
| 350 |
+
text-align: right;
|
| 351 |
+
}}
|
| 352 |
+
.status-healthy {{
|
| 353 |
+
color: #28a745;
|
| 354 |
+
font-weight: bold;
|
| 355 |
+
}}
|
| 356 |
+
.status-warning {{
|
| 357 |
+
color: #ffc107;
|
| 358 |
+
font-weight: bold;
|
| 359 |
+
}}
|
| 360 |
+
.status-error {{
|
| 361 |
+
color: #dc3545;
|
| 362 |
+
font-weight: bold;
|
| 363 |
+
}}
|
| 364 |
+
.code-block {{
|
| 365 |
+
background-color: #f1f1f1;
|
| 366 |
+
padding: 15px;
|
| 367 |
+
border-radius: 5px;
|
| 368 |
+
font-family: monospace;
|
| 369 |
+
overflow-x: auto;
|
| 370 |
+
}}
|
| 371 |
+
.footer {{
|
| 372 |
+
margin-top: 30px;
|
| 373 |
+
font-size: 0.9em;
|
| 374 |
+
color: #6c757d;
|
| 375 |
+
text-align: center;
|
| 376 |
+
}}
|
| 377 |
+
</style>
|
| 378 |
+
</head>
|
| 379 |
+
<body>
|
| 380 |
+
<h1>SambaNova OpenAI 代理服务</h1>
|
| 381 |
+
|
| 382 |
+
<div class="status-card">
|
| 383 |
+
<h2>服务状态</h2>
|
| 384 |
+
<div class="status-item">
|
| 385 |
+
<span class="status-label">状态:</span>
|
| 386 |
+
<span class="status-value status-healthy">运行中</span>
|
| 387 |
+
</div>
|
| 388 |
+
<div class="status-item">
|
| 389 |
+
<span class="status-label">版本:</span>
|
| 390 |
+
<span class="status-value">1.0.0</span>
|
| 391 |
+
</div>
|
| 392 |
+
<div class="status-item">
|
| 393 |
+
<span class="status-label">令牌状态:</span>
|
| 394 |
+
<span class="status-value {('status-healthy' if token_valid else 'status-error')}">
|
| 395 |
+
{('有效' if token_valid else '无效')}
|
| 396 |
+
</span>
|
| 397 |
+
</div>
|
| 398 |
+
<div class="status-item">
|
| 399 |
+
<span class="status-label">令牌过期时间:</span>
|
| 400 |
+
<span class="status-value">{expiry_readable}</span>
|
| 401 |
+
</div>
|
| 402 |
+
<div class="status-item">
|
| 403 |
+
<span class="status-label">SambaNova 凭据:</span>
|
| 404 |
+
<span class="status-value {('status-healthy' if settings.SAMBA_EMAIL and settings.SAMBA_PASSWORD else 'status-error')}">
|
| 405 |
+
{('已配置' if settings.SAMBA_EMAIL and settings.SAMBA_PASSWORD else '未配置')}
|
| 406 |
+
</span>
|
| 407 |
+
</div>
|
| 408 |
+
<div class="status-item">
|
| 409 |
+
<span class="status-label">本地API密钥:</span>
|
| 410 |
+
<span class="status-value {('status-healthy' if settings.LOCAL_API_KEY else 'status-warning')}">
|
| 411 |
+
{('已配置' if settings.LOCAL_API_KEY else '未配置')}
|
| 412 |
+
</span>
|
| 413 |
+
</div>
|
| 414 |
+
</div>
|
| 415 |
+
|
| 416 |
+
<div class="footer">
|
| 417 |
+
<p>当前时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}</p>
|
| 418 |
+
</div>
|
| 419 |
+
</body>
|
| 420 |
+
</html>
|
| 421 |
+
"""
|
| 422 |
+
|
| 423 |
+
return html_content
|
| 424 |
+
|
| 425 |
class SambaAuthAsync:
|
| 426 |
def __init__(self, email, password):
|
| 427 |
self.email = email
|