Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,41 @@
|
|
| 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 |
-
# --- ARSENAL DE CHAVES (Pool de 15
|
| 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
|
| 13 |
MODEL_MAP = {
|
| 14 |
-
"
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"
|
|
|
|
| 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;'>
|
| 34 |
-
<p style='font-size:1.2em;'>Arsenal: <strong>{len(KEYS)}
|
| 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;'>🔥
|
| 38 |
-
<p style='color:#666; font-size:0.8em;'>
|
| 39 |
</div>
|
| 40 |
</body>
|
| 41 |
</html>
|
|
@@ -43,42 +43,59 @@ async def root():
|
|
| 43 |
|
| 44 |
@app.post("/v1/chat/completions")
|
| 45 |
async def proxy(request: Request):
|
| 46 |
-
if not KEYS:
|
| 47 |
-
return Response(content='{"error": "
|
| 48 |
-
|
| 49 |
try:
|
| 50 |
body = await request.json()
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
clean_model = model_requested.replace("fortune/", "")
|
| 55 |
-
body["model"] = MODEL_MAP.get(clean_model, clean_model)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
body["max_tokens"] = 8192
|
| 60 |
|
|
|
|
|
|
|
| 61 |
headers = {
|
| 62 |
-
"Authorization": f"Bearer {
|
| 63 |
-
"
|
| 64 |
-
"
|
| 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 |
-
|
|
|
|
| 72 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 73 |
-
json=body,
|
| 74 |
-
headers=headers,
|
| 75 |
-
timeout=300.0
|
| 76 |
)
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
-
return Response(content=f'{{"error": "Falha
|
| 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)
|
|
|
|
| 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 |
+
# --- ARSENAL DE CHAVES (Pool de 15 Chaves - Operação Beira) ---
|
|
|
|
| 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 DE ELITE (Cérebros Pesados - Atualizado) ---
|
| 12 |
MODEL_MAP = {
|
| 13 |
+
"gpt-4o": "qwen/qwen3-coder:free",
|
| 14 |
+
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
| 15 |
+
"nexos": "qwen/qwen3-coder:free",
|
| 16 |
+
"nutrilens": "google/gemini-2.0-flash-lite-preview-02-05:free",
|
| 17 |
+
"default": "qwen/qwen3-coder:free"
|
| 18 |
}
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
+
# Painel Visual de Poder Total - V2.8.5
|
| 23 |
status_color = "#ff00ff" if KEYS else "#ff4444"
|
| 24 |
return f"""
|
| 25 |
<html>
|
| 26 |
<head>
|
| 27 |
+
<title>Aria Shield V2.8.5</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.5</h1>
|
| 33 |
+
<h2 style='color:#fff;'>HYBRID CORE EDITION</h2>
|
| 34 |
+
<p style='font-size:1.2em;'>Arsenal: <strong>{len(KEYS)} Chaves Operacionais</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;'>🔥 ROTEAMENTO INTELIGENTE ATIVADO</p>
|
| 38 |
+
<p style='color:#666; font-size:0.8em;'>Anti-Truncagem: 8K Tokens | Timeout: 300s</p>
|
| 39 |
</div>
|
| 40 |
</body>
|
| 41 |
</html>
|
|
|
|
| 43 |
|
| 44 |
@app.post("/v1/chat/completions")
|
| 45 |
async def proxy(request: Request):
|
| 46 |
+
if not KEYS:
|
| 47 |
+
return Response(content='{"error": "Adiciona as chaves nos Secrets!"}', status_code=500)
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
body = await request.json()
|
| 51 |
+
msg_content = str(body.get("messages", "")).lower()
|
| 52 |
+
|
| 53 |
+
# --- Roteador Inteligente de Projectos do CEO ---
|
| 54 |
+
target = MODEL_MAP["gpt-4o"]
|
| 55 |
+
if "vitalis" in msg_content:
|
| 56 |
+
target = MODEL_MAP["vitalis"]
|
| 57 |
+
elif "nexos" in msg_content:
|
| 58 |
+
target = MODEL_MAP["nexos"]
|
| 59 |
+
elif "nutrilens" in msg_content:
|
| 60 |
+
target = MODEL_MAP["nutrilens"]
|
| 61 |
|
| 62 |
+
body["model"] = target
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# --- OTIMIZAÇÃO PARA OUTPUTS LONGOS (Aria Unleashed) ---
|
| 65 |
+
body["max_tokens"] = 8192 # Força o modelo a entregar o código ou pesquisa completa sem truncar
|
|
|
|
| 66 |
|
| 67 |
+
selected_key = random.choice(KEYS)
|
| 68 |
+
|
| 69 |
headers = {
|
| 70 |
+
"Authorization": f"Bearer {selected_key}",
|
| 71 |
+
"X-Title": "Aria_Sovereign_System",
|
| 72 |
+
"HTTP-Referer": "https://fortune-dev-moz.io",
|
| 73 |
"Content-Type": "application/json"
|
| 74 |
}
|
| 75 |
|
|
|
|
|
|
|
| 76 |
async with httpx.AsyncClient() as client:
|
| 77 |
+
# Timeout de 5 minutos (300s) para não derrubar pesquisas massivas da web
|
| 78 |
+
resp = await client.post(
|
| 79 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 80 |
+
json=body, headers=headers, timeout=300.0
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
+
|
| 83 |
+
# --- Modo de Emergência Absoluta ---
|
| 84 |
+
# Se a chave ou modelo falhar (ex: Rate limit no Gemini Pro), troca de chave e força um modelo livre genérico
|
| 85 |
+
if resp.status_code != 200:
|
| 86 |
+
print(f"⚠️ Erro {resp.status_code} no modelo {target}. A ativar emergência...")
|
| 87 |
+
body["model"] = "openrouter/free"
|
| 88 |
+
headers["Authorization"] = f"Bearer {random.choice(KEYS)}"
|
| 89 |
+
|
| 90 |
+
resp = await client.post(
|
| 91 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 92 |
+
json=body, headers=headers, timeout=300.0
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
return Response(content=resp.content, status_code=resp.status_code)
|
| 96 |
|
| 97 |
except Exception as e:
|
| 98 |
+
return Response(content=f'{{"error": "Falha crítica no núcleo: {str(e)}"}}', status_code=500)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|
|
|
|
| 101 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|