From 2b9d8675ba04bb92d37c13f9f331c4d92c477b80 Mon Sep 17 00:00:00 2001 From: tdanielles Date: Thu, 28 Nov 2024 13:58:49 +0900 Subject: [PATCH 1/3] connected flask to node with /transcriptions endpoint --- backend/index.js | 11 +++-- backend/package-lock.json | 84 +++++++++++++++++++++++++++++++++ backend/package.json | 2 + backend/src/routes/logRoutes.js | 47 ++++++++++++++---- transcription/app.py | 4 +- 5 files changed, 133 insertions(+), 15 deletions(-) diff --git a/backend/index.js b/backend/index.js index fddd630d..0a90b8d0 100644 --- a/backend/index.js +++ b/backend/index.js @@ -3,23 +3,24 @@ import dotenv from "dotenv"; import express from "express"; import authRoutes from "./src/routes/authRoutes.js"; import logRoutes from "./src/routes/logRoutes.js"; +import fileUpload from "express-fileupload"; dotenv.config(); const corsOptions = { - origin: ["http://localhost:5173"], + origin: ["http://localhost:5173"], }; const app = express(); const PORT = process.env.PORT || 8080; app.use(cors(corsOptions)); app.use(express.json()); +app.use(fileUpload()); //Routes -app.use('/api/auth', authRoutes); -app.use('/api/log', logRoutes); +app.use("/api/auth", authRoutes); +app.use("/api/log", logRoutes); app.listen(PORT, () => { - console.log(`Server listening on ${PORT}`); + console.log(`Server listening on ${PORT}`); }); - diff --git a/backend/package-lock.json b/backend/package-lock.json index a19137fa..8867a781 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -11,9 +11,11 @@ "dependencies": { "dependencies": "^0.0.1", "@supabase/supabase-js": "^2.46.1", + "axios": "^1.7.8", "cors": "^2.8.5", "dotenv": "^16.4.5", "express": "^4.21.0", + "express-fileupload": "^1.5.1", "jsonwebtoken": "^9.0.2", "supabase": "^1.207.9" }, @@ -3047,6 +3049,31 @@ "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.13.2.tgz", "integrity": "sha512-lHe62zvbTB5eEABUVi/AwVh0ZKY9rMMDhmm+eeyuuUQbQ3+J+fONVQOZyj+DdrvD4BY33uYniyRJ4UJIaSKAfw==" }, + "node_modules/axios": { + "version": "1.7.8", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.8.tgz", + "integrity": "sha512-Uu0wb7KNqK2t5K+YQyVCLM76prD5sRFjKHbJYCP1J7JFGEQ6nN7HWn9+04LAeiJ3ji54lgS/gZCH1oxyrf1SPw==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, + "node_modules/axios/node_modules/form-data": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz", + "integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/babel-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", @@ -3362,6 +3389,17 @@ "integrity": "sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==", "dev": true }, + "node_modules/busboy": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/busboy/-/busboy-1.6.0.tgz", + "integrity": "sha512-8SFQbg/0hQ9xy3UNTB0YEnsNBbWfhf7RtnzpL7TkBiTBRfrQ9Fxcnz7VJsleJpyp6rVLvXiuORqjlHi5q+PYuA==", + "dependencies": { + "streamsearch": "^1.1.0" + }, + "engines": { + "node": ">=10.16.0" + } + }, "node_modules/bytes": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz", @@ -4287,6 +4325,18 @@ "node": ">= 0.10.0" } }, + "node_modules/express-fileupload": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.5.1.tgz", + "integrity": "sha512-LsYG1ALXEB7vlmjuSw8ABeOctMp8a31aUC5ZF55zuz7O2jLFnmJYrCv10py357ky48aEoBQ/9bVXgFynjvaPmA==", + "license": "MIT", + "dependencies": { + "busboy": "^1.6.0" + }, + "engines": { + "node": ">=12.0.0" + } + }, "node_modules/express/node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -4471,6 +4521,26 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", + "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/foreground-child": { "version": "3.3.0", "resolved": "https://registry.npmjs.org/foreground-child/-/foreground-child-3.3.0.tgz", @@ -6707,6 +6777,12 @@ "node": ">= 0.10" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, "node_modules/psl": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.10.0.tgz", @@ -7349,6 +7425,14 @@ "node": ">= 0.8" } }, + "node_modules/streamsearch": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-1.1.0.tgz", + "integrity": "sha512-Mcc5wHehp9aXz1ax6bZUyY5afg9u2rv5cqQI3mRrYkGC8rW2hM02jWuwjtL++LS5qinSyhj2QfLyNsuc+VsExg==", + "engines": { + "node": ">=10.0.0" + } + }, "node_modules/string-length": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/string-length/-/string-length-4.0.2.tgz", diff --git a/backend/package.json b/backend/package.json index 9634e7a3..32385f1d 100644 --- a/backend/package.json +++ b/backend/package.json @@ -15,10 +15,12 @@ "description": "", "dependencies": { "@supabase/supabase-js": "^2.46.1", + "axios": "^1.7.8", "cors": "^2.8.5", "dependencies": "^0.0.1", "dotenv": "^16.4.5", "express": "^4.21.0", + "express-fileupload": "^1.5.1", "jsonwebtoken": "^9.0.2", "supabase": "^1.207.9" }, diff --git a/backend/src/routes/logRoutes.js b/backend/src/routes/logRoutes.js index 4f7bca2f..4e2258d7 100644 --- a/backend/src/routes/logRoutes.js +++ b/backend/src/routes/logRoutes.js @@ -1,17 +1,46 @@ import express from "express"; import auth from "../middlewares/auth.js"; -import insertTable from "../services/cardiacSurgeryAdultService.js" +import insertTable from "../services/cardiacSurgeryAdultService.js"; +import axios from "axios"; +import FormData from "form-data"; const router = express.Router(); router.post("/cardiacSurgeryAdultService", auth, async (req, res) => { - try { - const result = await insertTable(req, res); - res.json(result); - } catch(error) { - console.error("Caught Error:", error.message); - res.status(500).json({message: error.message}); + try { + const result = await insertTable(req, res); + res.json(result); + } catch (error) { + console.error("Caught Error:", error.message); + res.status(500).json({ message: error.message }); + } +}); + +router.post("/transcriptions", auth, async (req, res) => { + try { + if (!req.files || !req.files.image) { + return res.status(400).json({ error: "No image file provided" }); } -}) -export default router; \ No newline at end of file + const imageFile = req.files.image; + const formData = new FormData(); + formData.append("image", imageFile.data, imageFile.name); + + const response = await axios.post( + "http://localhost:5000/transcribe", + formData, + { + headers: { + ...formData.getHeaders(), + }, + } + ); + + res.json(response.data); + } catch (error) { + console.error("Caught Error:", error.message); + res.status(500).json({ message: error.message }); + } +}); + +export default router; diff --git a/transcription/app.py b/transcription/app.py index bff6161e..d4b1947f 100644 --- a/transcription/app.py +++ b/transcription/app.py @@ -1,9 +1,11 @@ from flask import Flask, request, jsonify +from flask_cors import CORS from PIL import Image from transformers import AutoProcessor, AutoModelForCausalLM import torch app = Flask(__name__) +CORS(app) # load model and processor once during init device = "cuda:0" if torch.cuda.is_available() else "cpu" @@ -37,4 +39,4 @@ def transcribe(): return jsonify({"error": str(e)}), 500 if __name__ == "__main__": - app.run(debug=True) \ No newline at end of file + app.run(host='0.0.0.0',port=5000) \ No newline at end of file From 75ae009fbe58cd2367f9d2458af520b276fb8183 Mon Sep 17 00:00:00 2001 From: tdanielles Date: Fri, 29 Nov 2024 13:06:59 +0900 Subject: [PATCH 2/3] put logic in separate file --- backend/index.js | 2 ++ backend/src/routes/logRoutes.js | 29 ------------------- backend/src/routes/transcriptionRoutes.js | 35 +++++++++++++++++++++++ 3 files changed, 37 insertions(+), 29 deletions(-) create mode 100644 backend/src/routes/transcriptionRoutes.js diff --git a/backend/index.js b/backend/index.js index 0a90b8d0..0284af06 100644 --- a/backend/index.js +++ b/backend/index.js @@ -3,6 +3,7 @@ import dotenv from "dotenv"; import express from "express"; import authRoutes from "./src/routes/authRoutes.js"; import logRoutes from "./src/routes/logRoutes.js"; +import transcriptionRoutes from "./src/routes/transcriptionRoutes.js"; import fileUpload from "express-fileupload"; dotenv.config(); @@ -20,6 +21,7 @@ app.use(fileUpload()); //Routes app.use("/api/auth", authRoutes); app.use("/api/log", logRoutes); +app.use("/api/transcriptions", transcriptionRoutes); app.listen(PORT, () => { console.log(`Server listening on ${PORT}`); diff --git a/backend/src/routes/logRoutes.js b/backend/src/routes/logRoutes.js index 4e2258d7..e6955084 100644 --- a/backend/src/routes/logRoutes.js +++ b/backend/src/routes/logRoutes.js @@ -1,8 +1,6 @@ import express from "express"; import auth from "../middlewares/auth.js"; import insertTable from "../services/cardiacSurgeryAdultService.js"; -import axios from "axios"; -import FormData from "form-data"; const router = express.Router(); @@ -16,31 +14,4 @@ router.post("/cardiacSurgeryAdultService", auth, async (req, res) => { } }); -router.post("/transcriptions", auth, async (req, res) => { - try { - if (!req.files || !req.files.image) { - return res.status(400).json({ error: "No image file provided" }); - } - - const imageFile = req.files.image; - const formData = new FormData(); - formData.append("image", imageFile.data, imageFile.name); - - const response = await axios.post( - "http://localhost:5000/transcribe", - formData, - { - headers: { - ...formData.getHeaders(), - }, - } - ); - - res.json(response.data); - } catch (error) { - console.error("Caught Error:", error.message); - res.status(500).json({ message: error.message }); - } -}); - export default router; diff --git a/backend/src/routes/transcriptionRoutes.js b/backend/src/routes/transcriptionRoutes.js new file mode 100644 index 00000000..fac5d1fb --- /dev/null +++ b/backend/src/routes/transcriptionRoutes.js @@ -0,0 +1,35 @@ +import axios from "axios"; +import FormData from "form-data"; +import express from "express"; +import auth from "../middlewares/auth.js"; + +const router = express.Router(); + +router.post("/", auth, async (req, res) => { + try { + if (!req.files || !req.files.image) { + return res.status(400).json({ error: "No image file provided" }); + } + + const imageFile = req.files.image; + const formData = new FormData(); + formData.append("image", imageFile.data, imageFile.name); + + const response = await axios.post( + "http://localhost:5000/transcribe", + formData, + { + headers: { + ...formData.getHeaders(), + }, + } + ); + + res.json(response.data); + } catch (error) { + console.error("Caught Error:", error.message); + res.status(500).json({ message: error.message }); + } +}); + +export default router; From f530a407eeef53e6f22c49427566784a5f18e264 Mon Sep 17 00:00:00 2001 From: tdanielles Date: Fri, 29 Nov 2024 13:22:54 +0900 Subject: [PATCH 3/3] update package-lock.json --- backend/package-lock.json | 63 +++++++++++++-------------------------- 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/backend/package-lock.json b/backend/package-lock.json index 17c6721b..6f56cc3a 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -3010,45 +3010,11 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==" }, - "node_modules/asn1": { - "version": "0.2.6", - "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", - "integrity": "sha512-ix/FxPn0MDjeyJ7i/yoHGFt/EX6LyNbxSEhPPXODPL+KB0VPk86UYfL0lMdy+KCnv+fmvIzySwaK5COwqVbWTQ==", - "dependencies": { - "safer-buffer": "~2.1.0" - } - }, - "node_modules/assert-plus": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/assert-plus/-/assert-plus-1.0.0.tgz", - "integrity": "sha512-NfJ4UzBCcQGLDlQq7nHxH+tv3kyZ0hHQqF5BO6J7tNJeP5do1llPr8dZ8zHonfhAu0PHAdMkSo+8o0wxg9lZWw==", - "engines": { - "node": ">=0.8" - } - }, - "node_modules/async": { - "version": "3.2.6", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", - "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==" - }, "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, - "node_modules/aws-sign2": { - "version": "0.7.0", - "resolved": "https://registry.npmjs.org/aws-sign2/-/aws-sign2-0.7.0.tgz", - "integrity": "sha512-08kcGqnYf/YmjoRhfxyu+CLxBjUtHLXLXX/vUfx9l2LYzG3c1m61nrpyFUZI6zeS+Li/wWMMidD9KgrqtGq3mA==", - "engines": { - "node": "*" - } - }, - "node_modules/aws4": { - "version": "1.13.2", - "resolved": "https://registry.npmjs.org/aws4/-/aws4-1.13.2.tgz", - "integrity": "sha512-lHe62zvbTB5eEABUVi/AwVh0ZKY9rMMDhmm+eeyuuUQbQ3+J+fONVQOZyj+DdrvD4BY33uYniyRJ4UJIaSKAfw==" - }, "node_modules/axios": { "version": "1.7.8", "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.8.tgz", @@ -3588,6 +3554,18 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "6.2.1", "resolved": "https://registry.npmjs.org/commander/-/commander-6.2.1.tgz", @@ -3769,6 +3747,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -6581,14 +6568,6 @@ "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", "license": "MIT" }, - "node_modules/psl": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/psl/-/psl-1.10.0.tgz", - "integrity": "sha512-KSKHEbjAnpUuAUserOq0FxGXCUrzC3WniuSJhvdbs102rL55266ZcHBqLWOsG30spQMlPdpy7icATiAQehg/iA==", - "dependencies": { - "punycode": "^2.3.1" - } - }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",