afatuddin commited on
Commit
2072c53
·
verified ·
1 Parent(s): 7933427
Files changed (5) hide show
  1. .gitignore +218 -0
  2. README.md +10 -15
  3. app.py +196 -0
  4. hf_app.py +224 -0
  5. requirements.txt +104 -3
.gitignore ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+
204
+ # Ruff stuff:
205
+ .ruff_cache/
206
+
207
+ # PyPI configuration file
208
+ .pypirc
209
+
210
+ # Marimo
211
+ marimo/_static/
212
+ marimo/_lsp/
213
+ __marimo__/
214
+
215
+ # Streamlit
216
+ .streamlit/secrets.toml
217
+
218
+ .faiss_index
README.md CHANGED
@@ -1,19 +1,14 @@
1
  ---
2
- title: ChatWithPDF
3
- emoji: 🚀
4
- colorFrom: red
5
- colorTo: red
6
- sdk: docker
7
- app_port: 8501
8
- tags:
9
- - streamlit
10
  pinned: false
11
- short_description: Streamlit template space
12
  ---
13
 
14
- # Welcome to Streamlit!
15
-
16
- Edit `/src/streamlit_app.py` to customize this app to your heart's desire. :heart:
17
-
18
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
19
- forums](https://discuss.streamlit.io).
 
1
  ---
2
+ title: Chat With PDF
3
+ emoji: 🏢
4
+ colorFrom: indigo
5
+ colorTo: gray
6
+ sdk: streamlit
7
+ sdk_version: 1.31.0
8
+ app_file: app.py
 
9
  pinned: false
10
+ license: mit
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+ # ChatWithPDF
 
 
 
 
app.py ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+
4
+ # Import Document schema to hold text + metadata (page number)
5
+ from langchain.schema import Document
6
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
7
+ from langchain_community.vectorstores import FAISS
8
+ from langchain_community.chat_models import ChatOllama
9
+ from langchain_community.embeddings import OllamaEmbeddings
10
+
11
+ from langchain.chains.question_answering import load_qa_chain
12
+ from langchain_core.prompts import PromptTemplate
13
+
14
+
15
+ @st.cache_data
16
+ def get_pdf_text(pdf_docs):
17
+ """
18
+ Extracts text from PDFs and converts them into Document objects
19
+ containing the page number and filename in metadata.
20
+ """
21
+ documents = []
22
+ for pdf in pdf_docs:
23
+ pdf_reader = PdfReader(pdf)
24
+ pdf_filename = pdf.name
25
+
26
+ for i, page in enumerate(pdf_reader.pages):
27
+ text = page.extract_text()
28
+ if text:
29
+ # Create a Document object for every page
30
+ # 'i + 1' ensures pages start at 1, not 0
31
+ doc = Document(
32
+ page_content=text,
33
+ metadata={"page": i + 1, "source": pdf_filename}
34
+ )
35
+ documents.append(doc)
36
+ return documents
37
+
38
+
39
+ @st.cache_data
40
+ def get_text_chunks(documents):
41
+ """
42
+ Splits Document objects into manageable chunks while preserving metadata.
43
+ """
44
+ text_splitter = RecursiveCharacterTextSplitter(
45
+ chunk_size=2000, # Reduced size for better page granularity
46
+ chunk_overlap=200
47
+ )
48
+ # split_documents (instead of split_text) keeps the metadata attached to chunks
49
+ chunks = text_splitter.split_documents(documents)
50
+ return chunks
51
+
52
+
53
+ def get_vector_store(text_chunks):
54
+ """
55
+ Creates and saves a local FAISS vector store from Document chunks.
56
+ """
57
+ if not text_chunks:
58
+ st.error("No text chunks found. Cannot create vector store.")
59
+ return
60
+
61
+ # Use the local Ollama embedding model
62
+ embeddings = OllamaEmbeddings(model="nomic-embed-text")
63
+
64
+ try:
65
+ # from_documents handles the Document objects including metadata
66
+ vector_store = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
67
+ vector_store.save_local("faiss_index")
68
+ st.success("Vector store created and saved locally.")
69
+ except Exception as e:
70
+ st.error(f"Error creating vector store: {e}")
71
+ print(f"Error creating vector store: {e}")
72
+
73
+
74
+ def get_conversational_chain():
75
+ """
76
+ Creates a question-answering chain with a local Ollama model.
77
+ """
78
+ prompt_template = """
79
+ You are a helpful and informative AI assistant. Your primary goal is to answer questions based on the provided context, which consists of user-uploaded PDF documents.
80
+ Instructions:
81
+ 1. First, thoroughly analyze the content of the user-uploaded PDFs to extract relevant information for answering the question.
82
+ 2. If the answer can be fully derived from the PDF content, provide it directly and ensure accuracy and completeness.
83
+ 3. If you are uncertain about the answer or if the PDFs and your knowledge do not provide enough information to form a conclusive answer, state clearly that the information is not sufficient to provide a definitive answer.
84
+ 4. Avoid fabricating information or making assumptions that cannot be supported by the PDF content or reliable external knowledge.
85
+
86
+ **Important:** If the user prompt is not related to the PDF content,Just say "I don't know this answer".
87
+ **Important:** If the user prompt is not related to the PDF content,Never say the answer if you know then still dont say.
88
+
89
+ Context:\n{context}
90
+ Question: \n{question}
91
+ Answer:
92
+ """
93
+
94
+
95
+ model = ChatOllama(model="gemma3:1b", temperature=0.3)
96
+
97
+ prompt = PromptTemplate(
98
+ template=prompt_template,
99
+ input_variables=["context", "question"]
100
+ )
101
+ chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
102
+ return chain
103
+
104
+
105
+ def load_faiss_index():
106
+ """Loads the local FAISS index."""
107
+ try:
108
+ embeddings = OllamaEmbeddings(model="nomic-embed-text")
109
+ db = FAISS.load_local(
110
+ "faiss_index",
111
+ embeddings,
112
+ allow_dangerous_deserialization=True
113
+ )
114
+ return db
115
+ except Exception as e:
116
+ st.error(f"Failed to load FAISS index: {e}")
117
+ return None
118
+
119
+ def user_input(user_question, db):
120
+ """
121
+ Handles user question, performs similarity search, gets response,
122
+ and extracts source page numbers programmatically.
123
+ """
124
+ if db is None:
125
+ st.warning("Vector store is not loaded. Please process PDFs first.")
126
+ return
127
+
128
+ try:
129
+ # 1. Retrieve relevant chunks (Docs)
130
+ docs = db.similarity_search(user_question)
131
+
132
+ # 2. Generate Answer using LLM
133
+ chain = get_conversational_chain()
134
+ response = chain(
135
+ {"input_documents": docs, "question": user_question},
136
+ return_only_outputs=True
137
+ )
138
+
139
+ # 3. Display Answer
140
+ st.write("Reply: ", response["output_text"])
141
+
142
+ # 4. Display Sources (Page Numbers)
143
+ # We use a set to avoid duplicate page numbers if multiple chunks come from the same page
144
+ sources = set()
145
+ for doc in docs:
146
+ page_num = doc.metadata.get("page", "Unknown")
147
+ file_name = doc.metadata.get("source", "Unknown")
148
+ sources.add(f"{file_name} (Page {page_num})")
149
+
150
+ # Display the sources in an expandable box
151
+ with st.expander("📚 View Source Pages"):
152
+ for source in sources:
153
+ st.markdown(f"- {source}")
154
+
155
+ except Exception as e:
156
+ st.error(f"Error processing your question: {e}")
157
+ print(f"Error processing your question: {e}")
158
+
159
+
160
+ def main():
161
+ st.set_page_config(page_title="Chat PDF (Local)")
162
+ st.header("Chat with PDF using Gemma3:1b 🤖")
163
+
164
+ if "faiss_db" not in st.session_state:
165
+ st.session_state.faiss_db = None
166
+
167
+ with st.sidebar:
168
+ st.title("Menu:")
169
+ pdf_docs = st.file_uploader(
170
+ "Upload your PDF Files and Click on the Process Button",
171
+ accept_multiple_files=True
172
+ )
173
+
174
+ if st.button("Process PDFs"):
175
+ if pdf_docs:
176
+ with st.spinner("Processing PDF files..."):
177
+ # Pass document objects now, not raw text
178
+ raw_documents = get_pdf_text(pdf_docs)
179
+ text_chunks = get_text_chunks(raw_documents)
180
+ get_vector_store(text_chunks)
181
+
182
+ st.session_state.faiss_db = load_faiss_index()
183
+ else:
184
+ st.warning("Please upload at least one PDF file.")
185
+
186
+ user_question = st.text_input("Ask a Question from the PDF Files")
187
+
188
+ if user_question:
189
+ if st.session_state.faiss_db is not None:
190
+ user_input(user_question, st.session_state.faiss_db)
191
+ else:
192
+ st.warning("Please upload and process at least one PDF first.")
193
+
194
+
195
+ if __name__ == "__main__":
196
+ main()
hf_app.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PyPDF2 import PdfReader
3
+ import os
4
+
5
+ # --- NEW IMPORT ---
6
+ from langchain.schema import Document # Required to store metadata
7
+
8
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
9
+ from langchain_community.vectorstores import FAISS
10
+ from langchain_core.prompts import ChatPromptTemplate
11
+ from langchain_huggingface import HuggingFaceEndpoint, HuggingFaceEndpointEmbeddings, ChatHuggingFace
12
+ from langchain.chains import create_retrieval_chain
13
+ from langchain.chains.combine_documents import create_stuff_documents_chain
14
+
15
+ # --- PDF Functions (UPDATED) ---
16
+ @st.cache_data
17
+ def get_pdf_text(pdf_docs):
18
+ """
19
+ Extracts text from PDFs and creates Document objects with metadata.
20
+ Returns a list of Document objects, not a string.
21
+ """
22
+ documents = []
23
+ for pdf in pdf_docs:
24
+ pdf_reader = PdfReader(pdf)
25
+ pdf_filename = pdf.name
26
+
27
+ for i, page in enumerate(pdf_reader.pages):
28
+ text = page.extract_text()
29
+ if text:
30
+ # Create a Document with metadata (page number starts at 1)
31
+ doc = Document(
32
+ page_content=text,
33
+ metadata={"page": i + 1, "source": pdf_filename}
34
+ )
35
+ documents.append(doc)
36
+ return documents
37
+
38
+ @st.cache_data
39
+ def get_text_chunks(documents):
40
+ """
41
+ Splits Document objects into chunks while preserving metadata.
42
+ """
43
+ text_splitter = RecursiveCharacterTextSplitter(
44
+ chunk_size=2000, # 10k is often too big for precise page tracking; 2k is better
45
+ chunk_overlap=200
46
+ )
47
+ # split_documents ensures metadata (page num) is copied to new chunks
48
+ chunks = text_splitter.split_documents(documents)
49
+ return chunks
50
+
51
+ # --- Hugging Face Model Functions ---
52
+ @st.cache_resource
53
+ def get_hf_embeddings():
54
+ if "HF_API_TOKEN" not in st.secrets:
55
+ st.error("HF_API_TOKEN not found in secrets.toml.")
56
+ return None
57
+ return HuggingFaceEndpointEmbeddings(
58
+ model="BAAI/bge-small-en-v1.5",
59
+ huggingfacehub_api_token=st.secrets["HF_API_TOKEN"]
60
+ )
61
+
62
+ @st.cache_resource
63
+ def get_hf_llm():
64
+ if "HF_API_TOKEN" not in st.secrets:
65
+ return None
66
+
67
+ llm = HuggingFaceEndpoint(
68
+ huggingfacehub_api_token=st.secrets["HF_API_TOKEN"],
69
+ repo_id="mistralai/Mistral-7B-Instruct-v0.2",
70
+ temperature=0.3,
71
+ max_new_tokens=512,
72
+ task="conversational"
73
+ )
74
+ return ChatHuggingFace(llm=llm)
75
+
76
+ # --- Vector Store Functions (UPDATED) ---
77
+ def get_vector_store(text_chunks):
78
+ """Creates FAISS vector store from Document objects."""
79
+ if not text_chunks:
80
+ st.error("No text chunks found.")
81
+ return
82
+
83
+ embeddings = get_hf_embeddings()
84
+ if embeddings is None:
85
+ return
86
+
87
+ try:
88
+ # Use from_documents instead of from_texts to keep metadata
89
+ vector_store = FAISS.from_documents(documents=text_chunks, embedding=embeddings)
90
+ vector_store.save_local("faiss_index")
91
+ st.success("Vector store created and saved locally.")
92
+ except Exception as e:
93
+ st.error(f"Error creating vector store: {e}")
94
+
95
+ def load_faiss_index():
96
+ try:
97
+ embeddings = get_hf_embeddings()
98
+ if embeddings is None:
99
+ return None
100
+
101
+ db = FAISS.load_local(
102
+ "faiss_index",
103
+ embeddings,
104
+ allow_dangerous_deserialization=True
105
+ )
106
+ return db
107
+ except Exception as e:
108
+ # Silent fail on startup if index doesn't exist yet
109
+ return None
110
+
111
+ # --- Conversational Chain ---
112
+ def get_conversational_chain():
113
+ model = get_hf_llm()
114
+ if model is None:
115
+ return None
116
+
117
+ system_prompt = """
118
+ You are a helpful and informative AI assistant. Your primary goal is to answer questions based on the provided context, which consists of user-uploaded PDF documents.
119
+
120
+ Instructions:
121
+ 1. First, thoroughly analyze the content of the user-uploaded PDFs to extract relevant information for answering the question.
122
+ 2. If the answer can be fully derived from the PDF content, provide it directly and ensure accuracy and completeness.
123
+ 3. If you are uncertain about the answer or if the PDFs and your knowledge do not provide enough information to form a conclusive answer, state clearly that the information is not sufficient to provide a definitive answer.
124
+ 4. Always answer the question in details point by point.
125
+
126
+ **Important:** If the user prompt is not related to the PDF content, Just say "I don't know this answer".
127
+ **Important:** If the user prompt is not related to the PDF content, Never say the answer if you know then still don't say.
128
+
129
+ ### CONTEXT ###
130
+ {context}
131
+ #################
132
+ """
133
+
134
+ prompt = ChatPromptTemplate.from_messages([
135
+ ("system", system_prompt),
136
+ # The user's question will be passed here as the "human" message
137
+ ("human", "Question: {input}")
138
+ ])
139
+
140
+ return create_stuff_documents_chain(model, prompt)
141
+
142
+ # --- User Input Handler (UPDATED) ---
143
+ def user_input(user_question, db):
144
+ if db is None:
145
+ st.warning("Vector store is not loaded.")
146
+ return
147
+
148
+ try:
149
+ # 1. Get relevant documents
150
+ docs = db.similarity_search(user_question, k=4)
151
+
152
+ # 2. Get the response
153
+ chain = get_conversational_chain()
154
+ if chain is None:
155
+ st.error("Check API Key")
156
+ return
157
+
158
+ response = chain.invoke({
159
+ "context": docs,
160
+ "input": user_question
161
+ })
162
+
163
+ # 3. Display Response
164
+ st.markdown("### Reply:")
165
+ st.write(response["answer"] if isinstance(response, dict) and "answer" in response else response)
166
+
167
+ # # 4. Display Page Numbers (New Feature)
168
+ # st.markdown("---")
169
+ # st.markdown("#### 📄 Source Pages:")
170
+
171
+ # # Use a set to avoid duplicate page numbers
172
+ # unique_sources = set()
173
+ # for doc in docs:
174
+ # page_num = doc.metadata.get("page", "Unknown")
175
+ # file_name = doc.metadata.get("source", "Unknown")
176
+ # unique_sources.add(f"{file_name} (Page {page_num})")
177
+
178
+ # # Display sources cleanly
179
+ # for source in unique_sources:
180
+ # st.markdown(f"- {source}")
181
+
182
+ except Exception as e:
183
+ st.error(f"Error: {e}")
184
+
185
+ # --- Main App ---
186
+ def main():
187
+ st.set_page_config(page_title="Chat PDF (Hugging Face)")
188
+ st.header("Chat with PDF using Mistral-7B-Instruct-v0.2")
189
+
190
+ if "faiss_db" not in st.session_state:
191
+ st.session_state.faiss_db = None
192
+
193
+ # Attempt to load index on launch
194
+ if st.session_state.faiss_db is None and os.path.exists("faiss_index"):
195
+ st.session_state.faiss_db = load_faiss_index()
196
+
197
+ with st.sidebar:
198
+ st.title("Menu:")
199
+ pdf_docs = st.file_uploader("Upload PDFs", accept_multiple_files=True)
200
+
201
+ if st.button("Process PDFs"):
202
+ if pdf_docs:
203
+ with st.spinner("Processing..."):
204
+ # 1. Get Documents (not string)
205
+ raw_documents = get_pdf_text(pdf_docs)
206
+ # 2. Split Documents (preserves metadata)
207
+ text_chunks = get_text_chunks(raw_documents)
208
+ # 3. Create Vector Store
209
+ get_vector_store(text_chunks)
210
+
211
+ st.session_state.faiss_db = load_faiss_index()
212
+ else:
213
+ st.warning("Upload a PDF first.")
214
+
215
+ user_question = st.text_input("Ask a Question")
216
+
217
+ if user_question:
218
+ if st.session_state.faiss_db:
219
+ user_input(user_question, st.session_state.faiss_db)
220
+ else:
221
+ st.warning("Process PDFs first.")
222
+
223
+ if __name__ == "__main__":
224
+ main()
requirements.txt CHANGED
@@ -1,3 +1,104 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohappyeyeballs==2.6.1
2
+ aiohttp==3.13.2
3
+ aiosignal==1.4.0
4
+ altair==5.5.0
5
+ annotated-types==0.7.0
6
+ anyio==4.11.0
7
+ async-timeout==4.0.3
8
+ attrs==25.4.0
9
+ blinker==1.9.0
10
+ cachetools==6.2.2
11
+ certifi==2025.11.12
12
+ charset-normalizer==3.4.4
13
+ click==8.3.1
14
+ dataclasses-json==0.6.7
15
+ exceptiongroup==1.3.0
16
+ faiss-cpu==1.13.0
17
+ filelock==3.20.0
18
+ filetype==1.2.0
19
+ frozenlist==1.8.0
20
+ fsspec==2025.10.0
21
+ gitdb==4.0.12
22
+ GitPython==3.1.45
23
+ google-ai-generativelanguage==0.9.0
24
+ google-api-core==2.28.1
25
+ google-auth==2.43.0
26
+ googleapis-common-protos==1.72.0
27
+ greenlet==3.2.4
28
+ grpcio==1.76.0
29
+ grpcio-status==1.76.0
30
+ h11==0.16.0
31
+ hf-xet==1.2.0
32
+ httpcore==1.0.9
33
+ httpx==0.28.1
34
+ httpx-sse==0.4.3
35
+ huggingface-hub==0.36.0
36
+ idna==3.11
37
+ Jinja2==3.1.6
38
+ jsonpatch==1.33
39
+ jsonpointer==3.0.0
40
+ jsonschema==4.25.1
41
+ jsonschema-specifications==2025.9.1
42
+ langchain==0.3.27
43
+ langchain-classic==1.0.0
44
+ langchain-community==0.3.31
45
+ langchain-core==0.3.79
46
+ langchain-google-genai==3.0.3
47
+ langchain-huggingface==1.0.1
48
+ langchain-text-splitters==0.3.11
49
+ langgraph==1.0.3
50
+ langgraph-checkpoint==3.0.1
51
+ langgraph-prebuilt==1.0.4
52
+ langgraph-sdk==0.2.9
53
+ langsmith==0.4.43
54
+ MarkupSafe==3.0.3
55
+ marshmallow==3.26.1
56
+ multidict==6.7.0
57
+ mypy_extensions==1.1.0
58
+ narwhals==2.11.0
59
+ numpy==2.2.6
60
+ orjson==3.11.4
61
+ ormsgpack==1.12.0
62
+ packaging==25.0
63
+ pandas==2.3.3
64
+ pillow==12.0.0
65
+ propcache==0.4.1
66
+ proto-plus==1.26.1
67
+ protobuf==6.33.1
68
+ pyarrow==21.0.0
69
+ pyasn1==0.6.1
70
+ pyasn1_modules==0.4.2
71
+ pydantic==2.12.4
72
+ pydantic-settings==2.12.0
73
+ pydantic_core==2.41.5
74
+ pydeck==0.9.1
75
+ pypdf==6.3.0
76
+ PyPDF2==3.0.1
77
+ python-dateutil==2.9.0.post0
78
+ python-dotenv==1.2.1
79
+ pytz==2025.2
80
+ PyYAML==6.0.3
81
+ referencing==0.37.0
82
+ requests==2.32.5
83
+ requests-toolbelt==1.0.0
84
+ rpds-py==0.28.0
85
+ rsa==4.9.1
86
+ six==1.17.0
87
+ smmap==5.0.2
88
+ sniffio==1.3.1
89
+ SQLAlchemy==2.0.44
90
+ streamlit==1.51.0
91
+ tenacity==9.1.2
92
+ tokenizers==0.22.1
93
+ toml==0.10.2
94
+ tornado==6.5.2
95
+ tqdm==4.67.1
96
+ typing-inspect==0.9.0
97
+ typing-inspection==0.4.2
98
+ typing_extensions==4.15.0
99
+ tzdata==2025.2
100
+ urllib3==2.5.0
101
+ watchdog==6.0.0
102
+ xxhash==3.6.0
103
+ yarl==1.22.0
104
+ zstandard==0.25.0