import streamlit as st import pandas as pd import plotly.express as px import plotly.graph_objects as go from plotly.subplots import make_subplots import numpy as np from datetime import datetime import io # Page configuration st.set_page_config( page_title="π FinanceGPT Analyzer", page_icon="π", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) class FinanceAnalyzer: def __init__(self): self.data = None self.processed_data = {} def load_sample_data(self): """Load sample financial data""" sample_data = { 'Year': [2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2024, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023, 2023], 'Statement_Type': ['Income Statement'] * 10 + ['Income Statement'] * 10, 'Account_Name_Norwegian': ['Salgsinntekt', 'Varekostnad', 'Bruttoresultat', 'LΓΈnnskostnad', 'Andre driftskostnader', 'Driftsresultat', 'Finansinntekter', 'Finanskostnader', 'OrdinΓ¦rt resultat fΓΈr skatt', 'Γ rsresultat'] * 2, 'Account_Name_English': ['Sales Revenue', 'Cost of Goods Sold', 'Gross Profit', 'Salary Costs', 'Other Operating Expenses', 'Operating Result', 'Financial Income', 'Financial Expenses', 'Profit Before Tax', 'Net Profit'] * 2, '2024_Amount_NOK': [25107008, -15064205, 10042803, -3521456, -1987234, 4534113, 123456, -234567, 4422002, 3537602, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], '2023_Amount_NOK': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4891891, -2934535, 1957356, -1234567, -654321, 68468, 45678, -123456, -9310, -7448] } return pd.DataFrame(sample_data) def process_financial_data(self, df): """Process uploaded financial data""" self.data = df # Create pivot tables for easier analysis income_2024 = df[df['Year'] == 2024]['2024_Amount_NOK'].values income_2023 = df[df['Year'] == 2023]['2023_Amount_NOK'].values accounts = df[df['Year'] == 2024]['Account_Name_English'].values self.processed_data = { 'revenue_2024': income_2024[0] if len(income_2024) > 0 else 0, 'revenue_2023': income_2023[0] if len(income_2023) > 0 else 0, 'net_profit_2024': income_2024[9] if len(income_2024) > 9 else 0, 'net_profit_2023': income_2023[9] if len(income_2023) > 9 else 0, 'cogs_2024': abs(income_2024[1]) if len(income_2024) > 1 else 0, 'cogs_2023': abs(income_2023[1]) if len(income_2023) > 1 else 0, 'operating_profit_2024': income_2024[5] if len(income_2024) > 5 else 0, 'operating_profit_2023': income_2023[5] if len(income_2023) > 5 else 0, } def calculate_metrics(self): """Calculate key financial metrics""" if not self.processed_data: return {} data = self.processed_data # Growth rates revenue_growth = ((data['revenue_2024'] - data['revenue_2023']) / abs(data['revenue_2023']) * 100) if data['revenue_2023'] != 0 else 0 # Profitability ratios gross_margin_2024 = ((data['revenue_2024'] - data['cogs_2024']) / data['revenue_2024'] * 100) if data['revenue_2024'] != 0 else 0 net_margin_2024 = (data['net_profit_2024'] / data['revenue_2024'] * 100) if data['revenue_2024'] != 0 else 0 return { 'revenue_growth': revenue_growth, 'gross_margin_2024': gross_margin_2024, 'net_margin_2024': net_margin_2024, 'revenue_2024_m': data['revenue_2024'] / 1000000, 'net_profit_2024_m': data['net_profit_2024'] / 1000000, } def create_revenue_trend_chart(self): """Create revenue trend visualization""" if not self.processed_data: return go.Figure() fig = go.Figure() years = [2023, 2024] revenues = [self.processed_data['revenue_2023']/1000000, self.processed_data['revenue_2024']/1000000] net_profits = [self.processed_data['net_profit_2023']/1000000, self.processed_data['net_profit_2024']/1000000] fig.add_trace(go.Scatter(x=years, y=revenues, mode='lines+markers', name='Revenue (M NOK)', line=dict(color='#1f77b4', width=3))) fig.add_trace(go.Scatter(x=years, y=net_profits, mode='lines+markers', name='Net Profit (M NOK)', line=dict(color='#ff7f0e', width=3))) fig.update_layout(title='Revenue vs Profit Trend', xaxis_title='Year', yaxis_title='Amount (M NOK)', height=400) return fig def create_financial_health_radar(self): """Create financial health radar chart""" metrics = self.calculate_metrics() categories = ['Revenue Growth', 'Gross Margin', 'Net Margin', 'Profitability', 'Efficiency'] values = [ min(metrics.get('revenue_growth', 0) / 5, 100), # Scale revenue growth metrics.get('gross_margin_2024', 0), max(metrics.get('net_margin_2024', 0), 0), 70, # Sample value 65 # Sample value ] fig = go.Figure() fig.add_trace(go.Scatterpolar( r=values, theta=categories, fill='toself', name='Financial Health' )) fig.update_layout( polar=dict( radialaxis=dict(visible=True, range=[0, 100]) ), title="Financial Health Score", height=400 ) return fig def main(): st.title("π FinanceGPT Analyzer") st.markdown("### Professional Financial Analysis Dashboard") analyzer = FinanceAnalyzer() # Sidebar navigation with st.sidebar: st.header("Navigation") page = st.selectbox("Choose Analysis Page", [ "π Dashboard", "π° Income Analysis", "ποΈ Balance Sheet Analysis", "πΈ Cash Flow Analysis", "π Financial Ratios Hub", "π€ AI Finance Assistant" ]) st.header("Data Upload") uploaded_file = st.file_uploader("Upload CSV file", type=['csv']) if st.button("Use Sample Data"): analyzer.data = analyzer.load_sample_data() analyzer.process_financial_data(analyzer.data) st.success("Sample data loaded!") if uploaded_file: try: df = pd.read_csv(uploaded_file) analyzer.data = df analyzer.process_financial_data(df) st.success("Data uploaded successfully!") except Exception as e: st.error(f"Error loading file: {e}") # Main content based on selected page if page == "π Dashboard": dashboard_page(analyzer) elif page == "π° Income Analysis": income_analysis_page(analyzer) elif page == "ποΈ Balance Sheet Analysis": balance_sheet_page(analyzer) elif page == "πΈ Cash Flow Analysis": cash_flow_page(analyzer) elif page == "π Financial Ratios Hub": ratios_page(analyzer) elif page == "π€ AI Finance Assistant": ai_assistant_page(analyzer) def dashboard_page(analyzer): """Main dashboard page""" st.header("π Financial Dashboard") if analyzer.data is None: st.warning("Please upload data or use sample data to begin analysis.") return metrics = analyzer.calculate_metrics() # Key metrics cards col1, col2, col3, col4 = st.columns(4) with col1: st.markdown("""
+{:.0f}% π₯
Profitable β
Healthy πͺ
Strong π
Based on your financial data: