-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_emotion.py
85 lines (71 loc) · 3.43 KB
/
run_emotion.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Import the pipeline function and torch
from transformers import pipeline
import torch
import operator # To sort results later
print("-------------------------------------------")
print("Hugging Face Local Inference Example")
print("Task: Text Classification (Emotion Detection)")
print("Model: j-hartmann/emotion-english-distilroberta-base")
print("-------------------------------------------")
# 1. Load the text classification pipeline, explicitly specifying the emotion model
print("Loading emotion classification model (may download on first run)...")
try:
# Use the "text-classification" pipeline task
emotion_pipeline = pipeline(
"text-classification", # Standard task name
model="j-hartmann/emotion-english-distilroberta-base", # Explicit model name
tokenizer="j-hartmann/emotion-english-distilroberta-base",
# Get scores for all labels, not just the top one
return_all_scores=True,
device=0 if torch.cuda.is_available() else -1 # Use GPU if available, else CPU
)
print("Model loaded successfully.")
if torch.cuda.is_available():
print(f"Running on GPU: {torch.cuda.get_device_name(0)}")
else:
print("Running on CPU.")
except Exception as e:
print(f"Error loading model: {e}")
print("Please ensure 'transformers', 'sentencepiece', and 'torch' (or 'tensorflow') are installed.")
exit()
# 2. Define some sentences to classify for emotion
# Using context relevant to Friday afternoon in Perth
sentences_to_classify = [
"It's a beautiful sunny Friday afternoon in Perth, feeling wonderful!",
"This traffic jam getting out of the city is absolutely infuriating.",
"Slightly anxious about finishing this important work before the weekend.",
"Just noticed my train home might be delayed, how annoying.",
"Looking forward to relaxing by the Swan River later, should be peaceful.",
"The news report about the freeway accident is quite distressing.",
"Perfect weather today for a walk in Kings Park." # Might be neutral or joy
]
print("\nSentences to Classify for Emotion:")
for i, s in enumerate(sentences_to_classify):
print(f"{i+1}. \"{s}\"")
# 3. Run the emotion classification pipeline
print("\nClassifying emotions...")
try:
# The pipeline returns a list of lists (one list per sentence)
# Each inner list contains dictionaries {'label': emotion, 'score': probability}
results = emotion_pipeline(sentences_to_classify)
print("Classification complete.")
# 4. Print the results for each sentence
print("\n--- Emotion Classification Results ---")
if not results:
print("Could not classify emotions.")
else:
# Iterate through sentences and their corresponding results
for i, sentence_results in enumerate(results):
print(f"\nSentence {i+1}: \"{sentences_to_classify[i]}\"")
# Sort the results by score in descending order
sorted_results = sorted(sentence_results, key=operator.itemgetter('score'), reverse=True)
print(" Predicted Emotions (Top First):")
for prediction in sorted_results:
label = prediction['label']
score = prediction['score']
print(f" - {label.capitalize()}: {score:.4f}")
print("-" * 20) # Separator
print("------------------------------------")
except Exception as e:
print(f"Error during emotion classification: {e}")
print("\nExample finished.")