Files changed (2) hide show
  1. Dockerfile +2 -3
  2. app.py → hf_app.py +57 -79
Dockerfile CHANGED
@@ -9,8 +9,7 @@ RUN apt-get update && apt-get install -y \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  COPY requirements.txt ./
12
- COPY app.py ./
13
- COPY .env ./
14
 
15
  RUN pip3 install -r requirements.txt
16
 
@@ -18,4 +17,4 @@ EXPOSE 8501
18
 
19
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
20
 
21
- ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  COPY requirements.txt ./
12
+ COPY src/ ./src/
 
13
 
14
  RUN pip3 install -r requirements.txt
15
 
 
17
 
18
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
19
 
20
+ ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
app.py → hf_app.py RENAMED
@@ -1,10 +1,6 @@
1
  import streamlit as st
2
  from PyPDF2 import PdfReader
3
  import os
4
- from dotenv import load_dotenv
5
-
6
- # Load environment variables from .env file
7
- load_dotenv()
8
 
9
  # --- UPDATED IMPORTS (LANGCHAIN v1.1 COMPATIBLE) ---
10
  from langchain_core.documents import Document
@@ -15,13 +11,13 @@ from langchain_core.prompts import ChatPromptTemplate
15
  from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEndpointEmbeddings, ChatHuggingFace
16
  # ... rest of your code ...
17
  @st.cache_data
18
- def get_pdf_text(_pdf_docs):
19
  """
20
  Extracts text from PDFs and creates Document objects with metadata.
21
  Returns a list of Document objects, not a string.
22
  """
23
  documents = []
24
- for pdf in _pdf_docs:
25
  pdf_reader = PdfReader(pdf)
26
  pdf_filename = pdf.name
27
 
@@ -37,7 +33,7 @@ def get_pdf_text(_pdf_docs):
37
  return documents
38
 
39
  @st.cache_data
40
- def get_text_chunks(_documents):
41
  """
42
  Splits Document objects into chunks while preserving metadata.
43
  """
@@ -46,33 +42,29 @@ def get_text_chunks(_documents):
46
  chunk_overlap=200
47
  )
48
  # split_documents ensures metadata (page num) is copied to new chunks
49
- chunks = text_splitter.split_documents(_documents)
50
  return chunks
51
 
52
  # --- Hugging Face Model Functions ---
53
  @st.cache_resource
54
  def get_hf_embeddings():
55
- hf_token = os.getenv("HF_API_TOKEN") or st.secrets.get("HF_API_TOKEN")
56
- if not hf_token:
57
- st.error("HF_API_TOKEN not found in environment variables or secrets.toml.")
58
  return None
59
  return HuggingFaceEndpointEmbeddings(
60
  model="BAAI/bge-small-en-v1.5",
61
- huggingfacehub_api_token=hf_token
62
  )
63
 
64
  @st.cache_resource
65
  def get_hf_llm():
66
- hf_token = os.getenv("HF_API_TOKEN") or st.secrets.get("HF_API_TOKEN")
67
- if not hf_token:
68
  return None
69
 
70
- # Get model selection from session state
71
- model_choice = st.session_state.get("selected_model", "mistralai/Mistral-7B-Instruct-v0.2")
72
-
73
  llm = HuggingFaceEndpoint(
74
- huggingfacehub_api_token=hf_token,
75
- repo_id=model_choice,
 
76
  temperature=0.3,
77
  max_new_tokens=512,
78
  task="conversational"
@@ -80,9 +72,9 @@ def get_hf_llm():
80
  return ChatHuggingFace(llm=llm)
81
 
82
  # --- Vector Store Functions (UPDATED) ---
83
- def get_vector_store(_text_chunks):
84
- """Creates FAISS vector store from Document objects and stores in session state."""
85
- if not _text_chunks:
86
  st.error("No text chunks found.")
87
  return
88
 
@@ -91,23 +83,25 @@ def get_vector_store(_text_chunks):
91
  return
92
 
93
  try:
94
- # Create vector store in memory
95
- vector_store = FAISS.from_documents(documents=_text_chunks, embedding=embeddings)
96
-
97
- # Store in session state (persists during the session)
98
- st.session_state.vector_store = vector_store
99
-
100
- st.success("Vector store created and loaded successfully.")
101
  except Exception as e:
102
  st.error(f"Error creating vector store: {e}")
103
 
104
  def load_faiss_index():
105
- """Load vector store from session state."""
106
  try:
107
- # Check if vector store exists in session state
108
- if "vector_store" in st.session_state and st.session_state.vector_store is not None:
109
- return st.session_state.vector_store
110
- return None
 
 
 
 
 
 
111
  except Exception as e:
112
  # Silent fail on startup if index doesn't exist yet
113
  return None
@@ -118,22 +112,19 @@ def get_conversational_chain():
118
  if model is None:
119
  return None
120
 
121
- system_prompt = """You are a helpful AI assistant that answers questions based on the provided document content.
122
-
123
- ### Context from Documents:
124
- {context}
125
-
126
- ### Instructions:
127
- 1. Read the context carefully and extract relevant information to answer the user's question.
128
- 2. Base your answer ONLY on the information provided in the context above.
129
- 3. If the information needed to answer the question is not in the context, respond with: "I couldn't find this information in the provided documents. Please upload documents that contain the relevant information."
130
- 4. Be concise and direct in your answers.
131
- 5. If asking for clarification helps, you can ask follow-up questions.
132
- 6. Always cite which page or section your information comes from when possible."""
133
 
134
  prompt = ChatPromptTemplate.from_messages([
135
  ("system", system_prompt),
136
- ("human", "{input}")
 
137
  ])
138
 
139
  # Create a simple chain using the pipe operator (|)
@@ -175,61 +166,48 @@ def user_input(user_question, db):
175
  # Try to get string representation
176
  st.write(str(response))
177
 
178
- # 4. Display Retrieved Documents (Debug Info)
 
 
179
 
 
 
 
 
 
 
 
 
 
 
180
 
181
  except Exception as e:
182
  st.error(f"Error: {e}")
183
- import traceback
184
- st.error(traceback.format_exc())
185
 
186
  # --- Main App ---
187
  def main():
188
  st.set_page_config(page_title="Chat PDF (Hugging Face)")
189
- st.header("Chat with PDF using Hugging Face Models")
190
 
191
- # Initialize session state variables
192
  if "faiss_db" not in st.session_state:
193
  st.session_state.faiss_db = None
194
- if "vector_store" not in st.session_state:
195
- st.session_state.vector_store = None
196
- if "selected_model" not in st.session_state:
197
- st.session_state.selected_model = "mistralai/Mistral-7B-Instruct-v0.2"
198
 
199
  with st.sidebar:
200
  st.title("Menu:")
201
-
202
- # Model Selection Dropdown
203
- model_options = {
204
- "Mistral 7B": "mistralai/Mistral-7B-Instruct-v0.2",
205
- "Llama 3.2 3B": "meta-llama/Llama-3.2-3B-Instruct"
206
- }
207
-
208
- selected_model_name = st.selectbox(
209
- "Choose Model:",
210
- options=list(model_options.keys()),
211
- index=0
212
- )
213
-
214
- st.session_state.selected_model = model_options[selected_model_name]
215
- st.caption(f"Selected: `{st.session_state.selected_model}`")
216
-
217
- st.divider()
218
-
219
  pdf_docs = st.file_uploader("Upload PDFs", accept_multiple_files=True)
220
 
221
  if st.button("Process PDFs"):
222
  if pdf_docs:
223
  with st.spinner("Processing..."):
224
- # Clear previous cache to avoid using old vector stores
225
- st.cache_data.clear()
226
- st.cache_resource.clear()
227
-
228
  # 1. Get Documents (not string)
229
  raw_documents = get_pdf_text(pdf_docs)
230
  # 2. Split Documents (preserves metadata)
231
  text_chunks = get_text_chunks(raw_documents)
232
- # 3. Create Vector Store (stored in session state)
233
  get_vector_store(text_chunks)
234
 
235
  st.session_state.faiss_db = load_faiss_index()
 
1
  import streamlit as st
2
  from PyPDF2 import PdfReader
3
  import os
 
 
 
 
4
 
5
  # --- UPDATED IMPORTS (LANGCHAIN v1.1 COMPATIBLE) ---
6
  from langchain_core.documents import Document
 
11
  from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEndpointEmbeddings, ChatHuggingFace
12
  # ... rest of your code ...
13
  @st.cache_data
14
+ def get_pdf_text(pdf_docs):
15
  """
16
  Extracts text from PDFs and creates Document objects with metadata.
17
  Returns a list of Document objects, not a string.
18
  """
19
  documents = []
20
+ for pdf in pdf_docs:
21
  pdf_reader = PdfReader(pdf)
22
  pdf_filename = pdf.name
23
 
 
33
  return documents
34
 
35
  @st.cache_data
36
+ def get_text_chunks(documents):
37
  """
38
  Splits Document objects into chunks while preserving metadata.
39
  """
 
42
  chunk_overlap=200
43
  )
44
  # split_documents ensures metadata (page num) is copied to new chunks
45
+ chunks = text_splitter.split_documents(documents)
46
  return chunks
47
 
48
  # --- Hugging Face Model Functions ---
49
  @st.cache_resource
50
  def get_hf_embeddings():
51
+ if "HF_API_TOKEN" not in st.secrets:
52
+ st.error("HF_API_TOKEN not found in secrets.toml.")
 
53
  return None
54
  return HuggingFaceEndpointEmbeddings(
55
  model="BAAI/bge-small-en-v1.5",
56
+ huggingfacehub_api_token=st.secrets["HF_API_TOKEN"]
57
  )
58
 
59
  @st.cache_resource
60
  def get_hf_llm():
61
+ if "HF_API_TOKEN" not in st.secrets:
 
62
  return None
63
 
 
 
 
64
  llm = HuggingFaceEndpoint(
65
+ huggingfacehub_api_token=st.secrets["HF_API_TOKEN"],
66
+ repo_id="mistralai/Mistral-7B-Instruct-v0.2",
67
+ # repo_id="meta-llama/Llama-3.2-3B-Instruct",
68
  temperature=0.3,
69
  max_new_tokens=512,
70
  task="conversational"
 
72
  return ChatHuggingFace(llm=llm)
73
 
74
  # --- Vector Store Functions (UPDATED) ---
75
+ def get_vector_store(text_chunks):
76
+ """Creates FAISS vector store from Document objects."""
77
+ if not text_chunks:
78
  st.error("No text chunks found.")
79
  return
80
 
 
83
  return
84
 
85
  try:
86
+ # Use from_documents instead of from_texts to keep metadata
87
+ vector_store = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
88
+ vector_store.save_local("faiss_index")
89
+ st.success("Vector store created and saved locally.")
 
 
 
90
  except Exception as e:
91
  st.error(f"Error creating vector store: {e}")
92
 
93
  def load_faiss_index():
 
94
  try:
95
+ embeddings = get_hf_embeddings()
96
+ if embeddings is None:
97
+ return None
98
+
99
+ db = FAISS.load_local(
100
+ "faiss_index",
101
+ embeddings,
102
+ allow_dangerous_deserialization=True
103
+ )
104
+ return db
105
  except Exception as e:
106
  # Silent fail on startup if index doesn't exist yet
107
  return None
 
112
  if model is None:
113
  return None
114
 
115
+ system_prompt = """
116
+ You are a helpful AI assistant answering questions based on PDF documents.
117
+ If the answer is not in the context, say you don't know and do not give any text about the question, just say "I don't know."
118
+ Context:
119
+ {context}
120
+ **Note:** please do not answer questions that are not related to the context provided. If the context does not contain the answer, respond with "I don't know."
121
+
122
+ """
 
 
 
 
123
 
124
  prompt = ChatPromptTemplate.from_messages([
125
  ("system", system_prompt),
126
+ # The user's question will be passed here as the "human" message
127
+ ("human", "Question: {input}")
128
  ])
129
 
130
  # Create a simple chain using the pipe operator (|)
 
166
  # Try to get string representation
167
  st.write(str(response))
168
 
169
+ # # 4. Display Page Numbers (New Feature)
170
+ # st.markdown("---")
171
+ # st.markdown("#### 📄 Source Pages:")
172
 
173
+ # # Use a set to avoid duplicate page numbers
174
+ # unique_sources = set()
175
+ # for doc in docs:
176
+ # page_num = doc.metadata.get("page", "Unknown")
177
+ # file_name = doc.metadata.get("source", "Unknown")
178
+ # unique_sources.add(f"{file_name} (Page {page_num})")
179
+
180
+ # # Display sources cleanly
181
+ # for source in unique_sources:
182
+ # st.markdown(f"- {source}")
183
 
184
  except Exception as e:
185
  st.error(f"Error: {e}")
 
 
186
 
187
  # --- Main App ---
188
  def main():
189
  st.set_page_config(page_title="Chat PDF (Hugging Face)")
190
+ st.header("Chat with PDF using mistralai/Mistral-7B-Instruct-v0.2")
191
 
 
192
  if "faiss_db" not in st.session_state:
193
  st.session_state.faiss_db = None
194
+
195
+ # Attempt to load index on launch
196
+ if st.session_state.faiss_db is None and os.path.exists("faiss_index"):
197
+ st.session_state.faiss_db = load_faiss_index()
198
 
199
  with st.sidebar:
200
  st.title("Menu:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
  pdf_docs = st.file_uploader("Upload PDFs", accept_multiple_files=True)
202
 
203
  if st.button("Process PDFs"):
204
  if pdf_docs:
205
  with st.spinner("Processing..."):
 
 
 
 
206
  # 1. Get Documents (not string)
207
  raw_documents = get_pdf_text(pdf_docs)
208
  # 2. Split Documents (preserves metadata)
209
  text_chunks = get_text_chunks(raw_documents)
210
+ # 3. Create Vector Store
211
  get_vector_store(text_chunks)
212
 
213
  st.session_state.faiss_db = load_faiss_index()