File size: 5,280 Bytes
d0f4259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa5729c
d0f4259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
from dotenv import load_dotenv
import streamlit as st
import os
from langchain_openai import AzureChatOpenAI
from langchain_openai import AzureOpenAIEmbeddings
from langchain.chains import RetrievalQA

from langchain_community.document_loaders import PyPDFLoader

import streamlit as st
from langchain.text_splitter import RecursiveCharacterTextSplitter
import os
import tempfile

from langchain_community.vectorstores import Chroma

load_dotenv()

azure_config = {
    "AZURE_OPENAI_ENDPOINT": os.getenv("AZURE_OPENAI_ENDPOINT"),
    "AZURE_OPENAI_MODEL_DEPLOYMENT_NAME": os.getenv("AZURE_OPENAI_MODEL_DEPLOYMENT_NAME"),
    "AZURE_OPENAI_MODEL_NAME": os.getenv("AZURE_OPENAI_MODEL_NAME"),
    "AZURE_OPENAI_API_KEY": os.getenv("AZURE_OPENAI_API_KEY"),
    "AZURE_OPENAI_API_VERSION": os.getenv("AZURE_OPENAI_API_VERSION"),
    "AZURE_OPENAI_EMBEDDING_ENDPOINT": os.getenv("AZURE_OPENAI_API_VERSION"),
    "AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME" : os.getenv("AZURE_OPENAI_EMBEDDING_DEPLOYMENT_NAME"),
    "AZURE_OPENAI_EMBEDDING_NAME": os.getenv("AZURE_OPENAI_EMBEDDING_NAME"),
    "AZURE_OPENAI_EMBEDDING_API_KEY": os.getenv("AZURE_OPENAI_EMBEDDING_API_KEY"),
    "AZURE_OPENAI_EMBEDDING_API_VERSION" : os.getenv("AZURE_OPENAI_EMBEDDING_API_VERSION")
    }

CHROMA_PATH = os.path.join(os.getcwd(), "chroma_db")

st.set_page_config(page_title="PDF chatbot", layout="wide")
st.title("RAG Demo - PDF Q&A")

st.markdown("""
1. **Upload Your Documents**: The system accepts multiple PDF files at once, analyzing the content to provide comprehensive insights.

2. **Ask a Question**: After processing the documents, ask any question related to the content of your uploaded documents for a precise answer.
""")

def main():
    st.header("Ask a question")

    # Initialize vector store
    embeddings = AzureOpenAIEmbeddings(
        model="text-embedding-ada-002",
        api_key=azure_config["AZURE_OPENAI_API_KEY"],
        api_version="2024-02-01",
        azure_endpoint="https://ey-openai.openai.azure.com/openai/deployments/text-embedding-ada-002/embeddings?api-version=2023-05-15"
    )
    vectorstore = Chroma(
        persist_directory=CHROMA_PATH,
        embedding_function=embeddings
    )

    with st.sidebar:
        st.title("Menu:")
        uploaded_files = st.file_uploader(
            "Upload your PDF Files and Click on the Submit & Process Button", 
            accept_multiple_files=True, 
            key="pdf_uploader")
        if st.button("Submit & Process", key="process_button") and uploaded_files:  # Check if API key is provided before processing
            with st.spinner("Processing..."):
                for uploaded_file in uploaded_files:
                    try:
                        # Create a temporary file to store the uploaded PDF
                        with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
                            tmp_file.write(uploaded_file.getvalue())
                            tmp_file_path = tmp_file.name
                        
                        # Load PDF using PyPDFLoader
                        loader = PyPDFLoader(tmp_file_path)
                        pages = loader.load()
                        
                        # Add metadata to each page
                        for page in pages:
                            page.metadata["page_number"] = pages.index(page) + 1
                        
                        # Split into chunks
                        text_splitter = RecursiveCharacterTextSplitter(
                            chunk_size=1000,
                            chunk_overlap=200,
                            length_function=len,
                            separators=["\n\n", "\n", " ", ""]
                        )
                        chunks = text_splitter.split_documents(pages)
                        # Clean up temporary file
                        os.unlink(tmp_file_path)                        
                    
                        vectorstore.add_documents(chunks)
                        vectorstore.persist()
                    
                    except Exception as e:
                        st.error(f"Error processing {uploaded_file.name}: {str(e)}")
                        continue
                print("vector store created")
                st.success("Vector store created")

    user_question = st.text_input("Ask a Question from the PDF Files", key="user_question")
    if user_question:
        print("Question: ", user_question)

        retriever = vectorstore.as_retriever(search_type='similarity', search_kwargs={'k': 5})

        llm = AzureChatOpenAI(temperature=0,
                        api_key=azure_config["AZURE_OPENAI_API_KEY"],
                        openai_api_version=azure_config["AZURE_OPENAI_API_VERSION"],
                        azure_endpoint=azure_config["AZURE_OPENAI_ENDPOINT"],
                        model=azure_config["AZURE_OPENAI_MODEL_DEPLOYMENT_NAME"],
                        validate_base_url=False)

        chain = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever)
        answer = chain.invoke(user_question)

        print(answer)
        st.write("Reply: ", answer['result'])


if __name__ == "__main__":
    main()