Generative AI with Node.js in Hindi #12 Generate Text from Audio with UI

OpenAI and Node : Audio to Text with UI

Steps:-

  1. Make express js setup
  2. Make route for file upload
  3. upload file and get text from audio
  4. Check output
  5. Interview Questions
  6. Code and Notes link


Code:-


import OpenAI from "openai";
import dotenv from 'dotenv'
import express from 'express'
import multer from "multer";
import path from 'path'
import { createReadStream, unlinkSync } from "fs";

const app = express();
dotenv.config()
const client = new OpenAI({apiKey:process.env.openAI_Key})


app.get("/",(req,resp)=>{
resp.send(`<form action="/upload" method="post" enctype="multipart/form-data" >
<input type="file" name="audio" />
<button>Upload File</button>
</form>`)
})

const storage =multer.diskStorage({
diskStorage:'uploads/',
filename:(req,file,cb)=>{
const ext= path.extname(file.originalname);
cb(null,file.fieldname+ext)
}
})

const upload= multer({storage});

app.post("/upload",upload.single("audio"), async(req,resp)=>{

const filePath= req.file.path
const textResponse= await client.audio.transcriptions.create({
model:"whisper-1",
file:createReadStream(filePath),
language:'hi'

})
const output = textResponse.text;
// console.log(textResponse.text);
unlinkSync(filePath)
resp.send(`<h1>${output}<h1>`)
})


app.listen(3200)