CCockrum commited on
Commit
847dca7
·
verified ·
1 Parent(s): 8bf1fe5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -37,17 +37,32 @@ def get_company_fcf(symbol):
37
  response = requests.get(url)
38
  response.raise_for_status()
39
  data = response.json()
40
-
41
- # Navigate the JSON to find Free Cash Flow
42
  fcf = data['results'][0]['financials']['cash_flow_statement']['free_cash_flow']['value']
43
  return float(fcf)
44
-
45
  except Exception as e:
46
  print(f"DEBUG: Error fetching FCF for {symbol}: {e}")
47
  return None
48
 
49
- def generate_summary(symbol, intrinsic_value):
50
- text = f"Based on a DCF analysis, the intrinsic value of {symbol} is estimated to be ${intrinsic_value:,.2f}."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  summary = summarizer(text, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
52
  return summary
53
 
@@ -66,6 +81,7 @@ def dcf_interface(symbol, growth_rate, discount_rate, terminal_growth_rate, fore
66
  fcf_forecast, present_values, terminal_value, terminal_value_pv, total_intrinsic_value = discounted_cash_flow(
67
  fcf, growth_rate, discount_rate, terminal_growth_rate, forecast_years
68
  )
 
69
  df = pd.DataFrame({
70
  'Year': list(range(1, forecast_years + 1)),
71
  'Forecasted FCF ($)': fcf_forecast,
@@ -79,7 +95,11 @@ def dcf_interface(symbol, growth_rate, discount_rate, terminal_growth_rate, fore
79
  ax.set_ylabel('Free Cash Flow ($)')
80
  ax.grid(True)
81
 
82
- summary = generate_summary(symbol, total_intrinsic_value)
 
 
 
 
83
  return df, fig, summary
84
 
85
  iface = gr.Interface(
@@ -100,7 +120,7 @@ iface = gr.Interface(
100
  ["AAPL", 0.06, 0.08, 0.025, 5]
101
  ],
102
  title="AI-Powered DCF Valuation Calculator",
103
- description="Enter a stock symbol and your assumptions. The app pulls live data from Polygon.io, runs a DCF, and generates a research summary."
104
  )
105
 
106
  if __name__ == "__main__":
 
37
  response = requests.get(url)
38
  response.raise_for_status()
39
  data = response.json()
 
 
40
  fcf = data['results'][0]['financials']['cash_flow_statement']['free_cash_flow']['value']
41
  return float(fcf)
 
42
  except Exception as e:
43
  print(f"DEBUG: Error fetching FCF for {symbol}: {e}")
44
  return None
45
 
46
+ def get_current_stock_price(symbol):
47
+ api_key = os.getenv("POLYGON_API_KEY")
48
+ url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={api_key}"
49
+ try:
50
+ response = requests.get(url)
51
+ response.raise_for_status()
52
+ data = response.json()
53
+ price = data['results'][0]['c'] # 'c' = close price
54
+ return float(price)
55
+ except Exception as e:
56
+ print(f"DEBUG: Error fetching current price for {symbol}: {e}")
57
+ return None
58
+
59
+ def generate_summary(symbol, intrinsic_value, market_price):
60
+ verdict = "undervalued ✅" if intrinsic_value > market_price else "overvalued ⚠️"
61
+ text = (
62
+ f"Based on a DCF analysis, the intrinsic value of {symbol} is estimated to be "
63
+ f"${intrinsic_value:,.2f} compared to its current market price of ${market_price:,.2f}. "
64
+ f"The stock appears {verdict}."
65
+ )
66
  summary = summarizer(text, max_length=100, min_length=30, do_sample=False)[0]['summary_text']
67
  return summary
68
 
 
81
  fcf_forecast, present_values, terminal_value, terminal_value_pv, total_intrinsic_value = discounted_cash_flow(
82
  fcf, growth_rate, discount_rate, terminal_growth_rate, forecast_years
83
  )
84
+
85
  df = pd.DataFrame({
86
  'Year': list(range(1, forecast_years + 1)),
87
  'Forecasted FCF ($)': fcf_forecast,
 
95
  ax.set_ylabel('Free Cash Flow ($)')
96
  ax.grid(True)
97
 
98
+ market_price = get_current_stock_price(symbol)
99
+ if market_price is None:
100
+ market_price = 0 # fallback
101
+
102
+ summary = generate_summary(symbol, total_intrinsic_value, market_price)
103
  return df, fig, summary
104
 
105
  iface = gr.Interface(
 
120
  ["AAPL", 0.06, 0.08, 0.025, 5]
121
  ],
122
  title="AI-Powered DCF Valuation Calculator",
123
+ description="Enter a stock symbol and your assumptions. The app pulls live data from Polygon.io, runs a DCF, compares to the market price, and generates a research summary."
124
  )
125
 
126
  if __name__ == "__main__":