Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,21 +8,21 @@ app = FastAPI()
|
|
| 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 |
-
# MAPEAMENTO UNIFICADO
|
| 12 |
MODEL_MAP = {
|
| 13 |
-
"aria_med": "google/gemini-2.5-pro:free",
|
| 14 |
-
"aria_logic": "qwen/qwen3-coder-480b:free",
|
| 15 |
-
"aria_vision": "nvidia/nemotron-nano-12b-2-vl:free",
|
| 16 |
-
"aria_brain": "meta-llama/llama-3.3-70b-instruct:free",
|
| 17 |
-
"aria_default": "qwen/qwen3-coder:free"
|
| 18 |
}
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
status = "🟢 SISTEMA OPERACIONAL" if KEYS else "🔴 FALHA NAS CHAVES"
|
| 23 |
return f"""
|
| 24 |
-
<body style='background:#0a0a0a; color:#
|
| 25 |
-
<h1 style='border: 2px solid #
|
| 26 |
<p style='font-size:1.5em;'>Chaves Ativas: {len(KEYS)} | Local: Beira, MZ</p>
|
| 27 |
<p style='color:#555;'>Status: {status}</p>
|
| 28 |
</body>
|
|
@@ -31,64 +31,30 @@ async def root():
|
|
| 31 |
@app.post("/v1/chat/completions")
|
| 32 |
async def proxy(request: Request):
|
| 33 |
if not KEYS:
|
| 34 |
-
return Response(content='{"error": "
|
| 35 |
-
|
| 36 |
body = await request.json()
|
| 37 |
-
|
| 38 |
-
msg_content = str(body.get("messages", "")).lower()
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
# Extrai o modelo real enviado pelo CEO
|
| 43 |
-
target = incoming_model.replace("fortune/", "", 1)
|
| 44 |
-
else:
|
| 45 |
-
# Seleção por contexto se o CEO não especificar fortune/
|
| 46 |
-
if any(word in msg_content for word in ["medicina", "vitalis", "paciente"]):
|
| 47 |
-
target = MODEL_MAP["aria_med"]
|
| 48 |
-
elif any(word in msg_content for word in ["nexos", "venda", "gestão", "erp"]):
|
| 49 |
-
target = MODEL_MAP["aria_logic"]
|
| 50 |
-
elif any(word in msg_content for word in ["nutrilens", "vejo", "câmara", "visão"]):
|
| 51 |
-
target = MODEL_MAP["aria_vision"]
|
| 52 |
-
else:
|
| 53 |
-
target = MODEL_MAP["aria_brain"]
|
| 54 |
-
|
| 55 |
-
# Correção de Segurança: Garante que modelos free tenham o sufixo :free
|
| 56 |
-
if "instruct" in target and not target.endswith(":free"):
|
| 57 |
-
target = f"{target}:free"
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
# --- CICLO DE RESILIÊNCIA ---
|
| 63 |
async with httpx.AsyncClient() as client:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
try:
|
| 73 |
-
resp = await client.post(
|
| 74 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 75 |
-
json=body, headers=headers, timeout=120.0
|
| 76 |
-
)
|
| 77 |
-
|
| 78 |
-
if resp.status_code == 200:
|
| 79 |
-
return Response(content=resp.content, status_code=200)
|
| 80 |
-
|
| 81 |
-
print(f"⚠️ Tentativa {attempt+1} falhou (Status {resp.status_code}). Rodando chave...")
|
| 82 |
-
|
| 83 |
-
except Exception as e:
|
| 84 |
-
print(f"❌ Erro de rede: {str(e)}")
|
| 85 |
-
await asyncio.sleep(0.3)
|
| 86 |
-
continue
|
| 87 |
-
|
| 88 |
-
# Fallback Final para o roteador livre
|
| 89 |
-
body["model"] = "openrouter/free"
|
| 90 |
-
emerg = await client.post("https://openrouter.ai/api/v1/chat/completions", json=body, headers=headers, timeout=60.0)
|
| 91 |
-
return Response(content=emerg.content, status_code=emerg.status_code)
|
| 92 |
-
|
| 93 |
-
if __name__ == "__main__":
|
| 94 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 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 |
+
# MAPEAMENTO UNIFICADO (Traduz os teus apelidos para os IDs do OpenRouter)
|
| 12 |
MODEL_MAP = {
|
| 13 |
+
"aria_med": "google/gemini-2.5-pro:free",
|
| 14 |
+
"aria_logic": "qwen/qwen3-coder-480b:free",
|
| 15 |
+
"aria_vision": "nvidia/nemotron-nano-12b-2-vl:free",
|
| 16 |
+
"aria_brain": "meta-llama/llama-3.3-70b-instruct:free",
|
| 17 |
+
"aria_default": "qwen/qwen3-coder-480b:free"
|
| 18 |
}
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
status = "🟢 SISTEMA OPERACIONAL" if KEYS else "🔴 FALHA NAS CHAVES"
|
| 23 |
return f"""
|
| 24 |
+
<body style='background:#0a0a0a; color:#00ff88; font-family:sans-serif; text-align:center; padding-top:50px;'>
|
| 25 |
+
<h1 style='border: 2px solid #00ff88; display:inline-block; padding:20px;'>ARIA SOVEREIGN SHIELD V2.5</h1>
|
| 26 |
<p style='font-size:1.5em;'>Chaves Ativas: {len(KEYS)} | Local: Beira, MZ</p>
|
| 27 |
<p style='color:#555;'>Status: {status}</p>
|
| 28 |
</body>
|
|
|
|
| 31 |
@app.post("/v1/chat/completions")
|
| 32 |
async def proxy(request: Request):
|
| 33 |
if not KEYS:
|
| 34 |
+
return Response(content='{"error": "No API keys configured"}', status_code=500)
|
| 35 |
+
|
| 36 |
body = await request.json()
|
| 37 |
+
model_requested = body.get("model", "")
|
|
|
|
| 38 |
|
| 39 |
+
# LÓGICA DE LIMPEZA: Remove o prefixo "fortune/" se a Aria o enviar
|
| 40 |
+
clean_model = model_requested.replace("fortune/", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# TRADUÇÃO: Se for um apelido, usa o ID real. Se não, usa o que foi enviado.
|
| 43 |
+
target_model = MODEL_MAP.get(clean_model, clean_model)
|
| 44 |
+
body["model"] = target_model
|
| 45 |
+
|
| 46 |
+
# ROTAÇÃO DE CHAVES: Escolhe uma das 15 chaves aleatoriamente para evitar bloqueios
|
| 47 |
+
headers = {
|
| 48 |
+
"Authorization": f"Bearer {random.choice(KEYS)}",
|
| 49 |
+
"HTTP-Referer": "https://github.com/Fortnee/Ariaaa",
|
| 50 |
+
"Content-Type": "application/json"
|
| 51 |
+
}
|
| 52 |
|
|
|
|
| 53 |
async with httpx.AsyncClient() as client:
|
| 54 |
+
response = await client.post(
|
| 55 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 56 |
+
json=body,
|
| 57 |
+
headers=headers,
|
| 58 |
+
timeout=60.0
|
| 59 |
+
)
|
| 60 |
+
return Response(content=response.content, status_code=response.status_code)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|