Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import json
|
|
@@ -34,6 +37,16 @@ class FinancialAnalyzer:
|
|
| 34 |
except:
|
| 35 |
return 0.0
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def parse_financial_data(self, content):
|
| 38 |
"""Parse markdown content into structured data"""
|
| 39 |
try:
|
|
@@ -122,7 +135,8 @@ class FinancialAnalyzer:
|
|
| 122 |
def generate_analysis_prompt(self, metrics):
|
| 123 |
"""Create analysis prompt from metrics"""
|
| 124 |
try:
|
| 125 |
-
return f"""<human>
|
|
|
|
| 126 |
|
| 127 |
Revenue and Profitability:
|
| 128 |
- Total Revenue: ${metrics['Revenue']['2025']:,.1f}M
|
|
@@ -136,10 +150,8 @@ Balance Sheet Strength:
|
|
| 136 |
- Total Liabilities: ${metrics['Balance_Sheet']['Total_Liabilities_2025']:,.1f}M
|
| 137 |
- Shareholders' Equity: ${metrics['Balance_Sheet']['Equity_2025']:,.1f}M
|
| 138 |
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
2. Key Strengths and Weaknesses
|
| 142 |
-
3. Strategic Recommendations</human>"""
|
| 143 |
except Exception as e:
|
| 144 |
print(f"Error generating prompt: {str(e)}")
|
| 145 |
return ""
|
|
@@ -169,6 +181,10 @@ Provide a concise analysis of:
|
|
| 169 |
def analyze_financials(self, balance_sheet_file, income_stmt_file):
|
| 170 |
"""Main analysis function"""
|
| 171 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
# Read files
|
| 173 |
with open(balance_sheet_file, 'r') as f:
|
| 174 |
balance_sheet = f.read()
|
|
@@ -219,4 +235,7 @@ def create_interface():
|
|
| 219 |
|
| 220 |
if __name__ == "__main__":
|
| 221 |
iface = create_interface()
|
| 222 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Here is the updated complete code with the suggestions incorporated:
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
import gradio as gr
|
| 5 |
import pandas as pd
|
| 6 |
import json
|
|
|
|
| 37 |
except:
|
| 38 |
return 0.0
|
| 39 |
|
| 40 |
+
def is_valid_markdown(self, file_path):
|
| 41 |
+
"""Check if a file is a valid Markdown file"""
|
| 42 |
+
try:
|
| 43 |
+
with open(file_path, 'r') as f:
|
| 44 |
+
content = f.read()
|
| 45 |
+
# Simple check for Markdown structure
|
| 46 |
+
return any(line.startswith('#') or '|' in line for line in content.split('\n'))
|
| 47 |
+
except:
|
| 48 |
+
return False
|
| 49 |
+
|
| 50 |
def parse_financial_data(self, content):
|
| 51 |
"""Parse markdown content into structured data"""
|
| 52 |
try:
|
|
|
|
| 135 |
def generate_analysis_prompt(self, metrics):
|
| 136 |
"""Create analysis prompt from metrics"""
|
| 137 |
try:
|
| 138 |
+
return f"""<human>
|
| 139 |
+
Analyze these financial metrics for 2025 with a focus on business performance, trends, and risks:
|
| 140 |
|
| 141 |
Revenue and Profitability:
|
| 142 |
- Total Revenue: ${metrics['Revenue']['2025']:,.1f}M
|
|
|
|
| 150 |
- Total Liabilities: ${metrics['Balance_Sheet']['Total_Liabilities_2025']:,.1f}M
|
| 151 |
- Shareholders' Equity: ${metrics['Balance_Sheet']['Equity_2025']:,.1f}M
|
| 152 |
|
| 153 |
+
Explain key financial ratios and their implications. Discuss strategies for growth and risk mitigation.
|
| 154 |
+
</human>"""
|
|
|
|
|
|
|
| 155 |
except Exception as e:
|
| 156 |
print(f"Error generating prompt: {str(e)}")
|
| 157 |
return ""
|
|
|
|
| 181 |
def analyze_financials(self, balance_sheet_file, income_stmt_file):
|
| 182 |
"""Main analysis function"""
|
| 183 |
try:
|
| 184 |
+
# Validate files
|
| 185 |
+
if not (self.is_valid_markdown(balance_sheet_file) and self.is_valid_markdown(income_stmt_file)):
|
| 186 |
+
return "Error: One or both files are invalid or not in Markdown format."
|
| 187 |
+
|
| 188 |
# Read files
|
| 189 |
with open(balance_sheet_file, 'r') as f:
|
| 190 |
balance_sheet = f.read()
|
|
|
|
| 235 |
|
| 236 |
if __name__ == "__main__":
|
| 237 |
iface = create_interface()
|
| 238 |
+
iface.launch()
|
| 239 |
+
```
|
| 240 |
+
|
| 241 |
+
This version includes validation for Markdown files, enhanced error handling, and a refined analysis prompt for
|