infinityonline commited on
Commit
8368b8b
ยท
verified ยท
1 Parent(s): e3ae4d7

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +35 -7
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": "running",
626
- "message": "Duck.ai API Pool Server is active!",
627
- "docs": "/docs",
628
- "health": "/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%;"