Lei075fr commited on
Commit
33c03b7
·
verified ·
1 Parent(s): fb0181b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -1,8 +1,12 @@
1
  import os, random, httpx, uvicorn
2
  from fastapi import FastAPI, Request, Response
 
3
 
4
  app = FastAPI()
5
- KEYS = [os.getenv(f"OR_KEY_{i}") for i in range(1, 16) if os.getenv(f"OR_KEY_{i}")]
 
 
 
6
 
7
  MODEL_MAP = {
8
  "vitalis": "google/gemini-2.0-pro-exp-02-05:free",
@@ -11,31 +15,58 @@ MODEL_MAP = {
11
  "default": "google/gemini-2.0-flash-lite:preview"
12
  }
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  @app.post("/v1/chat/completions")
15
  async def proxy(request: Request):
 
 
 
16
  body = await request.json()
17
  msg = str(body.get("messages", "")).lower()
18
 
19
  # Roteamento Inteligente
20
  selected = MODEL_MAP["default"]
21
  for proj in ["vitalis", "nexos", "nutrilens"]:
22
- if proj in msg: selected = MODEL_MAP[proj]
 
 
23
  body["model"] = selected
24
 
 
 
25
  headers = {
26
- "Authorization": f"Bearer {random.choice(KEYS)}",
27
- "X-Title": f"Fortune_Shield_{random.randint(100,999)}",
28
  "HTTP-Referer": "https://fortune-dev.io",
29
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
30
  }
31
 
32
  async with httpx.AsyncClient() as client:
33
  try:
34
- resp = await client.post("https://openrouter.ai/api/v1/chat/completions",
35
- json=body, headers=headers, timeout=60.0)
 
 
 
 
36
  return Response(content=resp.content, status_code=resp.status_code)
37
  except Exception as e:
38
  return Response(content=f'{{"error": "{str(e)}"}}', status_code=500)
39
 
40
  if __name__ == "__main__":
41
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
  import os, random, httpx, uvicorn
2
  from fastapi import FastAPI, Request, Response
3
+ from fastapi.responses import HTMLResponse
4
 
5
  app = FastAPI()
6
+
7
+ # Puxar as chaves e filtrar as que estão vazias
8
+ KEYS = [os.getenv(f"OR_KEY_{i}") for i in range(1, 16)]
9
+ KEYS = [k for k in KEYS if k and k.strip()]
10
 
11
  MODEL_MAP = {
12
  "vitalis": "google/gemini-2.0-pro-exp-02-05:free",
 
15
  "default": "google/gemini-2.0-flash-lite:preview"
16
  }
17
 
18
+ # Rota Inicial (Resolve o erro "Not Found" do NutriLens)
19
+ @app.get("/", response_class=HTMLResponse)
20
+ async def root():
21
+ return """
22
+ <html>
23
+ <head><title>Fortune API Shield</title></head>
24
+ <body style="background:#000; color:#0f0; font-family:monospace; display:flex; justify-content:center; align-items:center; height:100vh;">
25
+ <div>
26
+ <h1>🛡️ ESCUDO ATIVO: PROTOCOLO SOMBRA</h1>
27
+ <p>> Status: Operacional em Beira</p>
28
+ <p>> Sistemas Ligados: Vitalis | Nexos | NutriLens</p>
29
+ </div>
30
+ </body>
31
+ </html>
32
+ """
33
+
34
  @app.post("/v1/chat/completions")
35
  async def proxy(request: Request):
36
+ if not KEYS:
37
+ return Response(content='{"error": "Nenhuma OR_KEY configurada nos Secrets!"}', status_code=500)
38
+
39
  body = await request.json()
40
  msg = str(body.get("messages", "")).lower()
41
 
42
  # Roteamento Inteligente
43
  selected = MODEL_MAP["default"]
44
  for proj in ["vitalis", "nexos", "nutrilens"]:
45
+ if proj in msg:
46
+ selected = MODEL_MAP[proj]
47
+
48
  body["model"] = selected
49
 
50
+ # Escolher uma chave válida e montar headers
51
+ current_key = random.choice(KEYS)
52
  headers = {
53
+ "Authorization": f"Bearer {current_key}",
54
+ "X-Title": f"Aria_System_{random.randint(100,999)}",
55
  "HTTP-Referer": "https://fortune-dev.io",
56
+ "Content-Type": "application/json"
57
  }
58
 
59
  async with httpx.AsyncClient() as client:
60
  try:
61
+ resp = await client.post(
62
+ "https://openrouter.ai/api/v1/chat/completions",
63
+ json=body,
64
+ headers=headers,
65
+ timeout=60.0
66
+ )
67
  return Response(content=resp.content, status_code=resp.status_code)
68
  except Exception as e:
69
  return Response(content=f'{{"error": "{str(e)}"}}', status_code=500)
70
 
71
  if __name__ == "__main__":
72
+ uvicorn.run(app, host="0.0.0.0", port=7860)