Created
March 23, 2023 08:55
-
-
Save deepakness/060260aa8bd9cb334e228e77ec6d8900 to your computer and use it in GitHub Desktop.
Use Whisper API to transcribe audio right inside Google Sheets. The script takes .mp3 URLs in the column A and outputs transcribed text in the column B.
This file contains 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
function transcribeAudio() { | |
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); | |
var numRows = sheet.getLastRow(); | |
var urlColumn = 1; // Column A | |
var transcriptColumn = 2; // Column B | |
var authToken = "YOUR_OPENAI_API"; // Your OpenAI API Token | |
var model = "whisper-1"; // The Whisper API Model to use | |
for (var i = 2; i <= numRows; i++) { | |
var url = sheet.getRange(i, urlColumn).getValue(); | |
var transcript = sheet.getRange(i, transcriptColumn).getValue(); | |
if (url && !transcript) { | |
var payload = { | |
"method": "post", | |
"headers": { | |
"Authorization": "Bearer " + authToken, | |
}, | |
"payload": { | |
"file": UrlFetchApp.fetch(url, {"followRedirects": true}).getBlob(), | |
"model": model | |
}, | |
"muteHttpExceptions": true | |
}; | |
var response = UrlFetchApp.fetch("https://api.openai.com/v1/audio/transcriptions", payload); | |
if (response.getResponseCode() == 200) { | |
var json = JSON.parse(response.getContentText()); | |
sheet.getRange(i, transcriptColumn).setValue(json.text); | |
} else { | |
Logger.log(response.getContentText()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To make the script work, just replace
YOUR_OPENAI_API
with your OpenAI API (get it from here), paste your.mp3
link in the column A and run the script. Within a few seconds, the transcribed text will appear in column B.Here are 2 screenshots showing the same: image-1, image-2