meesamraza commited on
Commit
f4cfcfd
Β·
verified Β·
1 Parent(s): e5f5057

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -5
app.py CHANGED
@@ -9,6 +9,8 @@ from langchain_community.vectorstores import FAISS
9
  from langchain.memory import ConversationBufferMemory
10
  from langchain.chains import ConversationalRetrievalChain
11
  from langchain_groq import ChatGroq
 
 
12
 
13
  # --------------------------
14
  # Load environment variables
@@ -28,13 +30,15 @@ logging.basicConfig(
28
  # --------------------------
29
  def get_pdf_text(pdf_docs):
30
  text = ""
 
31
  for pdf in pdf_docs:
32
  pdf_reader = PdfReader(pdf)
 
33
  for page in pdf_reader.pages:
34
  extracted_text = page.extract_text()
35
  if extracted_text:
36
  text += extracted_text + "\n"
37
- return text
38
 
39
  # --------------------------
40
  # Text chunking
@@ -78,9 +82,11 @@ def get_conversation_chain(vectorstore):
78
  # --------------------------
79
  def handle_userinput(user_question):
80
  if st.session_state.conversation is not None:
 
81
  with st.spinner("πŸ€– Thinking..."):
82
  response = st.session_state.conversation({'question': user_question})
83
  st.session_state.chat_history = response['chat_history']
 
84
 
85
  # Display chat history in a chat-like format
86
  for i, message in enumerate(st.session_state.chat_history):
@@ -88,22 +94,41 @@ def handle_userinput(user_question):
88
  st.markdown(f"πŸ§‘ **You:** {message.content}")
89
  else:
90
  st.markdown(f"πŸ€– **Bot:** {message.content}")
 
 
 
91
  else:
92
  st.warning("⚠ Please process the documents first.")
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  # --------------------------
95
  # Main Streamlit App
96
  # --------------------------
97
  def main():
98
  st.set_page_config(page_title="AI PDF Chatbot", page_icon="πŸ“š", layout="wide")
99
  st.title("πŸ“š AI-Powered PDF Chatbot")
100
- st.markdown("Chat with your documents using **LLaMA 3.3** and **Groq AI**. Perfect for research, learning, and exhibitions!")
101
 
102
  # Session state initialization
103
  if "conversation" not in st.session_state:
104
  st.session_state.conversation = None
105
  if "chat_history" not in st.session_state:
106
  st.session_state.chat_history = None
 
 
107
 
108
  # Sidebar - Upload PDFs
109
  with st.sidebar:
@@ -112,17 +137,23 @@ def main():
112
  if st.button("πŸš€ Process Documents"):
113
  if pdf_docs:
114
  with st.spinner("πŸ“– Reading & Processing..."):
115
- raw_text = get_pdf_text(pdf_docs)
 
116
  if raw_text.strip():
117
  text_chunks = get_text_chunks(raw_text)
118
  vectorstore = get_vectorstore(text_chunks)
119
  st.session_state.conversation = get_conversation_chain(vectorstore)
120
- st.success("βœ… Documents processed! You can now ask questions.")
121
  else:
122
  st.error("No valid text found in PDFs.")
123
  else:
124
  st.warning("Please upload at least one PDF.")
125
 
 
 
 
 
 
126
  # Main Chat Section
127
  st.subheader("πŸ’¬ Ask a Question")
128
  user_question = st.text_input("Type your question here...")
@@ -137,7 +168,14 @@ def main():
137
  st.subheader("πŸ“ Chat History")
138
  for i, message in enumerate(st.session_state.chat_history):
139
  role = "User" if i % 2 == 0 else "Bot"
140
- st.write(f"**{role}:** {message.content}")
 
 
 
 
 
 
 
141
 
142
  if __name__ == '__main__':
143
  main()
 
9
  from langchain.memory import ConversationBufferMemory
10
  from langchain.chains import ConversationalRetrievalChain
11
  from langchain_groq import ChatGroq
12
+ import time
13
+ import io
14
 
15
  # --------------------------
16
  # Load environment variables
 
30
  # --------------------------
31
  def get_pdf_text(pdf_docs):
32
  text = ""
33
+ page_count = 0
34
  for pdf in pdf_docs:
35
  pdf_reader = PdfReader(pdf)
36
+ page_count += len(pdf_reader.pages)
37
  for page in pdf_reader.pages:
38
  extracted_text = page.extract_text()
39
  if extracted_text:
40
  text += extracted_text + "\n"
41
+ return text, page_count
42
 
43
  # --------------------------
44
  # Text chunking
 
82
  # --------------------------
83
  def handle_userinput(user_question):
84
  if st.session_state.conversation is not None:
85
+ start_time = time.time()
86
  with st.spinner("πŸ€– Thinking..."):
87
  response = st.session_state.conversation({'question': user_question})
88
  st.session_state.chat_history = response['chat_history']
89
+ elapsed_time = round(time.time() - start_time, 2)
90
 
91
  # Display chat history in a chat-like format
92
  for i, message in enumerate(st.session_state.chat_history):
 
94
  st.markdown(f"πŸ§‘ **You:** {message.content}")
95
  else:
96
  st.markdown(f"πŸ€– **Bot:** {message.content}")
97
+
98
+ # Stats
99
+ st.info(f"⏱ Response Time: {elapsed_time}s | πŸ“„ Words: {len(response['answer'].split())}")
100
  else:
101
  st.warning("⚠ Please process the documents first.")
102
 
103
+ # --------------------------
104
+ # Export chat
105
+ # --------------------------
106
+ def export_chat():
107
+ if st.session_state.chat_history:
108
+ chat_text = "\n".join([f"{'User' if i % 2 == 0 else 'Bot'}: {m.content}" for i, m in enumerate(st.session_state.chat_history)])
109
+ buffer = io.BytesIO(chat_text.encode())
110
+ st.download_button(
111
+ label="πŸ’Ύ Download Chat",
112
+ data=buffer,
113
+ file_name="chat_history.txt",
114
+ mime="text/plain"
115
+ )
116
+
117
  # --------------------------
118
  # Main Streamlit App
119
  # --------------------------
120
  def main():
121
  st.set_page_config(page_title="AI PDF Chatbot", page_icon="πŸ“š", layout="wide")
122
  st.title("πŸ“š AI-Powered PDF Chatbot")
123
+ st.markdown("Chat with your documents using **LLaMA 3.3** and **Groq AI**. Perfect for research, learning, and exhibitions! πŸš€")
124
 
125
  # Session state initialization
126
  if "conversation" not in st.session_state:
127
  st.session_state.conversation = None
128
  if "chat_history" not in st.session_state:
129
  st.session_state.chat_history = None
130
+ if "pages_processed" not in st.session_state:
131
+ st.session_state.pages_processed = 0
132
 
133
  # Sidebar - Upload PDFs
134
  with st.sidebar:
 
137
  if st.button("πŸš€ Process Documents"):
138
  if pdf_docs:
139
  with st.spinner("πŸ“– Reading & Processing..."):
140
+ raw_text, page_count = get_pdf_text(pdf_docs)
141
+ st.session_state.pages_processed = page_count
142
  if raw_text.strip():
143
  text_chunks = get_text_chunks(raw_text)
144
  vectorstore = get_vectorstore(text_chunks)
145
  st.session_state.conversation = get_conversation_chain(vectorstore)
146
+ st.success(f"βœ… {len(pdf_docs)} file(s) processed | πŸ“„ {page_count} pages")
147
  else:
148
  st.error("No valid text found in PDFs.")
149
  else:
150
  st.warning("Please upload at least one PDF.")
151
 
152
+ # Clear chat
153
+ if st.button("πŸ—‘ Clear Chat"):
154
+ st.session_state.chat_history = None
155
+ st.success("Chat cleared.")
156
+
157
  # Main Chat Section
158
  st.subheader("πŸ’¬ Ask a Question")
159
  user_question = st.text_input("Type your question here...")
 
168
  st.subheader("πŸ“ Chat History")
169
  for i, message in enumerate(st.session_state.chat_history):
170
  role = "User" if i % 2 == 0 else "Bot"
171
+ st.markdown(f"**{role}:** {message.content}")
172
+
173
+ # Export chat
174
+ export_chat()
175
+
176
+ # Footer Branding
177
+ st.markdown("---")
178
+ st.markdown("**Made with ❀️ by Meesam Raza | Powered by LLaMA 3.3 & Groq AI**")
179
 
180
  if __name__ == '__main__':
181
  main()