Spaces:
Runtime error
Runtime error
File size: 6,790 Bytes
4060393 272755a 13d1927 4ac7ca7 de67278 cfe3a26 60685da 4060393 4ac7ca7 13d1927 4060393 4ac7ca7 13d1927 4060393 13d1927 4060393 b320475 4060393 13d1927 4060393 13d1927 4060393 2b8b374 4060393 13d1927 4ac7ca7 272755a 13d1927 272755a 13d1927 4060393 4ac7ca7 cfe3a26 de67278 cfe3a26 3ee6389 cfe3a26 de67278 cfe3a26 13d1927 de67278 cfe3a26 70b5f49 13d1927 de67278 cfe3a26 70b5f49 de67278 4ac7ca7 4060393 b320475 |
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
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)}")
|