Generative AI with Node.js in Hindi #13 Convert Audio from Text with UI
OpenAI and Node : Audio to Text with UI
Steps:-
- Make express js setup
- Make route for file upload
- upload file and get text from audio
- Check output
- Interview Questions
- Code and Notes link
Code:-
import OpenAI from "openai";
import dotenv from 'dotenv'
import {writeFileSync} from 'fs'
import express from "express";
const app = express();
app.use(express.urlencoded({extended:true}))
dotenv.config();
const client = new OpenAI({apiKey:process.env.openAI_Key})
app.get("/",(req,resp)=>{
resp.send(`<form action="/audio" method="post" >
<input type="text" name="inputData" />
<br/>
<br/>
<button> Convert Text in Audio </button>
</form>`)
})
app.post("/audio", async(req,resp)=>{
const response = await client.audio.speech.create({
model:"gpt-4o-mini-tts",
input:req?.body?.inputData,
voice:"coral",
language:"hi"
});
const baseResponse = Buffer.from( await response.arrayBuffer())
writeFileSync("audio2.mp3",baseResponse)
console.log(baseResponse)
resp.send("Text converted to audio")
})
app.listen(3200)