Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -54,22 +54,103 @@ class FastFinancialAnalyzer:
|
|
54 |
return 0.0
|
55 |
|
56 |
def extract_key_metrics(self, income_data, balance_data):
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
metrics = {
|
59 |
"Revenue": {
|
60 |
-
"2025":
|
61 |
-
"2021":
|
|
|
62 |
},
|
63 |
"Profit": {
|
64 |
-
"2025":
|
65 |
-
"2021":
|
|
|
|
|
66 |
},
|
67 |
"Assets": {
|
68 |
-
"2025":
|
69 |
-
"2021":
|
|
|
70 |
}
|
71 |
}
|
72 |
return metrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
|
74 |
def generate_analysis_prompt(self, metrics):
|
75 |
"""Create focused analysis prompt"""
|
|
|
54 |
return 0.0
|
55 |
|
56 |
def extract_key_metrics(self, income_data, balance_data):
|
57 |
+
"""Extract key financial metrics with safety checks"""
|
58 |
+
try:
|
59 |
+
# First, safely extract values with error handling
|
60 |
+
revenue_2025 = self.safe_extract_number(income_data, "Total Net Revenue", "2025")
|
61 |
+
revenue_2021 = self.safe_extract_number(income_data, "Total Net Revenue", "2021")
|
62 |
+
profit_2025 = self.safe_extract_number(income_data, "Net Earnings", "2025")
|
63 |
+
profit_2021 = self.safe_extract_number(income_data, "Net Earnings", "2021")
|
64 |
+
assets_2025 = self.safe_extract_number(balance_data, "Total_Assets", "2025")
|
65 |
+
assets_2021 = self.safe_extract_number(balance_data, "Total_Assets", "2021")
|
66 |
+
|
67 |
metrics = {
|
68 |
"Revenue": {
|
69 |
+
"2025": revenue_2025,
|
70 |
+
"2021": revenue_2021,
|
71 |
+
"Growth": self.calculate_growth(revenue_2025, revenue_2021)
|
72 |
},
|
73 |
"Profit": {
|
74 |
+
"2025": profit_2025,
|
75 |
+
"2021": profit_2021,
|
76 |
+
"Growth": self.calculate_growth(profit_2025, profit_2021),
|
77 |
+
"Margin_2025": self.calculate_percentage(profit_2025, revenue_2025)
|
78 |
},
|
79 |
"Assets": {
|
80 |
+
"2025": assets_2025,
|
81 |
+
"2021": assets_2021,
|
82 |
+
"Growth": self.calculate_growth(assets_2025, assets_2021)
|
83 |
}
|
84 |
}
|
85 |
return metrics
|
86 |
+
except Exception as e:
|
87 |
+
print(f"Error in metric extraction: {str(e)}")
|
88 |
+
return self.get_default_metrics()
|
89 |
+
|
90 |
+
def safe_extract_number(self, data_dict, key, year):
|
91 |
+
"""Safely extract and convert number from data"""
|
92 |
+
try:
|
93 |
+
if isinstance(data_dict, dict):
|
94 |
+
for k, v in data_dict.items():
|
95 |
+
if isinstance(v, dict) and key in k:
|
96 |
+
value = v.get(year, '0')
|
97 |
+
return self.clean_number(value)
|
98 |
+
return 0.0
|
99 |
+
except Exception as e:
|
100 |
+
print(f"Error extracting {key} for {year}: {str(e)}")
|
101 |
+
return 0.0
|
102 |
+
|
103 |
+
def calculate_growth(self, current, previous):
|
104 |
+
"""Calculate growth percentage with safety check"""
|
105 |
+
try:
|
106 |
+
if previous and previous != 0:
|
107 |
+
return ((current - previous) / abs(previous)) * 100
|
108 |
+
return 0.0
|
109 |
+
except:
|
110 |
+
return 0.0
|
111 |
+
|
112 |
+
def calculate_percentage(self, numerator, denominator):
|
113 |
+
"""Calculate percentage with safety check"""
|
114 |
+
try:
|
115 |
+
if denominator and denominator != 0:
|
116 |
+
return (numerator / denominator) * 100
|
117 |
+
return 0.0
|
118 |
+
except:
|
119 |
+
return 0.0
|
120 |
+
|
121 |
+
def get_default_metrics(self):
|
122 |
+
"""Return default metrics structure"""
|
123 |
+
return {
|
124 |
+
"Revenue": {"2025": 0, "2021": 0, "Growth": 0},
|
125 |
+
"Profit": {"2025": 0, "2021": 0, "Growth": 0, "Margin_2025": 0},
|
126 |
+
"Assets": {"2025": 0, "2021": 0, "Growth": 0}
|
127 |
+
}
|
128 |
+
|
129 |
+
def generate_analysis_prompt(self, metrics):
|
130 |
+
"""Create focused analysis prompt with safety checks"""
|
131 |
+
return f"""<human>Analyze these financial metrics and provide insights:
|
132 |
+
|
133 |
+
Key Performance Indicators:
|
134 |
+
1. Revenue Performance:
|
135 |
+
- 2025: ${metrics['Revenue']['2025']:,.1f}M
|
136 |
+
- 2021: ${metrics['Revenue']['2021']:,.1f}M
|
137 |
+
- 5-Year Growth: {metrics['Revenue']['Growth']:.1f}%
|
138 |
+
|
139 |
+
2. Profitability:
|
140 |
+
- 2025 Net Profit: ${metrics['Profit']['2025']:,.1f}M
|
141 |
+
- 2021 Net Profit: ${metrics['Profit']['2021']:,.1f}M
|
142 |
+
- Profit Growth: {metrics['Profit']['Growth']:.1f}%
|
143 |
+
- 2025 Profit Margin: {metrics['Profit']['Margin_2025']:.1f}%
|
144 |
+
|
145 |
+
3. Asset Base:
|
146 |
+
- 2025 Total Assets: ${metrics['Assets']['2025']:,.1f}M
|
147 |
+
- 2021 Total Assets: ${metrics['Assets']['2021']:,.1f}M
|
148 |
+
- Asset Growth: {metrics['Assets']['Growth']:.1f}%
|
149 |
+
|
150 |
+
Based on these metrics, provide:
|
151 |
+
1. Financial Performance Assessment
|
152 |
+
2. Key Strengths and Weaknesses
|
153 |
+
3. Strategic Recommendations</human>"""
|
154 |
|
155 |
def generate_analysis_prompt(self, metrics):
|
156 |
"""Create focused analysis prompt"""
|