File size: 9,812 Bytes
cf47052
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python3
"""
Simple TOEFL Independent Speaking Judge - Command Line Interface
Usage: python toefl_judge_cli.py
"""

from mlx_lm import load, generate
import re
import argparse
import sys

class SimpleTOEFLJudge:
    def __init__(self, model_path="mlx-community/Llama-3.2-3B-Instruct-4bit", adapter_path="toefl_judge_adapter"):
        """Initialize the TOEFL judge with model paths"""
        self.model_path = model_path
        self.adapter_path = adapter_path
        self.model = None
        self.tokenizer = None
        
        print("πŸŽ“ TOEFL Independent Speaking Judge")
        print("="*50)
        print("Loading model... (this may take a moment)")
        
        self.load_model()
    
    def load_model(self):
        """Load the fine-tuned model"""
        try:
            self.model, self.tokenizer = load(
                path_or_hf_repo=self.model_path,
                adapter_path=self.adapter_path
            )
            print("βœ… Model loaded successfully!")
            print(f"πŸ“Š Model Performance: 86.1% accuracy, 0.943 correlation")
            print("="*50)
        except Exception as e:
            print(f"❌ Error loading model: {e}")
            print("Please check that your model and adapter paths are correct.")
            sys.exit(1)
    
    def create_prompt(self, question, answer):
        """Create the evaluation prompt"""
        system_prompt = """You are an expert TOEFL iBT Independent Speaking evaluator. Your task is to score student responses on a scale of 0-4 based on the official TOEFL Independent Speaking rubric.

Scoring Criteria:
- Score 4: Fulfills task demands with sustained, coherent discourse. Well-paced delivery, effective grammar/vocabulary, well-developed and coherent ideas.
- Score 3: Addresses task appropriately but may lack full development. Generally clear speech with some fluency, fairly effective language use, mostly coherent with some limitations.
- Score 2: Addresses task but limited development. Intelligible speech requiring listener effort, limited grammar/vocabulary range, basic ideas with limited elaboration.
- Score 1: Very limited content/coherence, largely unintelligible speech, severely limited language control, lacks substance beyond basic ideas.
- Score 0: No attempt or unrelated to topic.

Provide your score and a brief explanation focusing on delivery, language use, and topic development."""
        
        prompt = f"""<|begin_of_text|><|start_header_id|>system<|end_header_id|>
{system_prompt}<|eot_id|><|start_header_id|>user<|end_header_id|>
Please evaluate this TOEFL Independent Speaking response:

Question: {question}

Student Response: {answer}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
"""
        return prompt
    
    def extract_score(self, response):
        """Extract numerical score from response"""
        score_match = re.search(r'Score:\s*(\d+)', response)
        return int(score_match.group(1)) if score_match else None
    
    def evaluate(self, question, answer):
        """Evaluate a TOEFL response"""
        print("πŸ€” Evaluating response...")
        
        try:
            prompt = self.create_prompt(question, answer)
            
            response = generate(
                self.model,
                self.tokenizer,
                prompt=prompt,
                max_tokens=400
            )
            
            score = self.extract_score(response)
            
            return {
                'score': score,
                'response': response,
                'success': True
            }
            
        except Exception as e:
            return {
                'score': None,
                'response': f"Error: {e}",
                'success': False
            }
    
    def print_result(self, question, answer, result):
        """Print evaluation results in a nice format"""
        print("\n" + "="*60)
        print("πŸ“ EVALUATION RESULTS")
        print("="*60)
        
        print(f"\nπŸ’¬ Question:")
        print(f"{question}")
        
        print(f"\nπŸ—£οΈ  Student Response:")
        print(f"{answer}")
        
        if result['success'] and result['score'] is not None:
            score = result['score']
            
            # Score with emoji
            if score == 4:
                print(f"\n🌟 SCORE: {score}/4 (Excellent)")
            elif score == 3:
                print(f"\nβœ… SCORE: {score}/4 (Good)")
            elif score == 2:
                print(f"\n⚠️  SCORE: {score}/4 (Limited)")
            elif score == 1:
                print(f"\n❌ SCORE: {score}/4 (Very Limited)")
            else:
                print(f"\nπŸ’€ SCORE: {score}/4 (No Response)")
            
            print(f"\nπŸ“‹ Detailed Feedback:")
            print(f"{result['response']}")
            
        else:
            print(f"\n❌ Evaluation failed:")
            print(f"{result['response']}")
        
        print("\n" + "="*60)
    
    def interactive_mode(self):
        """Run in interactive mode"""
        print("\n🎯 Interactive Mode")
        print("Enter 'quit' or 'exit' to stop, 'help' for assistance")
        print("-" * 50)
        
        while True:
            try:
                # Get question
                print("\nπŸ“ Enter the TOEFL Speaking question:")
                question = input("> ").strip()
                
                if question.lower() in ['quit', 'exit', 'q']:
                    print("πŸ‘‹ Goodbye!")
                    break
                elif question.lower() == 'help':
                    self.show_help()
                    continue
                elif not question:
                    print("⚠️  Please enter a question.")
                    continue
                
                # Get student response
                print("\nπŸ—£οΈ  Enter the student's response:")
                answer = input("> ").strip()
                
                if not answer:
                    print("⚠️  Please enter a response.")
                    continue
                
                # Evaluate
                result = self.evaluate(question, answer)
                self.print_result(question, answer, result)
                
                # Ask if they want to continue
                print("\nπŸ”„ Evaluate another response? (y/n)")
                continue_choice = input("> ").strip().lower()
                if continue_choice in ['n', 'no']:
                    print("πŸ‘‹ Goodbye!")
                    break
                    
            except KeyboardInterrupt:
                print("\nπŸ‘‹ Goodbye!")
                break
            except Exception as e:
                print(f"❌ Error: {e}")
    
    def show_help(self):
        """Show help information"""
        print("\nπŸ“š HELP - TOEFL Independent Speaking Judge")
        print("="*50)
        print("This tool evaluates TOEFL Independent Speaking responses on a 0-4 scale.")
        print("\nScoring Rubric:")
        print("🌟 Score 4: Excellent - Sustained, coherent discourse")
        print("βœ… Score 3: Good - Mostly coherent with minor limitations")  
        print("⚠️  Score 2: Limited - Basic ideas with limited development")
        print("❌ Score 1: Very Limited - Major language/content issues")
        print("πŸ’€ Score 0: No attempt or unrelated to topic")
        print("\nTips:")
        print("β€’ Enter the exact question as given in TOEFL")
        print("β€’ Provide the student's transcribed spoken response")
        print("β€’ The model evaluates based on language use, delivery, and topic development")
        print("β€’ Type 'quit' or 'exit' to stop")
        print("="*50)

def main():
    """Main function with command line argument support"""
    parser = argparse.ArgumentParser(description='TOEFL Independent Speaking Judge')
    parser.add_argument('--model', default='mlx-community/Llama-3.2-3B-Instruct-4bit', 
                       help='Model path')
    parser.add_argument('--adapter', default='toefl_judge_adapter', 
                       help='Adapter path')
    parser.add_argument('--question', '-q', help='TOEFL question')
    parser.add_argument('--answer', '-a', help='Student response')
    parser.add_argument('--interactive', '-i', action='store_true', 
                       help='Run in interactive mode')
    
    args = parser.parse_args()
    
    # Initialize judge
    judge = SimpleTOEFLJudge(model_path=args.model, adapter_path=args.adapter)
    
    # Command line mode
    if args.question and args.answer:
        result = judge.evaluate(args.question, args.answer)
        judge.print_result(args.question, args.answer, result)
    
    # Interactive mode (default)
    else:
        judge.interactive_mode()

# Sample questions for testing
SAMPLE_QUESTIONS = [
    "Do you agree or disagree with the following statement: it is better to work in teams than to work alone? Use specific examples to support your answer.",
    "Some people prefer to live in small towns, while others prefer big cities. Which do you prefer and why?",
    "Do you think students should be required to wear uniforms in school? Explain your opinion with specific reasons.",
    "Would you rather have a job that pays well but is stressful, or a job that pays less but is enjoyable? Explain your choice.",
    "Some people believe that technology has made our lives easier, while others think it has made life more complicated. What is your opinion?"
]

if __name__ == "__main__":
    print("πŸš€ Quick Test Mode")
    print("Want to try a sample question? Here are some examples:")
    for i, q in enumerate(SAMPLE_QUESTIONS[:3], 1):
        print(f"{i}. {q}")
    print("\nStarting TOEFL Judge...")
    print("-" * 70)
    
    main()