Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,12 @@
|
|
| 1 |
import os, random, httpx, uvicorn
|
| 2 |
from fastapi import FastAPI, Request, Response
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
MODEL_MAP = {
|
| 8 |
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
|
@@ -11,31 +15,58 @@ MODEL_MAP = {
|
|
| 11 |
"default": "google/gemini-2.0-flash-lite:preview"
|
| 12 |
}
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.post("/v1/chat/completions")
|
| 15 |
async def proxy(request: Request):
|
|
|
|
|
|
|
|
|
|
| 16 |
body = await request.json()
|
| 17 |
msg = str(body.get("messages", "")).lower()
|
| 18 |
|
| 19 |
# Roteamento Inteligente
|
| 20 |
selected = MODEL_MAP["default"]
|
| 21 |
for proj in ["vitalis", "nexos", "nutrilens"]:
|
| 22 |
-
if proj in msg:
|
|
|
|
|
|
|
| 23 |
body["model"] = selected
|
| 24 |
|
|
|
|
|
|
|
| 25 |
headers = {
|
| 26 |
-
"Authorization": f"Bearer {
|
| 27 |
-
"X-Title": f"
|
| 28 |
"HTTP-Referer": "https://fortune-dev.io",
|
| 29 |
-
"
|
| 30 |
}
|
| 31 |
|
| 32 |
async with httpx.AsyncClient() as client:
|
| 33 |
try:
|
| 34 |
-
resp = await client.post(
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return Response(content=resp.content, status_code=resp.status_code)
|
| 37 |
except Exception as e:
|
| 38 |
return Response(content=f'{{"error": "{str(e)}"}}', status_code=500)
|
| 39 |
|
| 40 |
if __name__ == "__main__":
|
| 41 |
-
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 |
+
# Puxar as chaves e filtrar as que estão vazias
|
| 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 |
"vitalis": "google/gemini-2.0-pro-exp-02-05:free",
|
|
|
|
| 15 |
"default": "google/gemini-2.0-flash-lite:preview"
|
| 16 |
}
|
| 17 |
|
| 18 |
+
# Rota Inicial (Resolve o erro "Not Found" do NutriLens)
|
| 19 |
+
@app.get("/", response_class=HTMLResponse)
|
| 20 |
+
async def root():
|
| 21 |
+
return """
|
| 22 |
+
<html>
|
| 23 |
+
<head><title>Fortune API Shield</title></head>
|
| 24 |
+
<body style="background:#000; color:#0f0; font-family:monospace; display:flex; justify-content:center; align-items:center; height:100vh;">
|
| 25 |
+
<div>
|
| 26 |
+
<h1>🛡️ ESCUDO ATIVO: PROTOCOLO SOMBRA</h1>
|
| 27 |
+
<p>> Status: Operacional em Beira</p>
|
| 28 |
+
<p>> Sistemas Ligados: Vitalis | Nexos | NutriLens</p>
|
| 29 |
+
</div>
|
| 30 |
+
</body>
|
| 31 |
+
</html>
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
@app.post("/v1/chat/completions")
|
| 35 |
async def proxy(request: Request):
|
| 36 |
+
if not KEYS:
|
| 37 |
+
return Response(content='{"error": "Nenhuma OR_KEY configurada nos Secrets!"}', status_code=500)
|
| 38 |
+
|
| 39 |
body = await request.json()
|
| 40 |
msg = str(body.get("messages", "")).lower()
|
| 41 |
|
| 42 |
# Roteamento Inteligente
|
| 43 |
selected = MODEL_MAP["default"]
|
| 44 |
for proj in ["vitalis", "nexos", "nutrilens"]:
|
| 45 |
+
if proj in msg:
|
| 46 |
+
selected = MODEL_MAP[proj]
|
| 47 |
+
|
| 48 |
body["model"] = selected
|
| 49 |
|
| 50 |
+
# Escolher uma chave válida e montar headers
|
| 51 |
+
current_key = random.choice(KEYS)
|
| 52 |
headers = {
|
| 53 |
+
"Authorization": f"Bearer {current_key}",
|
| 54 |
+
"X-Title": f"Aria_System_{random.randint(100,999)}",
|
| 55 |
"HTTP-Referer": "https://fortune-dev.io",
|
| 56 |
+
"Content-Type": "application/json"
|
| 57 |
}
|
| 58 |
|
| 59 |
async with httpx.AsyncClient() as client:
|
| 60 |
try:
|
| 61 |
+
resp = await client.post(
|
| 62 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 63 |
+
json=body,
|
| 64 |
+
headers=headers,
|
| 65 |
+
timeout=60.0
|
| 66 |
+
)
|
| 67 |
return Response(content=resp.content, status_code=resp.status_code)
|
| 68 |
except Exception as e:
|
| 69 |
return Response(content=f'{{"error": "{str(e)}"}}', status_code=500)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|