mr601s commited on
Commit
f518f63
Β·
verified Β·
1 Parent(s): 657d8f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -261
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  import requests
3
- import yfinance as yf
4
  import plotly.graph_objects as go
5
  import feedparser
6
  import json
@@ -9,7 +8,28 @@ import random
9
  from datetime import datetime, timedelta
10
  import pandas as pd
11
 
12
- # SEC Utilities
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  class SECUtils:
14
  def __init__(self):
15
  self.cik_lookup_url = "https://www.sec.gov/files/company_tickers.json"
@@ -17,16 +37,14 @@ class SECUtils:
17
  self.headers = {
18
  "User-Agent": "StockResearchMVP/1.0 (educational@example.com)"
19
  }
20
-
21
  def get_cik(self, ticker):
22
  try:
23
- # Add small delay to avoid rate limiting
24
  time.sleep(0.5)
25
  response = requests.get(self.cik_lookup_url, headers=self.headers, timeout=20)
26
  if response.status_code != 200:
27
  return None
28
  data = response.json()
29
-
30
  for k, v in data.items():
31
  if isinstance(v, dict) and v.get('ticker', '').upper() == ticker.upper():
32
  return str(v['cik_str']).zfill(10)
@@ -34,282 +52,80 @@ class SECUtils:
34
  except Exception as e:
35
  print(f"CIK lookup error: {e}")
36
  return None
37
-
38
  def get_recent_filings(self, ticker):
39
  try:
40
  cik = self.get_cik(ticker)
41
  if not cik:
42
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ CIK not found. This may be a newer company or ticker not in SEC database.\n\nπŸ’‘ Try checking [SEC EDGAR directly](https://www.sec.gov/edgar/search/) for this company."
43
-
44
- # Add delay for rate limiting
45
  time.sleep(0.5)
46
  url = self.edgar_search_url.format(cik=cik)
47
  response = requests.get(url, headers=self.headers, timeout=20)
48
-
49
  if response.status_code != 200:
50
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ Unable to fetch SEC data (Status: {response.status_code}).\n\nπŸ’‘ Try [SEC EDGAR search](https://www.sec.gov/edgar/search/) directly."
51
-
52
  data = response.json()
53
  filings = data.get('filings', {}).get('recent', {})
54
-
55
  if not filings or not filings.get('form'):
56
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ No recent filings found for CIK: {cik}."
57
-
58
  result = f"πŸ“„ **SEC Filings for {ticker}** (CIK: {cik})\n\n"
59
-
60
- # Get last 5 filings
61
  forms = filings.get('form', [])
62
  dates = filings.get('filingDate', [])
63
  accessions = filings.get('accessionNumber', [])
64
-
65
  for i in range(min(5, len(forms))):
66
  if i < len(dates) and i < len(accessions):
67
  form = forms[i]
68
  filing_date = dates[i]
69
  accession_num = accessions[i].replace('-', '')
70
-
71
  filing_url = f"https://www.sec.gov/Archives/edgar/data/{int(cik)}/{accession_num}/"
72
  result += f"β€’ **{form}** - {filing_date}\n"
73
  result += f" πŸ“Ž [View Filing]({filing_url})\n\n"
74
-
75
  return result
76
-
77
  except Exception as e:
78
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ Error fetching SEC filings: {str(e)}\n\nπŸ’‘ Try [SEC EDGAR search](https://www.sec.gov/edgar/search/) directly."
79
 
80
- # News Utilities with better error handling
81
  class NewsUtils:
82
  def __init__(self):
83
  self.headers = {"User-Agent": "StockResearchMVP/1.0 (educational@example.com)"}
84
-
85
  def get_yahoo_news(self, ticker):
86
  try:
87
- # Add delay to avoid rate limiting
88
  time.sleep(random.uniform(0.5, 1.0))
89
  url = f"https://feeds.finance.yahoo.com/rss/2.0/headline?s={ticker}&region=US&lang=en-US"
90
  feed = feedparser.parse(url)
91
-
92
  if not feed.entries:
93
  return f"πŸ“° **Latest News for {ticker}**\n\n❌ No recent news found via RSS feed.\n\nπŸ’‘ Try these alternatives:\nβ€’ [Yahoo Finance News](https://finance.yahoo.com/quote/{ticker}/news)\nβ€’ [Google Finance](https://www.google.com/finance/quote/{ticker}:NASDAQ)\nβ€’ [MarketWatch](https://www.marketwatch.com/investing/stock/{ticker})"
94
-
95
  result = f"πŸ“° **Latest News for {ticker}**\n\n"
96
-
97
  for i, entry in enumerate(feed.entries[:5]):
98
- title = getattr(entry, 'title', 'No title')[:100] + '...' if len(getattr(entry, 'title', '')) > 100 else getattr(entry, 'title', 'No title')
99
  link = getattr(entry, 'link', '#')
100
  pub_date = getattr(entry, 'published', 'Unknown date')
101
-
102
  result += f"{i+1}. **{title}**\n"
103
- result += f" πŸ“… {pub_date}\n"
104
- result += f" πŸ”— [Read More]({link})\n\n"
105
-
106
  return result
107
-
108
  except Exception as e:
109
  return f"πŸ“° **Latest News for {ticker}**\n\n❌ Error fetching news: {str(e)}\n\nπŸ’‘ Try these alternatives:\nβ€’ [Yahoo Finance News](https://finance.yahoo.com/quote/{ticker}/news)\nβ€’ [Google Finance](https://www.google.com/finance/quote/{ticker}:NASDAQ)\nβ€’ [MarketWatch](https://www.marketwatch.com/investing/stock/{ticker})"
110
 
111
- # Finance Utilities with retry logic
112
- class FinanceUtils:
113
- def __init__(self):
114
- self.session = requests.Session()
115
- self.session.headers.update({'User-Agent': 'StockResearchMVP/1.0'})
116
-
117
- def get_stock_quote_with_retry(self, ticker, max_retries=2):
118
- """Get stock quote with retry logic and delays"""
119
- for attempt in range(max_retries):
120
- try:
121
- # Add increasing delays between attempts
122
- if attempt > 0:
123
- delay = random.uniform(1, 3) * attempt
124
- time.sleep(delay)
125
-
126
- stock = yf.Ticker(ticker)
127
-
128
- # Try to get basic info first
129
- info = stock.info
130
-
131
- if info and len(info) > 5:
132
- return info
133
-
134
- except Exception as e:
135
- if attempt == max_retries - 1: # Last attempt
136
- print(f"Final attempt failed for {ticker}: {e}")
137
- return None
138
- else:
139
- print(f"Attempt {attempt + 1} failed for {ticker}: {e}")
140
- continue
141
-
142
- return None
143
-
144
- def get_stock_quote(self, ticker):
145
- try:
146
- info = self.get_stock_quote_with_retry(ticker)
147
-
148
- if not info:
149
- return f"πŸ’° **Stock Quote for {ticker}**\n\n❌ Unable to fetch stock data (likely rate limited).\n\nπŸ’‘ Try these alternatives:\nβ€’ [Yahoo Finance](https://finance.yahoo.com/quote/{ticker})\nβ€’ [Google Finance](https://www.google.com/finance/quote/{ticker}:NASDAQ)\nβ€’ [MarketWatch](https://www.marketwatch.com/investing/stock/{ticker})\nβ€’ Wait a moment and try again"
150
-
151
- # Get current price with multiple fallbacks
152
- price = (info.get('currentPrice') or
153
- info.get('regularMarketPrice') or
154
- info.get('previousClose') or
155
- info.get('ask') or
156
- info.get('bid'))
157
-
158
- prev_close = info.get('previousClose')
159
-
160
- result = f"πŸ’° **Stock Quote for {ticker}**\n\n"
161
-
162
- # Company name
163
- company_name = info.get('longName', info.get('shortName', ticker))
164
- if company_name and company_name != ticker:
165
- result += f"**{company_name}**\n\n"
166
-
167
- if price is not None and price != 'N/A':
168
- result += f"β€’ **Current Price**: ${price:.2f}\n"
169
-
170
- if prev_close is not None and prev_close != 'N/A' and price != prev_close:
171
- change = price - prev_close
172
- change_pct = (change / prev_close * 100) if prev_close != 0 else 0
173
- change_emoji = "πŸ“ˆ" if change >= 0 else "πŸ“‰"
174
- result += f"β€’ **Previous Close**: ${prev_close:.2f}\n"
175
- result += f"β€’ **Change**: {change_emoji} ${change:.2f} ({change_pct:+.2f}%)\n"
176
-
177
- # Additional info with null checks
178
- volume = info.get('volume') or info.get('regularMarketVolume')
179
- if volume is not None:
180
- result += f"β€’ **Volume**: {volume:,}\n"
181
-
182
- market_cap = info.get('marketCap')
183
- if market_cap is not None:
184
- if market_cap >= 1e12:
185
- result += f"β€’ **Market Cap**: ${market_cap/1e12:.2f}T\n"
186
- elif market_cap >= 1e9:
187
- result += f"β€’ **Market Cap**: ${market_cap/1e9:.2f}B\n"
188
- elif market_cap >= 1e6:
189
- result += f"β€’ **Market Cap**: ${market_cap/1e6:.2f}M\n"
190
- else:
191
- result += f"β€’ **Market Cap**: ${market_cap:,}\n"
192
-
193
- high_52w = info.get('fiftyTwoWeekHigh')
194
- low_52w = info.get('fiftyTwoWeekLow')
195
- if high_52w is not None:
196
- result += f"β€’ **52W High**: ${high_52w:.2f}\n"
197
- if low_52w is not None:
198
- result += f"β€’ **52W Low**: ${low_52w:.2f}\n"
199
-
200
- # Exchange info
201
- exchange = info.get('exchange', info.get('market', ''))
202
- if exchange:
203
- result += f"β€’ **Exchange**: {exchange}\n"
204
-
205
- result += f"\nπŸ’‘ [More details on Yahoo Finance](https://finance.yahoo.com/quote/{ticker})"
206
-
207
- return result
208
-
209
- except Exception as e:
210
- return f"πŸ’° **Stock Quote for {ticker}**\n\n❌ Error: {str(e)}\n\nπŸ’‘ This might be due to:\nβ€’ Rate limiting (try again in a moment)\nβ€’ Invalid ticker symbol\nβ€’ Network issues\n\nTry [Yahoo Finance](https://finance.yahoo.com/quote/{ticker}) directly."
211
-
212
- def get_financial_summary(self, ticker):
213
- try:
214
- info = self.get_stock_quote_with_retry(ticker)
215
-
216
- if not info:
217
- return f"πŸ“Š **Financial Summary for {ticker}**\n\n❌ Unable to fetch financial data (likely rate limited).\n\nπŸ’‘ Try [Yahoo Finance Financials](https://finance.yahoo.com/quote/{ticker}/financials) directly."
218
-
219
- result = f"πŸ“Š **Financial Summary for {ticker}**\n\n"
220
-
221
- # Company basics
222
- company_name = info.get('longName', info.get('shortName', ''))
223
- if company_name:
224
- result += f"**{company_name}**\n\n"
225
-
226
- sector = info.get('sector')
227
- industry = info.get('industry')
228
- if sector:
229
- result += f"β€’ **Sector**: {sector}\n"
230
- if industry:
231
- result += f"β€’ **Industry**: {industry}\n"
232
-
233
- if sector or industry:
234
- result += "\n"
235
-
236
- # Financial metrics
237
- revenue = info.get('totalRevenue')
238
- if revenue is not None:
239
- if revenue >= 1e12:
240
- result += f"β€’ **Revenue (TTM)**: ${revenue/1e12:.2f}T\n"
241
- elif revenue >= 1e9:
242
- result += f"β€’ **Revenue (TTM)**: ${revenue/1e9:.2f}B\n"
243
- elif revenue >= 1e6:
244
- result += f"β€’ **Revenue (TTM)**: ${revenue/1e6:.2f}M\n"
245
- else:
246
- result += f"β€’ **Revenue (TTM)**: ${revenue:,}\n"
247
-
248
- # Profitability
249
- profit_margins = info.get('profitMargins')
250
- if profit_margins is not None:
251
- result += f"β€’ **Profit Margin**: {profit_margins:.2%}\n"
252
-
253
- # Valuation ratios
254
- pe_ratio = info.get('trailingPE')
255
- if pe_ratio is not None and pe_ratio > 0:
256
- result += f"β€’ **P/E Ratio**: {pe_ratio:.2f}\n"
257
-
258
- pb_ratio = info.get('priceToBook')
259
- if pb_ratio is not None and pb_ratio > 0:
260
- result += f"β€’ **P/B Ratio**: {pb_ratio:.2f}\n"
261
-
262
- # Dividend info
263
- dividend_yield = info.get('dividendYield')
264
- if dividend_yield is not None:
265
- result += f"β€’ **Dividend Yield**: {dividend_yield:.2%}\n"
266
-
267
- # Beta (risk measure)
268
- beta = info.get('beta')
269
- if beta is not None:
270
- result += f"β€’ **Beta**: {beta:.2f}\n"
271
-
272
- # If very limited data, show what we can
273
- if result == f"πŸ“Š **Financial Summary for {ticker}**\n\n":
274
- result += "Limited financial data available for this ticker.\n"
275
- if company_name:
276
- result += f"β€’ **Company**: {company_name}\n"
277
- if sector:
278
- result += f"β€’ **Sector**: {sector}\n"
279
- if industry:
280
- result += f"β€’ **Industry**: {industry}\n"
281
-
282
- result += f"\nπŸ’‘ [More financials on Yahoo Finance](https://finance.yahoo.com/quote/{ticker}/financials)"
283
-
284
- return result
285
-
286
- except Exception as e:
287
- return f"πŸ“Š **Financial Summary for {ticker}**\n\n❌ Error: {str(e)}\n\nTry [Yahoo Finance Financials](https://finance.yahoo.com/quote/{ticker}/financials) directly."
288
-
289
- # Chart Utilities with error handling
290
  class ChartUtils:
291
- def create_price_chart(self, ticker, period="3mo"): # Shorter period for faster loading
292
  try:
293
- # Add delay for rate limiting
294
  time.sleep(random.uniform(0.5, 1.5))
295
-
296
  stock = yf.Ticker(ticker)
297
  hist = stock.history(period=period)
298
-
299
  if hist.empty:
300
  return None
301
-
302
  fig = go.Figure()
303
-
304
  fig.add_trace(go.Scatter(
305
  x=hist.index,
306
  y=hist['Close'],
307
  mode='lines',
308
  name=f'{ticker} Close Price',
309
  line=dict(color='#00d4aa', width=2),
310
- hovertemplate='Date: %{x}<br>Price: $%{y:.2f}<extra></extra>'
311
  ))
312
-
313
  fig.update_layout(
314
  title=f'{ticker} Stock Price - Last {period}',
315
  xaxis_title='Date',
@@ -321,48 +137,44 @@ class ChartUtils:
321
  plot_bgcolor='rgba(0,0,0,0)',
322
  paper_bgcolor='rgba(0,0,0,0)',
323
  )
324
-
325
  return fig
326
-
327
  except Exception as e:
328
  print(f"Chart error for {ticker}: {e}")
329
  return None
330
 
331
- # Initialize utility classes
 
 
 
 
 
332
  sec_utils = SECUtils()
333
  news_utils = NewsUtils()
334
- finance_utils = FinanceUtils()
335
  chart_utils = ChartUtils()
336
 
 
337
  def update_stock_info(ticker):
338
  """Update all sections when ticker changes"""
339
  if not ticker or not ticker.strip():
340
  empty_msg = "Enter a ticker symbol to see data"
341
  return empty_msg, empty_msg, empty_msg, empty_msg, None
342
-
343
  ticker = ticker.upper().strip()
344
-
345
- # Show loading message
346
- loading_msg = f"⏳ Loading data for {ticker}..."
347
-
348
- # Get data for each section with delays between calls
349
- quote_data = finance_utils.get_stock_quote(ticker)
350
- time.sleep(0.3) # Small delay between different API calls
351
-
352
  news_data = news_utils.get_yahoo_news(ticker)
353
  time.sleep(0.3)
354
-
355
  filings_data = sec_utils.get_recent_filings(ticker)
356
  time.sleep(0.3)
357
-
358
- financial_data = finance_utils.get_financial_summary(ticker)
359
  time.sleep(0.3)
360
-
361
  chart = chart_utils.create_price_chart(ticker)
362
-
363
  return quote_data, news_data, filings_data, financial_data, chart
364
 
365
- # Custom CSS
366
  css = """
367
  .gradio-container {
368
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
@@ -375,63 +187,51 @@ css = """
375
  }
376
  """
377
 
378
- # Create Gradio interface
379
  with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Stock Research Platform") as demo:
380
  gr.Markdown("""
381
- # πŸ“ˆ Stock Research Platform MVP
382
- **Comprehensive stock analysis with real-time data, news, SEC filings, and financial summaries.**
383
-
384
- 🎯 Enter a stock ticker symbol (like **AAPL**, **TSLA**, **MSFT**, **GOOGL**) to explore detailed information.
385
-
386
- ⚠️ **Note**: If you see rate limiting errors, wait 30 seconds and try again. This is a free service with usage limits.
387
- """)
388
-
389
  with gr.Row():
390
  with gr.Column(scale=3):
391
  ticker_input = gr.Textbox(
392
- label="Stock Ticker",
393
  placeholder="Enter ticker (e.g., AAPL, TSLA, MSFT)",
394
  value="AAPL"
395
  )
396
  with gr.Column(scale=1):
397
  refresh_btn = gr.Button("πŸ”„ Refresh Data", variant="primary", size="lg")
398
-
399
  with gr.Tabs():
400
  with gr.TabItem("πŸ’° Quote & Overview"):
401
  quote_output = gr.Markdown(value="Enter a ticker to see stock quote")
402
-
403
  with gr.TabItem("πŸ“° News"):
404
  news_output = gr.Markdown(value="Enter a ticker to see latest news")
405
-
406
  with gr.TabItem("πŸ“„ SEC Filings"):
407
  filings_output = gr.Markdown(value="Enter a ticker to see SEC filings")
408
-
409
  with gr.TabItem("πŸ“Š Financial Summary"):
410
  financial_output = gr.Markdown(value="Enter a ticker to see financial summary")
411
-
412
  with gr.TabItem("πŸ“ˆ Price Chart"):
413
  chart_output = gr.Plot(value=None)
414
-
415
  gr.Markdown("""
416
- ---
417
- **Data Sources:** Yahoo Finance, SEC EDGAR, Public RSS Feeds | **Educational Tool - Not Investment Advice**
418
-
419
- **Troubleshooting:** If you encounter errors, try waiting 30-60 seconds between requests to avoid rate limits.
420
- """)
421
-
422
- # Event handlers with rate limiting protection
423
  ticker_input.change(
424
  fn=update_stock_info,
425
  inputs=[ticker_input],
426
  outputs=[quote_output, news_output, filings_output, financial_output, chart_output]
427
  )
428
-
429
  refresh_btn.click(
430
  fn=update_stock_info,
431
  inputs=[ticker_input],
432
  outputs=[quote_output, news_output, filings_output, financial_output, chart_output]
433
  )
434
 
435
- # Launch the app
436
  if __name__ == "__main__":
437
  demo.launch(show_error=True)
 
1
  import gradio as gr
2
  import requests
 
3
  import plotly.graph_objects as go
4
  import feedparser
5
  import json
 
8
  from datetime import datetime, timedelta
9
  import pandas as pd
10
 
11
+ # === POLYGON CONFIG ===
12
+ # Insert your key here:
13
+ POLYGON_API_KEY = "fAhg47wPlf4FT6U2Hn23kQoQCQIyW0G_"
14
+
15
+ # === POLYGON QUOTE UTILITY ===
16
+ def fetch_polygon_quote(ticker, polygon_api_key=POLYGON_API_KEY):
17
+ url = f"https://api.polygon.io/v2/last/trade/{ticker.upper()}?apiKey={polygon_api_key}"
18
+ try:
19
+ response = requests.get(url, timeout=10)
20
+ response.raise_for_status()
21
+ data = response.json()
22
+ if "results" in data and "p" in data["results"]:
23
+ price = data["results"]["p"] # last trade price
24
+ ts = data["results"]["t"] # timestamp (optional, could convert)
25
+ return f"πŸ’° **Stock Quote for {ticker.upper()}**\n\nβ€’ **Current Price**: ${price:.2f}\n\n[More details on Polygon](https://polygon.io/stock/{ticker.upper()}/quote)"
26
+ else:
27
+ return f"❌ Quote data unavailable for {ticker.upper()}."
28
+ except Exception as e:
29
+ return f"❌ Error: {str(e)}"
30
+
31
+
32
+ # === SEC Utilities (no change) ===
33
  class SECUtils:
34
  def __init__(self):
35
  self.cik_lookup_url = "https://www.sec.gov/files/company_tickers.json"
 
37
  self.headers = {
38
  "User-Agent": "StockResearchMVP/1.0 (educational@example.com)"
39
  }
40
+
41
  def get_cik(self, ticker):
42
  try:
 
43
  time.sleep(0.5)
44
  response = requests.get(self.cik_lookup_url, headers=self.headers, timeout=20)
45
  if response.status_code != 200:
46
  return None
47
  data = response.json()
 
48
  for k, v in data.items():
49
  if isinstance(v, dict) and v.get('ticker', '').upper() == ticker.upper():
50
  return str(v['cik_str']).zfill(10)
 
52
  except Exception as e:
53
  print(f"CIK lookup error: {e}")
54
  return None
55
+
56
  def get_recent_filings(self, ticker):
57
  try:
58
  cik = self.get_cik(ticker)
59
  if not cik:
60
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ CIK not found. This may be a newer company or ticker not in SEC database.\n\nπŸ’‘ Try checking [SEC EDGAR directly](https://www.sec.gov/edgar/search/) for this company."
 
 
61
  time.sleep(0.5)
62
  url = self.edgar_search_url.format(cik=cik)
63
  response = requests.get(url, headers=self.headers, timeout=20)
 
64
  if response.status_code != 200:
65
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ Unable to fetch SEC data (Status: {response.status_code}).\n\nπŸ’‘ Try [SEC EDGAR search](https://www.sec.gov/edgar/search/) directly."
 
66
  data = response.json()
67
  filings = data.get('filings', {}).get('recent', {})
 
68
  if not filings or not filings.get('form'):
69
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ No recent filings found for CIK: {cik}."
 
70
  result = f"πŸ“„ **SEC Filings for {ticker}** (CIK: {cik})\n\n"
 
 
71
  forms = filings.get('form', [])
72
  dates = filings.get('filingDate', [])
73
  accessions = filings.get('accessionNumber', [])
 
74
  for i in range(min(5, len(forms))):
75
  if i < len(dates) and i < len(accessions):
76
  form = forms[i]
77
  filing_date = dates[i]
78
  accession_num = accessions[i].replace('-', '')
 
79
  filing_url = f"https://www.sec.gov/Archives/edgar/data/{int(cik)}/{accession_num}/"
80
  result += f"β€’ **{form}** - {filing_date}\n"
81
  result += f" πŸ“Ž [View Filing]({filing_url})\n\n"
 
82
  return result
 
83
  except Exception as e:
84
  return f"πŸ“„ **SEC Filings for {ticker}**\n\n❌ Error fetching SEC filings: {str(e)}\n\nπŸ’‘ Try [SEC EDGAR search](https://www.sec.gov/edgar/search/) directly."
85
 
86
+ # === News Utilities (no change) ===
87
  class NewsUtils:
88
  def __init__(self):
89
  self.headers = {"User-Agent": "StockResearchMVP/1.0 (educational@example.com)"}
 
90
  def get_yahoo_news(self, ticker):
91
  try:
 
92
  time.sleep(random.uniform(0.5, 1.0))
93
  url = f"https://feeds.finance.yahoo.com/rss/2.0/headline?s={ticker}&region=US&lang=en-US"
94
  feed = feedparser.parse(url)
 
95
  if not feed.entries:
96
  return f"πŸ“° **Latest News for {ticker}**\n\n❌ No recent news found via RSS feed.\n\nπŸ’‘ Try these alternatives:\nβ€’ [Yahoo Finance News](https://finance.yahoo.com/quote/{ticker}/news)\nβ€’ [Google Finance](https://www.google.com/finance/quote/{ticker}:NASDAQ)\nβ€’ [MarketWatch](https://www.marketwatch.com/investing/stock/{ticker})"
 
97
  result = f"πŸ“° **Latest News for {ticker}**\n\n"
 
98
  for i, entry in enumerate(feed.entries[:5]):
99
+ title = getattr(entry, 'title', 'No title')
100
  link = getattr(entry, 'link', '#')
101
  pub_date = getattr(entry, 'published', 'Unknown date')
 
102
  result += f"{i+1}. **{title}**\n"
103
+ result += f" πŸ“… {pub_date}\n"
104
+ result += f" πŸ”— [Read More]({link})\n\n"
 
105
  return result
 
106
  except Exception as e:
107
  return f"πŸ“° **Latest News for {ticker}**\n\n❌ Error fetching news: {str(e)}\n\nπŸ’‘ Try these alternatives:\nβ€’ [Yahoo Finance News](https://finance.yahoo.com/quote/{ticker}/news)\nβ€’ [Google Finance](https://www.google.com/finance/quote/{ticker}:NASDAQ)\nβ€’ [MarketWatch](https://www.marketwatch.com/investing/stock/{ticker})"
108
 
109
+ # === Chart Utilities (keeps using yfinance for now) ===
110
+ # If you want Polygon-powered charts, you can request that snippet.
111
+ import yfinance as yf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  class ChartUtils:
113
+ def create_price_chart(self, ticker, period="3mo"):
114
  try:
 
115
  time.sleep(random.uniform(0.5, 1.5))
 
116
  stock = yf.Ticker(ticker)
117
  hist = stock.history(period=period)
 
118
  if hist.empty:
119
  return None
 
120
  fig = go.Figure()
 
121
  fig.add_trace(go.Scatter(
122
  x=hist.index,
123
  y=hist['Close'],
124
  mode='lines',
125
  name=f'{ticker} Close Price',
126
  line=dict(color='#00d4aa', width=2),
127
+ hovertemplate='Date: %{x}<br>Price: $%{y:.2f}'
128
  ))
 
129
  fig.update_layout(
130
  title=f'{ticker} Stock Price - Last {period}',
131
  xaxis_title='Date',
 
137
  plot_bgcolor='rgba(0,0,0,0)',
138
  paper_bgcolor='rgba(0,0,0,0)',
139
  )
 
140
  return fig
 
141
  except Exception as e:
142
  print(f"Chart error for {ticker}: {e}")
143
  return None
144
 
145
+ # === Financial Summary Placeholder ===
146
+ # You could call Polygon's /vX/company endpoint, but not all summary info is available for free.
147
+ def get_financial_summary_placeholder(ticker):
148
+ return f"πŸ“Š **Financial Summary for {ticker.upper()}**\n\n❌ Detailed financials unavailable in the free Polygon tier.\n\nπŸ’‘ Try [Yahoo Finance Financials](https://finance.yahoo.com/quote/{ticker}/financials) for more."
149
+
150
+ # === Instantiate utilities ===
151
  sec_utils = SECUtils()
152
  news_utils = NewsUtils()
 
153
  chart_utils = ChartUtils()
154
 
155
+ # --- APP LOGIC ---
156
  def update_stock_info(ticker):
157
  """Update all sections when ticker changes"""
158
  if not ticker or not ticker.strip():
159
  empty_msg = "Enter a ticker symbol to see data"
160
  return empty_msg, empty_msg, empty_msg, empty_msg, None
 
161
  ticker = ticker.upper().strip()
162
+ # 1. Fetch quote via Polygon
163
+ quote_data = fetch_polygon_quote(ticker)
164
+ time.sleep(0.3)
165
+ # 2. News
 
 
 
 
166
  news_data = news_utils.get_yahoo_news(ticker)
167
  time.sleep(0.3)
168
+ # 3. SEC filings
169
  filings_data = sec_utils.get_recent_filings(ticker)
170
  time.sleep(0.3)
171
+ # 4. Financial summary (placeholder)
172
+ financial_data = get_financial_summary_placeholder(ticker)
173
  time.sleep(0.3)
174
+ # 5. Chart (yfinance)
175
  chart = chart_utils.create_price_chart(ticker)
 
176
  return quote_data, news_data, filings_data, financial_data, chart
177
 
 
178
  css = """
179
  .gradio-container {
180
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
 
187
  }
188
  """
189
 
 
190
  with gr.Blocks(css=css, theme=gr.themes.Soft(), title="Stock Research Platform") as demo:
191
  gr.Markdown("""
192
+ # πŸ“ˆ Stock Research Platform MVP
193
+ **Comprehensive stock analysis with real-time data, news, SEC filings, and financial summaries.**
194
+
195
+ 🎯 Enter a stock ticker symbol (like **AAPL**, **TSLA**, **MSFT**, **GOOGL**) to explore detailed information.
196
+
197
+ ⚠️ **Note**: Stock quote data provided by Polygon. News and SEC filings remain free. Charts may still be subject to Yahoo limits.
198
+ """)
 
199
  with gr.Row():
200
  with gr.Column(scale=3):
201
  ticker_input = gr.Textbox(
202
+ label="Stock Ticker",
203
  placeholder="Enter ticker (e.g., AAPL, TSLA, MSFT)",
204
  value="AAPL"
205
  )
206
  with gr.Column(scale=1):
207
  refresh_btn = gr.Button("πŸ”„ Refresh Data", variant="primary", size="lg")
 
208
  with gr.Tabs():
209
  with gr.TabItem("πŸ’° Quote & Overview"):
210
  quote_output = gr.Markdown(value="Enter a ticker to see stock quote")
 
211
  with gr.TabItem("πŸ“° News"):
212
  news_output = gr.Markdown(value="Enter a ticker to see latest news")
 
213
  with gr.TabItem("πŸ“„ SEC Filings"):
214
  filings_output = gr.Markdown(value="Enter a ticker to see SEC filings")
 
215
  with gr.TabItem("πŸ“Š Financial Summary"):
216
  financial_output = gr.Markdown(value="Enter a ticker to see financial summary")
 
217
  with gr.TabItem("πŸ“ˆ Price Chart"):
218
  chart_output = gr.Plot(value=None)
 
219
  gr.Markdown("""
220
+ ---
221
+ **Data Sources:** Polygon.io for quotes, Yahoo RSS for news, SEC EDGAR for filings.
222
+
223
+ **Troubleshooting:** If you encounter errors, double-check your ticker or wait and retry.
224
+ """)
 
 
225
  ticker_input.change(
226
  fn=update_stock_info,
227
  inputs=[ticker_input],
228
  outputs=[quote_output, news_output, filings_output, financial_output, chart_output]
229
  )
 
230
  refresh_btn.click(
231
  fn=update_stock_info,
232
  inputs=[ticker_input],
233
  outputs=[quote_output, news_output, filings_output, financial_output, chart_output]
234
  )
235
 
 
236
  if __name__ == "__main__":
237
  demo.launch(show_error=True)