ak0601 commited on
Commit
31ea1c1
·
verified ·
1 Parent(s): 2e7548b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -113
app.py CHANGED
@@ -13,124 +13,13 @@ 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
  HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
18
  os.environ['HUGGINGFACEHUB_API_TOKEN'] = HUGGINGFACEHUB_API_TOKEN
19
  GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
20
  os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
21
 
22
- reader = PdfReader("sound.pdf")
23
-
24
- num_pages = len(reader.pages)
25
-
26
- from langchain.schema.document import Document
27
- document = []
28
- dict1 = {}
29
- indx = 0
30
- for i in range(34,456):
31
- page = reader.pages[i]
32
- document.append(Document(page_content=page.extract_text()))
33
-
34
-
35
- document_splitter=CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=100)
36
- document_chunks=document_splitter.split_documents(document)
37
- # sentence-transformers/all-mpnet-base-v
38
- embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
39
- vectordb=Chroma.from_documents(document_chunks,embedding=embeddings, persist_directory='./data')
40
- vectordb.persist()
41
-
42
- llm = ChatGoogleGenerativeAI(
43
- model="gemini-1.5-pro-latest",
44
- temperature=0.2,
45
- max_tokens=None,
46
- timeout=None,
47
- max_retries=2,
48
- )
49
-
50
- retriever = vectordb.as_retriever()
51
-
52
- prompt = ChatPromptTemplate.from_messages([
53
- MessagesPlaceholder(variable_name="chat_history"),
54
- ("human", "{input}"),
55
- ("system", """Given the chat history and the latest user question, which might reference context in the chat history,
56
- formulate a standalone question that can be understood without the chat history.
57
- If the question is directly addressed within the provided document, provide a relevant answer.
58
- If the question is not explicitly addressed in the document, return the following message:
59
- 'This question is beyond the scope of the available information. Please contact the mentor for further assistance.'
60
- Do NOT answer the question directly, just reformulate it if needed and otherwise return it as is.
61
- Also if the questions is about any irrelevent topic like politics, war, homosexuality, transgender etc just reply the following message:
62
- 'Please reframe your question'""")
63
- ])
64
- retriever_chain = create_history_aware_retriever(llm, retriever, prompt)
65
-
66
- prompt = ChatPromptTemplate.from_messages([
67
- ("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."
68
-
69
- User Interaction Guidelines:
70
-
71
- 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.).
72
- Interactive Lessons: Provide engaging lessons tailored to their level. Use examples, simple explanations, and interactive questions. For example:
73
- Beginner: "What's the plural form of 'apple'?"
74
- Advanced: "Explain the difference between 'affect' and 'effect' with examples."
75
- 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.
76
- Speaking Practice: If requested, simulate conversations and provide constructive feedback on grammar, vocabulary, and pronunciation.
77
- Example Response:
78
-
79
- 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?"
80
- Interactive Lesson Prompt: "Let's practice forming questions. Convert this statement into a question: 'She is reading a book.'"
81
- Quiz Prompt: "Choose the correct option: 'The cat ____ on the mat.'
82
- A) sit
83
- B) sits
84
- C) sitting
85
- D) siting"
86
- (Correct answer: B - 'sits')
87
- Adaptability Features:
88
-
89
- Respond dynamically to user input, simplifying explanations or increasing complexity as needed.
90
- Encourage and motivate users by celebrating their progress.
91
- Offer follow-up suggestions after quizzes, e.g., "You did well on vocabulary! Shall we try a grammar-focused session next?
92
- """
93
- "\n\n"
94
- "{context}"),
95
- MessagesPlaceholder(variable_name="chat_history"),
96
- ("human", "{input}")
97
- ])
98
-
99
- stuff_documents_chain = create_stuff_documents_chain(llm, prompt)
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
- import os
108
- import streamlit as st
109
- import google.generativeai as genai
110
- # from langchain_openai import OpenAI /
111
- from langchain_google_genai import GoogleGenerativeAIEmbeddings
112
- from langchain_google_genai import ChatGoogleGenerativeAI
113
- # from langchain_openai import OpenAIEmbeddings
114
- from langchain_community.document_loaders import Docx2txtLoader
115
- from langchain.text_splitter import RecursiveCharacterTextSplitter
116
- from langchain_community.vectorstores import Chroma
117
- from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
118
- from langchain_core.messages import HumanMessage, SystemMessage
119
- from langchain.chains import create_history_aware_retriever, create_retrieval_chain
120
- from langchain.chains.combine_documents import create_stuff_documents_chain
121
- from dotenv import load_dotenv
122
- from langchain.embeddings import HuggingFaceEmbeddings
123
- import pysqlite3
124
- import sys
125
- sys.modules['sqlite3'] = pysqlite3
126
-
127
- import os
128
- os.environ["TRANSFORMERS_OFFLINE"] = "1"
129
- HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN ")
130
- os.environ['HUGGINGFACEHUB_API_TOKEN'] = HUGGINGFACEHUB_API_TOKEN
131
- load_dotenv()
132
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
133
- os.environ["GOOGLE_API_KEY"] = GOOGLE_API_KEY
134
 
135
  if not GOOGLE_API_KEY:
136
  raise ValueError("Gemini API key not found. Please set it in the .env file.")
@@ -152,6 +41,9 @@ embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-mpnet-b
152
 
153
  def load_preprocessed_vectorstore():
154
  try:
 
 
 
155
  document = []
156
  dict1 = {}
157
  indx = 0
@@ -278,7 +170,7 @@ else:
278
 
279
  # Add user input box below the chat
280
  with st.container():
281
- with st.form(key="chat_form", clear_on_submit=True):
282
  user_query = st.text_input("Type your message here...", key="user_input")
283
  submit_button = st.form_submit_button("Send")
284
 
 
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
+ from langchain.schema.document import Document
17
  import os
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
 
24
  if not GOOGLE_API_KEY:
25
  raise ValueError("Gemini API key not found. Please set it in the .env file.")
 
41
 
42
  def load_preprocessed_vectorstore():
43
  try:
44
+ reader = PdfReader("sound.pdf")
45
+ num_pages = len(reader.pages)
46
+
47
  document = []
48
  dict1 = {}
49
  indx = 0
 
170
 
171
  # Add user input box below the chat
172
  with st.container():
173
+ with st.form(key="chat_form"):
174
  user_query = st.text_input("Type your message here...", key="user_input")
175
  submit_button = st.form_submit_button("Send")
176