Spaces:
Sleeping
Sleeping
Update model2.py
#1
by Neha13 - opened
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134import osimport torchfrom langchain.text_splitter import CharacterTextSplitterfrom langchain_huggingface import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain.memory import ConversationBufferMemoryfrom langchain.prompts import PromptTemplatefrom langchain.chains import RetrievalQAfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain_groq import ChatGroqclass ChatbotModel:def __init__(self):# Initialize the environment variable for the GROQ API Keyos.environ["GROQ_API_KEY"] = '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'# Load documents from PDFspdf_folder_path = "acpc_data"documents = []for file in os.listdir(pdf_folder_path):if file.endswith('.pdf'):pdf_path = os.path.join(pdf_folder_path, file)loader = PyPDFLoader(pdf_path)documents.extend(loader.load())# Initialize embeddingsself.embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",model_kwargs={'device': 'cpu'},encode_kwargs={'normalize_embeddings': True})# Split documents into chunksself.text_splitter = CharacterTextSplitter(separator="\n",chunk_size=1200,chunk_overlap=500,length_function=len)self.text_chunks = self.text_splitter.split_documents(documents)# Create FAISS vector storeself.db1 = FAISS.from_documents(self.text_chunks, self.embeddings)# Initialize memory for conversationself.memory = ConversationBufferMemory(memory_key="history", input_key="question")# Initialize the chat modelself.llm = ChatGroq(model="llama-3.1-8b-instant",temperature=0.655,max_tokens=None,timeout=None,max_retries=2,)# Create the QA chain prompt templateself.template = """You are a smart and helpful assistant for the ACPC counseling process. You guide students and solve their queries related to ACPC, MYSY scholarship, admission, etc. You will be given the student's query and the history of the chat, and you need to answer the query to the best of your knowledge. If the query is completely different from the context then tell the student that you are not made to answer this query in a polite language. If the student has included any type of content related to violence, sex, drugs or used abusive language then tell the student that you can not answer that query and request them not to use such content.Also make sure to reply in the same language as used by the student in the current query.NOTE that your answer should be accurate. Explain the answer such that a student with no idea about the ACPC can understand well.For example,Example 1Chat history:The student named Priya says hello.Question:What is the maximum size of passport size photo allowed?Answer:The maximum size of passport size photo allowed is 200 KB.{context}------Chat history :{history}------Question: {question}Answer:"""self.QA_CHAIN_PROMPT = PromptTemplate(input_variables=["history", "context", "question"],template=self.template)self.qa_chain = RetrievalQA.from_chain_type(self.llm,retriever=self.db1.as_retriever(),chain_type='stuff',verbose=True,chain_type_kwargs={"verbose": True, "prompt": self.QA_CHAIN_PROMPT,"memory": self.memory})def save(self, path):# Save only the necessary parameterstorch.save({'text_chunks': self.text_chunks,'embeddings_model_name': self.embeddings.model_name, # Save the model name'faiss_index': self.db1.index # Save FAISS index if needed}, path)def get_response(self, user_input):# Call the QA chain with the user's inputresult = self.qa_chain({"query": user_input})return result["result"]@classmethoddef load(cls, path):# Load the model statestate = torch.load(path)chatbot_model = cls()# Restore other componentschatbot_model.text_chunks = state['text_chunks']# Recreate embeddings using the saved model namechatbot_model.embeddings = HuggingFaceEmbeddings(model_name=state['embeddings_model_name'],model_kwargs={'device': 'cpu'},encode_kwargs={'normalize_embeddings': True})# Recreate FAISS index if necessarychatbot_model.db1 = FAISS.from_documents(chatbot_model.text_chunks, chatbot_model.embeddings)return chatbot_model# Test saving the modelif __name__ == "__main__":chatbot = ChatbotModel()chatbot.save("model.pt")print("Model saved successfully.")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import osimport torchfrom langchain.text_splitter import CharacterTextSplitterfrom langchain_huggingface import HuggingFaceEmbeddingsfrom langchain_community.vectorstores import FAISSfrom langchain.memory import ConversationBufferMemoryfrom langchain.prompts import PromptTemplatefrom langchain.chains import RetrievalQAfrom langchain_community.document_loaders import PyPDFLoaderfrom langchain_groq import ChatGroqclass ChatbotModel:def __init__(self):# Initialize the environment variable for the GROQ API Keyos.environ["GROQ_API_KEY"] = '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'# Load documents from PDFspdf_folder_path = "acpc_data"documents = []for file in os.listdir(pdf_folder_path):if file.endswith('.pdf'):pdf_path = os.path.join(pdf_folder_path, file)loader = PyPDFLoader(pdf_path)documents.extend(loader.load())# Initialize embeddingsself.embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2",model_kwargs={'device': 'cpu'},encode_kwargs={'normalize_embeddings': True})# Split documents into chunksself.text_splitter = CharacterTextSplitter(separator="\n",chunk_size=1200,chunk_overlap=500,length_function=len)self.text_chunks = self.text_splitter.split_documents(documents)# Create FAISS vector storeself.db1 = FAISS.from_documents(self.text_chunks, self.embeddings)# Initialize memory for conversationself.memory = ConversationBufferMemory(memory_key="history", input_key="question")# Initialize the chat modelself.llm = ChatGroq(model="llama-3.1-8b-instant",temperature=0.655,max_tokens=None,timeout=None,max_retries=2,)# Create the QA chain prompt templateself.template = """You are a smart and helpful assistant for the ACPC counseling process. You guide students and solve their queries related to ACPC, MYSY scholarship, admission, etc. You will be given the student's query and the history of the chat, and you need to answer the query to the best of your knowledge. If the query is completely different from the context then tell the student that you are not made to answer this query in a polite language. If the student has included any type of content related to violence, sex, drugs or used abusive language then tell the student that you can not answer that query and request them not to use such content.Also make sure to reply in the same language as used by the student in the current query.NOTE that your answer should be accurate. Explain the answer such that a student with no idea about the ACPC can understand well.For example,Example 1Chat history:Question:What is the maximum size of passport size photo allowed?Answer:The maximum size of passport size photo allowed is 200 KB.{context}------Chat history :{history}------Question: {question}Answer:"""self.QA_CHAIN_PROMPT = PromptTemplate(input_variables=["history", "context", "question"],template=self.template)self.qa_chain = RetrievalQA.from_chain_type(self.llm,retriever=self.db1.as_retriever(),chain_type='stuff',verbose=True,chain_type_kwargs={"verbose": True, "prompt": self.QA_CHAIN_PROMPT,"memory": self.memory})def save(self, path):# Save only the necessary parameterstorch.save({'text_chunks': self.text_chunks,'embeddings_model_name': self.embeddings.model_name, # Save the model name'faiss_index': self.db1.index # Save FAISS index if needed}, path)def get_response(self, user_input):# Call the QA chain with the user's inputresult = self.qa_chain({"query": user_input})return result["result"]@classmethoddef load(cls, path):# Load the model statestate = torch.load(path)chatbot_model = cls()# Restore other componentschatbot_model.text_chunks = state['text_chunks']# Recreate embeddings using the saved model namechatbot_model.embeddings = HuggingFaceEmbeddings(model_name=state['embeddings_model_name'],model_kwargs={'device': 'cpu'},encode_kwargs={'normalize_embeddings': True})# Recreate FAISS index if necessarychatbot_model.db1 = FAISS.from_documents(chatbot_model.text_chunks, chatbot_model.embeddings)return chatbot_model# Test saving the modelif __name__ == "__main__":chatbot = ChatbotModel()chatbot.save("model.pt")print("Model saved successfully.")
model2.py+133-134