Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -17,6 +17,51 @@ from pydantic import BaseModel
|
|
| 17 |
|
| 18 |
app = FastAPI(title="Fer Private AI · API Proxy", version="6.0.0")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
app.add_middleware(
|
| 21 |
CORSMiddleware,
|
| 22 |
allow_origins=["*"],
|
|
@@ -124,26 +169,27 @@ def fetch_banxico(serie_id: str) -> dict:
|
|
| 124 |
|
| 125 |
# ── Proxy al cerebro principal ────────────────────────────────────────────────
|
| 126 |
def call_brain(query: str, session_id: str = "mobile") -> dict:
|
| 127 |
-
"""Llama al agente
|
| 128 |
try:
|
|
|
|
|
|
|
| 129 |
start = time.time()
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
headers={"Content-Type": "application/json"},
|
| 135 |
)
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
pass
|
| 145 |
|
| 146 |
-
# Fallback: Groq directo con datos Banxico
|
| 147 |
return call_groq_fallback(query)
|
| 148 |
|
| 149 |
def call_groq_fallback(query: str) -> dict:
|
|
|
|
| 17 |
|
| 18 |
app = FastAPI(title="Fer Private AI · API Proxy", version="6.0.0")
|
| 19 |
|
| 20 |
+
# ── Auto-sync módulos desde mx-data-intelligence al arrancar ─────────────────
|
| 21 |
+
def sync_modules_from_brain():
|
| 22 |
+
"""
|
| 23 |
+
Descarga los módulos del agente desde mx-data-intelligence.
|
| 24 |
+
Así mx-data-api siempre tiene el código más reciente sin duplicación manual.
|
| 25 |
+
"""
|
| 26 |
+
try:
|
| 27 |
+
from huggingface_hub import hf_hub_download
|
| 28 |
+
import shutil
|
| 29 |
+
|
| 30 |
+
HF_TOKEN = os.getenv("HF_TOKEN", "")
|
| 31 |
+
BRAIN_REPO = "Fercor95mx/mx-data-intelligence"
|
| 32 |
+
MODULES = [
|
| 33 |
+
"modules/agent_orchestrator.py",
|
| 34 |
+
"modules/agent_tools.py",
|
| 35 |
+
"modules/rag_chroma.py",
|
| 36 |
+
"modules/semantic_cache.py",
|
| 37 |
+
"modules/observability.py",
|
| 38 |
+
"modules/critic_loop.py",
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
os.makedirs("modules", exist_ok=True)
|
| 42 |
+
synced = 0
|
| 43 |
+
for module_path in MODULES:
|
| 44 |
+
try:
|
| 45 |
+
local = hf_hub_download(
|
| 46 |
+
repo_id=BRAIN_REPO,
|
| 47 |
+
filename=module_path,
|
| 48 |
+
repo_type="space",
|
| 49 |
+
token=HF_TOKEN,
|
| 50 |
+
force_download=True,
|
| 51 |
+
)
|
| 52 |
+
shutil.copy(local, module_path)
|
| 53 |
+
synced += 1
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"[sync] ⚠️ {module_path}: {e}")
|
| 56 |
+
|
| 57 |
+
print(f"[sync] ✅ {synced}/{len(MODULES)} módulos sincronizados desde {BRAIN_REPO}")
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"[sync] ❌ Error en sync: {e} — usando módulos locales existentes")
|
| 60 |
+
|
| 61 |
+
# Sincronizar al arrancar en background para no bloquear el inicio
|
| 62 |
+
import threading
|
| 63 |
+
threading.Thread(target=sync_modules_from_brain, daemon=True).start()
|
| 64 |
+
|
| 65 |
app.add_middleware(
|
| 66 |
CORSMiddleware,
|
| 67 |
allow_origins=["*"],
|
|
|
|
| 169 |
|
| 170 |
# ── Proxy al cerebro principal ────────────────────────────────────────────────
|
| 171 |
def call_brain(query: str, session_id: str = "mobile") -> dict:
|
| 172 |
+
"""Llama al agente via modulos locales — mismo codigo que mx-data-intelligence."""
|
| 173 |
try:
|
| 174 |
+
# Intentar importar el agente local (modulos sincronizados)
|
| 175 |
+
from modules.agent_orchestrator import run_agent
|
| 176 |
start = time.time()
|
| 177 |
+
response = run_agent(
|
| 178 |
+
user_question=query,
|
| 179 |
+
history=[],
|
| 180 |
+
stream_placeholder=None
|
|
|
|
| 181 |
)
|
| 182 |
+
return {
|
| 183 |
+
"response": response,
|
| 184 |
+
"modelo": "Agente Real · 7 tools · NVIDIA H100 + Groq LPU",
|
| 185 |
+
"latencia_ms": int((time.time() - start) * 1000),
|
| 186 |
+
}
|
| 187 |
+
except ImportError:
|
| 188 |
+
# Si no hay modulos locales, usar Groq fallback
|
| 189 |
+
pass
|
| 190 |
except Exception as e:
|
| 191 |
pass
|
| 192 |
|
|
|
|
| 193 |
return call_groq_fallback(query)
|
| 194 |
|
| 195 |
def call_groq_fallback(query: str) -> dict:
|