FabIndy commited on
Commit
e891d20
·
1 Parent(s): b355aa3

Fix FAISS index download from HF dataset at startup

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -11,30 +11,47 @@ import gradio as gr
11
  from huggingface_hub import hf_hub_download
12
  import os, shutil
13
 
 
14
  def ensure_faiss_index_present():
 
15
  base_dir = "db/faiss_code_edu_by_article"
16
  os.makedirs(base_dir, exist_ok=True)
17
 
18
  faiss_path = os.path.join(base_dir, "index.faiss")
19
  pkl_path = os.path.join(base_dir, "index.pkl")
20
 
 
21
  if os.path.exists(faiss_path) and os.path.exists(pkl_path):
22
  return
23
 
24
- repo_id = os.environ.get("INDEX_REPO_ID")
25
- if not repo_id:
26
- raise RuntimeError(
27
- "INDEX_REPO_ID non défini (repo HF contenant l’index FAISS)."
28
- )
29
 
30
- token = os.environ.get("HF_TOKEN")
 
 
 
31
 
32
- f_faiss = hf_hub_download(repo_id=repo_id, filename="index.faiss", token=token)
33
- f_pkl = hf_hub_download(repo_id=repo_id, filename="index.pkl", token=token)
 
 
 
 
 
 
 
 
 
 
 
34
 
 
35
  shutil.copyfile(f_faiss, faiss_path)
36
  shutil.copyfile(f_pkl, pkl_path)
37
 
 
 
38
  ensure_faiss_index_present()
39
 
40
 
 
11
  from huggingface_hub import hf_hub_download
12
  import os, shutil
13
 
14
+
15
  def ensure_faiss_index_present():
16
+ # Dossier attendu par le projet (FAISS local)
17
  base_dir = "db/faiss_code_edu_by_article"
18
  os.makedirs(base_dir, exist_ok=True)
19
 
20
  faiss_path = os.path.join(base_dir, "index.faiss")
21
  pkl_path = os.path.join(base_dir, "index.pkl")
22
 
23
+ # Déjà présent -> on ne retélécharge pas
24
  if os.path.exists(faiss_path) and os.path.exists(pkl_path):
25
  return
26
 
27
+ # Repo HF qui contient l'index (DATASET, pas model, pas space)
28
+ repo_id = os.getenv("FAISS_REPO_ID", "FabIndy/code-education-faiss-index")
 
 
 
29
 
30
+ # Token optionnel (uniquement si repo privé/gated)
31
+ token = os.getenv("HF_TOKEN")
32
+ if not token or not token.startswith("hf_"):
33
+ token = None
34
 
35
+ # Téléchargement depuis un dataset repo
36
+ f_faiss = hf_hub_download(
37
+ repo_id=repo_id,
38
+ repo_type="dataset",
39
+ filename="index.faiss",
40
+ token=token,
41
+ )
42
+ f_pkl = hf_hub_download(
43
+ repo_id=repo_id,
44
+ repo_type="dataset",
45
+ filename="index.pkl",
46
+ token=token,
47
+ )
48
 
49
+ # Copie vers le chemin local attendu
50
  shutil.copyfile(f_faiss, faiss_path)
51
  shutil.copyfile(f_pkl, pkl_path)
52
 
53
+
54
+
55
  ensure_faiss_index_present()
56
 
57