Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,44 @@
|
|
| 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 |
-
#
|
| 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 |
MODEL_MAP = {
|
| 12 |
-
"gpt-4o": "google/gemini-2.0-flash-lite:preview",
|
| 13 |
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
| 14 |
"nexos": "meta-llama/llama-4-8b-scout:free",
|
| 15 |
-
"
|
| 16 |
}
|
| 17 |
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
# Rota Inicial (Resolve o erro "Not Found" do NutriLens)
|
| 21 |
-
@app.get("/", response_class=HTMLResponse)
|
| 22 |
-
async def root():
|
| 23 |
-
return """
|
| 24 |
-
<html>
|
| 25 |
-
<head><title>Fortune API Shield</title></head>
|
| 26 |
-
<body style="background:#000; color:#0f0; font-family:monospace; display:flex; justify-content:center; align-items:center; height:100vh;">
|
| 27 |
-
<div>
|
| 28 |
-
<h1>🛡️ ESCUDO ATIVO: PROTOCOLO SOMBRA</h1>
|
| 29 |
-
<p>> Status: Operacional em Beira</p>
|
| 30 |
-
<p>> Sistemas Ligados: Vitalis | Nexos | NutriLens</p>
|
| 31 |
-
</div>
|
| 32 |
-
</body>
|
| 33 |
-
</html>
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
@app.post("/v1/chat/completions")
|
| 37 |
async def proxy(request: Request):
|
| 38 |
if not KEYS:
|
| 39 |
-
return Response(content='{"error": "
|
| 40 |
|
| 41 |
body = await request.json()
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Roteamento Inteligente
|
| 45 |
-
selected = MODEL_MAP["default"]
|
| 46 |
for proj in ["vitalis", "nexos", "nutrilens"]:
|
| 47 |
-
if proj in
|
| 48 |
-
|
| 49 |
|
| 50 |
-
body["model"] =
|
|
|
|
| 51 |
|
| 52 |
-
#
|
| 53 |
-
current_key = random.choice(KEYS)
|
| 54 |
headers = {
|
| 55 |
-
"Authorization": f"Bearer {
|
| 56 |
-
"X-Title": f"
|
| 57 |
-
"HTTP-Referer": "https://fortune-
|
| 58 |
"Content-Type": "application/json"
|
| 59 |
}
|
| 60 |
|
|
@@ -62,9 +46,7 @@ async def proxy(request: Request):
|
|
| 62 |
try:
|
| 63 |
resp = await client.post(
|
| 64 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 65 |
-
json=body,
|
| 66 |
-
headers=headers,
|
| 67 |
-
timeout=60.0
|
| 68 |
)
|
| 69 |
return Response(content=resp.content, status_code=resp.status_code)
|
| 70 |
except Exception as e:
|
|
|
|
| 1 |
import os, random, httpx, uvicorn
|
| 2 |
from fastapi import FastAPI, Request, Response
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
| 6 |
+
# Carregar as 15 chaves (POOL)
|
| 7 |
KEYS = [os.getenv(f"OR_KEY_{i}") for i in range(1, 16)]
|
| 8 |
KEYS = [k for k in KEYS if k and k.strip()]
|
| 9 |
|
| 10 |
+
# MAPA DE TRADUÇÃO: O que a Aria pede vs O que o OpenRouter recebe
|
| 11 |
MODEL_MAP = {
|
| 12 |
+
"gpt-4o": "google/gemini-2.0-flash-lite:preview", # Tradução padrão
|
| 13 |
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
| 14 |
"nexos": "meta-llama/llama-4-8b-scout:free",
|
| 15 |
+
"nutrilens": "anthropic/claude-3.5-sonnet:beta"
|
| 16 |
}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
@app.post("/v1/chat/completions")
|
| 19 |
async def proxy(request: Request):
|
| 20 |
if not KEYS:
|
| 21 |
+
return Response(content='{"error": "Secrets vazios!"}', status_code=500)
|
| 22 |
|
| 23 |
body = await request.json()
|
| 24 |
+
msg_content = str(body.get("messages", "")).lower()
|
| 25 |
+
|
| 26 |
+
# 1. LÓGICA DE TRADUÇÃO (GHOST MODE)
|
| 27 |
+
# Se a Aria pedir gpt-4o, nós trocamos pelo Gemini real aqui dentro
|
| 28 |
+
selected_model = MODEL_MAP["gpt-4o"]
|
| 29 |
|
|
|
|
|
|
|
| 30 |
for proj in ["vitalis", "nexos", "nutrilens"]:
|
| 31 |
+
if proj in msg_content:
|
| 32 |
+
selected_model = MODEL_MAP[proj]
|
| 33 |
|
| 34 |
+
body["model"] = selected_model
|
| 35 |
+
print(f"🔄 Ghost Translation: Aria pediu gpt-4o -> Enviando {selected_model} para OpenRouter")
|
| 36 |
|
| 37 |
+
# 2. CHAMADA COM CHAVE ALEATÓRIA
|
|
|
|
| 38 |
headers = {
|
| 39 |
+
"Authorization": f"Bearer {random.choice(KEYS)}",
|
| 40 |
+
"X-Title": f"Fortune_System_V3",
|
| 41 |
+
"HTTP-Referer": "https://fortune-moz.io",
|
| 42 |
"Content-Type": "application/json"
|
| 43 |
}
|
| 44 |
|
|
|
|
| 46 |
try:
|
| 47 |
resp = await client.post(
|
| 48 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 49 |
+
json=body, headers=headers, timeout=60.0
|
|
|
|
|
|
|
| 50 |
)
|
| 51 |
return Response(content=resp.content, status_code=resp.status_code)
|
| 52 |
except Exception as e:
|