ak0601 commited on
Commit
5ec76ac
·
verified ·
1 Parent(s): 02b58f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +295 -295
app.py CHANGED
@@ -1,296 +1,296 @@
1
- from pypdf import PdfReader
2
- from langchain.document_loaders import PyPDFLoader
3
- from langchain.document_loaders import TextLoader
4
- from langchain.document_loaders import Docx2txtLoader
5
- from langchain.text_splitter import CharacterTextSplitter
6
- from langchain.embeddings import HuggingFaceEmbeddings
7
- from langchain.vectorstores import Chroma
8
- from huggingface_hub import notebook_login
9
- import torch
10
- from langchain_google_genai import ChatGoogleGenerativeAI
11
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
12
- from langchain_core.messages import HumanMessage, SystemMessage
13
- from langchain.chains import create_history_aware_retriever, create_retrieval_chain
14
- from langchain.chains.combine_documents import create_stuff_documents_chain
15
- from dotenv import load_dotenv, dotenv_values
16
- import os
17
- load_dotenv()
18
- HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
19
- os.environ['HUGGINGFACEHUB_API_TOKEN'] = HUGGINGFACEHUB_API_TOKEN
20
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
21
- os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
22
-
23
- reader = PdfReader("sound.pdf")
24
-
25
- num_pages = len(reader.pages)
26
-
27
- from langchain.schema.document import Document
28
- document = []
29
- dict1 = {}
30
- indx = 0
31
- for i in range(34,456):
32
- page = reader.pages[i]
33
- document.append(Document(page_content=page.extract_text()))
34
-
35
-
36
- document_splitter=CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=100)
37
- document_chunks=document_splitter.split_documents(document)
38
- # sentence-transformers/all-mpnet-base-v
39
- embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
40
- vectordb=Chroma.from_documents(document_chunks,embedding=embeddings, persist_directory='./data')
41
- vectordb.persist()
42
-
43
- llm = ChatGoogleGenerativeAI(
44
- model="gemini-1.5-pro-latest",
45
- temperature=0.2,
46
- max_tokens=None,
47
- timeout=None,
48
- max_retries=2,
49
- )
50
-
51
- retriever = vectordb.as_retriever()
52
-
53
- prompt = ChatPromptTemplate.from_messages([
54
- MessagesPlaceholder(variable_name="chat_history"),
55
- ("human", "{input}"),
56
- ("system", """Given the chat history and the latest user question, which might reference context in the chat history,
57
- formulate a standalone question that can be understood without the chat history.
58
- If the question is directly addressed within the provided document, provide a relevant answer.
59
- If the question is not explicitly addressed in the document, return the following message:
60
- 'This question is beyond the scope of the available information. Please contact the mentor for further assistance.'
61
- Do NOT answer the question directly, just reformulate it if needed and otherwise return it as is.
62
- Also if the questions is about any irrelevent topic like politics, war, homosexuality, transgender etc just reply the following message:
63
- 'Please reframe your question'""")
64
- ])
65
- retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
66
-
67
- prompt = ChatPromptTemplate.from_messages([
68
- ("system", """I am an advanced English tutor designed to help users improve their English language skills through interactive lessons, personalized feedback, and quizzes. Your goal is to enhance their vocabulary, grammar, reading comprehension, and speaking ability. You should adapt to their skill level and learning preferences."
69
-
70
- User Interaction Guidelines:
71
-
72
- Learning Mode: Begin by asking the user their current proficiency level (Beginner, Intermediate, Advanced) and their learning goals (e.g., improve vocabulary, master grammar, prepare for an exam, etc.).
73
- Interactive Lessons: Provide engaging lessons tailored to their level. Use examples, simple explanations, and interactive questions. For example:
74
- Beginner: "What's the plural form of 'apple'?"
75
- Advanced: "Explain the difference between 'affect' and 'effect' with examples."
76
- Quizzes: Offer quizzes after lessons to reinforce learning. Use multiple-choice, fill-in-the-blank, or open-ended questions. Adjust the difficulty based on user performance. Provide immediate feedback, explaining why an answer is correct or incorrect.
77
- Speaking Practice: If requested, simulate conversations and provide constructive feedback on grammar, vocabulary, and pronunciation.
78
- Example Response:
79
-
80
- Learning Mode Prompt: "Hello! I'm here to help you improve your English. Could you tell me about your current level and goals? For instance, do you want to focus on grammar, expand your vocabulary, or practice speaking?"
81
- Interactive Lesson Prompt: "Let's practice forming questions. Convert this statement into a question: 'She is reading a book.'"
82
- Quiz Prompt: "Choose the correct option: 'The cat ____ on the mat.'
83
- A) sit
84
- B) sits
85
- C) sitting
86
- D) siting"
87
- (Correct answer: B - 'sits')
88
- Adaptability Features:
89
-
90
- Respond dynamically to user input, simplifying explanations or increasing complexity as needed.
91
- Encourage and motivate users by celebrating their progress.
92
- Offer follow-up suggestions after quizzes, e.g., "You did well on vocabulary! Shall we try a grammar-focused session next?
93
- """
94
- "\n\n"
95
- "{context}"),
96
- MessagesPlaceholder(variable_name="chat_history"),
97
- ("human", "{input}")
98
- ])
99
-
100
- stuff_documents_chain = create_stuff_documents_chain(llm, prompt)
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
- import os
109
- import streamlit as st
110
- import google.generativeai as genai
111
- # from langchain_openai import OpenAI /
112
- from langchain_google_genai import GoogleGenerativeAIEmbeddings
113
- from langchain_google_genai import ChatGoogleGenerativeAI
114
- # from langchain_openai import OpenAIEmbeddings
115
- from langchain_community.document_loaders import Docx2txtLoader
116
- from langchain.text_splitter import RecursiveCharacterTextSplitter
117
- from langchain_community.vectorstores import Chroma
118
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
119
- from langchain_core.messages import HumanMessage, SystemMessage
120
- from langchain.chains import create_history_aware_retriever, create_retrieval_chain
121
- from langchain.chains.combine_documents import create_stuff_documents_chain
122
- from dotenv import load_dotenv
123
- from langchain.embeddings import HuggingFaceEmbeddings
124
- import pysqlite3
125
- import sys
126
- sys.modules['sqlite3'] = pysqlite3
127
-
128
- import os
129
- os.environ["TRANSFORMERS_OFFLINE"] = "1"
130
- HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN ")
131
- os.environ['HUGGINGFACEHUB_API_TOKEN'] = HUGGINGFACEHUB_API_TOKEN
132
- load_dotenv()
133
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
134
- os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
135
-
136
- if not GOOGLE_API_KEY:
137
- raise ValueError("Gemini API key not found. Please set it in the .env file.")
138
-
139
- st.set_page_config(page_title="English Tutor Chatbot", layout="centered")
140
- st.title("English Turtor Bot")
141
-
142
- # Initialize OpenAI LLM
143
- llm = ChatGoogleGenerativeAI(
144
- model="gemini-1.5-pro-latest",
145
- temperature=0.2, # Slightly higher for varied responses
146
- max_tokens=None,
147
- timeout=None,
148
- max_retries=2,
149
- )
150
-
151
- # Initialize embeddings using OpenAI
152
- embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2')
153
-
154
- def load_preprocessed_vectorstore():
155
- try:
156
- document = []
157
- dict1 = {}
158
- indx = 0
159
- for i in range(34,456):
160
- page = reader.pages[i]
161
- document.append(Document(page_content=page.extract_text()))
162
- text_splitter = RecursiveCharacterTextSplitter(
163
- separators=["\n\n", "\n", ". ", " ", ""],
164
- chunk_size=3000,
165
- chunk_overlap=1000)
166
-
167
- document_chunks = text_splitter.split_documents(document)
168
-
169
- vector_store = Chroma.from_documents(
170
-
171
- embedding=embeddings,
172
- documents=document_chunks,
173
- persist_directory="./data32"
174
- )
175
- return vector_store
176
- except Exception as e:
177
- st.error(f"Error creating vector store: {e}")
178
- return None
179
-
180
- def get_context_retriever_chain(vector_store):
181
- """Creates a history-aware retriever chain."""
182
- retriever = vector_store.as_retriever()
183
-
184
- # Define the prompt for the retriever chain
185
- prompt = ChatPromptTemplate.from_messages([
186
- MessagesPlaceholder(variable_name="chat_history"),
187
- ("human", "{input}"),
188
- ("system", """Given the chat history and the latest user question, which might reference context in the chat history,
189
- formulate a standalone question that can be understood without the chat history.
190
- If the question is directly addressed within the provided document, provide a relevant answer.
191
- If the question is not explicitly addressed in the document, return the following message:
192
- 'This question is beyond the scope of the available information. Please contact the mentor for further assistance.'
193
- Do NOT answer the question directly, just reformulate it if needed and otherwise return it as is.
194
- Also if the questions is about any irrelevent topic like politics, war, homosexuality, transgender etc just reply the following message:
195
- 'Please reframe your question'""")
196
- ])
197
-
198
- retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
199
- return retriever_chain
200
-
201
- def get_conversational_chain(retriever_chain):
202
- """Creates a conversational chain using the retriever chain."""
203
- prompt = ChatPromptTemplate.from_messages([
204
- ("system", """I am an advanced English tutor designed to help users improve their English language skills through interactive lessons, personalized feedback, and quizzes. Your goal is to enhance their vocabulary, grammar, reading comprehension, and speaking ability. You should adapt to their skill level and learning preferences."
205
-
206
- User Interaction Guidelines:
207
-
208
- Learning Mode: Begin by asking the user their current proficiency level (Beginner, Intermediate, Advanced) and their learning goals (e.g., improve vocabulary, master grammar, prepare for an exam, etc.).
209
- Interactive Lessons: Provide engaging lessons tailored to their level. Use examples, simple explanations, and interactive questions. For example:
210
- Beginner: "What's the plural form of 'apple'?"
211
- Advanced: "Explain the difference between 'affect' and 'effect' with examples."
212
- Quizzes: Offer quizzes after lessons to reinforce learning. Use multiple-choice, fill-in-the-blank, or open-ended questions. Adjust the difficulty based on user performance. Provide immediate feedback, explaining why an answer is correct or incorrect.
213
- Speaking Practice: If requested, simulate conversations and provide constructive feedback on grammar, vocabulary, and pronunciation.
214
- Example Response:
215
-
216
- Learning Mode Prompt: "Hello! I'm here to help you improve your English. Could you tell me about your current level and goals? For instance, do you want to focus on grammar, expand your vocabulary, or practice speaking?"
217
- Interactive Lesson Prompt: "Let's practice forming questions. Convert this statement into a question: 'She is reading a book.'"
218
- Quiz Prompt: "Choose the correct option: 'The cat ____ on the mat.'
219
- A) sit
220
- B) sits
221
- C) sitting
222
- D) siting"
223
- (Correct answer: B - 'sits')
224
- Adaptability Features:
225
-
226
- Respond dynamically to user input, simplifying explanations or increasing complexity as needed.
227
- Encourage and motivate users by celebrating their progress.
228
- Offer follow-up suggestions after quizzes, e.g., "You did well on vocabulary! Shall we try a grammar-focused session next?
229
- """
230
- "\n\n"
231
- "{context}"),
232
- MessagesPlaceholder(variable_name="chat_history"),
233
- ("human", "{input}")
234
- ])
235
-
236
- stuff_documents_chain = create_stuff_documents_chain(llm, prompt)
237
- return create_retrieval_chain(retriever_chain, stuff_documents_chain)
238
-
239
- def get_response(user_query):
240
- retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
241
- conversation_rag_chain = get_conversational_chain(retriever_chain)
242
-
243
- formatted_chat_history = []
244
- for message in st.session_state.chat_history:
245
- if isinstance(message, HumanMessage):
246
- formatted_chat_history.append({"author": "user", "content": message.content})
247
- elif isinstance(message, SystemMessage):
248
- formatted_chat_history.append({"author": "assistant", "content": message.content})
249
-
250
- response = conversation_rag_chain.invoke({
251
- "chat_history": formatted_chat_history,
252
- "input": user_query
253
- })
254
-
255
- return response['answer']
256
-
257
- # Load the preprocessed vector store from the local directory
258
- st.session_state.vector_store = load_preprocessed_vectorstore()
259
-
260
- # Initialize chat history if not present
261
- if "chat_history" not in st.session_state:
262
- st.session_state.chat_history = [
263
- {"author": "assistant", "content": "Hello, I am an English Tutor. How can I help you?"}
264
- ]
265
-
266
- # Main app logic
267
- if st.session_state.get("vector_store") is None:
268
- st.error("Failed to load preprocessed data. Please ensure the data exists in './data' directory.")
269
- else:
270
- # Display chat history
271
- with st.container():
272
- for message in st.session_state.chat_history:
273
- if message["author"] == "assistant":
274
- with st.chat_message("system"):
275
- st.write(message["content"])
276
- elif message["author"] == "user":
277
- with st.chat_message("human"):
278
- st.write(message["content"])
279
-
280
- # Add user input box below the chat
281
- with st.container():
282
- with st.form(key="chat_form", clear_on_submit=True):
283
- user_query = st.text_input("Type your message here...", key="user_input")
284
- submit_button = st.form_submit_button("Send")
285
-
286
- if submit_button and user_query:
287
- # Get bot response
288
- response = get_response(user_query)
289
- st.session_state.chat_history.append({"author": "user", "content": user_query})
290
- st.session_state.chat_history.append({"author": "assistant", "content": response})
291
-
292
- # Rerun the app to refresh the chat display
293
- st.rerun()
294
-
295
-
296
  """"""
 
1
+ from pypdf import PdfReader
2
+ from langchain.document_loaders import PyPDFLoader
3
+ from langchain.document_loaders import TextLoader
4
+ from langchain.document_loaders import Docx2txtLoader
5
+ from langchain.text_splitter import CharacterTextSplitter
6
+ from langchain.embeddings import HuggingFaceEmbeddings
7
+ from langchain.vectorstores import Chroma
8
+ from huggingface_hub import notebook_login
9
+ import torch
10
+ from langchain_google_genai import ChatGoogleGenerativeAI
11
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
12
+ from langchain_core.messages import HumanMessage, SystemMessage
13
+ from langchain.chains import create_history_aware_retriever, create_retrieval_chain
14
+ from langchain.chains.combine_documents import create_stuff_documents_chain
15
+ from dotenv import load_dotenv, dotenv_values
16
+ import os
17
+ load_dotenv()
18
+ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
19
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = str(HUGGINGFACEHUB_API_TOKEN)
20
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
21
+ os.environ["GOOGLE_API_KEY"] = str(GOOGLE_API_KEY)
22
+
23
+ reader = PdfReader("sound.pdf")
24
+
25
+ num_pages = len(reader.pages)
26
+
27
+ from langchain.schema.document import Document
28
+ document = []
29
+ dict1 = {}
30
+ indx = 0
31
+ for i in range(34,456):
32
+ page = reader.pages[i]
33
+ document.append(Document(page_content=page.extract_text()))
34
+
35
+
36
+ document_splitter=CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=100)
37
+ document_chunks=document_splitter.split_documents(document)
38
+ # sentence-transformers/all-mpnet-base-v
39
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
40
+ vectordb=Chroma.from_documents(document_chunks,embedding=embeddings, persist_directory='./data')
41
+ vectordb.persist()
42
+
43
+ llm = ChatGoogleGenerativeAI(
44
+ model="gemini-1.5-pro-latest",
45
+ temperature=0.2,
46
+ max_tokens=None,
47
+ timeout=None,
48
+ max_retries=2,
49
+ )
50
+
51
+ retriever = vectordb.as_retriever()
52
+
53
+ prompt = ChatPromptTemplate.from_messages([
54
+ MessagesPlaceholder(variable_name="chat_history"),
55
+ ("human", "{input}"),
56
+ ("system", """Given the chat history and the latest user question, which might reference context in the chat history,
57
+ formulate a standalone question that can be understood without the chat history.
58
+ If the question is directly addressed within the provided document, provide a relevant answer.
59
+ If the question is not explicitly addressed in the document, return the following message:
60
+ 'This question is beyond the scope of the available information. Please contact the mentor for further assistance.'
61
+ Do NOT answer the question directly, just reformulate it if needed and otherwise return it as is.
62
+ Also if the questions is about any irrelevent topic like politics, war, homosexuality, transgender etc just reply the following message:
63
+ 'Please reframe your question'""")
64
+ ])
65
+ retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
66
+
67
+ prompt = ChatPromptTemplate.from_messages([
68
+ ("system", """I am an advanced English tutor designed to help users improve their English language skills through interactive lessons, personalized feedback, and quizzes. Your goal is to enhance their vocabulary, grammar, reading comprehension, and speaking ability. You should adapt to their skill level and learning preferences."
69
+
70
+ User Interaction Guidelines:
71
+
72
+ Learning Mode: Begin by asking the user their current proficiency level (Beginner, Intermediate, Advanced) and their learning goals (e.g., improve vocabulary, master grammar, prepare for an exam, etc.).
73
+ Interactive Lessons: Provide engaging lessons tailored to their level. Use examples, simple explanations, and interactive questions. For example:
74
+ Beginner: "What's the plural form of 'apple'?"
75
+ Advanced: "Explain the difference between 'affect' and 'effect' with examples."
76
+ Quizzes: Offer quizzes after lessons to reinforce learning. Use multiple-choice, fill-in-the-blank, or open-ended questions. Adjust the difficulty based on user performance. Provide immediate feedback, explaining why an answer is correct or incorrect.
77
+ Speaking Practice: If requested, simulate conversations and provide constructive feedback on grammar, vocabulary, and pronunciation.
78
+ Example Response:
79
+
80
+ Learning Mode Prompt: "Hello! I'm here to help you improve your English. Could you tell me about your current level and goals? For instance, do you want to focus on grammar, expand your vocabulary, or practice speaking?"
81
+ Interactive Lesson Prompt: "Let's practice forming questions. Convert this statement into a question: 'She is reading a book.'"
82
+ Quiz Prompt: "Choose the correct option: 'The cat ____ on the mat.'
83
+ A) sit
84
+ B) sits
85
+ C) sitting
86
+ D) siting"
87
+ (Correct answer: B - 'sits')
88
+ Adaptability Features:
89
+
90
+ Respond dynamically to user input, simplifying explanations or increasing complexity as needed.
91
+ Encourage and motivate users by celebrating their progress.
92
+ Offer follow-up suggestions after quizzes, e.g., "You did well on vocabulary! Shall we try a grammar-focused session next?
93
+ """
94
+ "\n\n"
95
+ "{context}"),
96
+ MessagesPlaceholder(variable_name="chat_history"),
97
+ ("human", "{input}")
98
+ ])
99
+
100
+ stuff_documents_chain = create_stuff_documents_chain(llm, prompt)
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+ import os
109
+ import streamlit as st
110
+ import google.generativeai as genai
111
+ # from langchain_openai import OpenAI /
112
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings
113
+ from langchain_google_genai import ChatGoogleGenerativeAI
114
+ # from langchain_openai import OpenAIEmbeddings
115
+ from langchain_community.document_loaders import Docx2txtLoader
116
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
117
+ from langchain_community.vectorstores import Chroma
118
+ from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
119
+ from langchain_core.messages import HumanMessage, SystemMessage
120
+ from langchain.chains import create_history_aware_retriever, create_retrieval_chain
121
+ from langchain.chains.combine_documents import create_stuff_documents_chain
122
+ from dotenv import load_dotenv
123
+ from langchain.embeddings import HuggingFaceEmbeddings
124
+ import pysqlite3
125
+ import sys
126
+ sys.modules['sqlite3'] = pysqlite3
127
+
128
+ import os
129
+ os.environ["TRANSFORMERS_OFFLINE"] = "1"
130
+ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN ")
131
+ os.environ['HUGGINGFACEHUB_API_TOKEN'] = HUGGINGFACEHUB_API_TOKEN
132
+ load_dotenv()
133
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
134
+ os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
135
+
136
+ if not GOOGLE_API_KEY:
137
+ raise ValueError("Gemini API key not found. Please set it in the .env file.")
138
+
139
+ st.set_page_config(page_title="English Tutor Chatbot", layout="centered")
140
+ st.title("English Turtor Bot")
141
+
142
+ # Initialize OpenAI LLM
143
+ llm = ChatGoogleGenerativeAI(
144
+ model="gemini-1.5-pro-latest",
145
+ temperature=0.2, # Slightly higher for varied responses
146
+ max_tokens=None,
147
+ timeout=None,
148
+ max_retries=2,
149
+ )
150
+
151
+ # Initialize embeddings using OpenAI
152
+ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-base-v2')
153
+
154
+ def load_preprocessed_vectorstore():
155
+ try:
156
+ document = []
157
+ dict1 = {}
158
+ indx = 0
159
+ for i in range(34,456):
160
+ page = reader.pages[i]
161
+ document.append(Document(page_content=page.extract_text()))
162
+ text_splitter = RecursiveCharacterTextSplitter(
163
+ separators=["\n\n", "\n", ". ", " ", ""],
164
+ chunk_size=3000,
165
+ chunk_overlap=1000)
166
+
167
+ document_chunks = text_splitter.split_documents(document)
168
+
169
+ vector_store = Chroma.from_documents(
170
+
171
+ embedding=embeddings,
172
+ documents=document_chunks,
173
+ persist_directory="./data32"
174
+ )
175
+ return vector_store
176
+ except Exception as e:
177
+ st.error(f"Error creating vector store: {e}")
178
+ return None
179
+
180
+ def get_context_retriever_chain(vector_store):
181
+ """Creates a history-aware retriever chain."""
182
+ retriever = vector_store.as_retriever()
183
+
184
+ # Define the prompt for the retriever chain
185
+ prompt = ChatPromptTemplate.from_messages([
186
+ MessagesPlaceholder(variable_name="chat_history"),
187
+ ("human", "{input}"),
188
+ ("system", """Given the chat history and the latest user question, which might reference context in the chat history,
189
+ formulate a standalone question that can be understood without the chat history.
190
+ If the question is directly addressed within the provided document, provide a relevant answer.
191
+ If the question is not explicitly addressed in the document, return the following message:
192
+ 'This question is beyond the scope of the available information. Please contact the mentor for further assistance.'
193
+ Do NOT answer the question directly, just reformulate it if needed and otherwise return it as is.
194
+ Also if the questions is about any irrelevent topic like politics, war, homosexuality, transgender etc just reply the following message:
195
+ 'Please reframe your question'""")
196
+ ])
197
+
198
+ retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
199
+ return retriever_chain
200
+
201
+ def get_conversational_chain(retriever_chain):
202
+ """Creates a conversational chain using the retriever chain."""
203
+ prompt = ChatPromptTemplate.from_messages([
204
+ ("system", """I am an advanced English tutor designed to help users improve their English language skills through interactive lessons, personalized feedback, and quizzes. Your goal is to enhance their vocabulary, grammar, reading comprehension, and speaking ability. You should adapt to their skill level and learning preferences."
205
+
206
+ User Interaction Guidelines:
207
+
208
+ Learning Mode: Begin by asking the user their current proficiency level (Beginner, Intermediate, Advanced) and their learning goals (e.g., improve vocabulary, master grammar, prepare for an exam, etc.).
209
+ Interactive Lessons: Provide engaging lessons tailored to their level. Use examples, simple explanations, and interactive questions. For example:
210
+ Beginner: "What's the plural form of 'apple'?"
211
+ Advanced: "Explain the difference between 'affect' and 'effect' with examples."
212
+ Quizzes: Offer quizzes after lessons to reinforce learning. Use multiple-choice, fill-in-the-blank, or open-ended questions. Adjust the difficulty based on user performance. Provide immediate feedback, explaining why an answer is correct or incorrect.
213
+ Speaking Practice: If requested, simulate conversations and provide constructive feedback on grammar, vocabulary, and pronunciation.
214
+ Example Response:
215
+
216
+ Learning Mode Prompt: "Hello! I'm here to help you improve your English. Could you tell me about your current level and goals? For instance, do you want to focus on grammar, expand your vocabulary, or practice speaking?"
217
+ Interactive Lesson Prompt: "Let's practice forming questions. Convert this statement into a question: 'She is reading a book.'"
218
+ Quiz Prompt: "Choose the correct option: 'The cat ____ on the mat.'
219
+ A) sit
220
+ B) sits
221
+ C) sitting
222
+ D) siting"
223
+ (Correct answer: B - 'sits')
224
+ Adaptability Features:
225
+
226
+ Respond dynamically to user input, simplifying explanations or increasing complexity as needed.
227
+ Encourage and motivate users by celebrating their progress.
228
+ Offer follow-up suggestions after quizzes, e.g., "You did well on vocabulary! Shall we try a grammar-focused session next?
229
+ """
230
+ "\n\n"
231
+ "{context}"),
232
+ MessagesPlaceholder(variable_name="chat_history"),
233
+ ("human", "{input}")
234
+ ])
235
+
236
+ stuff_documents_chain = create_stuff_documents_chain(llm, prompt)
237
+ return create_retrieval_chain(retriever_chain, stuff_documents_chain)
238
+
239
+ def get_response(user_query):
240
+ retriever_chain = get_context_retriever_chain(st.session_state.vector_store)
241
+ conversation_rag_chain = get_conversational_chain(retriever_chain)
242
+
243
+ formatted_chat_history = []
244
+ for message in st.session_state.chat_history:
245
+ if isinstance(message, HumanMessage):
246
+ formatted_chat_history.append({"author": "user", "content": message.content})
247
+ elif isinstance(message, SystemMessage):
248
+ formatted_chat_history.append({"author": "assistant", "content": message.content})
249
+
250
+ response = conversation_rag_chain.invoke({
251
+ "chat_history": formatted_chat_history,
252
+ "input": user_query
253
+ })
254
+
255
+ return response['answer']
256
+
257
+ # Load the preprocessed vector store from the local directory
258
+ st.session_state.vector_store = load_preprocessed_vectorstore()
259
+
260
+ # Initialize chat history if not present
261
+ if "chat_history" not in st.session_state:
262
+ st.session_state.chat_history = [
263
+ {"author": "assistant", "content": "Hello, I am an English Tutor. How can I help you?"}
264
+ ]
265
+
266
+ # Main app logic
267
+ if st.session_state.get("vector_store") is None:
268
+ st.error("Failed to load preprocessed data. Please ensure the data exists in './data' directory.")
269
+ else:
270
+ # Display chat history
271
+ with st.container():
272
+ for message in st.session_state.chat_history:
273
+ if message["author"] == "assistant":
274
+ with st.chat_message("system"):
275
+ st.write(message["content"])
276
+ elif message["author"] == "user":
277
+ with st.chat_message("human"):
278
+ st.write(message["content"])
279
+
280
+ # Add user input box below the chat
281
+ with st.container():
282
+ with st.form(key="chat_form", clear_on_submit=True):
283
+ user_query = st.text_input("Type your message here...", key="user_input")
284
+ submit_button = st.form_submit_button("Send")
285
+
286
+ if submit_button and user_query:
287
+ # Get bot response
288
+ response = get_response(user_query)
289
+ st.session_state.chat_history.append({"author": "user", "content": user_query})
290
+ st.session_state.chat_history.append({"author": "assistant", "content": response})
291
+
292
+ # Rerun the app to refresh the chat display
293
+ st.rerun()
294
+
295
+
296
  """"""