-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sentiment.py
58 lines (47 loc) · 1.94 KB
/
test_sentiment.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
import unittest
import time
import torch.package
class TestSentimentModel(unittest.TestCase):
def setUp(self):
self.model = torch.load(
"sentiment_model.pt",
torch.device("cuda" if torch.cuda.is_available() else "cpu"),
weights_only=False,
)
self.model.eval()
def test_inference_speed(self):
test_text = "This is a sample text to test inference speed."
start_time = time.time()
self.model.infer(test_text)
end_time = time.time()
inference_time = end_time - start_time
self.assertLess(inference_time, 0.1, "Inference should take less than 100ms")
def test_accuracy(self):
# You will likely have to change this test to match your own dataset
test_data = [
("This movie was great!", "admiration"),
("I hated every minute of it.", "annoyance"),
("It was okay, nothing special.", "neutral"),
("I wish I had some popcorn.", "desire"),
("This movie was terrible!", "disapproval"),
("That food was disgusting.", "disgust"),
("oh no, I forgot my wallet.", "realization"),
("Sadly, my goldfish died recently.", "sadness"),
("I'm so excited for the weekend!", "excitement"),
("I'm so happy to be here!", "joy"),
# Add more test cases here
]
correct = 0
total = len(test_data)
for text, expected_sentiment in test_data:
predicted_result = self.model.infer(text)
predicted_sentiment = predicted_result[0][0]
print(f"Predicted sentiment: {predicted_sentiment}")
if predicted_sentiment == expected_sentiment:
correct += 1
accuracy = correct / total
self.assertGreaterEqual(
accuracy, 0.7, "Model accuracy should be greater than or equal to 70%"
)
if __name__ == "__main__":
unittest.main()