Views
No views yet
1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5model_name = "auskola/sentimientos"
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
9def analyze_sentiment(text):
10 # Tokenize and predict
11 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
12 with torch.no_grad():
13 outputs = model(**inputs)
14 probabilities = torch.nn.functional.softmax(outputs.logits, dim=1)
15
16 # Get prediction and confidence
17 prediction = torch.argmax(probabilities, dim=1)
18 confidence = torch.max(probabilities).item()
19
20 return {
21 "sentiment": "Positive" if prediction.item() == 1 else "Negative",
22 "confidence": confidence
23 }
24
25# Ejemplos de uso
26texts = [
27 "This product exceeded my expectations!",
28 "Terrible service, would not recommend",
29 "The movie was pretty good"
30]
31
32for text in texts:
33 result = analyze_sentiment(text)
34 print(f"\nText: {text}")
35 print(f"Sentiment: {result['sentiment']}")
36 print(f"Confidence: {result['confidence']:.2f}")
37