Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,13 @@ 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 UNIFICADO (
|
|
|
|
| 12 |
MODEL_MAP = {
|
| 13 |
"aria_med": "google/gemini-2.5-pro:free",
|
| 14 |
"aria_logic": "qwen/qwen3-coder-480b:free",
|
|
@@ -19,42 +21,62 @@ MODEL_MAP = {
|
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
return f"""
|
| 24 |
-
<body style='background:#0a0a0a; color:
|
| 25 |
-
<
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
</body>
|
| 29 |
"""
|
| 30 |
|
| 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 |
-
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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
timeout=60.0
|
| 59 |
-
)
|
| 60 |
-
return Response(content=response.content, status_code=response.status_code)
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# --- ARSENAL DE CHAVES (Pool de 15 chaves para Beira, MZ) ---
|
| 8 |
+
# Recupera as chaves 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 (Dicionário de Tradução) ---
|
| 13 |
+
# Aqui traduzimos os teus comandos para os IDs reais do OpenRouter
|
| 14 |
MODEL_MAP = {
|
| 15 |
"aria_med": "google/gemini-2.5-pro:free",
|
| 16 |
"aria_logic": "qwen/qwen3-coder-480b:free",
|
|
|
|
| 21 |
|
| 22 |
@app.get("/", response_class=HTMLResponse)
|
| 23 |
async def root():
|
| 24 |
+
# Painel de controlo visual para o CEO
|
| 25 |
+
status_color = "#00ff88" if KEYS else "#ff4444"
|
| 26 |
+
status_text = "🟢 SISTEMA OPERACIONAL" if KEYS else "🔴 FALHA NAS CHAVES"
|
| 27 |
+
|
| 28 |
return f"""
|
| 29 |
+
<body style='background:#0a0a0a; color:{status_color}; font-family:sans-serif; text-align:center; padding-top:50px;'>
|
| 30 |
+
<div style='border: 2px solid {status_color}; display:inline-block; padding:30px; border-radius:15px;'>
|
| 31 |
+
<h1>ARIA SOVEREIGN SHIELD V2.5</h1>
|
| 32 |
+
<p style='font-size:1.5em;'>Chaves Ativas: <strong>{len(KEYS)}</strong></p>
|
| 33 |
+
<p style='color:#777;'>Localização: Beira, Sofala, Moçambique</p>
|
| 34 |
+
<hr style='border:0.5px solid #333;'>
|
| 35 |
+
<p style='font-weight:bold;'>Status: {status_text}</p>
|
| 36 |
+
</div>
|
| 37 |
</body>
|
| 38 |
"""
|
| 39 |
|
| 40 |
@app.post("/v1/chat/completions")
|
| 41 |
async def proxy(request: Request):
|
| 42 |
if not KEYS:
|
| 43 |
+
return Response(content='{{"error": "CEO, as 15 chaves não foram detetadas!"}}', status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
try:
|
| 46 |
+
body = await request.json()
|
| 47 |
+
model_requested = body.get("model", "")
|
| 48 |
+
|
| 49 |
+
# 1. LIMPEZA: Remove o prefixo "fortune/" se ele existir
|
| 50 |
+
clean_model = model_requested.replace("fortune/", "")
|
| 51 |
+
|
| 52 |
+
# 2. TRADUÇÃO: Busca o ID real no mapa ou usa o nome limpo
|
| 53 |
+
target_model = MODEL_MAP.get(clean_model, clean_model)
|
| 54 |
+
body["model"] = target_model
|
| 55 |
+
|
| 56 |
+
# 3. ROTAÇÃO: Escolhe uma das 15 chaves aleatoriamente para evitar limites
|
| 57 |
+
selected_key = random.choice(KEYS)
|
| 58 |
+
|
| 59 |
+
headers = {
|
| 60 |
+
"Authorization": f"Bearer {selected_key}",
|
| 61 |
+
"HTTP-Referer": "https://github.com/Fortnee/Ariaaa", # Identidade no GitHub
|
| 62 |
+
"Content-Type": "application/json"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
# 4. DISPARO: Envia o pedido para o OpenRouter
|
| 66 |
+
async with httpx.AsyncClient() as client:
|
| 67 |
+
response = await client.post(
|
| 68 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 69 |
+
json=body,
|
| 70 |
+
headers=headers,
|
| 71 |
+
timeout=90.0 # Timeout estendido para modelos grandes como Qwen 480B
|
| 72 |
+
)
|
| 73 |
+
return Response(content=response.content, status_code=response.status_code)
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return Response(content=f'{{"error": "Falha na Ponte: {str(e)}"}}', status_code=500)
|
| 77 |
|
| 78 |
+
# --- IGNIÇÃO DO SERVIDOR (A CORREÇÃO PARA O EXIT CODE 0) ---
|
| 79 |
+
# Este bloco garante que o servidor uvicorn fique ativo na porta 7860
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
print("🚀 Iniciando Escudo Soberano da Aria...")
|
| 82 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|