Skip to content

Commit 2f0ec3f

Browse files
Upload page (#7)
* initial setup for upload page * wip: uploading pdfs * wip: connecting front end and server * Finals changes before merge * Fix: added each file separately to formData --------- Co-authored-by: isabe <[email protected]>
1 parent 9c80844 commit 2f0ec3f

File tree

4 files changed

+98
-19
lines changed

4 files changed

+98
-19
lines changed

server/src/gpt-controller.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import { GPTModel } from "./enums";
33
import { FileObject } from "openai/resources";
44
import path, { resolve } from "path";
55
import fs from "fs";
6-
import { AssistantBody, GPTResponse, ThreadMessage, GPTData, Testing, TestLocation } from "./types";
6+
import {
7+
AssistantBody,
8+
GPTResponse,
9+
ThreadMessage,
10+
GPTData,
11+
Testing,
12+
TestLocation,
13+
} from "./types";
714
import { prompt, questions } from "./prompts.data";
815
import { threadId } from "worker_threads";
916

@@ -49,7 +56,7 @@ export class GPTController {
4956
const loopPromises = Array.from({ length: 3 }, async (_) => {
5057
const assistant = await this.createAssistant(assistantParams);
5158
const thread = await this.createThread(threadMessage);
52-
59+
5360
// Run the assistant on the thread and get the prompt results
5461
let run = await GPTController.client.beta.threads.runs.createAndPoll(
5562
thread.id,
@@ -65,17 +72,23 @@ export class GPTController {
6572
// console.log("Tokens used: ", run.usage)
6673
var n = 1;
6774
for (const message of messages.data.reverse()) {
68-
if (message.content[0].type == "text") { // Need to check if the message content is text before parsing it
75+
if (message.content[0].type == "text") {
76+
// Need to check if the message content is text before parsing it
6977
var result = message.content[0].text.value;
7078
//console.log("Result: ", result) // FOR DEBUGGING
71-
if(n % 2 == 0) { // Every second message has the data values
79+
if (n % 2 == 0) {
80+
// Every second message has the data values
7281
// console.log(`${message.role} > ${result}`); // FOR DEBUGGING
73-
let preres = result.split("ø").map((s) => s.replace("\n", "") && s.replace(/^\s+/g, "")); // Trimming each string
74-
console.log("After split: ", preres)
75-
var resvalues: GPTData = {
82+
let preres = result
83+
.split("ø")
84+
.map((s) => s.replace("\n", "") && s.replace(/^\s+/g, "")); // Trimming each string
85+
console.log("After split: ", preres);
86+
var resvalues: GPTData = {
7687
paper_name: preres[0],
7788
year: parseInt(preres[1]),
78-
author: preres[2].split("¶").map((s) => s.replace(/^\s+/g, "")),
89+
author: preres[2]
90+
.split("¶")
91+
.map((s) => s.replace(/^\s+/g, "")),
7992
part_no: preres[3],
8093
type: preres[4],
8194
manufacturer: preres[5],
@@ -84,9 +97,9 @@ export class GPTController {
8497
testing_type: <Testing>preres[7],
8598
// TODO: preres[7] is a list ("TID, TID, DD") if the paper has more than one testing type, so the cast may fail
8699
// Produces weird output: "SEE【4:0†source】"
87-
data_type: 0 // TODO: Need to be removed hear, from the defined data types and in db controller
100+
data_type: 0, // TODO: Need to be removed hear, from the defined data types and in db controller
88101
};
89-
console.log(resvalues)
102+
console.log(resvalues);
90103
threadResults.push(resvalues);
91104
}
92105
n++;

server/src/routes/admin-router.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ export default function adminRouter(
4343
authenticateJWT,
4444
upload.array("pdfs"),
4545
(req: Request, res: Response) => {
46+
4647
try {
47-
// TODO
48-
parsePapers(req.files, gptController).then((result: GPTResponse[]) => {
49-
res.send(responseToJSON(result));
50-
});
48+
console.log(req.files);
49+
// TODO
50+
// parsePapers(req.files, gptController).then((result: GPTResponse[]) => {
51+
// res.send(responseToJSON(result));
52+
//});
5153
} catch (error) {
5254
console.error(`${error}`);
5355
}

web/src/App.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function App() {
5050
</Routes>
5151
</BrowserRouter>
5252
);
53-
5453
}
5554

5655
export default App;

web/src/pages/UploadPage.tsx

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,72 @@
1-
import "../App.css"
1+
import { ChangeEvent, useState } from "react";
2+
import "../App.css";
3+
import { Button, Input } from "@nextui-org/react";
24

35
export default function UploadPage() {
6+
const [files, setFiles] = useState<File[]>([]);
7+
8+
const handleFileChange = (event: ChangeEvent<HTMLInputElement>) => {
9+
const selectedFiles = Array.from(event.target.files ?? []); // Convert FileList to an array
10+
const validFiles = selectedFiles.filter(
11+
(file) => file?.type === "application/pdf"
12+
);
13+
14+
if (validFiles.length === 0) {
15+
console.log("not valid files");
16+
return;
17+
} else {
18+
setFiles((prevFiles) => [...prevFiles, ...validFiles]);
19+
}
20+
};
21+
22+
async function submitPapers() {
23+
const token = localStorage.getItem("jwtToken");
24+
25+
const formData = new FormData();
26+
for (let i = 0; i < files.length; i++) {
27+
formData.append(files[i].name, files[i]);
28+
}
29+
30+
try {
31+
const response = await fetch(
32+
"http://localhost:3000/api/adminRequest/parseRequest",
33+
{
34+
method: "POST",
35+
headers: {
36+
"Content-Type": "application/json",
37+
Authorization: `Bearer ${token}`,
38+
},
39+
body: formData,
40+
}
41+
);
42+
if (response.ok) {
43+
const result = await response.json();
44+
console.log(result);
45+
} else {
46+
console.error(`Failed to fetch papers: ${response.status}`);
47+
}
48+
} catch (error) {
49+
console.error(`Error fetching papers: ${error}`);
50+
throw error;
51+
}
52+
}
53+
454
return (
5-
<div>Upload Page</div>
6-
)
7-
}
55+
<div>
56+
<div>Upload Page</div>
57+
<Input
58+
type="file"
59+
accept="application/pdf"
60+
multiple
61+
onChange={handleFileChange}
62+
/>
63+
<Button
64+
className="bg-usask-green text-[#DADADA]"
65+
type="submit"
66+
onClick={submitPapers}
67+
>
68+
Upload
69+
</Button>
70+
</div>
71+
);
72+
}

0 commit comments

Comments
 (0)