Upload rag_api.py with huggingface_hub
Browse files- rag_api.py +40 -10
rag_api.py
CHANGED
|
@@ -36,6 +36,9 @@ TEMP_CACHE_DIR = "/tmp/huggingface_cache"
|
|
| 36 |
os.environ.setdefault("TRANSFORMERS_CACHE", TEMP_CACHE_DIR)
|
| 37 |
os.environ.setdefault("HF_HOME", TEMP_CACHE_DIR)
|
| 38 |
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", TEMP_CACHE_DIR)
|
|
|
|
|
|
|
|
|
|
| 39 |
os.makedirs(TEMP_CACHE_DIR, exist_ok=True)
|
| 40 |
|
| 41 |
# --------------------------------------------------------
|
|
@@ -149,20 +152,47 @@ def ensure_index() -> str:
|
|
| 149 |
"""Descarga index.faiss e index.pkl desde el repo del Space a /tmp.
|
| 150 |
|
| 151 |
Los Spaces Docker no materializan los archivos del repo en disco (en especial
|
| 152 |
-
si están en Xet)
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
| 154 |
"""
|
| 155 |
-
os.
|
| 156 |
-
|
| 157 |
|
| 158 |
for fname in ["index.faiss", "index.pkl"]:
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
)
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
def load_rag():
|
|
|
|
| 36 |
os.environ.setdefault("TRANSFORMERS_CACHE", TEMP_CACHE_DIR)
|
| 37 |
os.environ.setdefault("HF_HOME", TEMP_CACHE_DIR)
|
| 38 |
os.environ.setdefault("SENTENCE_TRANSFORMERS_HOME", TEMP_CACHE_DIR)
|
| 39 |
+
# Evita la ruta "Xet" del Hub, que a veces responde sin la cabecera
|
| 40 |
+
# X-Repo-Commit y hace fallar la descarga del índice.
|
| 41 |
+
os.environ.setdefault("HF_HUB_DISABLE_XET", "1")
|
| 42 |
os.makedirs(TEMP_CACHE_DIR, exist_ok=True)
|
| 43 |
|
| 44 |
# --------------------------------------------------------
|
|
|
|
| 152 |
"""Descarga index.faiss e index.pkl desde el repo del Space a /tmp.
|
| 153 |
|
| 154 |
Los Spaces Docker no materializan los archivos del repo en disco (en especial
|
| 155 |
+
si están en Xet), por eso se descargan al arrancar.
|
| 156 |
+
|
| 157 |
+
Se intenta primero con `hf_hub_download` y, si el Hub responde sin la
|
| 158 |
+
cabecera `X-Repo-Commit` (típico cuando la petición pasa por un proxy),
|
| 159 |
+
se cae a una descarga HTTP directa, que no depende de esa cabecera.
|
| 160 |
"""
|
| 161 |
+
target_dir = os.path.join(INDEX_LOCAL_DIR, "db_faiss")
|
| 162 |
+
os.makedirs(target_dir, exist_ok=True)
|
| 163 |
|
| 164 |
for fname in ["index.faiss", "index.pkl"]:
|
| 165 |
+
dest = os.path.join(target_dir, fname)
|
| 166 |
+
|
| 167 |
+
# 1) Vía normal: huggingface_hub (usa HF_TOKEN si está disponible)
|
| 168 |
+
try:
|
| 169 |
+
from huggingface_hub import hf_hub_download
|
| 170 |
+
|
| 171 |
+
hf_hub_download(
|
| 172 |
+
repo_id=SPACE_REPO_ID,
|
| 173 |
+
filename=f"db_faiss/{fname}",
|
| 174 |
+
repo_type="space",
|
| 175 |
+
local_dir=INDEX_LOCAL_DIR,
|
| 176 |
+
token=os.environ.get("HF_TOKEN") or None,
|
| 177 |
+
)
|
| 178 |
+
print(f"✓ {fname} descargado con hf_hub_download")
|
| 179 |
+
continue
|
| 180 |
+
except Exception as e: # noqa: BLE001
|
| 181 |
+
print(f"⚠ hf_hub_download falló para {fname}: {type(e).__name__}: {e}")
|
| 182 |
+
|
| 183 |
+
# 2) Fallback: descarga HTTP directa (no usa la cabecera X-Repo-Commit)
|
| 184 |
+
import urllib.request
|
| 185 |
+
|
| 186 |
+
url = (
|
| 187 |
+
f"https://huggingface.co/spaces/{SPACE_REPO_ID}"
|
| 188 |
+
f"/resolve/main/db_faiss/{fname}"
|
| 189 |
)
|
| 190 |
+
print(f"↓ Descargando por HTTP directo: {url}")
|
| 191 |
+
with urllib.request.urlopen(url, timeout=60) as r, open(dest, "wb") as f:
|
| 192 |
+
f.write(r.read())
|
| 193 |
+
print(f"✓ {fname} descargado por HTTP directo")
|
| 194 |
+
|
| 195 |
+
return target_dir
|
| 196 |
|
| 197 |
|
| 198 |
def load_rag():
|