Merge branch 'feature/gemini-ai' into 'develop'

REST API & Auto Naming

See merge request proj-wise2526-video2document/video2document!31
This commit is contained in:
Spanier, Pit
2025-11-26 21:57:51 +01:00
+33 -12
View File
@@ -4,9 +4,10 @@ if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true }); // Create output directory if it doesn't exist fs.mkdirSync(outputDir, { recursive: true }); // Create output directory if it doesn't exist
} }
const ai = new genai.GoogleGenAI({ // Ensure Google API key is set in environment variables: export GOOGLE_API_KEY="your_api_key_here"
apiKey: process.env.GOOGLE_API_KEY // Ensure Google API key is set in environment variables: export GOOGLE_API_KEY="your_api_key_here"
}); const GEMINI_API_KEY = process.env.GOOGLE_API_KEY; // Ensure Google API key is set in environment variables: export GOOGLE_API_KEY="your_api_key_here"
const GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent"; //URL for the REST call, used model and action
module.exports = { module.exports = {
name: "llm-gemini", name: "llm-gemini",
@@ -31,19 +32,39 @@ module.exports = {
createDocumentFromTranscript: async function(transcriptPath, documentTypePath, language = "en") { // default language is English createDocumentFromTranscript: async function(transcriptPath, documentTypePath, language = "en") { // default language is English
try { try {
const transcript = await fs.promises.readFile(transcriptPath, "utf-8"); const transcript = await fs.promises.readFile(transcriptPath, "utf-8"); //read transcript file from Path
const documentType = await fs.promises.readFile(documentTypePath, "utf-8"); const documentType = await fs.promises.readFile(documentTypePath, "utf-8"); //read document type from Path
const promptText = `${documentType}, in language ${language}, transcript:\n\n${transcript}`; //combine doc type, language and transcript - Change prompt here if needed
const promptText = `${documentType}, in language ${language}, transcript:\n\n${transcript}`; // --- REST CALL ---
const response = await fetch(`${GEMINI_URL}?key=${GEMINI_API_KEY}`, { //safe model response in variable
const response = await ai.models.generateContent({ method: "POST",
model: "gemini-2.5-flash", // Specify the Gemini model to use headers: {
contents: promptText // Input prompt for content generation "Content-Type": "application/json"
},
body: JSON.stringify({
contents: [
{
parts: [
{ text: promptText } // Input prompt for content generation
]
}
]
})
}); });
const output = response.text || ""; // Get generated text from response or default to empty string (if null) if (!response.ok) { //ok is true when a responce was successfull
const text = await response.text();
throw new Error(`Gemini API error (${response.status}): ${text}`);
}
const outPath = path.join(outputDir, "test.md"); // Output file path & name TO BE DONE to make dynamic out of input transcript name const data = await response.json();
// Get generated text from response or default to empty string (if null)
const output = data?.candidates?.[0]?.content?.parts?.[0]?.text || "";
let inputTranscriptName = path.basename(transcriptPath, path.extname(transcriptPath)); // Name for the output file
console.log(inputTranscriptName);
const outPath = path.join(outputDir, `${inputTranscriptName}.md`); // Output file path & name to make naming dynamic. Pulled from input transcript name
fs.writeFileSync(outPath, output, "utf8"); // Write output to file fs.writeFileSync(outPath, output, "utf8"); // Write output to file
console.log("Generated document written to:", outPath); console.log("Generated document written to:", outPath);