import streamlit as st import time import PyPDF2 # Import PyPDF2 for PDF text extraction from openai import OpenAI from docx import Document from docx.shared import Pt from docx.enum.text import WD_PARAGRAPH_ALIGNMENT import os # Set the page configuration st.set_page_config(page_title="Report Generation AI") # Title and caption st.title("AI Report Generator") st.caption("Generate Professional Reports from PDF Documents") # Sidebar for API Key input with st.sidebar: OPENAI_API_KEY = st.text_input("Enter your Access Key", type="password") # Check for valid API key if OPENAI_API_KEY: client = OpenAI(api_key=OPENAI_API_KEY) else: st.error("Please enter your Access Key to continue.") st.stop() ASSISTANT_ID = "asst_d68xqxiRtvYkmewBPugwZaGx" # Initialize session state for chat history if "messages" not in st.session_state: st.session_state["messages"] = [] # Clear chat button above chat input if st.button("Clear Chat", use_container_width=True): st.session_state.messages = [] st.rerun() # Display chat history for message in st.session_state.messages: role, content = message["role"], message["content"] st.chat_message(role).write(content) # Process user input uploaded_file = st.file_uploader("Upload PDF file", type=["pdf"]) if uploaded_file: # Read the PDF file using PyPDF2 pdf_reader = PyPDF2.PdfReader(uploaded_file) full_text = "" # Extract text from each page for page_num in range(len(pdf_reader.pages)): page = pdf_reader.pages[page_num] full_text += page.extract_text() st.write("Text extracted from PDF:") st.text_area("Extracted Text", full_text, height=300) # Process user input for assistant prompt = f"Please analyze the following text from the PDF: {full_text[:1000]}" # Limit to the first 1000 chars for clarity st.session_state.messages.append({"role": "user", "content": prompt}) st.chat_message("user").write(prompt) try: # Create a new thread for conversation thread = client.beta.threads.create() thread_id = thread.id # Send user message to OpenAI API client.beta.threads.messages.create( thread_id=thread_id, role="user", content=prompt ) # Run the assistant to generate a response run = client.beta.threads.runs.create( thread_id=thread_id, assistant_id=ASSISTANT_ID ) # Wait for response while True: run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id) if run_status.status == "completed": break time.sleep(1) # Retrieve assistant response messages = client.beta.threads.messages.list(thread_id=thread_id) assistant_message = messages.data[0].content[0].text.value # Display assistant's response st.chat_message("assistant").write(assistant_message) # Store in session state st.session_state.messages.append({"role": "assistant", "content": assistant_message}) # Generate Word (DOCX) file for download doc = Document() # Add a title and style title = doc.add_heading('AI Assistant Report', 0) title.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER # Center align the title run = title.add_run("AI Assistant Report") run.bold = True run.font.size = Pt(18) # Add Executive Summary doc.add_heading('Executive Summary', level=1) doc.add_paragraph("Based on the provided PDF content, the analysis has been carried out. " "Key insights and analysis are provided below, along with a variance analysis " "and considerations for future actions.") # Step 1: Text Analysis doc.add_heading('Step 1: Text Analysis', level=1) # Create a table for the extracted data table = doc.add_table(rows=1, cols=6) hdr_cells = table.rows[0].cells hdr_cells[0].text = 'Sales (2025)' hdr_cells[1].text = 'Sales (2024)' hdr_cells[2].text = 'Sales Variance (%)' hdr_cells[3].text = 'Gross Profit (2025)' hdr_cells[4].text = 'Gross Profit (2024)' hdr_cells[5].text = 'Gross Profit Variance (%)' # Add sample row data (the actual data should be dynamically inserted here from the PDF) row = table.add_row().cells row[0].text = "1,392" row[1].text = "1,400" row[2].text = "-0.5" row[3].text = "200" row[4].text = "207" row[5].text = "-3.0" # Step 2: Assistant's Analysis doc.add_heading('Step 2: Assistant Analysis', level=1) doc.add_paragraph(f"The AI Assistant's insights are:\n{assistant_message}") # Add tables for key insights like sales, gross profit, and variance analysis doc.add_heading('Key Insights and Trends', level=2) doc.add_paragraph("Here is a summary of the analysis based on the provided data:") # Add another table for detailed analysis if necessary detailed_table = doc.add_table(rows=1, cols=3) detailed_hdr = detailed_table.rows[0].cells detailed_hdr[0].text = 'Month' detailed_hdr[1].text = 'Sales (ZAR)' detailed_hdr[2].text = 'Gross Profit (ZAR)' # Add sample data for detailed insights month_data = [ ("February 2025", "1,392,000", "200,000"), ("February 2024", "1,400,000", "207,000"), ("Budgeted February 2025", "1,498,000", "221,000") ] for month, sales, profit in month_data: row = detailed_table.add_row().cells row[0].text = month row[1].text = sales row[2].text = profit # Style Table Text (bold for headings, adjust font size) for cell in hdr_cells: cell.paragraphs[0].runs[0].bold = True cell.paragraphs[0].runs[0].font.size = Pt(10.5) # Adjust font size and add bold to data cells for row in table.rows: for cell in row.cells: cell.paragraphs[0].runs[0].font.size = Pt(10.5) # Saving the DOCX file word_filename = "AI_Report_Formatted.docx" doc.save(word_filename) # Provide download link with open(word_filename, "rb") as file: st.download_button( label="Download the Report", data=file.read(), file_name=word_filename, mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" ) except Exception as e: st.error(f"Error processing the chat: {str(e)}")