File size: 2,950 Bytes
85038b5
 
 
 
 
 
 
 
addd4ac
85038b5
 
 
 
 
4b12318
addd4ac
 
85038b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34d4635
 
85038b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import streamlit as st
from PIL import Image
from dotenv import load_dotenv
from streamlit_extras.add_vertical_space import add_vertical_space
from PyPDF2 import PdfReader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings
from langchain.vectorstores import chroma
#from langchain.chains.retrieval_qa.base import RetrievalQA
from langchain.chains.question_answering import load_qa_chain
from langchain_community.llms import huggingface_hub
from langchain.document_loaders.pdf import PyMuPDFLoader 
#from transformers import AutoTokenizer, AutoModelForCausalLM
#from langchain.llms import huggingface_endpoint
import os
#import fitz
#import tempfile

img = Image.open('image/nexio_logo1.png')
st.set_page_config(page_title="PDF Chatbot App",page_icon=img,layout="centered")

with st.sidebar:
    st.title('🤖 AI PDF Chatbot 💬')
    st.markdown('''
    ## About
    This app is an AI chatbot for the PDF files
    ''')
    add_vertical_space(12)
    st.write('Powered by ')
    st.image(image='image/nexio_logo2.png',width=150)

# load huggingface API key .env file
load_dotenv()

def main():
    st.header("Chat with PDF 💬")

    # upload pdf file
    pdf = st.file_uploader("Upload your PDF file",type='pdf')
    
    if pdf is not None:
        pdf_reader = PdfReader(pdf)
       
        text = ""
        for page in pdf_reader.pages:
            text += page.extract_text()
            
        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=1000,
            chunk_overlap=200,
            length_function=len
        )
        chunks = text_splitter.split_text(text=text)
        #chunks = text_splitter.create_documents(text)
        
        # embeddings
        embeddings = HuggingFaceEmbeddings()
        vector_store = chroma.Chroma.from_texts(chunks,embeddings)
        
        # Accept user question
        query = st.text_input("Ask questions about your PDF file:")

        if query:
            
            #PATH = 'model/'
            #llm = AutoModelForCausalLM.from_pretrained("CohereForAI/aya-101")
            # llm = AutoModelForCausalLM.from_pretrained(PATH,local_files_only=True)
            llm = huggingface_hub.HuggingFaceHub(repo_id="CohereForAI/aya-101",
                                                  model_kwargs={"temperature":1.0, "max_length":100})
            docs = vector_store.similarity_search(query=query, k=1)
            global chain
            chain = load_qa_chain(llm=llm, chain_type="stuff")
            response = chain.run(input_documents=docs, question=query)
            # retriever=vector_store.as_retriever()
            # st.write(retriever)
            #chain = RetrievalQA.from_chain_type(llm=llm,chain_type="stuff",retriever=retriever)
            #response = chain.run(chain)
            st.write(response)
            


if __name__ == '__main__':
    main()