Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"cmdk": "1.0.0",
"debounce": "^2.1.1",
"file-saver": "^2.0.5",
"fuse.js": "^7.0.0",
"geist": "^1.3.0",
"gridfs-stream": "^1.1.1",
"jose": "^5.4.1",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 47 additions & 45 deletions src/app/api/ai-upload/route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { NextResponse } from "next/server";
import { PDFDocument } from "pdf-lib";
import { campuses, exams, semesters, slots, years } from "@/components/select_options";
import {
campuses,
exams,
semesters,
slots,
years,
} from "@/components/select_options";
import { connectToDatabase } from "@/lib/mongoose";
import cloudinary from "cloudinary";
import { type ICourses, type CloudinaryUploadResult } from "@/interface";
import { PaperAdmin } from "@/db/papers";
import axios from "axios";
import processAndAnalyze from "@/util/mistral";
import { examMap } from "./map";
import Fuse from "fuse.js";
// import processAndAnalyze from "./mistral";
// TODO: REMOVE THUMBNAIL FROM admin-buffer DB
cloudinary.v2.config({
Expand All @@ -26,57 +33,54 @@ export async function POST(req: Request) {
const files: File[] = formData.getAll("files") as File[];
const isPdf = formData.get("isPdf") === "true"; // Convert string to boolean

let imageURL = ""
if(isPdf)
{
imageURL = formData.get("image") as string
}
else
{
let imageURL = "";
if (isPdf) {
imageURL = formData.get("image") as string;
} else {
const bytes = await files[0]?.arrayBuffer();
if (bytes) {
const buffer = await Buffer.from(bytes);
imageURL = `data:${"image/png"};base64,${buffer.toString("base64")}`;

}
}
const tags = await processAndAnalyze({imageURL})
let subject = ""
let slot = ""
let exam = ""
let year = ""
let campus = ""
let semester = ""

if(!tags)
{
console.log("Anaylsis failed, inputing blank strings as fields")
}
else{
subject = tags["course-name"]
slot = tags.slot
if("exam-type" in tags && tags["exam-type"] in examMap)
{
const examType = tags["exam-type"] as keyof typeof examMap;
exam = examMap[examType];

}
year = formData.get("year") as string;
campus = formData.get("campus") as string;
semester = formData.get("semester") as string;

}
}
console.log(exam, slot, subject)

const { data } = await axios.get<ICourses[]>(`${process.env.SERVER_URL}/api/course-list`);
const tags = await processAndAnalyze({ imageURL });
let subject = "";
let slot = "";
let exam = "";
let year = "";
let campus = "";
let semester = "";
const { data } = await axios.get<ICourses[]>(
`${process.env.SERVER_URL}/api/course-list`,
);
const courses = data.map((course: { name: string }) => course.name);
const coursesFuzy = new Fuse(courses);
if (!tags) {
console.log("Anaylsis failed, inputing blank strings as fields");
} else {
const subjectSearch = coursesFuzy.search(tags["course-name"])[0];
if (subjectSearch) {
subject = subjectSearch.item;
}
const slotPattern = new RegExp(`[${tags.slot}]`, "i");
const slotSearchResult = slots.find((s) => slotPattern.test(s));
slot = slotSearchResult ? slotSearchResult : tags.slot;
if ("exam-type" in tags && tags["exam-type"] in examMap) {
const examType = tags["exam-type"] as keyof typeof examMap;
exam = examMap[examType];
}
year = formData.get("year") as string;
campus = formData.get("campus") as string;
semester = formData.get("semester") as string;
}
console.log(exam, slot, subject);

if (
!(
exam.includes(exam) &&
years.includes(year) &&
campuses.includes(campus) &&
semesters.includes(semester)
semesters.includes(semester)
)
) {
return NextResponse.json({ message: "Bad request" }, { status: 400 });
Expand All @@ -93,9 +97,8 @@ export async function POST(req: Request) {
{ status: 400 },
);
}

if (!isPdf) {

if (!isPdf) {
try {
if (!process.env.NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET) {
return;
Expand Down Expand Up @@ -135,7 +138,7 @@ export async function POST(req: Request) {
year,
exam,
campus,
semester
semester,
});
await paper.save();
return NextResponse.json(
Expand Down Expand Up @@ -167,7 +170,6 @@ async function uploadFile(
fileType: string,
) {
try {

const buffer = Buffer.from(bytes);
const dataUrl = `data:${fileType};base64,${buffer.toString("base64")}`;
const uploadResult = (await cloudinary.v2.uploader.unsigned_upload(
Expand All @@ -182,7 +184,7 @@ async function uploadFile(

async function CreatePDF(files: File[]) {
const pdfDoc = await PDFDocument.create();
//sort files using name. Later remove to see if u can without names
//sort files using name. Later remove to see if u can without names
const orderedFiles = Array.from(files).sort((a, b) => {
return a.name.localeCompare(b.name);
});
Expand Down
4 changes: 0 additions & 4 deletions src/util/mistral.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import '@ungap/with-resolvers';
import { PDFDocument, } from "pdf-lib";
import { getDocument, GlobalWorkerOptions, version } from "pdfjs-dist";



import { Mistral } from "@mistralai/mistralai";

import { createCanvas } from "canvas";
// Type definitions
type ExamDetail = {
"course-name": string;
Expand Down