Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ from fastapi.responses import HTMLResponse
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
-
# Pool de 15 Chaves
|
| 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 (
|
| 12 |
MODEL_MAP = {
|
| 13 |
"gpt-4o": "qwen/qwen3-coder:free",
|
| 14 |
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
|
@@ -19,53 +19,72 @@ MODEL_MAP = {
|
|
| 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 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
|
| 48 |
-
|
| 49 |
-
try:
|
| 50 |
resp = await client.post(
|
| 51 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 52 |
-
json=body,
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Modo de Emergência
|
| 56 |
-
# Se a chave ou modelo falhar, ele usa o roteador livre do OpenRouter e troca de chave
|
| 57 |
if resp.status_code != 200:
|
| 58 |
-
print(f"⚠️ Erro {resp.status_code}
|
| 59 |
body["model"] = "openrouter/free"
|
| 60 |
-
|
|
|
|
|
|
|
| 61 |
resp = await client.post(
|
| 62 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 63 |
-
json=body,
|
|
|
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
return Response(content=resp.content, status_code=resp.status_code)
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# 🛡️ Pool de 15 Chaves - Carregamento Seguro
|
| 8 |
KEYS = [os.getenv(f"OR_KEY_{i}") for i in range(1, 16)]
|
| 9 |
+
KEYS = [k.strip() for k in KEYS if k and k.strip()]
|
| 10 |
|
| 11 |
+
# 🧠 MAPEAMENTO DE ELITE (Roteamento por Projeto)
|
| 12 |
MODEL_MAP = {
|
| 13 |
"gpt-4o": "qwen/qwen3-coder:free",
|
| 14 |
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
|
|
|
| 19 |
|
| 20 |
@app.get("/", response_class=HTMLResponse)
|
| 21 |
async def root():
|
| 22 |
+
status_color = "#00ff00" if KEYS else "#ff0000"
|
| 23 |
+
return f"""
|
| 24 |
+
<body style="background: #0e1117; color: white; font-family: sans-serif; text-align: center; padding-top: 50px;">
|
| 25 |
+
<h1 style="color: {status_color};">🛡️ Escudo Fortune Ativo</h1>
|
| 26 |
+
<p><b>{len(KEYS)}</b> chaves operacionais no sistema.</p>
|
| 27 |
+
<p style="color: #888;">Status: Pronto para processar Aria, Vitalis e Nexos.</p>
|
| 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": "Nenhuma chave configurada nos Secrets!"}', status_code=500)
|
| 35 |
|
| 36 |
+
try:
|
| 37 |
+
body = await request.json()
|
| 38 |
+
msg_content = str(body.get("messages", "")).lower()
|
| 39 |
+
|
| 40 |
+
# 🛰️ Roteador Inteligente de Projetos (Beira, Mozambique Hub)
|
| 41 |
+
target = MODEL_MAP["gpt-4o"]
|
| 42 |
+
if "vitalis" in msg_content:
|
| 43 |
+
target = MODEL_MAP["vitalis"]
|
| 44 |
+
elif "nexos" in msg_content:
|
| 45 |
+
target = MODEL_MAP["nexos"]
|
| 46 |
+
elif "nutrilens" in msg_content:
|
| 47 |
+
target = MODEL_MAP["nutrilens"]
|
| 48 |
+
|
| 49 |
+
body["model"] = target
|
| 50 |
+
|
| 51 |
+
# ⚡ Seleção de Chave com Limpeza de Caracteres Invisíveis
|
| 52 |
+
selected_key = random.choice(KEYS).strip()
|
| 53 |
|
| 54 |
+
headers = {
|
| 55 |
+
"Authorization": f"Bearer {selected_key}",
|
| 56 |
+
"X-Title": "Aria_Sovereign_System",
|
| 57 |
+
"HTTP-Referer": "https://fortune-dev-moz.io",
|
| 58 |
+
"Content-Type": "application/json"
|
| 59 |
+
}
|
| 60 |
|
| 61 |
+
async with httpx.AsyncClient() as client:
|
|
|
|
| 62 |
resp = await client.post(
|
| 63 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 64 |
+
json=body,
|
| 65 |
+
headers=headers,
|
| 66 |
+
timeout=60.0
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# 🚨 Modo de Emergência: Troca de chave e modelo em caso de falha
|
|
|
|
| 70 |
if resp.status_code != 200:
|
| 71 |
+
print(f"⚠️ Erro {resp.status_code} na chave atual. Tentando emergência...")
|
| 72 |
body["model"] = "openrouter/free"
|
| 73 |
+
new_key = random.choice(KEYS).strip()
|
| 74 |
+
headers["Authorization"] = f"Bearer {new_key}"
|
| 75 |
+
|
| 76 |
resp = await client.post(
|
| 77 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 78 |
+
json=body,
|
| 79 |
+
headers=headers,
|
| 80 |
+
timeout=60.0
|
| 81 |
)
|
| 82 |
|
| 83 |
return Response(content=resp.content, status_code=resp.status_code)
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
print(f"🔥 Erro Crítico: {str(e)}")
|
| 87 |
+
return Response(content=f'{{"error": "{str(e)}"}}', status_code=500)
|
| 88 |
|
| 89 |
if __name__ == "__main__":
|
| 90 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|