File size: 2,272 Bytes
8db7949
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
import argparse
import pandas as pd

from text_analysis import show_text_analysis
from binoculars_utils import initialize_binoculars, compute_scores
from model_utils import load_model, classify_text

def main():
    parser = argparse.ArgumentParser(description='Text classifier demonstration (Human vs AI)')
    parser.add_argument('--text', type=str, help='Text for classification')
    parser.add_argument('--file', type=str, help='Path to file with text')
    parser.add_argument('--analysis', action='store_true', help='Show detailed text analysis')
    parser.add_argument('--compute-scores', action='store_true', help='Compute score_chat and score_coder')
    args = parser.parse_args()
    
    bino_chat = None
    bino_coder = None
    if args.compute_scores:
        bino_chat, bino_coder = initialize_binoculars()
    
    print("Loading binary classifier model...")
    model, scaler, label_encoder, imputer = load_model()
    
    if args.text:
        text = args.text
    elif args.file:
        with open(args.file, 'r', encoding='utf-8') as f:
            text = f.read()
    else:
        text = input("Enter text for classification: ")
    
    scores = None
    if args.compute_scores:
        scores = compute_scores(text, bino_chat, bino_coder)
    
    print(f"\nAnalyzing text...")
    result = classify_text(text, model, scaler, label_encoder, imputer=imputer, scores=scores)
    
    print("\n" + "="*50)
    print("CLASSIFICATION RESULTS")
    print("="*50)
    print(f"Predicted class: {result['predicted_class']}")
    print("Class probabilities:")
    for cls, prob in result['probabilities'].items():
        print(f"  - {cls}: {prob:.4f}")
    
    if scores:
        print("\nComputed scores:")
        if 'score_chat' in scores:
            print(f"  - Score Chat: {scores['score_chat']:.4f}")
        if 'score_coder' in scores:
            print(f"  - Score Coder: {scores['score_coder']:.4f}")
    
    if args.analysis:
        show_text_analysis(result['text_analysis'])
        
    if args.compute_scores:
        if bino_chat:
            bino_chat.free_memory()
        if bino_coder:
            bino_coder.free_memory()

if __name__ == "__main__":
    main()