sentivity commited on
Commit
bf08959
·
verified ·
1 Parent(s): 7561d72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -101,7 +101,7 @@ def fetch_articles(ticker):
101
  title = article.get("title", "")
102
  description = article.get("description", "")
103
  return title + " " + description
104
- return f"No news articles found for {ticker}."
105
  # checks specific HTTP errors
106
  except requests.exceptions.HTTPError as http_err:
107
  print(f"[ERROR] HTTP error for {ticker}: {http_err}")
@@ -136,8 +136,15 @@ def analyze_ticker(user_ticker: str):
136
  continue
137
 
138
  print(f"[INFO] Fetching fresh data for {tk}")
 
139
  article_text = fetch_articles(tk)
140
- sentiment_score = predict_sentiment(article_text)
 
 
 
 
 
 
141
  timestamp = datetime.datetime.utcnow()
142
 
143
  cache_entry = {
@@ -156,15 +163,21 @@ def analyze_ticker(user_ticker: str):
156
  def display_sentiment(results):
157
  html = "<h2>Sentiment Analysis</h2><ul>"
158
  for r in results:
159
- ts_str = r["timestamp"].strftime("%Y‑%m‑%d %H:%M:%S UTC")
 
 
 
 
 
160
  html += (
161
  f"<li><b>{r['ticker']}</b> &nbsp;({ts_str})<br>"
162
  f"{r['article']}<br>"
163
- f"<i>Sentiment score:</i> {r['sentiment']:.2f}" "</li>"
164
  )
165
  html += "</ul>"
166
  return html
167
 
 
168
  with gr.Blocks() as demo:
169
  gr.Markdown("# Ticker vs. SPY Sentiment Tracker")
170
  input_box = gr.Textbox(label="Enter any ticker symbol (e.g., AAPL)")
 
101
  title = article.get("title", "")
102
  description = article.get("description", "")
103
  return title + " " + description
104
+ return None
105
  # checks specific HTTP errors
106
  except requests.exceptions.HTTPError as http_err:
107
  print(f"[ERROR] HTTP error for {ticker}: {http_err}")
 
136
  continue
137
 
138
  print(f"[INFO] Fetching fresh data for {tk}")
139
+
140
  article_text = fetch_articles(tk)
141
+
142
+ if article_text is None:
143
+ sentiment_score = None
144
+ article_text = f"No news articles found for {tk}."
145
+ else:
146
+ sentiment_score = predict_sentiment(article_text)
147
+
148
  timestamp = datetime.datetime.utcnow()
149
 
150
  cache_entry = {
 
163
  def display_sentiment(results):
164
  html = "<h2>Sentiment Analysis</h2><ul>"
165
  for r in results:
166
+ ts_str = r["timestamp"].strftime("%Y-%m-%d %H:%M:%S UTC")
167
+ score_display = (
168
+ f"{r['sentiment']:.2f}"
169
+ if r['sentiment'] is not None else
170
+ "—"
171
+ )
172
  html += (
173
  f"<li><b>{r['ticker']}</b> &nbsp;({ts_str})<br>"
174
  f"{r['article']}<br>"
175
+ f"<i>Sentiment score:</i> {score_display}</li>"
176
  )
177
  html += "</ul>"
178
  return html
179
 
180
+
181
  with gr.Blocks() as demo:
182
  gr.Markdown("# Ticker vs. SPY Sentiment Tracker")
183
  input_box = gr.Textbox(label="Enter any ticker symbol (e.g., AAPL)")