translateAudio method
Future<GptTranslationObject>
translateAudio({
- required String apiKey,
- required String organizationId,
- required GptAudioTranslationRequest request,
- required Uint8List fileBytes,
Translates audio into English.
Implementation
Future<GptTranslationObject> translateAudio(
{required String apiKey,
required String organizationId,
required GptAudioTranslationRequest request,
required Uint8List fileBytes}) async {
Map<String, String> headers = {
"Authorization": "Bearer $apiKey",
"OpenAI-Organization": organizationId,
'Content-Type': 'application/json',
"Access-Control-Allow-Origin": "*", // Required for CORS support to work
};
Uri endpoint;
if (secure) {
endpoint = Uri.https(baseUrl, "/v1/audio/translations");
} else {
endpoint = Uri.http(baseUrl, "/v1/audio/translations");
}
String? targetFilename = request.fileName;
request.fileName = null;
var httpRequest = http.MultipartRequest('POST', endpoint)
..headers.addAll(headers)
..fields.addAll(
request.toJson().map((key, value) => MapEntry(key, value.toString())))
..files.add(http.MultipartFile.fromBytes("file", fileBytes,
filename: targetFilename));
var response = await httpRequest.send();
if (response.statusCode == 200 || response.statusCode == 201) {
return GptTranslationObject.fromJson(jsonDecode(
const Utf8Decoder().convert(await response.stream.toBytes())));
} else {
var error = ServerError.fromJson(jsonDecode(
const Utf8Decoder().convert(await response.stream.toBytes())));
throw AudioException(
statusCode: response.statusCode, message: error.message);
}
}