Spaces:
Runtime error
Runtime error
Upload file
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from edubot import EduBotCreator
|
| 2 |
+
from config import *
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from streamlit_chat import message
|
| 5 |
+
|
| 6 |
+
@st.cache_resource(show_spinner=True)
|
| 7 |
+
def create_edubot():
|
| 8 |
+
edubotcreator = EduBotCreator()
|
| 9 |
+
edubot = edubotcreator.create_edubot()
|
| 10 |
+
return edubot
|
| 11 |
+
edubot = create_edubot()
|
| 12 |
+
|
| 13 |
+
def infer_edubot(prompt):
|
| 14 |
+
model_out = edubot(prompt)
|
| 15 |
+
answer = model_out['result']
|
| 16 |
+
return answer
|
| 17 |
+
|
| 18 |
+
def display_conversation(history):
|
| 19 |
+
for i in range(len(history["assistant"])):
|
| 20 |
+
message(history["user"][i], is_user=True, key=str(i) + "_user")
|
| 21 |
+
message(history["assistant"][i],key=str(i))
|
| 22 |
+
|
| 23 |
+
def main():
|
| 24 |
+
|
| 25 |
+
st.title("Edubot: Your Smart Education Sidekick 📚🤖")
|
| 26 |
+
st.subheader("A bot created using Langchain 🦜 to run on cpu making your learning process easier")
|
| 27 |
+
|
| 28 |
+
user_input = st.text_input("Enter your query")
|
| 29 |
+
|
| 30 |
+
if "assistant" not in st.session_state:
|
| 31 |
+
st.session_state["assistant"] = ["I am ready to help you"]
|
| 32 |
+
if "user" not in st.session_state:
|
| 33 |
+
st.session_state["user"] = ["Hey there!"]
|
| 34 |
+
|
| 35 |
+
if st.button("Answer"):
|
| 36 |
+
|
| 37 |
+
answer = infer_edubot({'query': user_input})
|
| 38 |
+
st.session_state["user"].append(user_input)
|
| 39 |
+
st.session_state["assistant"].append(answer)
|
| 40 |
+
|
| 41 |
+
if st.session_state["assistant"]:
|
| 42 |
+
display_conversation(st.session_state)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
main()
|
| 46 |
+
|
| 47 |
+
|
config.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
DATA_DIR_PATH = "data/"
|
| 2 |
+
VECTOR_DB_PATH = "faiss/education"
|
| 3 |
+
CHUNK_SIZE = 500
|
| 4 |
+
CHUNK_OVERLAP = 200
|
| 5 |
+
EMBEDDER = "thenlper/gte-large"
|
| 6 |
+
DEVICE = "cpu"
|
| 7 |
+
PROMPT_TEMPLATE = '''
|
| 8 |
+
With the information provided try to answer the question.
|
| 9 |
+
If you cant answer the question based on the information either say you cant find an answer or unable to find an answer.
|
| 10 |
+
So try to understand in depth about the context and answer only based on the information provided. Dont generate irrelevant answers
|
| 11 |
+
|
| 12 |
+
Context: {context}
|
| 13 |
+
Question: {question}
|
| 14 |
+
Do provide only helpful answers
|
| 15 |
+
|
| 16 |
+
Helpful answer:
|
| 17 |
+
'''
|
| 18 |
+
INP_VARS = ['context', 'question']
|
| 19 |
+
CHAIN_TYPE = "stuff"
|
| 20 |
+
SEARCH_KWARGS = {'k': 2}
|
| 21 |
+
MODEL_CKPT = "res/llama-2-7b-chat.ggmlv3.q4_1.bin"
|
| 22 |
+
MODEL_TYPE = "llama"
|
| 23 |
+
MAX_NEW_TOKENS = 512
|
| 24 |
+
TEMPERATURE = 0.9
|
edubot.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain import PromptTemplate
|
| 2 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 3 |
+
from langchain.vectorstores import FAISS
|
| 4 |
+
from langchain.llms import CTransformers
|
| 5 |
+
from langchain.chains import RetrievalQA
|
| 6 |
+
from config import *
|
| 7 |
+
|
| 8 |
+
class EduBotCreator:
|
| 9 |
+
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.prompt_temp = PROMPT_TEMPLATE
|
| 12 |
+
self.input_variables = INP_VARS
|
| 13 |
+
self.chain_type = CHAIN_TYPE
|
| 14 |
+
self.search_kwargs = SEARCH_KWARGS
|
| 15 |
+
self.embedder = EMBEDDER
|
| 16 |
+
self.vector_db_path = VECTOR_DB_PATH
|
| 17 |
+
self.model_ckpt = MODEL_CKPT
|
| 18 |
+
self.model_type = MODEL_TYPE
|
| 19 |
+
self.max_new_tokens = MAX_NEW_TOKENS
|
| 20 |
+
self.temperature = TEMPERATURE
|
| 21 |
+
|
| 22 |
+
def create_custom_prompt(self):
|
| 23 |
+
custom_prompt_temp = PromptTemplate(template=self.prompt_temp,
|
| 24 |
+
input_variables=self.input_variables)
|
| 25 |
+
return custom_prompt_temp
|
| 26 |
+
|
| 27 |
+
def load_llm(self):
|
| 28 |
+
llm = CTransformers(
|
| 29 |
+
model = self.model_ckpt,
|
| 30 |
+
model_type=self.model_type,
|
| 31 |
+
max_new_tokens = self.max_new_tokens,
|
| 32 |
+
temperature = self.temperature
|
| 33 |
+
)
|
| 34 |
+
return llm
|
| 35 |
+
|
| 36 |
+
def load_vectordb(self):
|
| 37 |
+
hfembeddings = HuggingFaceEmbeddings(
|
| 38 |
+
model_name=self.embedder,
|
| 39 |
+
model_kwargs={'device': 'cpu'}
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
vector_db = FAISS.load_local(self.vector_db_path, hfembeddings)
|
| 43 |
+
return vector_db
|
| 44 |
+
|
| 45 |
+
def create_bot(self, custom_prompt, vectordb, llm):
|
| 46 |
+
retrieval_qa_chain = RetrievalQA.from_chain_type(
|
| 47 |
+
llm=llm,
|
| 48 |
+
chain_type=self.chain_type,
|
| 49 |
+
retriever=vectordb.as_retriever(search_kwargs=self.search_kwargs),
|
| 50 |
+
return_source_documents=True,
|
| 51 |
+
chain_type_kwargs={"prompt": custom_prompt}
|
| 52 |
+
)
|
| 53 |
+
return retrieval_qa_chain
|
| 54 |
+
|
| 55 |
+
def create_edubot(self):
|
| 56 |
+
self.custom_prompt = self.create_custom_prompt()
|
| 57 |
+
self.vector_db = self.load_vectordb()
|
| 58 |
+
self.llm = self.load_llm()
|
| 59 |
+
self.bot = self.create_bot(self.custom_prompt, self.vector_db, self.llm)
|
| 60 |
+
return self.bot
|
vector_db.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 2 |
+
from langchain.vectorstores import FAISS
|
| 3 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from config import *
|
| 6 |
+
|
| 7 |
+
def faiss_vector_db():
|
| 8 |
+
|
| 9 |
+
dir_loader = DirectoryLoader(
|
| 10 |
+
DATA_DIR_PATH,
|
| 11 |
+
glob='*.pdf',
|
| 12 |
+
loader_cls=PyPDFLoader
|
| 13 |
+
)
|
| 14 |
+
docs = dir_loader.load()
|
| 15 |
+
print("PDFs Loaded")
|
| 16 |
+
|
| 17 |
+
txt_splitter = RecursiveCharacterTextSplitter(
|
| 18 |
+
chunk_size=CHUNK_SIZE,
|
| 19 |
+
chunk_overlap=CHUNK_OVERLAP
|
| 20 |
+
)
|
| 21 |
+
inp_txt = txt_splitter.split_documents(docs)
|
| 22 |
+
print("Data Chunks Created")
|
| 23 |
+
|
| 24 |
+
hfembeddings = HuggingFaceEmbeddings(
|
| 25 |
+
model_name=EMBEDDER,
|
| 26 |
+
model_kwargs={'device': 'cpu'}
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
db = FAISS.from_documents(inp_txt, hfembeddings)
|
| 30 |
+
db.save_local(VECTOR_DB_PATH)
|
| 31 |
+
print("Vector Store Creation Completed")
|
| 32 |
+
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
faiss_vector_db()
|