File size: 2,718 Bytes
9b6e71b
 
 
 
 
f1955c9
9b6e71b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fecabd0
9b6e71b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from dotenv import load_dotenv
import streamlit as st
from app_style import css, bot_template, user_template
from llm_chain import RAG_PDF

logo_image_path = "Logo.png"

def handle_userinput(user_question):
    response = st.session_state.conversation({'question': user_question})
    st.session_state.chat_history = response['chat_history']

    for i, message in enumerate(st.session_state.chat_history):
        if i % 2 == 0:
            st.write(user_template.replace(
                "{{MSG}}", message.content), unsafe_allow_html=True)
        else:
            st.write(bot_template.replace(
                "{{MSG}}", message.content), unsafe_allow_html=True)


def main():
    # loading environment varibales
    load_dotenv()
    # Page Config
    st.set_page_config(page_title="Ask-your-PDFs",
                       page_icon=":books:")
    st.write(css, unsafe_allow_html=True)
    # Chat history session management

    if "conversation" not in st.session_state:
        st.session_state.conversation = None
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = None
    
    # for rendering the background image (Uncomment the next line to update the background img of the application)
    # render_background_img(background_path)
    
    # Chat User input
    st.header("Chat with PDFs (Open-Source LLM):books:")
    user_question = st.text_input("Ask a question about your documents:")
    styl = f"""
            <style>
                .stTextInput {{
                position: fixed;
                bottom: 3rem;
                }}
            </style>
            """
    st.markdown(styl, unsafe_allow_html=True)

    # Handling user input
    if user_question:
        handle_userinput(user_question)

    with st.sidebar:
        # Loading the Logo
        st.image(logo_image_path, use_column_width=True)
        
        # Header text for the sidebar
        st.subheader("Your documents")

        # File Uploader (Allowing multiple files upload)
        pdf_docs = st.file_uploader(
            "Upload your PDFs here and click on 'Submit'", accept_multiple_files=True)
        
        # When the submit button is clicked
        if st.button("Submit"):
            # Processing Bar
            with st.spinner("Processing"):
                # Creating an object of RAG pipeline
                RAG_object = RAG_PDF(pdf_docs)

                # Activating the RAG Pipeline
                st.session_state.conversation = RAG_object.activate_RAG_pipeline()
                
                # Posting an update when the upload and processing of RAG architecture done
                st.write("Processing Completed.")

if __name__ == '__main__':
    main()