NavyaNayer commited on
Commit
4a12c41
·
verified ·
1 Parent(s): cc40705

Delete predict_emotions.py

Browse files
Files changed (1) hide show
  1. predict_emotions.py +0 -48
predict_emotions.py DELETED
@@ -1,48 +0,0 @@
1
- import torch
2
- from transformers import BertTokenizer, DistilBertForSequenceClassification
3
-
4
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
5
-
6
- # Load the trained model and tokenizer
7
- try:
8
- model = DistilBertForSequenceClassification.from_pretrained("./saved_model")
9
- tokenizer = BertTokenizer.from_pretrained("./saved_model")
10
- except Exception as e:
11
- print(f"Error loading model or tokenizer: {e}")
12
- exit()
13
-
14
- model.to(device)
15
- model.eval()
16
-
17
- # Define the sentences
18
- sentences = [
19
- "I am so happy today!",
20
- "This is the worst day ever.",
21
- "I feel so loved and appreciated.",
22
- "I am really angry right now.",
23
- "I am so done cant take this anymore",
24
- "i have to finish this report by tomorrow but so tired",
25
- "let's do it",
26
- "i have got this,, yayyyy",
27
- "energetic",
28
- "worst tired lazy",
29
- "I am feeling very sad and lonely."
30
- ]
31
-
32
- # Define the label names
33
- label_names = ["admiration", "amusement", "anger", "annoyance", "approval", "caring", "confusion", "curiosity", "desire", "disappointment", "disapproval", "disgust", "embarrassment", "excitement", "fear", "gratitude", "grief", "joy", "love", "nervousness", "optimism", "pride", "realization", "relief", "remorse", "sadness", "surprise", "neutral"]
34
-
35
- def predict_emotion(sentence):
36
- inputs = tokenizer(sentence, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
37
- inputs = {key: val.to(device) for key, val in inputs.items() if key != "token_type_ids"}
38
-
39
- with torch.no_grad():
40
- outputs = model(**inputs)
41
- predicted_class = torch.argmax(outputs.logits, dim=1).cpu().numpy()[0]
42
-
43
- return predicted_class, label_names[predicted_class]
44
-
45
- # Predict emotions for the sentences
46
- for sentence in sentences:
47
- predicted_emotion, predicted_label_name = predict_emotion(sentence)
48
- print(f"Predicted emotion for '{sentence}': {predicted_emotion} ({predicted_label_name})")