yk07577 commited on
Commit
3cb357d
·
verified ·
1 Parent(s): 52d7c80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py CHANGED
@@ -238,6 +238,44 @@ def handle_custom_question(question):
238
  buf.seek(0)
239
  return "Here is the forecasted revenue for the next 2 quarters.", Image.open(buf)
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  return None, None
242
 
243
  def ask_question(history, question):
 
238
  buf.seek(0)
239
  return "Here is the forecasted revenue for the next 2 quarters.", Image.open(buf)
240
 
241
+ if "financial health" in question.lower():
242
+ df["year"] = df["ddate"].dt.year
243
+ latest_year = df["year"].max()
244
+
245
+ # Extract required metrics
246
+ revenue = df[df["tlabel"].str.contains("revenue", case=False, na=False)]
247
+ net_income = df[df["tlabel"].str.contains("net income", case=False, na=False)]
248
+ assets = df[df["tlabel"].str.contains("total assets", case=False, na=False)]
249
+ liabilities = df[df["tlabel"].str.contains("total liabilities", case=False, na=False)]
250
+ equity = df[df["tlabel"].str.contains("equity", case=False, na=False)]
251
+
252
+ rev_latest = revenue[revenue["year"] == latest_year]["value"].sum()
253
+ net_income_latest = net_income[net_income["year"] == latest_year]["value"].sum()
254
+ assets_latest = assets[assets["year"] == latest_year]["value"].sum()
255
+ liabilities_latest = liabilities[liabilities["year"] == latest_year]["value"].sum()
256
+ equity_latest = equity[equity["year"] == latest_year]["value"].sum()
257
+
258
+ # Calculate ratios
259
+ profit_margin = (net_income_latest / rev_latest * 100) if rev_latest > 0 else 0
260
+ debt_to_equity = (liabilities_latest / equity_latest) if equity_latest > 0 else 0
261
+ roa = (net_income_latest / assets_latest * 100) if assets_latest > 0 else 0
262
+
263
+ # Classification logic
264
+ if profit_margin > 15 and debt_to_equity < 1 and roa > 5:
265
+ health = "Healthy"
266
+ elif profit_margin > 5 and debt_to_equity < 2:
267
+ health = "Watchlist"
268
+ else:
269
+ health = "Risky"
270
+
271
+ return (
272
+ f"For {latest_year}, Apple is classified as **{health}** based on:\n"
273
+ f"- Profit Margin: {profit_margin:.2f}%\n"
274
+ f"- Debt-to-Equity: {debt_to_equity:.2f}\n"
275
+ f"- Return on Assets (ROA): {roa:.2f}%",
276
+ None
277
+ )
278
+
279
  return None, None
280
 
281
  def ask_question(history, question):