Created
November 29, 2024 11:22
-
-
Save michaelkove/ebf20937d82ab412a345c9d8a93204c4 to your computer and use it in GitHub Desktop.
OpenAI API Script to Transcribe Voice to Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the required modules | |
import fs from "fs"; | |
import OpenAI from "openai"; | |
import dotenv from "dotenv"; | |
dotenv.config(); | |
// Initialize the OpenAI client with your API key | |
const openai = new OpenAI({ | |
apiKey: process.env.OPENAI_API_KEY, //use your API Key | |
}); | |
async function main(filePath = null) { | |
try { | |
// Ensure the file exists before proceeding | |
if (!fs.existsSync(filePath)) { | |
throw new Error(`File not found at path: ${filePath}`); | |
} | |
// Call the OpenAI transcription API | |
const transcription = await openai.audio.transcriptions.create({ | |
file: fs.createReadStream(filePath), | |
model: "whisper-1", | |
response_format: "text", | |
}); | |
// Output the transcribed text to the console | |
console.log("Transcribed text:"); | |
console.log(transcription); | |
} catch (error) { | |
console.error("Error occurred while transcribing audio:", error.message); | |
} | |
} | |
// Run the main function via node transcribe.js | |
const pathToFile = "/Users/.../Documents/recording.mp3" // use your own path, must be full path. | |
main(pathToFile).then(() => { | |
console.log("Transcription completed successfully."); | |
}).catch((error) => { | |
console.error("Error:", error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment