Spaces:
Sleeping
Sleeping
import streamlit as st | |
from langchain_core.runnables import RunnablePassthrough | |
from langchain_core.output_parsers import StrOutputParser | |
from langchain.prompts import ChatPromptTemplate | |
from PyPDF2 import PdfReader | |
from langchain_text_splitters import RecursiveCharacterTextSplitter | |
import os | |
from langchain_community.vectorstores import Chroma | |
from langchain.chains.question_answering import load_qa_chain | |
from langchain.prompts import PromptTemplate | |
from langchain_community.document_loaders import PyPDFLoader | |
from langchain_chroma import Chroma | |
from langchain_community.embeddings import HuggingFaceEmbeddings | |
from transformers import pipeline | |
def get_pdf(pdf_docs): | |
docs=[] | |
for pdf in pdf_docs: | |
temp_file = "./temp.pdf" | |
# Delete the existing temp.pdf file if it exists | |
if os.path.exists(temp_file): | |
os.remove(temp_file) | |
with open(temp_file, "wb") as file: | |
file.write(pdf.getvalue()) | |
file_name = pdf.name | |
loader = PyPDFLoader(temp_file) | |
docs.extend(loader.load()) | |
return docs | |
def text_splitter(text): | |
text_splitter = RecursiveCharacterTextSplitter( | |
# Set a really small chunk size, just to show. | |
chunk_size=10000, | |
chunk_overlap=500, | |
separators=["\n\n","\n"," ",".",","]) | |
chunks=text_splitter.split_documents(text) | |
return chunks | |
def get_conversational_chain(retriever): | |
prompt_template = """ | |
Given the following extracted parts of a long document and a question, create a final answer. | |
Answer the question as detailed as possible from the provided context, make sure to provide all the details, if the answer is not in | |
provided context just say, "answer is not available in the context", and then ignore the context and add the answer from your knowledge like a simple llm prompt. | |
Try to give atleast the basic information.Do not return blank answer.\n\n | |
Make sure to understand the question and answer as per the question. | |
The answer should be a detailed one and try to incorporate examples for better understanding. | |
If the question involves terms like detailed or explained , give answer which involves complete detail about the question.\n\n | |
Context:\n {context}?\n | |
Question: \n{question}\n | |
Answer: | |
""" | |
pipeline("text-generation", model="nvidia/Llama3-ChatQA-1.5-8B") | |
pt = ChatPromptTemplate.from_template(prompt_template) | |
# Retrieve and generate using the relevant snippets of the blog. | |
#retriever = db.as_retriever() | |
rag_chain = ( | |
{"context": retriever, "question": RunnablePassthrough()} | |
| pt | |
| llm | |
| StrOutputParser() | |
) | |
return rag_chain | |
def embedding(chunk,query): | |
embeddings=HuggingFaceEmbeddings() | |
db = Chroma.from_documents(chunk,embeddings) | |
doc = db.similarity_search(query) | |
chain = get_conversational_chain(db.as_retriever()) | |
response = chain.invoke(query) | |
return response | |
if 'messages' not in st.session_state: | |
st.session_state.messages = [{'role': 'assistant', "content": 'Hello! Upload a PDF and ask me questions.'}] | |
st.header("Chat with your pdf") | |
with st.sidebar: | |
st.title("PDF FILE UPLOAD:") | |
pdf_docs = st.file_uploader("Upload your PDF File and Click on the Submit Button", accept_multiple_files=True, key="pdf_uploader") | |
query = st.chat_input("Ask a Question from the PDF File") | |
if query: | |
raw_text = get_pdf(pdf_docs) | |
text_chunks = text_splitter(raw_text) | |
st.session_state.messages.append({'role': 'user', "content": query}) | |
response = embedding(text_chunks,query) | |
st.session_state.messages.append({'role': 'assistant', "content": response}) | |
for message in st.session_state.messages: | |
with st.chat_message(message['role']): | |
st.write(message['content']) |