CCockrum commited on
Commit
d780f21
·
verified ·
1 Parent(s): ac76927

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -2
app.py CHANGED
@@ -22,7 +22,7 @@ sector_averages = {
22
  "Energy": {"P/E Ratio": 12, "P/S Ratio": 1.2, "P/B Ratio": 1.3},
23
  }
24
 
25
- # --- Helper Functions ---
26
 
27
  def safe_request(url):
28
  try:
@@ -35,6 +35,7 @@ def safe_request(url):
35
  print(f"DEBUG: Other error occurred: {err}")
36
  return None
37
 
 
38
  def get_company_info(symbol):
39
  api_key = os.getenv("POLYGON_API_KEY")
40
  if not api_key:
@@ -53,6 +54,7 @@ def get_company_info(symbol):
53
  }
54
  return None
55
 
 
56
  def get_current_price(symbol):
57
  api_key = os.getenv("POLYGON_API_KEY")
58
  url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={api_key}"
@@ -62,6 +64,7 @@ def get_current_price(symbol):
62
  return float(data['c'])
63
  return None
64
 
 
65
  def get_dividends(symbol):
66
  api_key = os.getenv("POLYGON_API_KEY")
67
  url = f"https://api.polygon.io/v3/reference/dividends?ticker={symbol}&apiKey={api_key}"
@@ -74,6 +77,7 @@ def get_dividends(symbol):
74
  }
75
  return {'Dividend Amount': 0, 'Ex-Dividend Date': 'N/A'}
76
 
 
77
  def get_historical_prices(symbol):
78
  api_key = os.getenv("POLYGON_API_KEY")
79
  end = datetime.date.today()
@@ -87,6 +91,7 @@ def get_historical_prices(symbol):
87
  return dates, prices
88
  return [], []
89
 
 
90
  def generate_summary(info, ratios):
91
  recommendation = "Hold"
92
  if ratios['P/E Ratio'] < 15 and ratios['P/B Ratio'] < 2 and ratios['PEG Ratio'] < 1.0 and ratios['Dividend Yield (%)'] > 2:
@@ -112,15 +117,20 @@ def generate_summary(info, ratios):
112
  summary = summarizer(report, max_length=250, min_length=100, do_sample=False)[0]['summary_text']
113
  return summary
114
 
 
115
  def answer_investing_question(question):
116
  prompt = (
117
  f"Someone asked: '{question}'. "
118
- f"Please answer clearly but thoroughly and in a conversational tone without restating the question. "
119
  f"Keep the answer beginner-friendly and encouraging."
120
  )
121
  response = summarizer(prompt, max_length=200, min_length=60, do_sample=False)[0]['summary_text']
122
  return response
123
 
 
 
 
 
124
  def calculate_ratios(market_cap, total_revenue, price, dividend_amount, assumed_eps=5.0, growth_rate=0.1, book_value=500000000):
125
  pe_ratio = price / assumed_eps if assumed_eps else 0
126
  ps_ratio = market_cap / total_revenue if total_revenue else 0
 
22
  "Energy": {"P/E Ratio": 12, "P/S Ratio": 1.2, "P/B Ratio": 1.3},
23
  }
24
 
25
+ # Helper Functions
26
 
27
  def safe_request(url):
28
  try:
 
35
  print(f"DEBUG: Other error occurred: {err}")
36
  return None
37
 
38
+
39
  def get_company_info(symbol):
40
  api_key = os.getenv("POLYGON_API_KEY")
41
  if not api_key:
 
54
  }
55
  return None
56
 
57
+
58
  def get_current_price(symbol):
59
  api_key = os.getenv("POLYGON_API_KEY")
60
  url = f"https://api.polygon.io/v2/aggs/ticker/{symbol}/prev?adjusted=true&apiKey={api_key}"
 
64
  return float(data['c'])
65
  return None
66
 
67
+
68
  def get_dividends(symbol):
69
  api_key = os.getenv("POLYGON_API_KEY")
70
  url = f"https://api.polygon.io/v3/reference/dividends?ticker={symbol}&apiKey={api_key}"
 
77
  }
78
  return {'Dividend Amount': 0, 'Ex-Dividend Date': 'N/A'}
79
 
80
+
81
  def get_historical_prices(symbol):
82
  api_key = os.getenv("POLYGON_API_KEY")
83
  end = datetime.date.today()
 
91
  return dates, prices
92
  return [], []
93
 
94
+
95
  def generate_summary(info, ratios):
96
  recommendation = "Hold"
97
  if ratios['P/E Ratio'] < 15 and ratios['P/B Ratio'] < 2 and ratios['PEG Ratio'] < 1.0 and ratios['Dividend Yield (%)'] > 2:
 
117
  summary = summarizer(report, max_length=250, min_length=100, do_sample=False)[0]['summary_text']
118
  return summary
119
 
120
+
121
  def answer_investing_question(question):
122
  prompt = (
123
  f"Someone asked: '{question}'. "
124
+ f"Please answer clearly, simply, and in a conversational tone without restating the question. "
125
  f"Keep the answer beginner-friendly and encouraging."
126
  )
127
  response = summarizer(prompt, max_length=200, min_length=60, do_sample=False)[0]['summary_text']
128
  return response
129
 
130
+
131
+ # (Rest of the app continues with stock_research, download_report, and Gradio UI, including improved Valuation Ratios with sector ideal comparison and polished UI.)
132
+
133
+
134
  def calculate_ratios(market_cap, total_revenue, price, dividend_amount, assumed_eps=5.0, growth_rate=0.1, book_value=500000000):
135
  pe_ratio = price / assumed_eps if assumed_eps else 0
136
  ps_ratio = market_cap / total_revenue if total_revenue else 0