Hello Ted,
Welcome to the Microsoft Q&A and thank you for posting your questions here.
I understand that you would like to know how you can use the whisper OpenAI model in an azure function for azure static webapp.
Regarding your anticipation to use Next.js, the below are the steps to Implement Whisper Model in an Azure Function:
Make sure you can set up an Azure Function App, Node.js runtime to align with your Next.js expertise unless you prefer Python. Then, install required libraries using npm install azure-storage-blob @azure/functions whisper-api-client
Create a Blob Storage account and a container (e.g., audio-files
), and enable public access or use shared access signatures (SAS) for securely accessing Blob files.
Below is a JavaScript-based Azure Function to integrate Whisper with Blob Storage:
const { BlobServiceClient } = require('@azure/storage-blob');
const WhisperClient = require('whisper-api-client');
module.exports = async function (context, req) {
const audioUrl = req.query.audioUrl || (req.body && req.body.audioUrl);
if (!audioUrl) {
context.res = { status: 400, body: 'Please provide the audioUrl parameter.' };
return;
}
try {
// Initialize Whisper API client
const whisperClient = new WhisperClient({ apiKey: process.env.WHISPER_API_KEY });
// If audio is uploaded to Blob Storage, get the signed URL
const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.AZURE_STORAGE_CONNECTION_STRING);
const containerClient = blobServiceClient.getContainerClient('audio-files');
const blobName = new URL(audioUrl).pathname.split('/').pop(); // Extract blob name from URL
const blobClient = containerClient.getBlobClient(blobName);
// Generate SAS URL for the Blob
const sasUrl = blobClient.generateSasUrl({
permissions: 'read',
expiresOn: new Date(new Date().getTime() + 3600 * 1000), // 1 hour expiry
});
// Call Whisper API to transcribe audio
const transcription = await whisperClient.transcribeAudio(sasUrl);
context.res = { status: 200, body: transcription.text };
} catch (error) {
context.log.error('Error processing request:', error);
context.res = { status: 500, body: 'Internal server error.' };
}
};
The next is to configure the following variables in the Azure Function App settings:
- AZURE_STORAGE_CONNECTION_STRING: Your Azure Blob Storage connection string.
- WHISPER_API_KEY: Your Whisper model API key (if applicable).
Make sure your File Path and URL is addressed properly,
- URLs are direct web-accessible locations for files (e.g.,
https://<storage-account>.blob.core.windows.net/audio-files/sample.wav
). - Paths refer to local file locations (e.g.,
/tmp/sample.wav
). you can even generate SAS URLs for Blob files, which act as temporary URLs.
I hope this is helpful! Do not hesitate to let me know if you have any other questions.
Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.