Spaces:
Sleeping
Sleeping
Update sentiment.py
Browse files- sentiment.py +1 -28
sentiment.py
CHANGED
|
@@ -1,8 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
-
News sentiment analysis using NLTK VADER + ProsusAI/FinBERT ensemble.
|
| 3 |
-
VADER provides fast rule-based scoring; FinBERT provides deep financial context.
|
| 4 |
-
The final score is a weighted blend (40% VADER, 60% FinBERT).
|
| 5 |
-
"""
|
| 6 |
|
| 7 |
import torch
|
| 8 |
from transformers import BertTokenizer, BertForSequenceClassification
|
|
@@ -11,31 +6,24 @@ import yfinance as yf
|
|
| 11 |
import nltk
|
| 12 |
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 13 |
|
| 14 |
-
# ββ Download VADER lexicon if missing ββββββββββββββββββββββββββ
|
| 15 |
try:
|
| 16 |
nltk.data.find("sentiment/vader_lexicon.zip")
|
| 17 |
except LookupError:
|
| 18 |
nltk.download("vader_lexicon", quiet=True)
|
| 19 |
|
| 20 |
-
# ββ Load models once at import βββββββββββββββββββββββββββββββββ
|
| 21 |
-
# VADER
|
| 22 |
_vader = SentimentIntensityAnalyzer()
|
| 23 |
|
| 24 |
-
# FinBERT
|
| 25 |
MODEL_NAME = "ProsusAI/finbert"
|
| 26 |
_tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
|
| 27 |
_finbert = BertForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 28 |
_finbert.eval()
|
| 29 |
FINBERT_LABELS = ["negative", "neutral", "positive"]
|
| 30 |
|
| 31 |
-
# Ensemble weights
|
| 32 |
VADER_WEIGHT = 0.4
|
| 33 |
FINBERT_WEIGHT = 0.6
|
| 34 |
|
| 35 |
|
| 36 |
-
# ββ Individual scorers βββββββββββββββββββββββββββββββββββββββββ
|
| 37 |
def _score_vader(text: str) -> dict:
|
| 38 |
-
"""VADER compound score mapped to label + confidence."""
|
| 39 |
scores = _vader.polarity_scores(text)
|
| 40 |
compound = scores["compound"]
|
| 41 |
|
|
@@ -51,7 +39,6 @@ def _score_vader(text: str) -> dict:
|
|
| 51 |
|
| 52 |
|
| 53 |
def _score_finbert(text: str) -> dict:
|
| 54 |
-
"""FinBERT probability-based label + confidence."""
|
| 55 |
tokens = _tokenizer(text, return_tensors="pt", padding=True,
|
| 56 |
truncation=True, max_length=128)
|
| 57 |
with torch.no_grad():
|
|
@@ -59,8 +46,7 @@ def _score_finbert(text: str) -> dict:
|
|
| 59 |
probs = torch.nn.functional.softmax(logits, dim=-1).squeeze().tolist()
|
| 60 |
idx = int(np.argmax(probs))
|
| 61 |
|
| 62 |
-
|
| 63 |
-
signed_score = probs[2] - probs[0] # positive_prob - negative_prob
|
| 64 |
return {
|
| 65 |
"label": FINBERT_LABELS[idx],
|
| 66 |
"score": round(signed_score, 4),
|
|
@@ -69,14 +55,9 @@ def _score_finbert(text: str) -> dict:
|
|
| 69 |
|
| 70 |
|
| 71 |
def _score_text(text: str) -> dict:
|
| 72 |
-
"""
|
| 73 |
-
Ensemble: blend VADER and FinBERT scores.
|
| 74 |
-
Returns per-headline detail with both individual and combined scores.
|
| 75 |
-
"""
|
| 76 |
vader = _score_vader(text)
|
| 77 |
finbert = _score_finbert(text)
|
| 78 |
|
| 79 |
-
# Weighted blend of signed scores
|
| 80 |
combined_score = (VADER_WEIGHT * vader["score"]) + (FINBERT_WEIGHT * finbert["score"])
|
| 81 |
|
| 82 |
if combined_score > 0.1:
|
|
@@ -98,14 +79,11 @@ def _score_text(text: str) -> dict:
|
|
| 98 |
}
|
| 99 |
|
| 100 |
|
| 101 |
-
# ββ News fetcher βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 102 |
def fetch_news_headlines(ticker: str, max_items: int = 10) -> list[str]:
|
| 103 |
-
"""Fetch recent news headlines from Yahoo Finance."""
|
| 104 |
stock = yf.Ticker(ticker)
|
| 105 |
news = stock.news or []
|
| 106 |
headlines = []
|
| 107 |
for item in news[:max_items]:
|
| 108 |
-
# yfinance v0.2.36+ nests data under "content"
|
| 109 |
content = item.get("content", {})
|
| 110 |
title = content.get("title", "") or item.get("title", "")
|
| 111 |
if title:
|
|
@@ -113,11 +91,7 @@ def fetch_news_headlines(ticker: str, max_items: int = 10) -> list[str]:
|
|
| 113 |
return headlines
|
| 114 |
|
| 115 |
|
| 116 |
-
# ββ Public API βββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 117 |
def analyze_sentiment(ticker: str) -> dict:
|
| 118 |
-
"""
|
| 119 |
-
End-to-end: fetch headlines β score each with VADER + FinBERT β return aggregate.
|
| 120 |
-
"""
|
| 121 |
headlines = fetch_news_headlines(ticker)
|
| 122 |
if not headlines:
|
| 123 |
return {
|
|
@@ -131,7 +105,6 @@ def analyze_sentiment(ticker: str) -> dict:
|
|
| 131 |
|
| 132 |
details = [_score_text(h) for h in headlines]
|
| 133 |
|
| 134 |
-
# Aggregate combined scores
|
| 135 |
avg = float(np.mean([d["combined_score"] for d in details]))
|
| 136 |
|
| 137 |
if avg > 0.15:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
import torch
|
| 3 |
from transformers import BertTokenizer, BertForSequenceClassification
|
|
|
|
| 6 |
import nltk
|
| 7 |
from nltk.sentiment.vader import SentimentIntensityAnalyzer
|
| 8 |
|
|
|
|
| 9 |
try:
|
| 10 |
nltk.data.find("sentiment/vader_lexicon.zip")
|
| 11 |
except LookupError:
|
| 12 |
nltk.download("vader_lexicon", quiet=True)
|
| 13 |
|
|
|
|
|
|
|
| 14 |
_vader = SentimentIntensityAnalyzer()
|
| 15 |
|
|
|
|
| 16 |
MODEL_NAME = "ProsusAI/finbert"
|
| 17 |
_tokenizer = BertTokenizer.from_pretrained(MODEL_NAME)
|
| 18 |
_finbert = BertForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 19 |
_finbert.eval()
|
| 20 |
FINBERT_LABELS = ["negative", "neutral", "positive"]
|
| 21 |
|
|
|
|
| 22 |
VADER_WEIGHT = 0.4
|
| 23 |
FINBERT_WEIGHT = 0.6
|
| 24 |
|
| 25 |
|
|
|
|
| 26 |
def _score_vader(text: str) -> dict:
|
|
|
|
| 27 |
scores = _vader.polarity_scores(text)
|
| 28 |
compound = scores["compound"]
|
| 29 |
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
def _score_finbert(text: str) -> dict:
|
|
|
|
| 42 |
tokens = _tokenizer(text, return_tensors="pt", padding=True,
|
| 43 |
truncation=True, max_length=128)
|
| 44 |
with torch.no_grad():
|
|
|
|
| 46 |
probs = torch.nn.functional.softmax(logits, dim=-1).squeeze().tolist()
|
| 47 |
idx = int(np.argmax(probs))
|
| 48 |
|
| 49 |
+
signed_score = probs[2] - probs[0]
|
|
|
|
| 50 |
return {
|
| 51 |
"label": FINBERT_LABELS[idx],
|
| 52 |
"score": round(signed_score, 4),
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
def _score_text(text: str) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
vader = _score_vader(text)
|
| 59 |
finbert = _score_finbert(text)
|
| 60 |
|
|
|
|
| 61 |
combined_score = (VADER_WEIGHT * vader["score"]) + (FINBERT_WEIGHT * finbert["score"])
|
| 62 |
|
| 63 |
if combined_score > 0.1:
|
|
|
|
| 79 |
}
|
| 80 |
|
| 81 |
|
|
|
|
| 82 |
def fetch_news_headlines(ticker: str, max_items: int = 10) -> list[str]:
|
|
|
|
| 83 |
stock = yf.Ticker(ticker)
|
| 84 |
news = stock.news or []
|
| 85 |
headlines = []
|
| 86 |
for item in news[:max_items]:
|
|
|
|
| 87 |
content = item.get("content", {})
|
| 88 |
title = content.get("title", "") or item.get("title", "")
|
| 89 |
if title:
|
|
|
|
| 91 |
return headlines
|
| 92 |
|
| 93 |
|
|
|
|
| 94 |
def analyze_sentiment(ticker: str) -> dict:
|
|
|
|
|
|
|
|
|
|
| 95 |
headlines = fetch_news_headlines(ticker)
|
| 96 |
if not headlines:
|
| 97 |
return {
|
|
|
|
| 105 |
|
| 106 |
details = [_score_text(h) for h in headlines]
|
| 107 |
|
|
|
|
| 108 |
avg = float(np.mean([d["combined_score"] for d in details]))
|
| 109 |
|
| 110 |
if avg > 0.15:
|