File size: 1,249 Bytes
085ecee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# evaluation.py

import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from sklearn.metrics import accuracy_score

def evaluate_model(model_path, test_data):
    """
    Evaluates the model based on test data and returns the score.
    This function assumes a classification task and requires the test data in a specific format.
    
    Args:
    - model_path: Path to the model file (e.g., PyTorch model file)
    - test_data: A list of tuples (text, label) for evaluation

    Returns:
    - score: Evaluation score (e.g., accuracy)
    """
    
    # Load model and tokenizer
    model = AutoModelForSequenceClassification.from_pretrained(model_path)
    tokenizer = AutoTokenizer.from_pretrained(model_path)

    model.eval()
    all_preds = []
    all_labels = []

    for text, label in test_data:
        inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
        outputs = model(**inputs)
        logits = outputs.logits
        predictions = torch.argmax(logits, dim=-1).item()
        
        all_preds.append(predictions)
        all_labels.append(label)

    # Calculate accuracy (or any other metric)
    accuracy = accuracy_score(all_labels, all_preds)
    return accuracy