veltrixcode commited on
Commit
63dbfcd
·
verified ·
1 Parent(s): c49440d

Upload 17 files

Browse files
Dockerfile CHANGED
@@ -21,7 +21,10 @@ COPY web/dist/ ./web/dist/
21
  COPY server.py .
22
 
23
  # Hugging Face Spaces uses user id 1000 and port 7860
24
- RUN useradd -m -u 1000 user
 
 
 
25
  USER user
26
  ENV HOME=/home/user \
27
  PATH=/home/user/.local/bin:$PATH \
 
21
  COPY server.py .
22
 
23
  # Hugging Face Spaces uses user id 1000 and port 7860
24
+ RUN useradd -m -u 1000 user && \
25
+ chown -R user:user /app && \
26
+ chmod -R 777 /app/data
27
+
28
  USER user
29
  ENV HOME=/home/user \
30
  PATH=/home/user/.local/bin:$PATH \
src/__pycache__/retriever.cpython-314.pyc CHANGED
Binary files a/src/__pycache__/retriever.cpython-314.pyc and b/src/__pycache__/retriever.cpython-314.pyc differ
 
src/retriever.py CHANGED
@@ -23,12 +23,27 @@ class HybridLegalRetriever:
23
  self.cf_token = os.getenv("CLOUDFLARE_API_TOKEN")
24
  self.cf_account_id = os.getenv("CLOUDFLARE_ACCOUNT_ID")
25
 
26
- # Initialize ChromaDB client with persistent local vector storage
27
- self.chroma_client = chromadb.PersistentClient(path=os.path.join(persist_dir, "chroma"))
28
- self.collection = self.chroma_client.get_or_create_collection(
29
- name="icai_business_laws",
30
- metadata={"hnsw:space": "cosine"}
31
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  self.bm25_index_path = os.path.join(persist_dir, "bm25.pkl")
34
  self.chunks_cache_path = os.path.join(persist_dir, "chunks_cache.json")
@@ -138,24 +153,25 @@ class HybridLegalRetriever:
138
 
139
  # 1. Dense Semantic Search via Cloudflare embedding (with BM25 fallback on quota limit)
140
  vector_scores: Dict[str, float] = {}
141
- try:
142
- query_embedding = self._get_cloudflare_embeddings([query])[0]
143
- where_filter = {"chapter": chapter_filter} if chapter_filter and chapter_filter != "All" else None
144
-
145
- vector_res = self.collection.query(
146
- query_embeddings=[query_embedding],
147
- n_results=min(top_k * 3, len(self.indexed_chunks)),
148
- where=where_filter
149
- )
150
-
151
- if vector_res and vector_res["ids"] and vector_res["ids"][0]:
152
- for doc_id, dist in zip(vector_res["ids"][0], vector_res["distances"][0]):
153
- similarity = 1.0 - dist
154
- vector_scores[doc_id] = max(0.0, similarity)
155
- except Exception as e:
156
- # Fallback to BM25 keyword matching if Cloudflare neurons quota is exhausted
157
- vector_weight = 0.0
158
- bm25_weight = 1.0
 
159
 
160
  # 2. Sparse BM25 Search
161
  bm25_scores: Dict[str, float] = {}
 
23
  self.cf_token = os.getenv("CLOUDFLARE_API_TOKEN")
24
  self.cf_account_id = os.getenv("CLOUDFLARE_ACCOUNT_ID")
25
 
26
+ # Initialize ChromaDB client with persistent local vector storage (with graceful fallback for read-only environments)
27
+ self.chroma_client = None
28
+ self.collection = None
29
+ chroma_path = os.path.join(persist_dir, "chroma")
30
+ try:
31
+ self.chroma_client = chromadb.PersistentClient(path=chroma_path)
32
+ self.collection = self.chroma_client.get_or_create_collection(
33
+ name="icai_business_laws",
34
+ metadata={"hnsw:space": "cosine"}
35
+ )
36
+ except Exception as e:
37
+ print(f"Notice: Persistent ChromaDB init encountered {e}. Using in-memory client for container runtime.")
38
+ try:
39
+ self.chroma_client = chromadb.Client()
40
+ self.collection = self.chroma_client.get_or_create_collection(
41
+ name="icai_business_laws",
42
+ metadata={"hnsw:space": "cosine"}
43
+ )
44
+ except Exception as inner_e:
45
+ print(f"Notice: Vector collection unavailable ({inner_e}). Operating in high-speed BM25 retrieval mode.")
46
+ self.collection = None
47
 
48
  self.bm25_index_path = os.path.join(persist_dir, "bm25.pkl")
49
  self.chunks_cache_path = os.path.join(persist_dir, "chunks_cache.json")
 
153
 
154
  # 1. Dense Semantic Search via Cloudflare embedding (with BM25 fallback on quota limit)
155
  vector_scores: Dict[str, float] = {}
156
+ if self.collection is not None:
157
+ try:
158
+ query_embedding = self._get_cloudflare_embeddings([query])[0]
159
+ where_filter = {"chapter": chapter_filter} if chapter_filter and chapter_filter != "All" else None
160
+
161
+ vector_res = self.collection.query(
162
+ query_embeddings=[query_embedding],
163
+ n_results=min(top_k * 3, len(self.indexed_chunks)),
164
+ where=where_filter
165
+ )
166
+
167
+ if vector_res and vector_res["ids"] and vector_res["ids"][0]:
168
+ for doc_id, dist in zip(vector_res["ids"][0], vector_res["distances"][0]):
169
+ similarity = 1.0 - dist
170
+ vector_scores[doc_id] = max(0.0, similarity)
171
+ except Exception as e:
172
+ # Fallback to BM25 keyword matching if Cloudflare neurons quota is exhausted
173
+ vector_weight = 0.0
174
+ bm25_weight = 1.0
175
 
176
  # 2. Sparse BM25 Search
177
  bm25_scores: Dict[str, float] = {}