Fix question_type handling
Browse files
app.py
CHANGED
|
@@ -881,6 +881,11 @@ async def homework_validate(
|
|
| 881 |
erp_row = fetch_student_record(homework_id, student_id)
|
| 882 |
student_level = fetch_student_level_from_erp(erp_row)
|
| 883 |
policy = level_policy(student_level)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
|
| 885 |
# 1) Infer question_type from prompt automatically (NO EXTRA FIELD)
|
| 886 |
# Try to parse mixed questions first
|
|
@@ -889,20 +894,19 @@ async def homework_validate(
|
|
| 889 |
has_narrative = any(q.get('type') == 'narrative' for q in parsed_questions)
|
| 890 |
|
| 891 |
# Infer question type from prompt
|
| 892 |
-
question_type = infer_question_type_from_prompt(prompt)
|
| 893 |
|
| 894 |
# 2) Extract student text
|
| 895 |
student_info = await extract_text_from_upload(student_file)
|
| 896 |
student_text = (student_info.get("text") or "").strip()
|
| 897 |
|
| 898 |
-
MIN_WORDS = 3 if
|
| 899 |
if len(student_text.split()) < MIN_WORDS:
|
| 900 |
return {
|
| 901 |
"student_id": student_id,
|
| 902 |
"homework_id": homework_id,
|
| 903 |
"sub_institute_id": sub_institute_id,
|
| 904 |
"syear": syear,
|
| 905 |
-
"question_type":
|
| 906 |
"student_level": student_level,
|
| 907 |
"status": "Unreadable",
|
| 908 |
"match_percentage": 0,
|
|
@@ -919,7 +923,7 @@ async def homework_validate(
|
|
| 919 |
"homework_id": homework_id,
|
| 920 |
"sub_institute_id": sub_institute_id,
|
| 921 |
"syear": syear,
|
| 922 |
-
"question_type":
|
| 923 |
"student_level": student_level,
|
| 924 |
"status": "Unreadable",
|
| 925 |
"match_percentage": 0,
|
|
@@ -931,7 +935,7 @@ async def homework_validate(
|
|
| 931 |
}
|
| 932 |
|
| 933 |
|
| 934 |
-
if
|
| 935 |
# Process each question type separately and combine results
|
| 936 |
mcq_results = []
|
| 937 |
narrative_results = []
|
|
@@ -1072,7 +1076,7 @@ async def homework_validate(
|
|
| 1072 |
},
|
| 1073 |
}
|
| 1074 |
|
| 1075 |
-
elif
|
| 1076 |
correct = extract_correct_mcq_from_prompt(prompt)
|
| 1077 |
chosen = extract_mcq_choice(student_text)
|
| 1078 |
|
|
@@ -1090,7 +1094,7 @@ async def homework_validate(
|
|
| 1090 |
|
| 1091 |
# If answer looks like narrative, redirect to narrative processing
|
| 1092 |
if answer_looks_like_narrative and gemini_client:
|
| 1093 |
-
|
| 1094 |
redirect_to_narrative = True
|
| 1095 |
else:
|
| 1096 |
redirect_to_narrative = False
|
|
|
|
| 881 |
erp_row = fetch_student_record(homework_id, student_id)
|
| 882 |
student_level = fetch_student_level_from_erp(erp_row)
|
| 883 |
policy = level_policy(student_level)
|
| 884 |
+
# Decide final question type: respect request value if valid, else infer
|
| 885 |
+
final_question_type = (question_type or "").strip().lower()
|
| 886 |
+
if final_question_type not in ("mcq", "narrative", "mixed"):
|
| 887 |
+
final_question_type = infer_question_type_from_prompt(prompt)
|
| 888 |
+
|
| 889 |
|
| 890 |
# 1) Infer question_type from prompt automatically (NO EXTRA FIELD)
|
| 891 |
# Try to parse mixed questions first
|
|
|
|
| 894 |
has_narrative = any(q.get('type') == 'narrative' for q in parsed_questions)
|
| 895 |
|
| 896 |
# Infer question type from prompt
|
|
|
|
| 897 |
|
| 898 |
# 2) Extract student text
|
| 899 |
student_info = await extract_text_from_upload(student_file)
|
| 900 |
student_text = (student_info.get("text") or "").strip()
|
| 901 |
|
| 902 |
+
MIN_WORDS = 3 if final_question_type == "mcq" else 8
|
| 903 |
if len(student_text.split()) < MIN_WORDS:
|
| 904 |
return {
|
| 905 |
"student_id": student_id,
|
| 906 |
"homework_id": homework_id,
|
| 907 |
"sub_institute_id": sub_institute_id,
|
| 908 |
"syear": syear,
|
| 909 |
+
"question_type": final_question_type,
|
| 910 |
"student_level": student_level,
|
| 911 |
"status": "Unreadable",
|
| 912 |
"match_percentage": 0,
|
|
|
|
| 923 |
"homework_id": homework_id,
|
| 924 |
"sub_institute_id": sub_institute_id,
|
| 925 |
"syear": syear,
|
| 926 |
+
"question_type": final_question_type,
|
| 927 |
"student_level": student_level,
|
| 928 |
"status": "Unreadable",
|
| 929 |
"match_percentage": 0,
|
|
|
|
| 935 |
}
|
| 936 |
|
| 937 |
|
| 938 |
+
if final_question_type == "mixed":
|
| 939 |
# Process each question type separately and combine results
|
| 940 |
mcq_results = []
|
| 941 |
narrative_results = []
|
|
|
|
| 1076 |
},
|
| 1077 |
}
|
| 1078 |
|
| 1079 |
+
elif final_question_type == "mcq":
|
| 1080 |
correct = extract_correct_mcq_from_prompt(prompt)
|
| 1081 |
chosen = extract_mcq_choice(student_text)
|
| 1082 |
|
|
|
|
| 1094 |
|
| 1095 |
# If answer looks like narrative, redirect to narrative processing
|
| 1096 |
if answer_looks_like_narrative and gemini_client:
|
| 1097 |
+
final_question_type = "narrative"
|
| 1098 |
redirect_to_narrative = True
|
| 1099 |
else:
|
| 1100 |
redirect_to_narrative = False
|