Krish30 commited on
Commit
7b38378
·
verified ·
1 Parent(s): 2174258

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +206 -206
app.py CHANGED
@@ -1,201 +1,3 @@
1
- import os
2
- import json
3
- from datetime import datetime
4
- import streamlit as st
5
- from langchain_huggingface import HuggingFaceEmbeddings
6
- from langchain_chroma import Chroma
7
- from langchain_groq import ChatGroq
8
- from langchain.memory import ConversationBufferMemory
9
- from langchain.chains import ConversationalRetrievalChain
10
- from vectorize_documents import embeddings
11
-
12
- working_dir = os.path.dirname(os.path.abspath(__file__))
13
- config_data = json.load(open(f"{working_dir}/config.json"))
14
- GROQ_API_KEY = config_data["GROQ_API_KEY"]
15
- os.environ["GROQ_API_KEY"] = GROQ_API_KEY
16
-
17
- # Ensure the JSON file exists
18
- chat_history_file = "chat_histories.json"
19
- if not os.path.exists(chat_history_file):
20
- with open(chat_history_file, "w") as f:
21
- json.dump({}, f)
22
-
23
- # Functions to handle chat history
24
- def load_chat_history():
25
- with open(chat_history_file, "r") as f:
26
- return json.load(f)
27
-
28
- def save_chat_history(chat_histories):
29
- with open(chat_history_file, "w") as f:
30
- json.dump(chat_histories, f, indent=4)
31
-
32
- # Function to set up vectorstore
33
- def setup_vectorstore():
34
- embeddings = HuggingFaceEmbeddings()
35
- vectorstore = Chroma(persist_directory="vector_db_dir_notes_ai",
36
- embedding_function=embeddings)
37
- return vectorstore
38
-
39
- # Function to set up chatbot chain
40
- def chat_chain(vectorstore):
41
- llm = ChatGroq(
42
- model="llama-3.1-70b-versatile",
43
- temperature=0
44
- )
45
- retriever = vectorstore.as_retriever()
46
- memory = ConversationBufferMemory(
47
- llm=llm,
48
- output_key="answer",
49
- memory_key="chat_history",
50
- return_messages=True
51
- )
52
- chain = ConversationalRetrievalChain.from_llm(
53
- llm=llm,
54
- retriever=retriever,
55
- chain_type="stuff",
56
- memory=memory,
57
- verbose=True,
58
- return_source_documents=True
59
- )
60
- return chain
61
-
62
- # Streamlit UI
63
- st.set_page_config(
64
- page_title="Notes.AI",
65
- page_icon="🤖AI",
66
- layout="centered"
67
- )
68
-
69
- st.title("🤖 Notes.AI")
70
- st.subheader("Hey! Upload your question bank and get answers instantly!")
71
-
72
- # Step 1: Input user's name
73
- if "username" not in st.session_state:
74
- username = st.text_input("Enter your name to proceed:")
75
- if username:
76
- with st.spinner("Loading chatbot interface... Please wait."):
77
- st.session_state.username = username
78
- st.session_state.chat_history = [] # Initialize empty chat history
79
- st.session_state.vectorstore = setup_vectorstore()
80
- st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
81
- st.success(f"Welcome, {username}! The chatbot interface is ready.")
82
- else:
83
- username = st.session_state.username
84
-
85
- # Step 2: Initialize components if not already set
86
- if "conversational_chain" not in st.session_state:
87
- st.session_state.vectorstore = setup_vectorstore()
88
- st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
89
-
90
- # Step 3: File upload for question bank
91
- st.subheader("Upload your question bank (PDF or DOC):")
92
- uploaded_file = st.file_uploader("Choose a file", type=["pdf", "doc", "docx"])
93
-
94
- if uploaded_file:
95
- # Process the uploaded file
96
- with st.spinner("Reading and processing your question bank..."):
97
- import docx2txt
98
- from PyPDF2 import PdfReader
99
-
100
- # Extract questions from the file
101
- def extract_questions(file):
102
- if file.name.endswith(".pdf"):
103
- reader = PdfReader(file)
104
- text = "\n".join([page.extract_text() for page in reader.pages])
105
- elif file.name.endswith((".doc", ".docx")):
106
- text = docx2txt.process(file)
107
- else:
108
- text = ""
109
- return text.strip().split("\n")
110
-
111
- questions = extract_questions(uploaded_file)
112
-
113
- # Generate answers using the LLM
114
- answers = []
115
- for question in questions:
116
- if question.strip():
117
- response = st.session_state.conversational_chain({"question": question})
118
- answers.append({"question": question, "answer": response["answer"]})
119
-
120
- # Save Q&A to a file
121
- output_file_path = f"question_answers_{username}.txt"
122
- with open(output_file_path, "w") as f:
123
- for qa in answers:
124
- f.write(f"Q: {qa['question']}\nA: {qa['answer']}\n\n")
125
-
126
- st.success("All questions have been answered and saved!")
127
-
128
- # Provide download link
129
- with open(output_file_path, "rb") as f:
130
- st.download_button(
131
- label="Download Q&A File",
132
- data=f,
133
- file_name=output_file_path,
134
- mime="text/plain"
135
- )
136
-
137
- # Chatbot interface for additional questions
138
- st.subheader(f"Hello {username}, ask additional questions below!")
139
- if "username" in st.session_state:
140
- # Display existing chat history dynamically
141
- for message in st.session_state.chat_history:
142
- if message["role"] == "user":
143
- with st.chat_message("user"):
144
- st.markdown(message["content"])
145
- elif message["role"] == "assistant":
146
- with st.chat_message("assistant"):
147
- st.markdown(message["content"])
148
-
149
- # User input section
150
- user_input = st.chat_input("Ask AI....")
151
- if user_input:
152
- with st.spinner("Processing your query... Please wait."):
153
- # Save user input to session state
154
- st.session_state.chat_history.append({"role": "user", "content": user_input})
155
-
156
- # Display user's message
157
- with st.chat_message("user"):
158
- st.markdown(user_input)
159
-
160
- # Get assistant's response
161
- with st.chat_message("assistant"):
162
- response = st.session_state.conversational_chain({"question": user_input})
163
- assistant_response = response["answer"]
164
- st.markdown(assistant_response)
165
-
166
- # Save assistant's response to session state
167
- st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
168
-
169
- # Save chat history to file with timestamp
170
- chat_histories = load_chat_history()
171
- timestamp = datetime.now().strftime("%Y-%m-%d %A") # Added day to timestamp
172
- if username not in chat_histories:
173
- chat_histories[username] = []
174
- chat_histories[username].append({
175
- "timestamp": timestamp,
176
- "user": user_input,
177
- "assistant": assistant_response
178
- })
179
- save_chat_history(chat_histories)
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
- # all working but file upload is not added in below code
198
-
199
  # import os
200
  # import json
201
  # from datetime import datetime
@@ -205,14 +7,12 @@ if "username" in st.session_state:
205
  # from langchain_groq import ChatGroq
206
  # from langchain.memory import ConversationBufferMemory
207
  # from langchain.chains import ConversationalRetrievalChain
208
-
209
-
210
  # from vectorize_documents import embeddings
211
 
212
  # working_dir = os.path.dirname(os.path.abspath(__file__))
213
  # config_data = json.load(open(f"{working_dir}/config.json"))
214
  # GROQ_API_KEY = config_data["GROQ_API_KEY"]
215
- # os.environ["GROQ_API_KEY"]= GROQ_API_KEY
216
 
217
  # # Ensure the JSON file exists
218
  # chat_history_file = "chat_histories.json"
@@ -267,7 +67,7 @@ if "username" in st.session_state:
267
  # )
268
 
269
  # st.title("🤖 Notes.AI")
270
- # st.subheader("Hey! Here you can search for notes of CSE 7th Sem! Read Notes, Read PYQ answers also!!")
271
 
272
  # # Step 1: Input user's name
273
  # if "username" not in st.session_state:
@@ -287,10 +87,56 @@ if "username" in st.session_state:
287
  # st.session_state.vectorstore = setup_vectorstore()
288
  # st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
289
 
290
- # # Step 3: Show chatbot interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
291
  # if "username" in st.session_state:
292
- # st.subheader(f"Hello {username}, start your query below!")
293
-
294
  # # Display existing chat history dynamically
295
  # for message in st.session_state.chat_history:
296
  # if message["role"] == "user":
@@ -322,7 +168,7 @@ if "username" in st.session_state:
322
 
323
  # # Save chat history to file with timestamp
324
  # chat_histories = load_chat_history()
325
- # timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
326
  # if username not in chat_histories:
327
  # chat_histories[username] = []
328
  # chat_histories[username].append({
@@ -339,3 +185,157 @@ if "username" in st.session_state:
339
 
340
 
341
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # import os
2
  # import json
3
  # from datetime import datetime
 
7
  # from langchain_groq import ChatGroq
8
  # from langchain.memory import ConversationBufferMemory
9
  # from langchain.chains import ConversationalRetrievalChain
 
 
10
  # from vectorize_documents import embeddings
11
 
12
  # working_dir = os.path.dirname(os.path.abspath(__file__))
13
  # config_data = json.load(open(f"{working_dir}/config.json"))
14
  # GROQ_API_KEY = config_data["GROQ_API_KEY"]
15
+ # os.environ["GROQ_API_KEY"] = GROQ_API_KEY
16
 
17
  # # Ensure the JSON file exists
18
  # chat_history_file = "chat_histories.json"
 
67
  # )
68
 
69
  # st.title("🤖 Notes.AI")
70
+ # st.subheader("Hey! Upload your question bank and get answers instantly!")
71
 
72
  # # Step 1: Input user's name
73
  # if "username" not in st.session_state:
 
87
  # st.session_state.vectorstore = setup_vectorstore()
88
  # st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
89
 
90
+ # # Step 3: File upload for question bank
91
+ # st.subheader("Upload your question bank (PDF or DOC):")
92
+ # uploaded_file = st.file_uploader("Choose a file", type=["pdf", "doc", "docx"])
93
+
94
+ # if uploaded_file:
95
+ # # Process the uploaded file
96
+ # with st.spinner("Reading and processing your question bank..."):
97
+ # import docx2txt
98
+ # from PyPDF2 import PdfReader
99
+
100
+ # # Extract questions from the file
101
+ # def extract_questions(file):
102
+ # if file.name.endswith(".pdf"):
103
+ # reader = PdfReader(file)
104
+ # text = "\n".join([page.extract_text() for page in reader.pages])
105
+ # elif file.name.endswith((".doc", ".docx")):
106
+ # text = docx2txt.process(file)
107
+ # else:
108
+ # text = ""
109
+ # return text.strip().split("\n")
110
+
111
+ # questions = extract_questions(uploaded_file)
112
+
113
+ # # Generate answers using the LLM
114
+ # answers = []
115
+ # for question in questions:
116
+ # if question.strip():
117
+ # response = st.session_state.conversational_chain({"question": question})
118
+ # answers.append({"question": question, "answer": response["answer"]})
119
+
120
+ # # Save Q&A to a file
121
+ # output_file_path = f"question_answers_{username}.txt"
122
+ # with open(output_file_path, "w") as f:
123
+ # for qa in answers:
124
+ # f.write(f"Q: {qa['question']}\nA: {qa['answer']}\n\n")
125
+
126
+ # st.success("All questions have been answered and saved!")
127
+
128
+ # # Provide download link
129
+ # with open(output_file_path, "rb") as f:
130
+ # st.download_button(
131
+ # label="Download Q&A File",
132
+ # data=f,
133
+ # file_name=output_file_path,
134
+ # mime="text/plain"
135
+ # )
136
+
137
+ # # Chatbot interface for additional questions
138
+ # st.subheader(f"Hello {username}, ask additional questions below!")
139
  # if "username" in st.session_state:
 
 
140
  # # Display existing chat history dynamically
141
  # for message in st.session_state.chat_history:
142
  # if message["role"] == "user":
 
168
 
169
  # # Save chat history to file with timestamp
170
  # chat_histories = load_chat_history()
171
+ # timestamp = datetime.now().strftime("%Y-%m-%d %A") # Added day to timestamp
172
  # if username not in chat_histories:
173
  # chat_histories[username] = []
174
  # chat_histories[username].append({
 
185
 
186
 
187
 
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+
196
+
197
+ all working but file upload is not added in below code
198
+
199
+ import os
200
+ import json
201
+ from datetime import datetime
202
+ import streamlit as st
203
+ from langchain_huggingface import HuggingFaceEmbeddings
204
+ from langchain_chroma import Chroma
205
+ from langchain_groq import ChatGroq
206
+ from langchain.memory import ConversationBufferMemory
207
+ from langchain.chains import ConversationalRetrievalChain
208
+
209
+
210
+ from vectorize_documents import embeddings
211
+
212
+ working_dir = os.path.dirname(os.path.abspath(__file__))
213
+ config_data = json.load(open(f"{working_dir}/config.json"))
214
+ GROQ_API_KEY = config_data["GROQ_API_KEY"]
215
+ os.environ["GROQ_API_KEY"]= GROQ_API_KEY
216
+
217
+ # Ensure the JSON file exists
218
+ chat_history_file = "chat_histories.json"
219
+ if not os.path.exists(chat_history_file):
220
+ with open(chat_history_file, "w") as f:
221
+ json.dump({}, f)
222
+
223
+ # Functions to handle chat history
224
+ def load_chat_history():
225
+ with open(chat_history_file, "r") as f:
226
+ return json.load(f)
227
+
228
+ def save_chat_history(chat_histories):
229
+ with open(chat_history_file, "w") as f:
230
+ json.dump(chat_histories, f, indent=4)
231
+
232
+ # Function to set up vectorstore
233
+ def setup_vectorstore():
234
+ embeddings = HuggingFaceEmbeddings()
235
+ vectorstore = Chroma(persist_directory="vector_db_dir_notes_ai",
236
+ embedding_function=embeddings)
237
+ return vectorstore
238
+
239
+ # Function to set up chatbot chain
240
+ def chat_chain(vectorstore):
241
+ llm = ChatGroq(
242
+ model="llama-3.1-70b-versatile",
243
+ temperature=0
244
+ )
245
+ retriever = vectorstore.as_retriever()
246
+ memory = ConversationBufferMemory(
247
+ llm=llm,
248
+ output_key="answer",
249
+ memory_key="chat_history",
250
+ return_messages=True
251
+ )
252
+ chain = ConversationalRetrievalChain.from_llm(
253
+ llm=llm,
254
+ retriever=retriever,
255
+ chain_type="stuff",
256
+ memory=memory,
257
+ verbose=True,
258
+ return_source_documents=True
259
+ )
260
+ return chain
261
+
262
+ # Streamlit UI
263
+ st.set_page_config(
264
+ page_title="Notes.AI",
265
+ page_icon="🤖AI",
266
+ layout="centered"
267
+ )
268
+
269
+ st.title("🤖 Notes.AI")
270
+ st.subheader("Hey! Here you can search for notes of CSE 7th Sem! Read Notes, Read PYQ answers also!!")
271
+
272
+ # Step 1: Input user's name
273
+ if "username" not in st.session_state:
274
+ username = st.text_input("Enter your name to proceed:")
275
+ if username:
276
+ with st.spinner("Loading chatbot interface... Please wait."):
277
+ st.session_state.username = username
278
+ st.session_state.chat_history = [] # Initialize empty chat history
279
+ st.session_state.vectorstore = setup_vectorstore()
280
+ st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
281
+ st.success(f"Welcome, {username}! The chatbot interface is ready.")
282
+ else:
283
+ username = st.session_state.username
284
+
285
+ # Step 2: Initialize components if not already set
286
+ if "conversational_chain" not in st.session_state:
287
+ st.session_state.vectorstore = setup_vectorstore()
288
+ st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
289
+
290
+ # Step 3: Show chatbot interface
291
+ if "username" in st.session_state:
292
+ st.subheader(f"Hello {username}, start your query below!")
293
+
294
+ # Display existing chat history dynamically
295
+ for message in st.session_state.chat_history:
296
+ if message["role"] == "user":
297
+ with st.chat_message("user"):
298
+ st.markdown(message["content"])
299
+ elif message["role"] == "assistant":
300
+ with st.chat_message("assistant"):
301
+ st.markdown(message["content"])
302
+
303
+ # User input section
304
+ user_input = st.chat_input("Ask AI....")
305
+ if user_input:
306
+ with st.spinner("Processing your query... Please wait."):
307
+ # Save user input to session state
308
+ st.session_state.chat_history.append({"role": "user", "content": user_input})
309
+
310
+ # Display user's message
311
+ with st.chat_message("user"):
312
+ st.markdown(user_input)
313
+
314
+ # Get assistant's response
315
+ with st.chat_message("assistant"):
316
+ response = st.session_state.conversational_chain({"question": user_input})
317
+ assistant_response = response["answer"]
318
+ st.markdown(assistant_response)
319
+
320
+ # Save assistant's response to session state
321
+ st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
322
+
323
+ # Save chat history to file with timestamp
324
+ chat_histories = load_chat_history()
325
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
326
+ if username not in chat_histories:
327
+ chat_histories[username] = []
328
+ chat_histories[username].append({
329
+ "timestamp": timestamp,
330
+ "user": user_input,
331
+ "assistant": assistant_response
332
+ })
333
+ save_chat_history(chat_histories)
334
+
335
+
336
+
337
+
338
+
339
+
340
+
341
+