ai-news-analyzer / ai_analysis /fin_sentiment.py
Sigrid De los Santos
Remove remaining binary file for Hugging Face
9df4cc0
raw
history blame contribute delete
521 Bytes
#uses HuggingFace pipeline to classify text sentiment (positive, neutral, negative) with FinBERT.
from transformers import pipeline
# Load FinBERT financial sentiment pipeline
sentiment_pipeline = pipeline(
"sentiment-analysis",
model="ProsusAI/finbert",
top_k=None
)
def analyze_sentiment(text):
try:
result = sentiment_pipeline(text[:512])[0] # limit size for tokenizer
return result["label"].lower(), round(result["score"], 3)
except Exception as e:
return "error", 0.0