Akshay Babbar commited on
Commit
07a9d4b
·
1 Parent(s): 98a937f

Build RAG index at startup from transcripts; drop binary from git.

Browse files

HF Spaces reject binary blobs in git history. Ship text transcripts only;
load_index builds data/index.npz on first use when missing.

Files changed (3) hide show
  1. .gitignore +1 -2
  2. app.py +3 -4
  3. witgym/retriever.py +15 -0
.gitignore CHANGED
@@ -1,6 +1,5 @@
1
- # Local data (generated); ship compact index + transcripts for Spaces
2
  data/*
3
- !data/index.npz
4
  !data/transcripts/
5
  !data/transcripts/**
6
 
 
1
+ # Local index is generated; ship transcripts only (Space builds index on startup)
2
  data/*
 
3
  !data/transcripts/
4
  !data/transcripts/**
5
 
app.py CHANGED
@@ -138,10 +138,9 @@ _warmup_error: str | None = None
138
 
139
 
140
  def _ensure_index():
141
- if not Path(INDEX_PATH).exists():
142
- raise FileNotFoundError(
143
- f"Index not found at {INDEX_PATH}. Run `witgym-index` or bundle data/index.npz in the Space."
144
- )
145
 
146
 
147
  def _get_shared():
 
138
 
139
 
140
  def _ensure_index():
141
+ from witgym.retriever import load_index
142
+
143
+ load_index(INDEX_PATH)
 
144
 
145
 
146
  def _get_shared():
witgym/retriever.py CHANGED
@@ -5,6 +5,7 @@ Returns analogous situations (same violation type), not similar words.
5
  """
6
  import json
7
  import os
 
8
  from typing import List
9
  from loguru import logger
10
  import numpy as np
@@ -27,6 +28,20 @@ def load_index(index_path: str = config.INDEX_PATH) -> dict:
27
  if os.path.exists(npz_fallback):
28
  path = npz_fallback
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  if path.endswith(".npz"):
31
  archive = np.load(path, allow_pickle=True)
32
  scenes = [
 
5
  """
6
  import json
7
  import os
8
+ from pathlib import Path
9
  from typing import List
10
  from loguru import logger
11
  import numpy as np
 
28
  if os.path.exists(npz_fallback):
29
  path = npz_fallback
30
 
31
+ if not os.path.exists(path):
32
+ transcript_dir = Path(config.TRANSCRIPT_DIR)
33
+ if transcript_dir.is_dir() and any(transcript_dir.glob("*.txt")):
34
+ from witgym.indexer import build_index
35
+
36
+ build_path = path if path.endswith(".npz") else config.INDEX_PATH
37
+ logger.info(f"Index missing — building {build_path} from {transcript_dir}")
38
+ build_index(index_path=build_path)
39
+ path = build_path
40
+ else:
41
+ raise FileNotFoundError(
42
+ f"Index not found at {index_path} and no transcripts in {config.TRANSCRIPT_DIR}"
43
+ )
44
+
45
  if path.endswith(".npz"):
46
  archive = np.load(path, allow_pickle=True)
47
  scenes = [