Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -3,10 +3,9 @@
|
|
| 3 |
FastAPI + Gradio : service d’indexation asynchrone avec FAISS.
|
| 4 |
Ce fichier a été corrigé pour :
|
| 5 |
|
| 6 |
-
* importer correctement `JobState`
|
| 7 |
-
* garantir que
|
| 8 |
-
*
|
| 9 |
-
* garder la même logique que la version originale.
|
| 10 |
"""
|
| 11 |
|
| 12 |
from __future__ import annotations
|
|
@@ -18,6 +17,7 @@ import time
|
|
| 18 |
import hashlib
|
| 19 |
import logging
|
| 20 |
import tarfile
|
|
|
|
| 21 |
from pathlib import Path
|
| 22 |
from typing import List, Dict, Any, Tuple, Optional
|
| 23 |
|
|
@@ -32,6 +32,16 @@ from pydantic import BaseModel
|
|
| 32 |
|
| 33 |
import gradio as gr
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# --------------------------------------------------------------------------- #
|
| 36 |
# LOGGING
|
| 37 |
# --------------------------------------------------------------------------- #
|
|
@@ -100,11 +110,11 @@ def _setup_cache_dirs() -> Dict[str, str]:
|
|
| 100 |
CACHE_PATHS = _setup_cache_dirs()
|
| 101 |
|
| 102 |
# --------------------------------------------------------------------------- #
|
| 103 |
-
# IMPORT DE LA CLASSE DE STATE (
|
| 104 |
# --------------------------------------------------------------------------- #
|
| 105 |
-
#
|
| 106 |
-
#
|
| 107 |
-
from
|
| 108 |
|
| 109 |
# --------------------------------------------------------------------------- #
|
| 110 |
# GLOBALS
|
|
@@ -255,9 +265,7 @@ def _save_faiss(fx_dir: str, xb: np.ndarray, meta: Dict[str, Any]) -> None:
|
|
| 255 |
os.makedirs(fx_dir, exist_ok=True)
|
| 256 |
idx_path = os.path.join(fx_dir, "emb.faiss")
|
| 257 |
|
| 258 |
-
#
|
| 259 |
-
# Index quantisé (IVF‑PQ) – optimisation mémoire / disque
|
| 260 |
-
# ------------------------------------------------------------------- #
|
| 261 |
quantizer = faiss.IndexFlatIP(xb.shape[1]) # inner‑product (cosine si normalisé)
|
| 262 |
index = faiss.IndexIVFPQ(quantizer, xb.shape[1], 100, 8, 8) # nlist=100, m=8, nbits=8
|
| 263 |
|
|
@@ -338,7 +346,7 @@ def _do_index_job(
|
|
| 338 |
else:
|
| 339 |
xb = _emb_hf(texts)
|
| 340 |
|
| 341 |
-
# ------------------- 3️⃣ Réduction
|
| 342 |
if xb.shape[1] != EMB_DIM:
|
| 343 |
from sklearn.decomposition import PCA
|
| 344 |
pca = PCA(n_components=EMB_DIM, random_state=0)
|
|
|
|
| 3 |
FastAPI + Gradio : service d’indexation asynchrone avec FAISS.
|
| 4 |
Ce fichier a été corrigé pour :
|
| 5 |
|
| 6 |
+
* importer correctement `JobState` (import relatif)
|
| 7 |
+
* garantir que le répertoire `app` est dans le PYTHONPATH lorsqu’on lance le script
|
| 8 |
+
* conserver toutes les fonctionnalités précédentes (indexation, recherche, UI)
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
|
|
|
| 17 |
import hashlib
|
| 18 |
import logging
|
| 19 |
import tarfile
|
| 20 |
+
import sys
|
| 21 |
from pathlib import Path
|
| 22 |
from typing import List, Dict, Any, Tuple, Optional
|
| 23 |
|
|
|
|
| 32 |
|
| 33 |
import gradio as gr
|
| 34 |
|
| 35 |
+
# --------------------------------------------------------------------------- #
|
| 36 |
+
# RÉGLAGE DU PYTHONPATH (pour que les imports relatifs fonctionnent)
|
| 37 |
+
# --------------------------------------------------------------------------- #
|
| 38 |
+
# Si le script est lancé depuis le répertoire `app/`, le package `app` n’est pas
|
| 39 |
+
# découvert automatiquement. On ajoute le répertoire parent au sys.path.
|
| 40 |
+
CURRENT_DIR = Path(__file__).resolve().parent
|
| 41 |
+
PROJECT_ROOT = CURRENT_DIR.parent
|
| 42 |
+
if str(PROJECT_ROOT) not in sys.path:
|
| 43 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 44 |
+
|
| 45 |
# --------------------------------------------------------------------------- #
|
| 46 |
# LOGGING
|
| 47 |
# --------------------------------------------------------------------------- #
|
|
|
|
| 110 |
CACHE_PATHS = _setup_cache_dirs()
|
| 111 |
|
| 112 |
# --------------------------------------------------------------------------- #
|
| 113 |
+
# IMPORT DE LA CLASSE DE STATE (corrigé : import relatif)
|
| 114 |
# --------------------------------------------------------------------------- #
|
| 115 |
+
# Le fichier `index_state.py` se trouve dans `app/core/`.
|
| 116 |
+
# En étant dans le répertoire `app`, on peut l’importer via le package `core`.
|
| 117 |
+
from core.index_state import JobState # <-- IMPORT CORRIGÉ
|
| 118 |
|
| 119 |
# --------------------------------------------------------------------------- #
|
| 120 |
# GLOBALS
|
|
|
|
| 265 |
os.makedirs(fx_dir, exist_ok=True)
|
| 266 |
idx_path = os.path.join(fx_dir, "emb.faiss")
|
| 267 |
|
| 268 |
+
# ------------------- INDEX QUANTISÉ (IVF‑PQ) ------------------- #
|
|
|
|
|
|
|
| 269 |
quantizer = faiss.IndexFlatIP(xb.shape[1]) # inner‑product (cosine si normalisé)
|
| 270 |
index = faiss.IndexIVFPQ(quantizer, xb.shape[1], 100, 8, 8) # nlist=100, m=8, nbits=8
|
| 271 |
|
|
|
|
| 346 |
else:
|
| 347 |
xb = _emb_hf(texts)
|
| 348 |
|
| 349 |
+
# ------------------- 3️⃣ Réduction PCA (si besoin) -------------------
|
| 350 |
if xb.shape[1] != EMB_DIM:
|
| 351 |
from sklearn.decomposition import PCA
|
| 352 |
pca = PCA(n_components=EMB_DIM, random_state=0)
|