Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,6 +7,7 @@ import threading
|
|
| 7 |
import json
|
| 8 |
import re
|
| 9 |
import psutil
|
|
|
|
| 10 |
from fastapi import FastAPI, Request
|
| 11 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 12 |
|
|
@@ -366,6 +367,28 @@ pool = BrowserPool()
|
|
| 366 |
pool.start()
|
| 367 |
|
| 368 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
# ====================================================================
|
| 370 |
# Helpers
|
| 371 |
# ====================================================================
|
|
@@ -440,7 +463,6 @@ def _auth(request: Request) -> bool:
|
|
| 440 |
|
| 441 |
|
| 442 |
def _auth_token(request: Request, token: str = "") -> bool:
|
| 443 |
-
"""ููุจู ุงูู
ุตุงุฏูุฉ ู
ู Header ุฃู ู
ู ?token= ูู ุงูู URL"""
|
| 444 |
header_key = (
|
| 445 |
request.headers.get("authorization", "")
|
| 446 |
.replace("Bearer ", "").strip()
|
|
@@ -481,6 +503,12 @@ def _make_completion(start_time, model, text, messages, tools=None):
|
|
| 481 |
app = FastAPI(title="Duck.ai API Server")
|
| 482 |
|
| 483 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
@app.post("/v1/chat/completions")
|
| 485 |
async def chat_completions(request: Request):
|
| 486 |
try:
|
|
@@ -584,7 +612,6 @@ async def list_models(request: Request):
|
|
| 584 |
|
| 585 |
@app.get("/health")
|
| 586 |
async def health(request: Request, token: str = ""):
|
| 587 |
-
# โโ /health ููุจู Header ุฃู ?token= โโโโโโโโโโโโโโโโโโโโโโ
|
| 588 |
if not _auth_token(request, token):
|
| 589 |
return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
|
| 590 |
|
|
@@ -609,6 +636,7 @@ async def health(request: Request, token: str = ""):
|
|
| 609 |
"total_requests": pool._total_requests,
|
| 610 |
"rejected_requests": pool._rejected,
|
| 611 |
"queue_timeout_sec": QUEUE_TIMEOUT,
|
|
|
|
| 612 |
"workers": stats,
|
| 613 |
"ram": {
|
| 614 |
"used_gb": round(mem.used / 1024**3, 2),
|
|
@@ -622,16 +650,16 @@ async def health(request: Request, token: str = ""):
|
|
| 622 |
@app.get("/")
|
| 623 |
async def root():
|
| 624 |
return {
|
| 625 |
-
"status":
|
| 626 |
-
"message":
|
| 627 |
-
"docs":
|
| 628 |
-
"health":
|
|
|
|
| 629 |
}
|
| 630 |
|
| 631 |
|
| 632 |
@app.get("/dashboard", response_class=HTMLResponse)
|
| 633 |
async def dashboard(request: Request, token: str = ""):
|
| 634 |
-
# โโ ููุจู Header ุฃู ?token= ูู ุงูู URL โโโโโโโโโโโโโโโโโโโ
|
| 635 |
if not _auth_token(request, token):
|
| 636 |
return HTMLResponse(
|
| 637 |
"<h1 style='font-family:sans-serif;text-align:center;margin-top:20%;"
|
|
|
|
| 7 |
import json
|
| 8 |
import re
|
| 9 |
import psutil
|
| 10 |
+
import httpx
|
| 11 |
from fastapi import FastAPI, Request
|
| 12 |
from fastapi.responses import JSONResponse, HTMLResponse
|
| 13 |
|
|
|
|
| 367 |
pool.start()
|
| 368 |
|
| 369 |
|
| 370 |
+
# ====================================================================
|
| 371 |
+
# Keep-Alive (ูู
ูุน HuggingFace ู
ู ุฅููุงู Space ุจุณุจุจ ุนุฏู
ุงููุดุงุท)
|
| 372 |
+
# ====================================================================
|
| 373 |
+
|
| 374 |
+
_keep_alive_count = 0
|
| 375 |
+
|
| 376 |
+
async def _keep_alive():
|
| 377 |
+
global _keep_alive_count
|
| 378 |
+
# ุงูุชุธุฑ ุญุชู ูููู ุงูุณูุฑูุฑ ุฌุงูุฒุงู
|
| 379 |
+
await asyncio.sleep(30)
|
| 380 |
+
print("[KEEP-ALIVE] Started โ ping every 4 minutes โ")
|
| 381 |
+
while True:
|
| 382 |
+
await asyncio.sleep(240) # ูู 4 ุฏูุงุฆู
|
| 383 |
+
try:
|
| 384 |
+
async with httpx.AsyncClient(timeout=10) as client:
|
| 385 |
+
r = await client.get("http://localhost:7860/")
|
| 386 |
+
_keep_alive_count += 1
|
| 387 |
+
print(f"[KEEP-ALIVE] Ping #{_keep_alive_count} โ {r.status_code} โ")
|
| 388 |
+
except Exception as e:
|
| 389 |
+
print(f"[KEEP-ALIVE] Ping failed (non-fatal): {e}")
|
| 390 |
+
|
| 391 |
+
|
| 392 |
# ====================================================================
|
| 393 |
# Helpers
|
| 394 |
# ====================================================================
|
|
|
|
| 463 |
|
| 464 |
|
| 465 |
def _auth_token(request: Request, token: str = "") -> bool:
|
|
|
|
| 466 |
header_key = (
|
| 467 |
request.headers.get("authorization", "")
|
| 468 |
.replace("Bearer ", "").strip()
|
|
|
|
| 503 |
app = FastAPI(title="Duck.ai API Server")
|
| 504 |
|
| 505 |
|
| 506 |
+
@app.on_event("startup")
|
| 507 |
+
async def startup_event():
|
| 508 |
+
asyncio.create_task(_keep_alive())
|
| 509 |
+
print("[APP] Keep-alive task registered โ")
|
| 510 |
+
|
| 511 |
+
|
| 512 |
@app.post("/v1/chat/completions")
|
| 513 |
async def chat_completions(request: Request):
|
| 514 |
try:
|
|
|
|
| 612 |
|
| 613 |
@app.get("/health")
|
| 614 |
async def health(request: Request, token: str = ""):
|
|
|
|
| 615 |
if not _auth_token(request, token):
|
| 616 |
return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
|
| 617 |
|
|
|
|
| 636 |
"total_requests": pool._total_requests,
|
| 637 |
"rejected_requests": pool._rejected,
|
| 638 |
"queue_timeout_sec": QUEUE_TIMEOUT,
|
| 639 |
+
"keep_alive_pings": _keep_alive_count,
|
| 640 |
"workers": stats,
|
| 641 |
"ram": {
|
| 642 |
"used_gb": round(mem.used / 1024**3, 2),
|
|
|
|
| 650 |
@app.get("/")
|
| 651 |
async def root():
|
| 652 |
return {
|
| 653 |
+
"status": "running",
|
| 654 |
+
"message": "Duck.ai API Pool Server is active!",
|
| 655 |
+
"docs": "/docs",
|
| 656 |
+
"health": "/health",
|
| 657 |
+
"keep_alive_pings": _keep_alive_count,
|
| 658 |
}
|
| 659 |
|
| 660 |
|
| 661 |
@app.get("/dashboard", response_class=HTMLResponse)
|
| 662 |
async def dashboard(request: Request, token: str = ""):
|
|
|
|
| 663 |
if not _auth_token(request, token):
|
| 664 |
return HTMLResponse(
|
| 665 |
"<h1 style='font-family:sans-serif;text-align:center;margin-top:20%;"
|