andreska commited on
Commit
baeb731
ยท
1 Parent(s): 85f2838

CODEFIX: Added lazy loading and specific requirements, trying to solve problem with this not running via HuggingFace spaces

Browse files
Files changed (2) hide show
  1. app.py +24 -15
  2. requirements.txt +18 -8
app.py CHANGED
@@ -185,17 +185,26 @@ Question:
185
  ]
186
  )
187
 
188
- # ๐Ÿ”น Load and process dataset once
189
- if "initialized" not in st.session_state:
190
- dataset = load_dataset("andreska/Adrega61Docs", split="train")
191
-
192
- def read_dataset(dataset):
193
- return "\n\n".join(
194
- [
195
- f"Title: {item['title']}\nModule: {item['module']}\nContent: {item['content']}"
196
- for item in dataset
197
- ]
198
- )
 
 
 
 
 
 
 
 
 
199
 
200
  raw_text = read_dataset(dataset)
201
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
@@ -213,8 +222,7 @@ if "initialized" not in st.session_state:
213
 
214
  st.session_state.db = db
215
  st.session_state.docs = docs
216
- st.session_state.initialized = True
217
-
218
 
219
  # ๐Ÿ”น Ollama LLM setup
220
  def get_llm():
@@ -242,9 +250,8 @@ def get_llm():
242
  )
243
 
244
 
245
- # ๐Ÿ”น Build the LLM and retriever
246
  llm = get_llm()
247
- retriever = st.session_state.db.as_retriever(search_kwargs={"k": 5})
248
 
249
  # ๐Ÿ”น UI Styling
250
  st.markdown(
@@ -424,6 +431,8 @@ user_input = st.chat_input("Ask a question about Adrega:")
424
 
425
  # ๐Ÿ”น Process chat
426
  if user_input:
 
 
427
  st.session_state.chat_history.append({"role": "user", "content": user_input})
428
 
429
  # Truncate oldest messages if history exceeds limit
 
185
  ]
186
  )
187
 
188
+ # ๐Ÿ”น Helper โ€” lazy-load RAG resources ONCE on the first user interaction.
189
+
190
+ def _ensure_rag_resources():
191
+ """Load dataset + embeddings model + FAISS index lazily (on demand).
192
+ HF Spaces Free tier has limited RAM, so we defer this heavy work until the first user
193
+ message rather than loading eagerly at container startup (which often OOMs before any user opens the chat page)."""
194
+
195
+ if "rag_initialized" in st.session_state and st.session_state["rag_initialized"]:
196
+ return # resources already built for this session, skip
197
+
198
+ with st.spinner("Loading RAG resources (embeddings model + FAISS index)โ€ฆ this may take ~30s"):
199
+ dataset = load_dataset("andreska/Adrega61Docs", split="train")
200
+
201
+ def read_dataset(dataset):
202
+ return "\n\n".join(
203
+ [
204
+ f"Title: {item['title']}\nModule: {item['module']}\nContent: {item['content']}"
205
+ for item in dataset
206
+ ]
207
+ )
208
 
209
  raw_text = read_dataset(dataset)
210
  text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
 
222
 
223
  st.session_state.db = db
224
  st.session_state.docs = docs
225
+ st.session_state.rag_initialized = True
 
226
 
227
  # ๐Ÿ”น Ollama LLM setup
228
  def get_llm():
 
250
  )
251
 
252
 
253
+ # ๐Ÿ”น Build the LLM (retriever is created lazily inside the chat handler)
254
  llm = get_llm()
 
255
 
256
  # ๐Ÿ”น UI Styling
257
  st.markdown(
 
431
 
432
  # ๐Ÿ”น Process chat
433
  if user_input:
434
+ _ensure_rag_resources()
435
+ retriever = st.session_state.db.as_retriever(search_kwargs={"k": 5})
436
  st.session_state.chat_history.append({"role": "user", "content": user_input})
437
 
438
  # Truncate oldest messages if history exceeds limit
requirements.txt CHANGED
@@ -1,20 +1,30 @@
1
  datasets
2
  openai>=1.54.0,<2.0.0
3
  streamlit
4
- torch
5
  httpx
6
  python-dotenv
7
 
8
- # Use compatible versions that avoid dependency conflicts
9
  langchain
10
  langchain-community
11
- langchain-text-splitters
12
- langchain-core
13
 
14
- # integrations (use versions that support 0.3.x)
15
  langchain-huggingface
16
  langchain-openai
17
 
18
- sentence-transformers>=2.2,<3.0
19
- faiss-cpu
20
- accelerate
 
 
 
 
 
 
 
 
 
 
 
1
  datasets
2
  openai>=1.54.0,<2.0.0
3
  streamlit
4
+ torch-cpu>=2.1 # explicit CPU build so HF Spaces Free tier doesn't try to wire CUDA
5
  httpx
6
  python-dotenv
7
 
8
+ # Core LangChain
9
  langchain
10
  langchain-community
11
+ langchain-core~=0.3
12
+ langchain-text-splitters~=0.3
13
 
14
+ # Integrations (compatible with langchain 0.3.x)
15
  langchain-huggingface
16
  langchain-openai
17
 
18
+ # Embeddings + Vector store โ€” CPU-only variant for HF Spaces (no CUDA)
19
+ sentence-transformers>=2.6,<3.0 # pinned to avoid breaking changes between transformers 4.x versions
20
+ faiss-cpu>=1.7,<2.0 # only load faiss CPU build; HF free tier has no GPU so faiss-gpu would never work
21
+
22
+ # Model inference acceleration โ€” pin for stable installs on free/restricted builds
23
+ accelerate>=0.34,<1.0
24
+
25
+ # HuggingFace auth helpers (used implicitly by datasets + langchain-huggingface)
26
+ # If `HF_API_KEY` env var is set, HF Hub auto-authenticates to the corresponding account.
27
+ huggingface_hub>=0.23,<1.0
28
+
29
+ # Optional image handling for downstream RAG pipelines (PDF/image loaders โ€” not used today but kept for future-proofing)
30
+ Pillow>=9.5