From a5a60635fcfffa6931d62899bd6c39c9f2bf56f5 Mon Sep 17 00:00:00 2001 From: santa Date: Mon, 24 Nov 2025 16:40:12 +0100 Subject: [PATCH] worked on fixing the code --- main.js | 8 +- package-lock.json | 2 + .../jsonTools/transcriptionSummarizer.js | 207 +++++++++--------- .../modules/transcription-remote/assembly.js | 46 ++-- 4 files changed, 140 insertions(+), 123 deletions(-) diff --git a/main.js b/main.js index c58c261..9e7ee55 100644 --- a/main.js +++ b/main.js @@ -139,7 +139,7 @@ electron.ipcMain.on("file_submit", async (event, args) => { let audiopath = "" let transcriptpath = "" - console.log("\n\n Running the Video to Audio Extractor"); + /* console.log("\n\n Running the Video to Audio Extractor"); // This code handles the Video to Audio extraction module call await mapFunctions.get("module-handler").function(args.video.module, {inputVideoPath: args.video.inputVideoPath, outputType: args.video.outputType}).then(resp => { console.log(resp); @@ -164,11 +164,13 @@ electron.ipcMain.on("file_submit", async (event, args) => { mainWindow.webContents.send("error", err) return }) - +*/ console.log("\n\n Running the Transcription Summarizer module"); // This code summarises the transcript, so that it can be used by an llm - await mapFunctions.get("summarize-transcription").function(transcriptpath).then(resp => { + // await mapFunctions.get("summarize-transcription").function(transcriptpath).then(resp => { + await mapFunctions.get("summarize-transcription").function('/Users/santa/Proj25/video2document/storage/transcripts/IMG_2978.json').then(resp => { + console.log(resp); transcriptpath = resp curstep++ diff --git a/package-lock.json b/package-lock.json index 76227c9..4de511e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -299,6 +299,7 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-24.9.2.tgz", "integrity": "sha512-uWN8YqxXxqFMX2RqGOrumsKeti4LlmIMIyV0lgut4jx7KQBcBiW6vkDtIBvHnHIquwNfJhk8v2OtmO8zXWHfPA==", "license": "MIT", + "peer": true, "dependencies": { "undici-types": "~7.16.0" } @@ -2534,6 +2535,7 @@ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", "dev": true, "license": "Apache-2.0", + "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/services/modules/jsonTools/transcriptionSummarizer.js b/services/modules/jsonTools/transcriptionSummarizer.js index e6e1318..4abaa62 100644 --- a/services/modules/jsonTools/transcriptionSummarizer.js +++ b/services/modules/jsonTools/transcriptionSummarizer.js @@ -1,5 +1,3 @@ -const fs = require("fs"); -const path = require("path"); // Prepare output directory (always storage/transcriptionSummaries under project root) const outputDir = `${__dirname}/../../../storage/transcriptionSummaries`; @@ -14,49 +12,98 @@ module.exports = { type: "summarizer", // value used to differentiate each module to order them in the UI displayname: "Summarizer", // The displayname used within the UI async function(args) { - let inputJson = args.json; + return new Promise(async (resolve, reject) => { + let inputJson = args.json; - //JSON Path - if (args.jsonPath) { - try { - const raw = fs.readFileSync(args.jsonPath, "utf-8"); - inputJson = JSON.parse(raw); - } catch (e) { - console.error("Failed to load JSON from file:", e); - return { error: "Could not read JSON from file path." }; + //JSON Path + if (args.jsonPath) { + try { + const raw = fs.readFileSync(args.jsonPath, "utf-8"); + inputJson = JSON.parse(raw); + } catch (e) { + console.error("Failed to load JSON from file:", e); + return { error: "Could not read JSON from file path." }; + } } - } - // JSON parsen - if (typeof inputJson === "string") { - try { - inputJson = JSON.parse(inputJson); - } catch (e) { - console.log("Invalid JSON in summarize-transcription"); - return { error: "Invalid JSON" }; + // JSON parsen + if (typeof args === "string") { + try { + await fs.readFile(args, 'utf8', function (err, data) { + if (err) throw err; + inputJson = JSON.parse(data); + }); + } catch (e) { + console.log("Invalid JSON in summarize-transcription"); + console.log(e) + return { error: "Invalid JSON" }; + } } - } - const words = inputJson.words; - if (!Array.isArray(words)) { - return { error: "No words Array found" }; - } + const words = inputJson.words; + if (!Array.isArray(words)) { + return { error: "No words Array found" }; + } - const ENDINGS = [".", "!", "?"]; // '...' auch als Satzende ? - const ABBREVIATIONS = new Set(["z.B.", "bzw.", "u.a.", "Dr.", "Mr.", "Mrs.", "Prof.", "etc."]); //TODO weitere Ergaenzen + const ENDINGS = [".", "!", "?"]; // '...' auch als Satzende ? + const ABBREVIATIONS = new Set(["z.B.", "bzw.", "u.a.", "Dr.", "Mr.", "Mrs.", "Prof.", "etc."]); //TODO weitere Ergaenzen - const result = []; - let currentSentence = ""; - let currentSpeaker = null; - let startTime = null; - let endTime = null; + const result = []; + let currentSentence = ""; + let currentSpeaker = null; + let startTime = null; + let endTime = null; - for (const w of words) { - if (!currentSpeaker) currentSpeaker = w.speaker; - if (startTime === null) startTime = w.start; - endTime = w.end; - - //speaker changing - if (currentSpeaker !== w.speaker && currentSentence) { + for (const w of words) { + if (!currentSpeaker) currentSpeaker = w.speaker; + if (startTime === null) startTime = w.start; + endTime = w.end; + + //speaker changing + if (currentSpeaker !== w.speaker && currentSentence) { + const lastEntry = result[result.length - 1]; + if (lastEntry && lastEntry.speaker === currentSpeaker) { + lastEntry.sentence += " " + currentSentence; + lastEntry.end = endTime; + } else { + result.push({ + speaker: currentSpeaker, + sentence: currentSentence, + start: startTime, + end: endTime + }); + } + currentSentence = ""; + startTime = w.start; + } + currentSpeaker = w.speaker; + currentSentence += (currentSentence ? " " : "") + w.text; //sentence beginning or not + const lastWord = w.text.trim(); + const lastChar = lastWord.slice(-1); + const isAbbreviation = ABBREVIATIONS.has(lastWord); + + //sentence ending + if (ENDINGS.includes(lastChar) && !isAbbreviation) { + const lastEntry = result[result.length - 1]; + if (lastEntry && lastEntry.speaker === currentSpeaker) { + lastEntry.sentence += " " + currentSentence; + lastEntry.end = endTime; + } else { + result.push({ + speaker: currentSpeaker, + sentence: currentSentence, + start: startTime, + end: endTime + }); + } + currentSentence = ""; + startTime = null; + endTime = null; + currentSpeaker = null; + } + } + + // safe last sentence + if (currentSentence) { const lastEntry = result[result.length - 1]; if (lastEntry && lastEntry.speaker === currentSpeaker) { lastEntry.sentence += " " + currentSentence; @@ -69,70 +116,30 @@ module.exports = { end: endTime }); } - currentSentence = ""; - startTime = w.start; } - currentSpeaker = w.speaker; - currentSentence += (currentSentence ? " " : "") + w.text; //sentence beginning or not - const lastWord = w.text.trim(); - const lastChar = lastWord.slice(-1); - const isAbbreviation = ABBREVIATIONS.has(lastWord); - //sentence ending - if (ENDINGS.includes(lastChar) && !isAbbreviation) { - const lastEntry = result[result.length - 1]; - if (lastEntry && lastEntry.speaker === currentSpeaker) { - lastEntry.sentence += " " + currentSentence; - lastEntry.end = endTime; - } else { - result.push({ - speaker: currentSpeaker, - sentence: currentSentence, - start: startTime, - end: endTime - }); - } - currentSentence = ""; - startTime = null; - endTime = null; - currentSpeaker = null; + // Output as Text + const output = result.map(r => + `Sprecher ${r.speaker} [${r.start.toFixed(2)} - ${r.end.toFixed(2)}]: ${r.sentence}` + ); + + // Output on cosole + //console.log("\n------------\nMerged Transcription Result:\n", output, "\n------------\n"); + + try { + const jsonPath = path.join(outputDir, "transcription_result.json"); + fs.writeFileSync(jsonPath, JSON.stringify(result, null, 2), "utf-8"); + + const txtPath = path.join(outputDir, "transcription_result.txt"); + fs.writeFileSync(txtPath, output.join("\n"), "utf-8"); + + console.log(`Summary successfully saved:\n- ${jsonPath}\n- ${txtPath}`); + + resolve(jsonPath); + } catch (err) { + console.error("Error saving Summary:", err); + reject(err); } - } - - // safe last sentence - if (currentSentence) { - const lastEntry = result[result.length - 1]; - if (lastEntry && lastEntry.speaker === currentSpeaker) { - lastEntry.sentence += " " + currentSentence; - lastEntry.end = endTime; - } else { - result.push({ - speaker: currentSpeaker, - sentence: currentSentence, - start: startTime, - end: endTime - }); - } - } - - // Output as Text - const output = result.map(r => - `Sprecher ${r.speaker} [${r.start.toFixed(2)} - ${r.end.toFixed(2)}]: ${r.sentence}` - ); - - // Output on cosole - //console.log("\n------------\nMerged Transcription Result:\n", output, "\n------------\n"); - - try { - const jsonPath = path.join(outputDir, "transcription_result.json"); - fs.writeFileSync(jsonPath, JSON.stringify(result, null, 2), "utf-8"); - - const txtPath = path.join(outputDir, "transcription_result.txt"); - fs.writeFileSync(txtPath, output.join("\n"), "utf-8"); - - console.log(`Summary successfully saved:\n- ${jsonPath}\n- ${txtPath}`); - } catch (err) { - console.error("Error saving Summary:", err); - } + }) } } diff --git a/services/modules/transcription-remote/assembly.js b/services/modules/transcription-remote/assembly.js index f2fbe0f..3b02e4c 100644 --- a/services/modules/transcription-remote/assembly.js +++ b/services/modules/transcription-remote/assembly.js @@ -79,6 +79,8 @@ function saveTranscript(transcript, sessionId) { fs.writeFileSync(outputPath, JSON.stringify(transcript, null, 2)); console.log(`Transcript saved: ${outputPath}`); + + return outputPath; } //---------------------------------------------------Modul--------------------------------------------------- @@ -89,29 +91,33 @@ module.exports = { displayname: 'AssemblyAI', async function(audioFileName) { - try { - // audioFileName ist nur "datei.mp3" - const audioPath = audioFileName; + return new Promise(async (resolve, reject) => { + try { + // audioFileName ist nur "datei.mp3" + const audioPath = audioFileName; - let audioUrl; + let audioUrl; - if (/^https?:\/\//i.test(audioFileName)) { - audioUrl = audioFileName; - } else { - if (!fs.existsSync(audioPath)) { - throw new Error(`Audio file not found: ${audioPath}`); + if (/^https?:\/\//i.test(audioFileName)) { + audioUrl = audioFileName; + } else { + if (!fs.existsSync(audioPath)) { + throw new Error(`Audio file not found: ${audioPath}`); + } + audioUrl = await uploadAudio(audioPath); } - audioUrl = await uploadAudio(audioPath); + + const transcriptId = await createTranscript(audioUrl); + const transcript = await pollTranscript(transcriptId); + + const sessionId = getSessionId(audioFileName); + + resolve(saveTranscript(transcript, sessionId)); + + } catch (error) { + console.error('Transcription error:', error.message); + reject(error); } - - const transcriptId = await createTranscript(audioUrl); - const transcript = await pollTranscript(transcriptId); - - const sessionId = getSessionId(audioFileName); - saveTranscript(transcript, sessionId); - - } catch (error) { - console.error('Transcription error:', error.message); - } + }) } };