Spaces:
Sleeping
Sleeping
# 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() |