File size: 2,054 Bytes
ea08e05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Load libraries and dependencies
from dotenv import load_dotenv
from logic import get_pdf_text, get_text_chunks, init_embeddings, get_conversation_chain
import streamlit as st

# Initialization function
def init():
    # Load environmental variables to extract API secrets
    load_dotenv()

    # Create and configure streamlit page 
    st.set_page_config(page_title="Work-Assistant App", page_icon="🐮")
    st.header("Work-Assistant App 🐮")

    # Initialize session state
    if "conversation_chain" not in st.session_state:
        st.session_state.conversation_chain = None
    if "chat_history" not in st.session_state:
        st.session_state.chat_history = None

# Main function
def main():
    init()

    # UI main layout
    user_input = st.text_input("Ask a question:")
    if user_input:
        # Obtain response from conversation chain
        response = st.session_state.conversation_chain({'question': user_input})
        # Update chat history via response history
        st.session_state.chat_history = response['chat_history']
        # Show conversation
        for i, message in enumerate(st.session_state.chat_history):
            if i % 2 == 0:
                st.write("User:" + message.content)
            else:
                st.write("Bot:" + message.content)

    # UI sidebar layout
    with st.sidebar:
        st.subheader("Your documents")
        pdf_docs = st.file_uploader("Upload your PDFs", accept_multiple_files=True)
        if st.button("Process files"):
            with st.spinner("Processing..."):
                # Extract text content from pdf
                text = get_pdf_text(pdf_docs)
                # Split text into chunks
                chunks = get_text_chunks(text)
                # Obtain embeddings
                embeddings = init_embeddings()
                # Start conversation chain
                st.session_state.conversation_chain = get_conversation_chain(chunks=chunks, embeddings=embeddings)
    
# Main program of the application
if __name__ == '__main__':
    main()