SpawnedShoyo commited on
Commit
52cebf7
·
verified ·
1 Parent(s): 3493b06

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yaml
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
+
5
+ # Load test data from YAML file
6
+ with open("test_data.yaml", "r") as file:
7
+ test_data = yaml.safe_load(file)["test_data"]
8
+
9
+ # Load pre-trained model and tokenizer
10
+ model_name = "distilbert-base-uncased"
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)
13
+
14
+ # Load your fine-tuned model weights
15
+ model.load_state_dict(torch.load("path/to/your/fine-tuned/model.pth"))
16
+ model.eval()
17
+
18
+ # Evaluate on test data
19
+ correct_predictions = 0
20
+ total_samples = 0
21
+
22
+ for sample in test_data:
23
+ text = sample["text"]
24
+ expected_label = sample["label"]
25
+
26
+ # Tokenize and encode input text
27
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
28
+
29
+ # Get model predictions
30
+ outputs = model(**inputs)
31
+ logits = outputs.logits
32
+ predicted_label = "Positive" if logits.argmax().item() else "Negative"
33
+
34
+ # Check if prediction matches expected label
35
+ if predicted_label == expected_label:
36
+ correct_predictions += 1
37
+ total_samples += 1
38
+
39
+ # Calculate accuracy
40
+ accuracy = correct_predictions / total_samples
41
+ print(f"Accuracy on test data: {accuracy * 100:.2f}%")
42
+
43
+ # Demonstrate model predictions
44
+ print("\nModel Predictions:")
45
+ for sample in test_data:
46
+ text = sample["text"]
47
+ expected_label = sample["label"]
48
+
49
+ # Tokenize and encode input text
50
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
51
+
52
+ # Get model predictions
53
+ outputs = model(**inputs)
54
+ logits = outputs.logits
55
+ predicted_label = "Positive" if logits.argmax().item() else "Negative"
56
+
57
+ print(f"Text: {text}")
58
+ print(f"Expected Label: {expected_label}")
59
+ print(f"Predicted Label: {predicted_label}")
60
+ print()