Java or Kotlin equivalent for Transcribe API

Hello community,

I want to use java or kotlin code for the Transcribe API. The only available I see here is Python, node and curl.
I am trying to use Retrofit with the below code:

fun transcribeAudio(audioFile: File): String {
        val audioRequestBody = audioFile.asRequestBody("audio/*".toMediaType())

        val request = Request.Builder()
            .url("https://api.openai.com/v1/audio/transcriptions")
            .header("Authorization", "Bearer API_KEY")
            .post(audioRequestBody)

        return client.newCall(request.build()).execute().use { response ->
            response.body?.string() ?: ""
        }
    }

but I do not know how can I also send the model “whisper-1” at the request? Whatever combination I have tried I am getting an error from the API.

Thanks

1 Like

I found an alternative that works

fun transcribeAudio(audioFile: File): String {
        val audioRequestBody = audioFile.asRequestBody("audio/*".toMediaType())

        val formBody = MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", audioFile.name, audioRequestBody)
            .addFormDataPart("model", "whisper-1")
            .addFormDataPart("language", "en")
            .build()

        val request = Request.Builder()
            .url("https://api.openai.com/v1/audio/transcriptions")
            .header("Authorization", "Bearer API_KEY")
            .post(formBody)

        return client.newCall(request.build()).execute().use { response ->
            response.body?.string() ?: ""
        }
    }

For someone that will search it in the future the above is correct.

2 Likes

I will be doing this soon myself so, that will give me a head start. Thanks for sharing how you did it.

1 Like