Navigation rework, added prompt edit to components that were missing
This commit is contained in:
@@ -7,11 +7,9 @@ import formidable from "formidable-serverless";
|
||||
import {getDownloadURL, ref, uploadBytes} from "firebase/storage";
|
||||
import fs from "fs";
|
||||
import {storage} from "@/firebase";
|
||||
import client from "@/lib/mongodb";
|
||||
import {Stat} from "@/interfaces/user";
|
||||
import {speakingReverseMarking} from "@/utils/score";
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
|
||||
export default withIronSessionApiRoute(handler, sessionOptions);
|
||||
|
||||
function delay(ms: number) {
|
||||
@@ -30,54 +28,29 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
|
||||
const audioFile = files.audio;
|
||||
const audioFileRef = ref(storage, `speaking_recordings/${fields.id}.wav`);
|
||||
const task = parseInt(fields.task.toString());
|
||||
|
||||
const binary = fs.readFileSync((audioFile as any).path).buffer;
|
||||
|
||||
const snapshot = await uploadBytes(audioFileRef, binary);
|
||||
const url = await getDownloadURL(snapshot.ref);
|
||||
const path = snapshot.metadata.fullPath;
|
||||
|
||||
res.status(200).json(null);
|
||||
|
||||
console.log("🌱 - Still processing");
|
||||
const backendRequest = await evaluate({answer: path, question: fields.question}, task);
|
||||
console.log("🌱 - Process complete");
|
||||
|
||||
const correspondingStat = await getCorrespondingStat(fields.id, 1);
|
||||
|
||||
const solutions = correspondingStat.solutions.map((x) => ({
|
||||
/*const solutions = correspondingStat.solutions.map((x) => ({
|
||||
...x,
|
||||
evaluation: backendRequest.data,
|
||||
solution: url,
|
||||
}));
|
||||
}));*/
|
||||
|
||||
await db.collection("stats").updateOne(
|
||||
{ id: fields.id },
|
||||
{
|
||||
id: fields.id,
|
||||
solutions,
|
||||
score: {
|
||||
correct: speakingReverseMarking[backendRequest.data.overall || 0] || 0,
|
||||
total: 100,
|
||||
missing: 0,
|
||||
},
|
||||
isDisabled: false,
|
||||
await axios.post(`${process.env.BACKEND_URL}/grade/speaking/2`, {answer: path, question: fields.question}, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${process.env.BACKEND_JWT}`,
|
||||
},
|
||||
{upsert: true},
|
||||
);
|
||||
console.log("🌱 - Updated the DB");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getCorrespondingStat(id: string, index: number): Promise<Stat> {
|
||||
console.log(`🌱 - Try number ${index} - ${id}`);
|
||||
const correspondingStat = await db.collection("stats").findOne<Stat>({ id: id });
|
||||
|
||||
if (correspondingStat) return correspondingStat;
|
||||
await delay(3 * 10000);
|
||||
return getCorrespondingStat(id, index + 1);
|
||||
}
|
||||
|
||||
async function evaluate(body: {answer: string; question: string}, task: number): Promise<AxiosResponse> {
|
||||
const backendRequest = await axios.post(`${process.env.BACKEND_URL}/grade/speaking/2`, body, {
|
||||
headers: {
|
||||
@@ -85,7 +58,7 @@ async function evaluate(body: {answer: string; question: string}, task: number):
|
||||
},
|
||||
});
|
||||
|
||||
if (typeof backendRequest.data === "string") return evaluate(body, task);
|
||||
if (backendRequest.status !== 200) return evaluate(body, task);
|
||||
return backendRequest;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user