3
3
import csv
4
4
import time
5
5
import requests
6
+ import logging
6
7
7
8
from typing import List , Dict , Any
8
9
from lib .assessment .config import VALID_GRADES
@@ -42,18 +43,18 @@ def grade_student_work(self, prompt, rubric, student_code, student_id, examples=
42
43
try :
43
44
response = requests .post (api_url , headers = headers , json = data , timeout = 120 )
44
45
except requests .exceptions .ReadTimeout :
45
- print (f"{ student_id } request timed out in { (time .time () - start_time ):.0f} seconds." )
46
+ logging . error (f"{ student_id } request timed out in { (time .time () - start_time ):.0f} seconds." )
46
47
return None
47
48
48
49
if response .status_code != 200 :
49
- print (f"{ student_id } Error calling the API: { response .status_code } " )
50
- print (f"{ student_id } Response body: { response .text } " )
50
+ logging . error (f"{ student_id } Error calling the API: { response .status_code } " )
51
+ logging . info (f"{ student_id } Response body: { response .text } " )
51
52
return None
52
53
53
54
info = response .json ()
54
55
tokens = info ['usage' ]['total_tokens' ]
55
56
elapsed = time .time () - start_time
56
- print (f"{ student_id } request succeeded in { elapsed :.0f} seconds. { tokens } tokens used." )
57
+ logging . info (f"{ student_id } request succeeded in { elapsed :.0f} seconds. { tokens } tokens used." )
57
58
58
59
tsv_data_choices = [self .get_tsv_data_if_valid (choice ['message' ]['content' ], rubric , student_id , choice_index = index ) for index , choice in enumerate (info ['choices' ]) if choice ['message' ]['content' ]]
59
60
tsv_data_choices = [choice for choice in tsv_data_choices if choice ]
@@ -107,7 +108,7 @@ def compute_messages(self, prompt, rubric, student_code, examples=[]):
107
108
def get_tsv_data_if_valid (self , response_text , rubric , student_id , choice_index = None ):
108
109
choice_text = f"Choice { choice_index } : " if choice_index is not None else ''
109
110
if not response_text :
110
- print (f"{ student_id } { choice_text } Invalid response: empty response" )
111
+ logging . error (f"{ student_id } { choice_text } Invalid response: empty response" )
111
112
return None
112
113
text = response_text .strip ()
113
114
@@ -131,12 +132,12 @@ def get_tsv_data_if_valid(self, response_text, rubric, student_id, choice_index=
131
132
lines = text .split ('\n ' )
132
133
lines = list (filter (lambda x : "---" not in x , lines ))
133
134
text = "\n " .join (lines )
134
- print ("response was markdown and not tsv, delimiting by '|'" )
135
+ logging . info ("response was markdown and not tsv, delimiting by '|'" )
135
136
136
137
tsv_data = list (csv .DictReader (StringIO (text ), delimiter = '|' ))
137
138
else :
138
139
# Let's assume it is CSV
139
- print ("response had no tabs so is not tsv, delimiting by ','" )
140
+ logging . info ("response had no tabs so is not tsv, delimiting by ','" )
140
141
tsv_data = list (csv .DictReader (StringIO (text ), delimiter = ',' ))
141
142
else :
142
143
# Let's assume it is TSV
@@ -147,7 +148,7 @@ def get_tsv_data_if_valid(self, response_text, rubric, student_id, choice_index=
147
148
self .validate_server_response (tsv_data , rubric )
148
149
return [row for row in tsv_data ]
149
150
except InvalidResponseError as e :
150
- print (f"{ student_id } { choice_text } Invalid response: { str (e )} \n { response_text } " )
151
+ logging . error (f"{ student_id } { choice_text } Invalid response: { str (e )} \n { response_text } " )
151
152
return None
152
153
153
154
def parse_tsv (self , tsv_text ):
@@ -210,7 +211,7 @@ def get_consensus_response(self, choices, student_id):
210
211
majority_grade = Counter (grades ).most_common (1 )[0 ][0 ]
211
212
key_concept_to_majority_grade [key_concept ] = majority_grade
212
213
if majority_grade != grades [0 ]:
213
- print (f"outvoted { student_id } Key Concept: { key_concept } first grade: { grades [0 ]} majority grade: { majority_grade } " )
214
+ logging . info (f"outvoted { student_id } Key Concept: { key_concept } first grade: { grades [0 ]} majority grade: { majority_grade } " )
214
215
215
216
key_concept_to_observations = {}
216
217
key_concept_to_reason = {}
0 commit comments