-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluation.py
More file actions
executable file
·69 lines (61 loc) · 2.49 KB
/
evaluation.py
File metadata and controls
executable file
·69 lines (61 loc) · 2.49 KB
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
from typing import Any, Tuple
from lf_toolkit.evaluation import Result as LFResult
from evaluation_function.schemas.params import Params
from .schemas import FSA, FSAFrontend
from .schemas.result import FSAFeedback, Result
from .correction import analyze_fsa_correction
import json
def validate_fsa(value: str | dict) -> Tuple[FSA, Params]:
"""Parse a FSA from JSON string or dict."""
if isinstance(value, str):
return FSAFrontend.model_validate_json(value).toFSA()
return FSAFrontend.model_validate(value).toFSA(), Params.model_validate_json(FSAFrontend.model_validate(value).config)
def evaluation_function(
response: Any = None,
answer: Any = None,
params: Any = None # Temp workaround: treat params as Any
) -> LFResult:
"""
Temporary FSA evaluation function.
Args:
response: Student's FSA (may be None if frontend wraps everything in params)
answer: Expected FSA (may be None)
params: Additional parameters (or full payload if frontend wraps everything here)
Returns:
LFResult with is_correct and feedback_items
"""
try:
if not response or not answer:
response = params.get("response") if isinstance(params, dict) else None
answer = params.get("answer") if isinstance(params, dict) else None
params = params.get("params") if isinstance(params, dict) else None
# raise ValueError(
# f"Missing FSA data: response or answer is None\n"
# f"response: {response}\nanswer: {answer}"
# )
# Parse FSAs
student_fsa, _ = validate_fsa(response)
expected_fsa, expected_config = validate_fsa(answer)
# Run correction pipeline
result: Result = analyze_fsa_correction(student_fsa, expected_fsa, expected_config)
# Return LFResult
return LFResult(
is_correct=result.is_correct,
feedback_items=[("result", result.feedback), ("errors", result.fsa_feedback.model_dump_json())]
)
except Exception as e:
result: Result = Result(
is_correct=False,
feedback=f"Error during evaluation: {str(e)}",
fsa_feedback=FSAFeedback(
summary=f"Error during evaluation: {str(e)}",
errors=[]
)
)
return LFResult(
is_correct=False,
feedback_items=[(
"error",
result.fsa_feedback.model_dump_json()
)]
)