File size: 1,516 Bytes
1572793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import pandas as pd

def analyze_sentiment_transformers(reviews):
    # Set device to "cpu" if CUDA (GPU) is unavailable
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

    # Initialize tokenizer and model
    tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased")
    model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased").to(device)

    results = []
    for review in reviews:
        # Tokenize with truncation and padding to max length
        tokenized_review = tokenizer(review, return_tensors="pt", truncation=True, padding="max_length", max_length=512)
        tokenized_review = {key: val.to(device) for key, val in tokenized_review.items()}  # Ensure tensors are on the correct device

        # Get the model's output (logits)
        with torch.no_grad():
            outputs = model(**tokenized_review)
        
        # Convert logits to probabilities
        probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
        
        # Get the predicted label and score
        score = probabilities.max().item()
        label = "POSITIVE" if torch.argmax(probabilities).item() == 1 else "NEGATIVE"
        
        # Append the result
        results.append({"label": label, "score": score})

    # Convert results to DataFrame
    sentiment_df = pd.DataFrame(results)
    return sentiment_df