Analiza sentymentów 1: Tekst1

 avatar
unknown
python
a month ago
2.9 kB
4
Indexable
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

model_name = "tabularisai/multilingual-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def predict_sentiment(texts):
    inputs = tokenizer(texts, return_tensors="pt", truncation=True, padding=True, max_length=512)
    with torch.no_grad():
        outputs = model(**inputs)
    probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
    sentiment_map = {0: "Very Negative", 1: "Negative", 2: "Neutral", 3: "Positive", 4: "Very Positive"}
    return [sentiment_map[p] for p in torch.argmax(probabilities, dim=-1).tolist()]

texts = [
   """The liberal hosts behind "Pod Save America" trashed President Biden’s Washington Post op-ed about January 6 as "feckless" and blasted the Democrats for "performing virtue" after Vice President Kamala Harris certified Donald Trump’s election Monday.

"There's like an entire language that Democrats just need to f--king throw in the trash about like 'institutions,' 'democracy,' 'sacred this,'" said Jon Favreau, who served as one of former President Barack Obama’s speechwriters.

Biden warned on Monday that America risks a repeat of the January 6, 2021, Capitol riot if the history of the day is forgotten, and called on the country to "commit to remembering" the incident annually.

"But we should not forget. We must remember the wisdom of the adage that any nation that forgets its past is doomed to repeat it. We cannot accept a repeat of what occurred four years ago," Biden wrote.

Podcast co-host and former Obama speechwriter Jon Lovett claimed Biden misunderstood the quote about repeating history, coined by philosopher George Santayana and went so far as to liken Biden to a "stubborn old man."

"If you’re too mired in the past, if you become, this is from the original philosopher, stubborn like an old man, you also become doomed to repeat yourself," Lovett said.

"That’s pretty on the nose," co-host and former Obama spokesman Tommy Vietor said.

The hosts savaged the op-ed as being out of step with the American electorate, conceding that the majority of voters "didn’t care" about the events of January 6.

"Biden’s op-ed didn’t say anything," Favreau said. "An op-ed about Jan. 6 is the perfect encapsulation of how feckless everything feels in this moment."

The Democratic Party has faced increasing calls to change how it communicates with voters, with critics alleging that it relies on overly "woke" and academic rhetoric that alienates ordinary people.

Clinton campaign alum James Carville called on Democrats to tone down the "idiotic NPR jargon" when talking to voters in an interview Sunday with MSNBC host Jen Psaki."""

]

for text, sentiment in zip(texts, predict_sentiment(texts)):
    print(f"Text: {text}\nSentiment: {sentiment}\n")
Editor is loading...
Leave a Comment