Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -265,29 +265,32 @@ def handle_custom_question(question):
|
|
| 265 |
|
| 266 |
if "financial health" in q:
|
| 267 |
latest_year = df["year"].max()
|
|
|
|
|
|
|
| 268 |
revenue = df[df["tlabel"].astype(str).str.contains("revenue", case=False, na=False)]
|
| 269 |
net_income = df[df["tag"].astype(str).isin(["NetIncomeLoss", "NetIncome"])]
|
| 270 |
assets = df[df["tag"].astype(str).str.contains("Assets", case=False, na=False)]
|
| 271 |
-
equity = df[df["tag"].astype(str).str.contains("Equity", case=False, na=False) & ~df["tag"].astype(str).str.contains("Liabilities")]
|
| 272 |
-
|
| 273 |
-
|
| 274 |
rev_latest = revenue[revenue["year"] == latest_year]["value"].sum()
|
| 275 |
net_income_latest = net_income[net_income["year"] == latest_year]["value"].sum()
|
| 276 |
assets_latest = assets[assets["year"] == latest_year]["value"].sum()
|
| 277 |
-
liabilities_latest = liabilities[liabilities["year"] == latest_year]["value"].sum()
|
| 278 |
equity_latest = equity[equity["year"] == latest_year]["value"].sum()
|
| 279 |
-
|
|
|
|
| 280 |
profit_margin = (net_income_latest / rev_latest * 100) if rev_latest > 0 else 0
|
| 281 |
-
debt_to_equity = (
|
| 282 |
roa = (net_income_latest / assets_latest * 100) if assets_latest > 0 else 0
|
| 283 |
-
|
|
|
|
| 284 |
if profit_margin > 15 and debt_to_equity < 1 and roa > 5:
|
| 285 |
health = "Healthy"
|
| 286 |
elif profit_margin > 5 and debt_to_equity < 2:
|
| 287 |
health = "Watchlist"
|
| 288 |
else:
|
| 289 |
health = "Risky"
|
| 290 |
-
|
| 291 |
return (
|
| 292 |
f"For {latest_year}, Apple is classified as **{health}** based on:\n"
|
| 293 |
f"- Profit Margin: {profit_margin:.2f}%\n"
|
|
|
|
| 265 |
|
| 266 |
if "financial health" in q:
|
| 267 |
latest_year = df["year"].max()
|
| 268 |
+
|
| 269 |
+
# Filter relevant rows
|
| 270 |
revenue = df[df["tlabel"].astype(str).str.contains("revenue", case=False, na=False)]
|
| 271 |
net_income = df[df["tag"].astype(str).isin(["NetIncomeLoss", "NetIncome"])]
|
| 272 |
assets = df[df["tag"].astype(str).str.contains("Assets", case=False, na=False)]
|
| 273 |
+
equity = df[df["tag"].astype(str).str.contains("Equity", case=False, na=False) & ~df["tag"].astype(str).str.contains("Liabilities", case=False, na=False)]
|
| 274 |
+
|
| 275 |
+
# Get latest values
|
| 276 |
rev_latest = revenue[revenue["year"] == latest_year]["value"].sum()
|
| 277 |
net_income_latest = net_income[net_income["year"] == latest_year]["value"].sum()
|
| 278 |
assets_latest = assets[assets["year"] == latest_year]["value"].sum()
|
|
|
|
| 279 |
equity_latest = equity[equity["year"] == latest_year]["value"].sum()
|
| 280 |
+
|
| 281 |
+
# Calculate metrics
|
| 282 |
profit_margin = (net_income_latest / rev_latest * 100) if rev_latest > 0 else 0
|
| 283 |
+
debt_to_equity = ((assets_latest - equity_latest) / equity_latest) if equity_latest > 0 else 0
|
| 284 |
roa = (net_income_latest / assets_latest * 100) if assets_latest > 0 else 0
|
| 285 |
+
|
| 286 |
+
# Classification
|
| 287 |
if profit_margin > 15 and debt_to_equity < 1 and roa > 5:
|
| 288 |
health = "Healthy"
|
| 289 |
elif profit_margin > 5 and debt_to_equity < 2:
|
| 290 |
health = "Watchlist"
|
| 291 |
else:
|
| 292 |
health = "Risky"
|
| 293 |
+
|
| 294 |
return (
|
| 295 |
f"For {latest_year}, Apple is classified as **{health}** based on:\n"
|
| 296 |
f"- Profit Margin: {profit_margin:.2f}%\n"
|