Krish30 commited on
Commit
31de0b2
·
verified ·
1 Parent(s): 86d28bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +341 -233
app.py CHANGED
@@ -1,233 +1,341 @@
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
-
11
-
12
- from vectorize_documents import embeddings
13
-
14
- working_dir = os.path.dirname(os.path.abspath(__file__))
15
- config_data = json.load(open(f"{working_dir}/config.json"))
16
- GROQ_API_KEY = config_data["GROQ_API_KEY"]
17
- os.environ["GROQ_API_KEY"]= GROQ_API_KEY
18
-
19
- # Ensure the JSON file exists
20
- chat_history_file = "chat_histories.json"
21
- if not os.path.exists(chat_history_file):
22
- with open(chat_history_file, "w") as f:
23
- json.dump({}, f)
24
-
25
- # Functions to handle chat history
26
- def load_chat_history():
27
- with open(chat_history_file, "r") as f:
28
- return json.load(f)
29
-
30
- def save_chat_history(chat_histories):
31
- with open(chat_history_file, "w") as f:
32
- json.dump(chat_histories, f, indent=4)
33
-
34
- # Function to set up vectorstore
35
- def setup_vectorstore():
36
- embeddings = HuggingFaceEmbeddings()
37
- vectorstore = Chroma(persist_directory="vector_db_dir_notes_ai",
38
- embedding_function=embeddings)
39
- return vectorstore
40
-
41
- # Function to set up chatbot chain
42
- def chat_chain(vectorstore):
43
- llm = ChatGroq(
44
- model="llama-3.1-70b-versatile",
45
- temperature=0
46
- )
47
- retriever = vectorstore.as_retriever()
48
- memory = ConversationBufferMemory(
49
- llm=llm,
50
- output_key="answer",
51
- memory_key="chat_history",
52
- return_messages=True
53
- )
54
- chain = ConversationalRetrievalChain.from_llm(
55
- llm=llm,
56
- retriever=retriever,
57
- chain_type="stuff",
58
- memory=memory,
59
- verbose=True,
60
- return_source_documents=True
61
- )
62
- return chain
63
-
64
- # Streamlit UI
65
- st.set_page_config(
66
- page_title="Notes.AI",
67
- page_icon="🤖AI",
68
- layout="centered"
69
- )
70
-
71
- st.title("🤖 Notes.AI")
72
- st.subheader("Hey! Here you can search for notes of CSE 7th Sem! Read Notes, Read PYQ answers also!!")
73
-
74
- # Step 1: Input user's name
75
- if "username" not in st.session_state:
76
- username = st.text_input("Enter your name to proceed:")
77
- if username:
78
- with st.spinner("Loading chatbot interface... Please wait."):
79
- st.session_state.username = username
80
- st.session_state.chat_history = [] # Initialize empty chat history
81
- st.session_state.vectorstore = setup_vectorstore()
82
- st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
83
- st.success(f"Welcome, {username}! The chatbot interface is ready.")
84
- else:
85
- username = st.session_state.username
86
-
87
- # Step 2: Initialize components if not already set
88
- if "conversational_chain" not in st.session_state:
89
- st.session_state.vectorstore = setup_vectorstore()
90
- st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
91
-
92
- # Step 3: Show chatbot interface
93
- if "username" in st.session_state:
94
- st.subheader(f"Hello {username}, start your query below!")
95
-
96
- # Display existing chat history dynamically
97
- for message in st.session_state.chat_history:
98
- if message["role"] == "user":
99
- with st.chat_message("user"):
100
- st.markdown(message["content"])
101
- elif message["role"] == "assistant":
102
- with st.chat_message("assistant"):
103
- st.markdown(message["content"])
104
-
105
- # User input section
106
- user_input = st.chat_input("Ask AI....")
107
- if user_input:
108
- with st.spinner("Processing your query... Please wait."):
109
- # Save user input to session state
110
- st.session_state.chat_history.append({"role": "user", "content": user_input})
111
-
112
- # Display user's message
113
- with st.chat_message("user"):
114
- st.markdown(user_input)
115
-
116
- # Get assistant's response
117
- with st.chat_message("assistant"):
118
- response = st.session_state.conversational_chain({"question": user_input})
119
- assistant_response = response["answer"]
120
- st.markdown(assistant_response)
121
-
122
- # Save assistant's response to session state
123
- st.session_state.chat_history.append({"role": "assistant", "content": assistant_response})
124
-
125
- # Save chat history to file with timestamp
126
- chat_histories = load_chat_history()
127
- timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
128
- if username not in chat_histories:
129
- chat_histories[username] = []
130
- chat_histories[username].append({
131
- "timestamp": timestamp,
132
- "user": user_input,
133
- "assistant": assistant_response
134
- })
135
- save_chat_history(chat_histories)
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
- # import os
147
- # import json
148
-
149
- # import streamlit as st
150
- # from langchain_huggingface import HuggingFaceEmbeddings
151
- # from langchain_chroma import Chroma
152
- # from langchain_groq import ChatGroq
153
- # from langchain.memory import ConversationBufferMemory
154
- # from langchain.chains import ConversationalRetrievalChain
155
-
156
- # from vectorize_documents import embeddings
157
-
158
-
159
- # working_dir = os.path.dirname(os.path.abspath(__file__))
160
- # config_data = json.load(open(f"{working_dir}/config.json"))
161
- # GROQ_API_KEY = config_data["GROQ_API_KEY"]
162
- # os.environ["GROQ_API_KEY"]= GROQ_API_KEY
163
-
164
-
165
- # def setup_vectorstore():
166
- # persist_directory = f"{working_dir}/vector_db_dir_notes_ai"
167
- # embeddings = HuggingFaceEmbeddings()
168
- # vectorstore = Chroma(persist_directory=persist_directory,
169
- # embedding_function=embeddings)
170
- # return vectorstore
171
-
172
- # def chat_chain(vectorstore):
173
- # llm = ChatGroq(
174
- # model = "llama-3.1-70b-versatile",
175
- # temperature = 0
176
- # )
177
- # retriever = vectorstore.as_retriever()
178
- # memory = ConversationBufferMemory(
179
- # llm = llm,
180
- # output_key = "answer",
181
- # memory_key = "chat_history",
182
- # return_messages = True
183
- # )
184
- # chain = ConversationalRetrievalChain.from_llm(
185
- # llm=llm,
186
- # retriever = retriever,
187
- # chain_type = "stuff",
188
- # memory = memory,
189
- # verbose=True,
190
- # return_source_documents= True
191
- # )
192
- # return chain
193
-
194
- # st.set_page_config(
195
- # page_title="Notes.AI",
196
- # page_icon="🤖AI",
197
- # layout="centered"
198
- # )
199
-
200
- # st.title("🤖 Notes.AI")
201
-
202
- # # st.title("🤖 Hey! Here you can search for notes of CSE 7th Sem! Read Notes, Read PYQ answers also!!")
203
-
204
- # st.subheader("Hey! Here you can search for notes of CSE 7th Sem! Read Notes, Read PYQ answers also!!")
205
-
206
- # # Additional subheading
207
- # st.subheader("Start your query below to get instant help!")
208
-
209
- # if "chat_history" not in st.session_state:
210
- # st.session_state.chat_history = []
211
-
212
- # if "vectorstore" not in st.session_state:
213
- # st.session_state.vectorstore = setup_vectorstore()
214
-
215
- # if "conversational_chain" not in st.session_state:
216
- # st.session_state.conversational_chain = chat_chain(st.session_state.vectorstore)
217
-
218
- # for message in st.session_state.chat_history:
219
- # with st.chat_message(message["role"]):
220
- # st.markdown(message["content"])
221
- # user_input = st.chat_input("Ask AI....")
222
-
223
- # if user_input:
224
- # st.session_state.chat_history.append({"role":"user", "content":user_input})
225
-
226
- # with st.chat_message("user"):
227
- # st.markdown(user_input)
228
-
229
- # with st.chat_message("assistant"):
230
- # response = st.session_state.conversational_chain({"question":user_input})
231
- # assistant_response = response["answer"]
232
- # st.markdown(assistant_response)
233
- # st.session_state.chat_history.append({"role":"assistant","content": assistant_response})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
+