I am using openAI whisper to convert voice to text: https://platform.openai.com/docs/api-reference/audio/create
the json response is
{
"text": "some text"
}
the response data return only one text field and has no other information
I want to know how much tokens I have used, how can I do that?
3 Answers 3
In case someone was to see the question now, it is now possible to track usage
There is a field usage in the json response that allows for example to write code like:
from openai import OpenAI
client = OpenAI()
audio_file = open("speech.mp3", "rb")
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
print (transcript.usage)
You will then get a UsageDuration object that has a seconds field
So basically by doing math.ceil(transcript.usage.seconds / 60) * 0.006 you can get the cost of your transcription.
Pricing is available here
Comments
There are no tokens for OpenAI Audio API endpoints. You pay per minute.
As stated on the official OpenAI website:
As of March 2023, using the OpenAI Whisper audio model, you pay 0ドル.006/minute (rounded to the nearest second).
Note: You can't get minute usage from the OpenAI response like you can get token usage when using other OpenAI API endpoints. You need to write code and get the audio duration manually.
8 Comments
minute (or token or whatever) from the response???In NodeJS I did in this way:
/**
* Transcribe audio file
* @param {string} outputFilePath
* @returns {Promise<string>}
*/
async transcribeAudio(outputFilePath) {
const url = 'https://api.openai.com/v1/audio/transcriptions';
const apiKey = process.env.OPENAI_API_KEY;
const language = 'pt';
const formData = new FormData();
formData.append('file', createReadStream(path.resolve(outputFilePath)));
formData.append('model', 'whisper-1');
formData.append('response_format', 'verbose_json');
formData.append('language', language);
const headers = {'Authorization': `Bearer ${apiKey}`, ...formData.getHeaders()};
return await new Promise((resolve, reject) => {
const req = https.request(url, {method: 'POST', headers: headers}, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const response = JSON.parse(data);
console.log(`transcribeAudio response: "${response.text}" - Seconds: ${response.duration}`);
const segments = response && response.segments ? response.segments : [];
const transcription = segments.map((segment) => segment.text).join('\n');
resolve(transcription);
});
});
req.on('error', (error) => {
console.error('error on transcribe mp3 audio in openai whisper', error);
reject(error);
});
formData.pipe(req);
});
}