import streamlit as st import os from utils.pdf_utils import PDFProcessor from utils.embeddings_utils import EmbeddingsManager from utils.qa_utils import QASystem from dotenv import load_dotenv import openai def initialize_session_state(): if 'pdf_processor' not in st.session_state: st.session_state['pdf_processor'] = None if 'embeddings_manager' not in st.session_state: st.session_state['embeddings_manager'] = None if 'qa_system' not in st.session_state: st.session_state['qa_system'] = None if 'processed_pdfs' not in st.session_state: st.session_state['processed_pdfs'] = set() if 'all_text_chunks' not in st.session_state: st.session_state['all_text_chunks'] = [] def main(): load_dotenv() st.set_page_config(page_title="AI-Powered PDF Assistant", layout="wide") initialize_session_state() # Header Section st.markdown( """
Drag and drop your PDF files below to extract and process content for analysis.
""", unsafe_allow_html=True ) uploaded_files = st.file_uploader( "Upload PDF files", type=['pdf'], accept_multiple_files=True ) if uploaded_files: new_files = [f for f in uploaded_files if f.name not in st.session_state['processed_pdfs']] if new_files: with st.spinner("Processing PDFs..."): for pdf_file in new_files: try: pages = st.session_state['pdf_processor'].extract_text(pdf_file) for page_text in pages.values(): chunks = st.session_state['pdf_processor'].chunk_text(page_text) st.session_state['all_text_chunks'].extend(chunks) st.session_state['processed_pdfs'].add(pdf_file.name) except Exception as e: st.error(f"Error processing {pdf_file.name}: {str(e)}") continue with st.spinner("Generating embeddings..."): try: st.session_state['embeddings_manager'].generate_embeddings( st.session_state['all_text_chunks'] ) st.success("â Documents processed successfully!") except Exception as e: st.error(f"Error generating embeddings: {str(e)}") elif selected_page == "Ask Questions": st.header("â Ask Questions") st.markdown( """Query your uploaded documents and get precise answers backed by AI-powered analysis.
""", unsafe_allow_html=True ) if st.session_state['all_text_chunks']: question = st.text_input("Enter your question:") if question: try: with st.spinner("Finding relevant information..."): relevant_chunks = st.session_state['embeddings_manager'].find_relevant_chunks( question, k=3 ) answer = st.session_state['qa_system'].generate_answer( question, relevant_chunks ) st.markdown("### đ¤ Answer") st.write(answer) with st.expander("đ View Source Context"): for i, chunk in enumerate(relevant_chunks, 1): st.markdown(f"**Context {i}:**") st.write(chunk) st.markdown("---") except openai.error.RateLimitError: st.error("Rate limit exceeded. Please try again later.") except Exception as e: st.error(f"Error: {str(e)}") else: st.warning("Please upload and process documents in the 'Upload PDFs' section first.") elif selected_page == "About": st.header("âšī¸ About This App") st.markdown( """AI-Powered PDF Assistant is a smart solution for extracting and querying information from PDF files. With powerful AI integrations, this tool allows seamless document analysis and interaction.
Built with â¤ī¸ by [Your Name]
""", unsafe_allow_html=True ) if __name__ == "__main__": main()