Updated the evaluation to work recursively when failing

This commit is contained in:
Tiago Ribeiro
2024-01-03 15:32:51 +00:00
parent 35d1157b0c
commit 026730c077
4 changed files with 26 additions and 16 deletions

View File

@@ -93,8 +93,8 @@ export default function Writing({
)} )}
<div className="flex flex-col h-full w-full gap-9 mb-20"> <div className="flex flex-col h-full w-full gap-9 mb-20">
<div className="flex flex-col w-full gap-7 bg-mti-gray-smoke rounded-xl py-8 pb-12 px-16"> <div className="flex flex-col w-full gap-7 bg-mti-gray-smoke rounded-xl py-8 pb-12 px-16">
<span className="whitespace-pre-wrap">{prefix}</span> <span className="whitespace-pre-wrap">{prefix.replaceAll("\\n", "\n")}</span>
<span className="font-semibold whitespace-pre-wrap">{prompt}</span> <span className="font-semibold whitespace-pre-wrap">{prompt.replaceAll("\\n", "\n")}</span>
{attachment && ( {attachment && (
<img <img
onClick={() => setIsModalOpen(true)} onClick={() => setIsModalOpen(true)}

View File

@@ -2,7 +2,7 @@
import type {NextApiRequest, NextApiResponse} from "next"; import type {NextApiRequest, NextApiResponse} from "next";
import {withIronSessionApiRoute} from "iron-session/next"; import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session"; import {sessionOptions} from "@/lib/session";
import axios from "axios"; import axios, {AxiosResponse} from "axios";
import formidable from "formidable-serverless"; import formidable from "formidable-serverless";
import {ref, uploadBytes} from "firebase/storage"; import {ref, uploadBytes} from "firebase/storage";
import fs from "fs"; import fs from "fs";
@@ -26,21 +26,24 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const binary = fs.readFileSync((audioFile as any).path).buffer; const binary = fs.readFileSync((audioFile as any).path).buffer;
const snapshot = await uploadBytes(audioFileRef, binary); const snapshot = await uploadBytes(audioFileRef, binary);
const backendRequest = await axios.post( const backendRequest = await evaluate({answers: [{question: fields.question, answer: snapshot.metadata.fullPath}]});
`${process.env.BACKEND_URL}/speaking_task_3`,
{answers: [{question: fields.question, answer: snapshot.metadata.fullPath}]},
{
headers: {
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
},
},
);
fs.rmSync((audioFile as any).path); fs.rmSync((audioFile as any).path);
res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath}); res.status(200).json({...backendRequest.data, fullPath: snapshot.metadata.fullPath});
}); });
} }
async function evaluate(body: {answers: object[]}): Promise<AxiosResponse> {
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/speaking_task_3`, body, {
headers: {
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
},
});
if (typeof backendRequest.data === "string") return evaluate(body);
return backendRequest;
}
export const config = { export const config = {
api: { api: {
bodyParser: false, bodyParser: false,

View File

@@ -3,7 +3,7 @@ import type {NextApiRequest, NextApiResponse} from "next";
import {getFirestore, doc, getDoc} from "firebase/firestore"; import {getFirestore, doc, getDoc} from "firebase/firestore";
import {withIronSessionApiRoute} from "iron-session/next"; import {withIronSessionApiRoute} from "iron-session/next";
import {sessionOptions} from "@/lib/session"; import {sessionOptions} from "@/lib/session";
import axios from "axios"; import axios, {AxiosResponse} from "axios";
interface Body { interface Body {
question: string; question: string;
@@ -18,11 +18,18 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
return; return;
} }
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/writing_task2`, req.body as Body, { const backendRequest = await evaluate(req.body as Body);
res.status(backendRequest.status).json(backendRequest.data);
}
async function evaluate(body: Body): Promise<AxiosResponse> {
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/writing_task2`, body as Body, {
headers: { headers: {
Authorization: `Bearer ${process.env.BACKEND_JWT}`, Authorization: `Bearer ${process.env.BACKEND_JWT}`,
}, },
}); });
res.status(backendRequest.status).json(backendRequest.data); if (typeof backendRequest.data === "string") return evaluate(body);
return backendRequest;
} }

View File

@@ -11,7 +11,7 @@ import {
import axios from "axios"; import axios from "axios";
import {speakingReverseMarking, writingReverseMarking} from "./score"; import {speakingReverseMarking, writingReverseMarking} from "./score";
export const evaluateWritingAnswer = async (exercise: WritingExercise, solution: UserSolution) => { export const evaluateWritingAnswer = async (exercise: WritingExercise, solution: UserSolution): Promise<object | undefined> => {
const response = await axios.post<Evaluation>("/api/evaluate/writing", { const response = await axios.post<Evaluation>("/api/evaluate/writing", {
question: `${exercise.prompt} ${exercise.attachment ? exercise.attachment.description : ""}`.replaceAll("\n", ""), question: `${exercise.prompt} ${exercise.attachment ? exercise.attachment.description : ""}`.replaceAll("\n", ""),
answer: solution.solutions[0].solution.trim().replaceAll("\n", " "), answer: solution.solutions[0].solution.trim().replaceAll("\n", " "),