infinityonline commited on
Commit
7a433fd
Β·
verified Β·
1 Parent(s): 4355a9a

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -21
main.py CHANGED
@@ -61,10 +61,6 @@ class BrowserWorker:
61
  def tag(self, msg: str) -> str:
62
  return f"[W{self.id}] {msg}"
63
 
64
- # ──────────────────────────────────────────────
65
- # Init
66
- # ──────────────────────────────────────────────
67
-
68
  async def init(self, playwright):
69
  self._playwright = playwright
70
  self._browser = await playwright.chromium.launch(
@@ -109,10 +105,6 @@ class BrowserWorker:
109
  finally:
110
  await page.close()
111
 
112
- # ──────────────────────────────────────────────
113
- # Context Rotation
114
- # ──────────────────────────────────────────────
115
-
116
  async def _rotate_context(self):
117
  print(self.tag(f"Rotating context after {self._request_count} requests..."))
118
  try:
@@ -124,10 +116,6 @@ class BrowserWorker:
124
  self._request_count = 0
125
  print(self.tag("Context rotated βœ“"))
126
 
127
- # ──────────────────────────────────────────────
128
- # Chat
129
- # ──────────────────────────────────────────────
130
-
131
  async def chat(self, model_label: str, prompt: str) -> str:
132
  async with self._lock:
133
  self.busy = True
@@ -172,10 +160,6 @@ class BrowserWorker:
172
  finally:
173
  await page.close()
174
 
175
- # ──────────────────────────────────────────────
176
- # Helpers
177
- # ──────────────────────────────────────────────
178
-
179
  async def _select_model(self, page, model_label: str):
180
  try:
181
  btn = page.locator('button[data-testid="model-select-button"]')
@@ -455,6 +439,15 @@ def _auth(request: Request) -> bool:
455
  )
456
 
457
 
 
 
 
 
 
 
 
 
 
458
  def _get_model_label(model: str) -> str:
459
  return DUCK_MODELS.get(model, DUCK_MODELS[DEFAULT_MODEL])
460
 
@@ -496,6 +489,7 @@ async def chat_completions(request: Request):
496
  return JSONResponse(status_code=400, content={"error": {"message": "Invalid JSON"}})
497
  if not _auth(request):
498
  return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
 
499
  messages = data.get("messages", [])
500
  if not messages:
501
  return JSONResponse(status_code=400, content={"error": {"message": "messages required"}})
@@ -589,8 +583,11 @@ async def list_models(request: Request):
589
 
590
 
591
  @app.get("/health")
592
- @app.get("/")
593
- async def health():
 
 
 
594
  busy = sum(1 for w in pool.workers if w.busy)
595
  stats = [
596
  {
@@ -622,10 +619,25 @@ async def health():
622
  }
623
 
624
 
 
 
 
 
 
 
 
 
 
 
625
  @app.get("/dashboard", response_class=HTMLResponse)
626
- async def dashboard(request: Request):
627
- if not _auth(request):
628
- return HTMLResponse("<h1>401 Unauthorized</h1>", status_code=401)
 
 
 
 
 
629
  with open("dashboard.html", "r", encoding="utf-8") as f:
630
  return HTMLResponse(f.read())
631
 
 
61
  def tag(self, msg: str) -> str:
62
  return f"[W{self.id}] {msg}"
63
 
 
 
 
 
64
  async def init(self, playwright):
65
  self._playwright = playwright
66
  self._browser = await playwright.chromium.launch(
 
105
  finally:
106
  await page.close()
107
 
 
 
 
 
108
  async def _rotate_context(self):
109
  print(self.tag(f"Rotating context after {self._request_count} requests..."))
110
  try:
 
116
  self._request_count = 0
117
  print(self.tag("Context rotated βœ“"))
118
 
 
 
 
 
119
  async def chat(self, model_label: str, prompt: str) -> str:
120
  async with self._lock:
121
  self.busy = True
 
160
  finally:
161
  await page.close()
162
 
 
 
 
 
163
  async def _select_model(self, page, model_label: str):
164
  try:
165
  btn = page.locator('button[data-testid="model-select-button"]')
 
439
  )
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()
447
+ )
448
+ return header_key == API_SECRET_KEY or token == API_SECRET_KEY
449
+
450
+
451
  def _get_model_label(model: str) -> str:
452
  return DUCK_MODELS.get(model, DUCK_MODELS[DEFAULT_MODEL])
453
 
 
489
  return JSONResponse(status_code=400, content={"error": {"message": "Invalid JSON"}})
490
  if not _auth(request):
491
  return JSONResponse(status_code=401, content={"error": {"message": "Invalid API Key"}})
492
+
493
  messages = data.get("messages", [])
494
  if not messages:
495
  return JSONResponse(status_code=400, content={"error": {"message": "messages required"}})
 
583
 
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
+
591
  busy = sum(1 for w in pool.workers if w.busy)
592
  stats = [
593
  {
 
619
  }
620
 
621
 
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%;"
638
+ "color:#ef5350'>401 β€” Unauthorized</h1>",
639
+ status_code=401
640
+ )
641
  with open("dashboard.html", "r", encoding="utf-8") as f:
642
  return HTMLResponse(f.read())
643