|
1 | 1 | from typing import Any |
2 | | -from lf_toolkit.evaluation import Result as LFResult, Params |
3 | | - |
4 | | -# note: this file is a temperary workaround, if the frontend -> backend communication succeed, fix this file |
5 | | - |
6 | | -from .schemas import FSA#, FSAFrontend |
| 2 | +from lf_toolkit.evaluation import Result as LFResult |
| 3 | +from .schemas import FSA |
7 | 4 | from .schemas.result import Result |
8 | 5 | from .correction import analyze_fsa_correction |
9 | 6 |
|
10 | | -# def evaluation_function( |
11 | | -# payload: Any |
12 | | -# ) -> LFResult: |
13 | | -# return LFResult( |
14 | | -# is_correct=False, |
15 | | -# feedback_items=[("error", f"{payload}")] |
16 | | -# ) |
17 | | - |
18 | 7 | def validate_fsa(value: str | dict) -> FSA: |
19 | 8 | if isinstance(value, str): |
20 | 9 | return FSA.model_validate_json(value) |
21 | 10 | return FSA.model_validate(value) |
22 | 11 |
|
23 | | -def evaluation_function( |
24 | | - response: Any, |
25 | | - answer: Any, |
26 | | - params: Params, |
27 | | -) -> LFResult: |
| 12 | +def evaluation_function(payload: Any) -> LFResult: |
28 | 13 | """ |
29 | 14 | Evaluate a student's FSA response against the expected answer. |
30 | 15 | |
31 | 16 | Args: |
32 | | - response: Student's FSA (dict with states, alphabet, transitions, etc.), since frontend constriants, this is FSAFrontend |
33 | | - answer: Expected FSA still, FSAFrontend for the same reason |
34 | | - params: Extra parameters (e.g., require_minimal) |
| 17 | + payload: dict with keys 'response', 'answer', 'params' (front-end may wrap everything) |
35 | 18 | |
36 | 19 | Returns: |
37 | | - LFResult with is_correct and feedback |
| 20 | + LFResult |
38 | 21 | """ |
39 | 22 | try: |
40 | | - # Parse FSAs from input |
41 | | - # student_fsa_ = FSAFrontend.model_validate(response) |
42 | | - # expected_fsa_ = FSAFrontend.model_validate(answer) |
| 23 | + # Extract response/answer from the payload |
| 24 | + raw_response = payload.get("response") or payload.get("params", {}).get("response") |
| 25 | + raw_answer = payload.get("answer") or payload.get("params", {}).get("answer") |
| 26 | + params = payload.get("params", {}) |
43 | 27 |
|
44 | | - # student_fsa = student_fsa_.from_flattened() |
45 | | - # expected_fsa = expected_fsa_.from_flattened() |
| 28 | + if raw_response is None or raw_answer is None: |
| 29 | + raise ValueError("Missing response or answer in payload") |
46 | 30 |
|
47 | | - # as a temporary workaround we assume the response and answer are all valid json strings |
48 | | - student_fsa = validate_fsa(response) |
49 | | - expected_fsa = validate_fsa(answer) |
| 31 | + # Parse FSAs |
| 32 | + student_fsa = validate_fsa(raw_response) |
| 33 | + expected_fsa = validate_fsa(raw_answer) |
50 | 34 |
|
| 35 | + require_minimal = params.get("require_minimal", False) |
51 | 36 |
|
52 | | - |
53 | | - # Get require_minimal from params if present |
54 | | - require_minimal = params.get("require_minimal", False) if hasattr(params, "get") else False |
55 | | - |
56 | | - # Run correction pipeline |
| 37 | + # Run correction |
57 | 38 | result: Result = analyze_fsa_correction(student_fsa, expected_fsa, require_minimal) |
58 | | - |
59 | | - # Convert to lf_toolkit Result |
| 39 | + |
| 40 | + # Convert to LFResult |
60 | 41 | return LFResult( |
61 | 42 | is_correct=result.is_correct, |
62 | 43 | feedback_items=[("feedback", result.feedback)] |
63 | 44 | ) |
64 | | - |
| 45 | + |
65 | 46 | except Exception as e: |
66 | 47 | return LFResult( |
67 | 48 | is_correct=False, |
68 | | - feedback_items=[("error", f"Invalid FSA format: {str(e)}, received: \n\nresponse: {response}\n\n answer: {answer}, \n\nparams: {params}")] |
| 49 | + feedback_items=[( |
| 50 | + "error", |
| 51 | + f"Invalid FSA format: {str(e)}\n\npayload received:\n{payload}" |
| 52 | + )] |
69 | 53 | ) |
0 commit comments