File size: 1,832 Bytes
9220b75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""

Demo script for EdTech Feedback Validation Model

"""

import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification

def load_model(model_name):
    """Load the model and tokenizer"""
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    model = AutoModelForSequenceClassification.from_pretrained(model_name)
    return tokenizer, model

def predict_alignment(text, reason, tokenizer, model):
    """Predict whether text aligns with reason"""
    # Tokenize inputs
    inputs = tokenizer(
        text, 
        reason, 
        return_tensors="pt", 
        padding=True, 
        truncation=True, 
        max_length=512
    )
    
    # Get prediction
    with torch.no_grad():
        outputs = model(**inputs)
        probabilities = torch.softmax(outputs.logits, dim=1)
        prediction = torch.argmax(probabilities, dim=1).item()
        confidence = probabilities[0][prediction].item()
    
    return prediction, confidence

if __name__ == "__main__":
    # Example usage
    model_name = "your-username/edtech-feedback-validation"
    
    # Load model
    tokenizer, model = load_model(model_name)
    
    # Test examples
    test_cases = [
        ("this is an amazing app for online classes!", "good app for conducting online classes"),
        ("i cannot login to zoom", "help"),
        ("very practical and easy to use", "app is user-friendly")
    ]
    
    for text, reason in test_cases:
        prediction, confidence = predict_alignment(text, reason, tokenizer, model)
        result = "ALIGNED" if prediction == 1 else "NOT ALIGNED"
        print(f"Text: {text}")
        print(f"Reason: {reason}")
        print(f"Result: {result} (Confidence: {confidence:.3f})")
        print("-" * 50)