Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,84 @@
|
|
| 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 |
-
# Pool de 15
|
|
|
|
| 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
|
| 12 |
MODEL_MAP = {
|
| 13 |
-
"
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"default": "qwen/qwen3-coder:free"
|
| 18 |
}
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@app.post("/v1/chat/completions")
|
| 25 |
async def proxy(request: Request):
|
| 26 |
-
if not KEYS:
|
| 27 |
-
return Response(content='{"error": "
|
| 28 |
-
|
| 29 |
-
body = await request.json()
|
| 30 |
-
msg_content = str(body.get("messages", "")).lower()
|
| 31 |
-
|
| 32 |
-
# Roteador Inteligente de Projectos
|
| 33 |
-
target = MODEL_MAP["gpt-4o"]
|
| 34 |
-
if "vitalis" in msg_content: target = MODEL_MAP["vitalis"]
|
| 35 |
-
elif "nexos" in msg_content: target = MODEL_MAP["nexos"]
|
| 36 |
-
elif "nutrilens" in msg_content: target = MODEL_MAP["nutrilens"]
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 52 |
-
json=body,
|
|
|
|
|
|
|
| 53 |
)
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
if resp.status_code != 200:
|
| 58 |
-
print(f"⚠️ Erro {resp.status_code}, a ativar emergência...")
|
| 59 |
-
body["model"] = "openrouter/free"
|
| 60 |
-
headers["Authorization"] = f"Bearer {random.choice(KEYS)}"
|
| 61 |
-
resp = await client.post(
|
| 62 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 63 |
-
json=body, headers=headers, timeout=60.0
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
return Response(content=resp.content, status_code=resp.status_code)
|
| 67 |
-
except Exception as e:
|
| 68 |
-
return Response(content=f'{{"error": "{str(e)}"}}', status_code=500)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
|
|
|
|
|
|
| 1 |
+
import os, random, httpx, uvicorn, asyncio
|
| 2 |
from fastapi import FastAPI, Request, Response
|
| 3 |
from fastapi.responses import HTMLResponse
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# --- ARSENAL DE CHAVES (Pool de 15 chaves - Operação Sofala) ---
|
| 8 |
+
# Carrega as chaves do OpenRouter configuradas nos Secrets do Hugging Face
|
| 9 |
KEYS = [os.getenv(f"OR_KEY_{i}") for i in range(1, 16)]
|
| 10 |
KEYS = [k for k in KEYS if k and k.strip()]
|
| 11 |
|
| 12 |
+
# --- MAPEAMENTO UNIFICADO (Cérebro Qwen3 Coder como Padrão) ---
|
| 13 |
MODEL_MAP = {
|
| 14 |
+
"aria_coder": "qwen/qwen3-coder:free",
|
| 15 |
+
"aria_brain": "nvidia/nemotron-3-super:free",
|
| 16 |
+
"aria_med": "google/gemini-2.5-pro:free",
|
| 17 |
+
"aria_default": "qwen/qwen3-coder:free"
|
|
|
|
| 18 |
}
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
+
# Painel Visual de Poder Total
|
| 23 |
+
status_color = "#ff00ff" if KEYS else "#ff4444"
|
| 24 |
+
return f"""
|
| 25 |
+
<html>
|
| 26 |
+
<head>
|
| 27 |
+
<title>Aria Shield V2.8</title>
|
| 28 |
+
<meta charset="utf-8">
|
| 29 |
+
</head>
|
| 30 |
+
<body style='background:#050505; color:{status_color}; font-family:monospace; text-align:center; padding-top:100px;'>
|
| 31 |
+
<div style='border: 2px solid {status_color}; display:inline-block; padding:50px; background:#000; box-shadow: 0 0 30px {status_color}55; border-radius:15px;'>
|
| 32 |
+
<h1 style='letter-spacing:10px;'>ARIA SHIELD V2.8</h1>
|
| 33 |
+
<h2 style='color:#fff;'>UNLEASHED POWER EDITION</h2>
|
| 34 |
+
<p style='font-size:1.2em;'>Arsenal: <strong>{len(KEYS)} APIs Simultâneas</strong></p>
|
| 35 |
+
<p style='color:#444;'>Localização: Beira, Sofala, MZ</p>
|
| 36 |
+
<hr style='border:0.5px solid #222; margin: 30px 0;'>
|
| 37 |
+
<p style='font-size:1.8em; font-weight:bold;'>🔥 PODER TOTAL ATIVADO</p>
|
| 38 |
+
<p style='color:#666; font-size:0.8em;'>Buffers: 8K Tokens | Timeout: 300s</p>
|
| 39 |
+
</div>
|
| 40 |
+
</body>
|
| 41 |
+
</html>
|
| 42 |
+
"""
|
| 43 |
|
| 44 |
@app.post("/v1/chat/completions")
|
| 45 |
async def proxy(request: Request):
|
| 46 |
+
if not KEYS:
|
| 47 |
+
return Response(content='{"error": "Sem combustível! Configure as chaves OR_KEY_1 a 15."}', status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
try:
|
| 50 |
+
body = await request.json()
|
| 51 |
+
model_requested = body.get("model", "")
|
| 52 |
+
|
| 53 |
+
# 1. TRADUÇÃO DE MODELO: Limpa prefixos e mapeia para os IDs do OpenRouter
|
| 54 |
+
clean_model = model_requested.replace("fortune/", "")
|
| 55 |
+
body["model"] = MODEL_MAP.get(clean_model, clean_model)
|
| 56 |
+
|
| 57 |
+
# 2. OTIMIZAÇÃO DE FLUXO MASSIVO:
|
| 58 |
+
# Forçamos 8192 tokens para evitar que a Aria trunque o raciocínio clínico ou código.
|
| 59 |
+
body["max_tokens"] = 8192
|
| 60 |
+
|
| 61 |
+
headers = {
|
| 62 |
+
"Authorization": f"Bearer {random.choice(KEYS)}",
|
| 63 |
+
"HTTP-Referer": "https://github.com/Fortnee/Ariaaa",
|
| 64 |
+
"X-Title": "Aria Sovereign System",
|
| 65 |
+
"Content-Type": "application/json"
|
| 66 |
+
}
|
| 67 |
|
| 68 |
+
# 3. CONEXÃO RESILIENTE:
|
| 69 |
+
# Aumentamos o timeout para 5 minutos (300.0) para buscas profundas na Internet.
|
| 70 |
+
async with httpx.AsyncClient() as client:
|
| 71 |
+
response = await client.post(
|
| 72 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 73 |
+
json=body,
|
| 74 |
+
headers=headers,
|
| 75 |
+
timeout=300.0
|
| 76 |
)
|
| 77 |
+
return Response(content=response.content, status_code=response.status_code)
|
| 78 |
|
| 79 |
+
except Exception as e:
|
| 80 |
+
return Response(content=f'{{"error": "Falha na combustão do motor: {str(e)}"}}', status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
+
print("🛰️ Injetando 15 chaves no sistema... Aria Shield V2.8 Online.")
|
| 84 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|