afatuddin commited on
Commit
d1bedad
·
verified ·
1 Parent(s): 204ae63

Delete hf_app.py

Browse files
Files changed (1) hide show
  1. hf_app.py +0 -232
hf_app.py DELETED
@@ -1,232 +0,0 @@
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
11
-
12
- from langchain_text_splitters import RecursiveCharacterTextSplitter
13
- from langchain_community.vectorstores import FAISS
14
- 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
-
28
- for i, page in enumerate(pdf_reader.pages):
29
- text = page.extract_text()
30
- if text:
31
- # Create a Document with metadata (page number starts at 1)
32
- doc = Document(
33
- page_content=text,
34
- metadata={"page": i + 1, "source": pdf_filename}
35
- )
36
- documents.append(doc)
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
- """
44
- text_splitter = RecursiveCharacterTextSplitter(
45
- chunk_size=2000, # 10k is often too big for precise page tracking; 2k is better
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
- llm = HuggingFaceEndpoint(
71
- huggingfacehub_api_token=hf_token,
72
- repo_id="mistralai/Mistral-7B-Instruct-v0.2",
73
- # repo_id="meta-llama/Llama-3.2-3B-Instruct",
74
- temperature=0.3,
75
- max_new_tokens=512,
76
- task="conversational"
77
- )
78
- return ChatHuggingFace(llm=llm)
79
-
80
- # --- Vector Store Functions (UPDATED) ---
81
- def get_vector_store(text_chunks):
82
- """Creates FAISS vector store from Document objects."""
83
- if not text_chunks:
84
- st.error("No text chunks found.")
85
- return
86
-
87
- embeddings = get_hf_embeddings()
88
- if embeddings is None:
89
- return
90
-
91
- try:
92
- # Use from_documents instead of from_texts to keep metadata
93
- vector_store = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
94
- vector_store.save_local("faiss_index")
95
- st.success("Vector store created and saved locally.")
96
- except Exception as e:
97
- st.error(f"Error creating vector store: {e}")
98
-
99
- def load_faiss_index():
100
- try:
101
- embeddings = get_hf_embeddings()
102
- if embeddings is None:
103
- return None
104
-
105
- db = FAISS.load_local(
106
- "faiss_index",
107
- embeddings,
108
- allow_dangerous_deserialization=True
109
- )
110
- return db
111
- except Exception as e:
112
- # Silent fail on startup if index doesn't exist yet
113
- return None
114
-
115
- # --- Conversational Chain ---
116
- def get_conversational_chain():
117
- model = get_hf_llm()
118
- if model is None:
119
- return None
120
-
121
- system_prompt = """
122
- You are a helpful AI assistant answering questions based on PDF documents.
123
- 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."
124
- Context:
125
- {context}
126
- **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."
127
-
128
- """
129
-
130
- prompt = ChatPromptTemplate.from_messages([
131
- ("system", system_prompt),
132
- # The user's question will be passed here as the "human" message
133
- ("human", "Question: {input}")
134
- ])
135
-
136
- # Create a simple chain using the pipe operator (|)
137
- chain = prompt | model
138
- return chain
139
-
140
- # --- User Input Handler (UPDATED) ---
141
- def user_input(user_question, db):
142
- if db is None:
143
- st.warning("Vector store is not loaded.")
144
- return
145
-
146
- try:
147
- # 1. Get relevant documents
148
- docs = db.similarity_search(user_question, k=4)
149
-
150
- # 2. Get the response
151
- chain = get_conversational_chain()
152
- if chain is None:
153
- st.error("Check API Key")
154
- return
155
-
156
- response = chain.invoke({
157
- "context": docs,
158
- "input": user_question
159
- })
160
-
161
- # 3. Display Response
162
- st.markdown("### Reply:")
163
- # Extract just the content text from the response
164
- if hasattr(response, 'content'):
165
- # Response is an AIMessage object
166
- st.write(response.content)
167
- elif isinstance(response, dict) and "answer" in response:
168
- st.write(response["answer"])
169
- elif isinstance(response, dict) and "content" in response:
170
- st.write(response["content"])
171
- else:
172
- # Try to get string representation
173
- st.write(str(response))
174
-
175
- # # 4. Display Page Numbers (New Feature)
176
- # st.markdown("---")
177
- # st.markdown("#### 📄 Source Pages:")
178
-
179
- # # Use a set to avoid duplicate page numbers
180
- # unique_sources = set()
181
- # for doc in docs:
182
- # page_num = doc.metadata.get("page", "Unknown")
183
- # file_name = doc.metadata.get("source", "Unknown")
184
- # unique_sources.add(f"{file_name} (Page {page_num})")
185
-
186
- # # Display sources cleanly
187
- # for source in unique_sources:
188
- # st.markdown(f"- {source}")
189
-
190
- except Exception as e:
191
- st.error(f"Error: {e}")
192
-
193
- # --- Main App ---
194
- def main():
195
- st.set_page_config(page_title="Chat PDF (Hugging Face)")
196
- st.header("Chat with PDF using mistralai/Mistral-7B-Instruct-v0.2")
197
-
198
- if "faiss_db" not in st.session_state:
199
- st.session_state.faiss_db = None
200
-
201
- # Attempt to load index on launch
202
- if st.session_state.faiss_db is None and os.path.exists("faiss_index"):
203
- st.session_state.faiss_db = load_faiss_index()
204
-
205
- with st.sidebar:
206
- st.title("Menu:")
207
- pdf_docs = st.file_uploader("Upload PDFs", accept_multiple_files=True)
208
-
209
- if st.button("Process PDFs"):
210
- if pdf_docs:
211
- with st.spinner("Processing..."):
212
- # 1. Get Documents (not string)
213
- raw_documents = get_pdf_text(pdf_docs)
214
- # 2. Split Documents (preserves metadata)
215
- text_chunks = get_text_chunks(raw_documents)
216
- # 3. Create Vector Store
217
- get_vector_store(text_chunks)
218
-
219
- st.session_state.faiss_db = load_faiss_index()
220
- else:
221
- st.warning("Upload a PDF first.")
222
-
223
- user_question = st.text_input("Ask a Question")
224
-
225
- if user_question:
226
- if st.session_state.faiss_db:
227
- user_input(user_question, st.session_state.faiss_db)
228
- else:
229
- st.warning("Process PDFs first.")
230
-
231
- if __name__ == "__main__":
232
- main()