Views
No views yet
ConSenBertFacebookAI/roberta-baseFacebookAI/roberta-base architecture, designed to perform sentiment analysis with a focus on context-aware entity-based sentiment classification. The model is fine-tuned to identify whether a comment expresses a positive, negative or neutral sentiment towards a specific entity (product, company, etc.).1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3from scipy.special import softmax
4
5model_name = "SoloAlphus/ConSenBert-V1"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
9def analyze_sentiment(text):
10 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512, padding=True)
11
12 with torch.no_grad():
13 outputs = model(**inputs)
14
15 scores = outputs.logits.squeeze().numpy()
16 scores = softmax(scores)
17
18 labels = ['Negative', 'Neutral', 'Positive']
19 result = {label: float(score) for label, score in zip(labels, scores)}
20
21 predicted_sentiment = max(result, key=result.get)
22
23 return result, predicted_sentiment
24
25# Example usage
26comment = "abc product looks much better compared to xyz product!"
27entity = "xyz"
28text = comment + "[SEP]" + entity
29sentiment_scores, predicted_sentiment = analyze_sentiment(text)
30
31print(f"Comment: {comment}")
32print(f"Entity: {entity}")
33print(f"Sentiment Scores: {sentiment_scores}")
34print(f"Predicted Sentiment: {predicted_sentiment}")
35
36#Result
37#Comment: abc product looks much better compared to xyz product
38#Entity: xyz
39#Sentiment Scores: {'Negative': 0.9783487915992737, 'Neutral': 0.001976581523194909, 'Positive': 0.01967463828623295}
40#Predicted Sentiment: NegativePositive, Negative or Neutral (along with score), indicating the sentiment of the comment towards the specified entity.