Spaces:
Runtime error

File size: 2,709 Bytes
a881790
a76f201
 
 
 
 
 
 
 
 
 
 
b301f89
 
a76f201
 
 
 
a881790
a76f201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb0d7ac
a76f201
 
4624648
a76f201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8dbacb4
a76f201
 
 
 
 
 
 
 
 
 
 
 
 
26e382d
 
71e57f3
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

import streamlit as st
from PyPDF2 import PdfReader
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
import pickle
from langchain.chains import ConversationalRetrievalChain
from langchain.chat_models import ChatOpenAI
from langchain.vectorstores import FAISS


# Function to read PDF content
def read_pdf():
    pdf_reader = PdfReader('hey.pdf')
    text = ""
    for page in pdf_reader.pages:
        text += page.extract_text()
    return text
    
# Main Streamlit app
def main():
    st.title("Query your PDF")
    with st.sidebar:
        st.title('🤗💬 PDF Chat App')
        st.markdown('''
        ## About
        Choose the desired PDF, then perform a query.
        ''')

        text_splitter = RecursiveCharacterTextSplitter(
            chunk_size=1000,
            chunk_overlap=150,
            length_function=len
        )

        # Process the PDF text and create the documents list
        documents = text_splitter.split_text(text=read_pdf())

        # Vectorize the documents and create vectorstore
        embeddings = OpenAIEmbeddings(openai_api_key='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')
        vectorstore = FAISS.from_texts(documents, embedding=embeddings)

        st.session_state.processed_data = {
            "document_chunks": documents,
            "vectorstore": vectorstore,
        }
        # Load the Langchain chatbot
        llm = ChatOpenAI(temperature=0, max_tokens=1000, model_name="gpt-3.5-turbo")
        qa = ConversationalRetrievalChain.from_llm(llm, vectorstore.as_retriever())

        # Initialize Streamlit chat UI
        if "messages" not in st.session_state:
            st.session_state.messages = []

        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

        if prompt := st.chat_input("Ask your questions from PDF ?"):
            st.session_state.messages.append({"role": "user", "content": prompt})
            with st.chat_message("user"):
                st.markdown(prompt)

            result = qa({"question": prompt, "chat_history": [(message["role"], message["content"]) for message in st.session_state.messages]})
            print(prompt)

            with st.chat_message("assistant"):
                message_placeholder = st.empty()
                full_response = result["answer"]
                message_placeholder.markdown(full_response + "|")
            message_placeholder.markdown(full_response)
            print(full_response)
            st.session_state.messages.append({"role": "assistant", "content": full_response})

main()