Test / app.py
Namitg02's picture
Update app.py
f4c65b4 verified
raw
history blame
2.25 kB
#from langchain_community.document_loaders import PyPDFLoader
from datasets import load_dataset
dataset = load_dataset("Namitg02/Test")
print(dataset)
from langchain.docstore.document import Document as LangchainDocument
#RAW_KNOWLEDGE_BASE = [LangchainDocument(page_content=["dataset"])]
from langchain.text_splitter import RecursiveCharacterTextSplitter
splitter = RecursiveCharacterTextSplitter(chunk_size=100, chunk_overlap=15,separators=["\n\n", "\n", " ", ""])
#docs = splitter.split_documents(RAW_KNOWLEDGE_BASE)
docs = splitter.create_documents(str(dataset))
from langchain_community.embeddings import HuggingFaceEmbeddings
embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# embeddings = embedding_model.encode(docs)
from langchain_community.vectorstores import Chroma
persist_directory = 'docs/chroma/'
vectordb = Chroma.from_documents(
documents=docs,
embedding=embedding_model,
persist_directory=persist_directory
)
retriever = vectordb.as_retriever()
#docs_ss = vectordb.similarity_search(question,k=3)
#qa_chain = RetrievalQA.from_chain_type(
# models/HuggingFaceH4/zephyr-7b-beta,
# retriever=vectordb.as_retriever()
#)
from langchain.prompts import PromptTemplate
template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. Use three sentences maximum. Keep the answer as concise as possible. Always say "thanks for asking!" at the end of the answer.
{You are a helpful dietician}
Question: {question}
Helpful Answer:"""
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True
)
question = "Can I reverse Diabetes?"
print("template")
from langchain.chains import ConversationalRetrievalChain
retriever=vectordb.as_retriever()
READER_MODEL = "HuggingFaceH4/zephyr-7b-beta"
qa = ConversationalRetrievalChain.from_llm(llm=READER_MODEL,retriever=retriever,memory=memory,chain_type_kwargs={"prompt": QA_CHAIN_PROMPT})
import gradio as gr
gr.load("READER_MODEL").launch()
#result = ({"query": question})
print("qa")