-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
140 lines (118 loc) · 5.88 KB
/
Copy pathapp.py
File metadata and controls
140 lines (118 loc) · 5.88 KB
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
import streamlit as st
import pandas as pd
import numpy as np
from utils import (
read_csv, read_excel, read_pdf, detect_file_type,
clean_dataframe, generate_summary_stats, extract_financial_kpis,
FileProcessingError
)
import visualizations
import logging
# Configure the app
st.set_page_config(
page_title="Financial Document Analyzer",
page_icon="📊",
layout="wide"
)
# Initialize session state
if 'data' not in st.session_state:
st.session_state.data = None
if 'file_type' not in st.session_state:
st.session_state.file_type = None
if 'summary_stats' not in st.session_state:
st.session_state.summary_stats = None
if 'kpis' not in st.session_state:
st.session_state.kpis = None
def main():
st.title("📊 Financial Document Analyzer")
st.write("""
Upload your financial documents (CSV, Excel, or PDF) for automated analysis and visualization.
This tool will help you extract key financial metrics and generate insightful visualizations.
""")
# File upload
uploaded_file = st.file_uploader("Choose a file", type=['csv', 'xlsx', 'xls', 'pdf'])
if uploaded_file is not None:
try:
# Detect file type and read file
file_type = detect_file_type(uploaded_file.name)
st.session_state.file_type = file_type
with st.spinner('Reading and processing file...'):
if file_type == 'csv':
df = read_csv(uploaded_file)
elif file_type == 'excel':
df = read_excel(uploaded_file)
elif file_type == 'pdf':
df = read_pdf(uploaded_file)
# Clean the dataframe
df = clean_dataframe(df)
st.session_state.data = df
# Generate summary statistics
st.session_state.summary_stats = generate_summary_stats(df)
# Extract KPIs
st.session_state.kpis = extract_financial_kpis(df)
# Display success message
st.success('File processed successfully!')
# Create tabs for different analyses
tab1, tab2, tab3, tab4 = st.tabs([
"📊 Data Overview",
"📈 Financial KPIs",
"🔍 Detailed Analysis",
"📉 Visualizations"
])
with tab1:
st.header("Data Overview")
st.write("First few rows of the data:")
st.dataframe(df.head())
st.subheader("Summary Statistics")
col1, col2 = st.columns(2)
with col1:
st.write("Basic Information:")
st.write(f"- Rows: {st.session_state.summary_stats['row_count']}")
st.write(f"- Columns: {st.session_state.summary_stats['column_count']}")
with col2:
st.write("Column Types:")
st.write(f"- Numeric Columns: {', '.join(st.session_state.summary_stats['numeric_columns'])}")
st.write(f"- Categorical Columns: {', '.join(st.session_state.summary_stats['categorical_columns'])}")
with tab2:
st.header("Financial KPIs")
if st.session_state.kpis:
# Display KPIs in a dashboard
st.plotly_chart(visualizations.create_summary_dashboard(st.session_state.kpis), use_container_width=True)
else:
st.warning("No financial KPIs could be extracted from this document.")
with tab3:
st.header("Detailed Analysis")
# Correlation analysis for numeric columns
if len(st.session_state.summary_stats['numeric_columns']) > 1:
st.subheader("Correlation Analysis")
st.plotly_chart(visualizations.create_correlation_heatmap(df), use_container_width=True)
# Distribution analysis
st.subheader("Distribution Analysis")
selected_column = st.selectbox(
"Select column for distribution analysis",
st.session_state.summary_stats['numeric_columns']
)
if selected_column:
st.plotly_chart(visualizations.create_distribution_plots(df, selected_column), use_container_width=True)
with tab4:
st.header("Visualizations")
# Time series analysis if date column exists
date_columns = df.select_dtypes(include=['datetime64']).columns
if len(date_columns) > 0:
st.subheader("Time Series Analysis")
date_col = st.selectbox("Select date column", date_columns)
value_col = st.selectbox("Select value column", st.session_state.summary_stats['numeric_columns'])
st.plotly_chart(visualizations.create_time_series_plot(df, date_col, value_col), use_container_width=True)
# Categorical analysis
if len(st.session_state.summary_stats['categorical_columns']) > 0:
st.subheader("Categorical Analysis")
cat_col = st.selectbox("Select categorical column", st.session_state.summary_stats['categorical_columns'])
val_col = st.selectbox("Select value column for analysis", st.session_state.summary_stats['numeric_columns'])
st.plotly_chart(visualizations.create_categorical_analysis(df, cat_col, val_col), use_container_width=True)
except FileProcessingError as e:
st.error(f"Error processing file: {str(e)}")
except Exception as e:
st.error(f"An unexpected error occurred: {str(e)}")
logging.exception("Unexpected error in main application")
if __name__ == "__main__":
main()