Transcribing Audio Files With OpenAI in Spring AI

Transcribing Audio Files With OpenAI in Spring AI

1. Overview

Businesses typically have to extract significant knowledge from varied varieties of audio content material, equivalent to transcribing buyer assist requires sentiment evaluation, producing subtitles for movies, or producing minutes from conferences. However, manually transcribing audio recordsdata is a time-consuming and costly course of.

To automate this course of, OpenAI provides highly effective speech-to-text fashions able to precisely transcribing audio recordsdata in a number of languages.

In this tutorial, we’ll discover the way to transcribe audio recordsdata with OpenAI’s speech-to-text fashions utilizing Spring AI.

To comply with together with this tutorial, we’ll want an OpenAI API key.

2. Setting up the Project

Before we will begin implementing our audio transcriber, we’ll want to incorporate the mandatory dependency and configure our utility accurately.

2.1. Dependencies

Let’s begin by including Spring AI’s OpenAI starter dependency to our mission’s pom.xml file:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-openai</artifactId>
    <model>1.0.0-M7</model>
</dependency>

Since the present model, 1.0.0-M7, is a milestone launch, we’ll additionally want so as to add the Spring Milestones repository to our pom.xml:

<repositories>
    <repository>
        <id>spring-milestones</id>
        <title>Spring Milestones</title>
        <url>https://repo.spring.io/milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

This repository is the place milestone variations are revealed, versus the usual Maven Central repository.

2.2. Configuring OpenAI Properties

Next, let’s configure the OpenAI API key and speech-to-text mannequin in our utility.yaml file:

spring:
  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      audio:
        transcription:
          choices:
            mannequin: whisper-1
            language: en

We use the ${} property placeholder to load the worth of our API key from an surroundings variable.

Here, we specify OpenAI’s Whisper mannequin by way of its model-id of whisper-1. It’s vital to notice that OpenAI provides extra superior and higher-quality speech-to-text fashions like gpt-4o-transcribe and gpt-4o-mini-transcribe. However, they’re not supported by the present model of Spring AI.

Additionally, we specify en because the language of our audio recordsdata. Alternatively, we will specify a unique enter language in ISO-639-1 format based on necessities. If not specified, the required mannequin will try to detect the language spoken within the audio robotically.

On configuring the above properties, Spring AI robotically creates a bean of kind OpenAiAudioTranscriptionModel, permitting us to work together with the required mannequin.

3. Building Our Audio Transcriber

With our configurations in place, let’s create an AudioTranscriber service class. We’ll inject the OpenAiAudioTranscriptionModel bean that Spring AI robotically creates for us.

But first, let’s outline two easy records to characterize the request and response payloads:

report TranscriptionRequest(MultipartFile audioFile, @Nullable String context) {}
report TranscriptionResponse(String transcription) {}

The TranscriptionRequest accommodates the audioFile to be transcribed and an elective context to assist the mannequin with the transcription course of. It’s vital to notice that OpenAI at present helps audio recordsdata in mp3, mp4, mpeg, mpga, m4a, wav, and webm codecs.

Similarly, the TranscriptionResponse merely holds the generated transcription.

Now, let’s implement the supposed performance:

TranscriptionResponse transcribe(TranscriptionRequest transcriptionRequest) {
    AudioTranscriptionPrompt immediate = new AudioTranscriptionPrompt(
      transcriptionRequest.audioFile().getResource(),
      OpenAiAudioTranscriptionOptions
        .builder()
        .immediate(transcriptionRequest.context())
        .construct()
    );
    AudioTranscriptionResponse response = openAiAudioTranscriptionModel.name(immediate);
    return new TranscriptionResponse(response.getResult().getOutput());
}

Here, we add a brand new transcribe() technique to our AudioTranscriber class.

We create an AudioTranscriptionPrompt object utilizing the audioFile useful resource and the elective context immediate. Then, we use it to invoke the name() technique of the autowired OpenAiAudioTranscriptionModel bean.

Finally, we extract the transcribed textual content from the response and return it wrapped in our TranscriptionResponse report.

Currently, for the speech-to-text fashions, the audio file dimension is restricted to 25 MB. However, by default, Spring Boot limits the dimensions of uploaded recordsdata to 1 MB. Let’s enhance this restrict in our utility.yaml file:

spring:
  servlet:
    multipart:
      max-file-size: 25MB
      max-request-size: 25MB

We set the utmost file dimension and request dimension to 25MB, which needs to be enough for many audio transcription requests.

4. Testing Our Audio Transcriber

Now that we’ve carried out our service layer, let’s expose a REST API on prime of it:

@PostMapping("/transcribe")
ResponseEntity<TranscriptionResponse> transcribe(
  @RequestParam("audioFile") MultipartFile audioFile,
  @RequestParam("context") String context
) {
    TranscriptionRequest transcriptionRequest = new TranscriptionRequest(audioFile, context);
    TranscriptionResponse response = audioTranscriber.transcribe(transcriptionRequest);
    return ResponseEntity.okay(response);
}

Next, let’s use the HTTPie CLI to invoke the above API endpoint:

http -f POST :8080/transcribe [email protected] context="Short description about Baeldung"

Here, we invoke our /transcribe API and ship the audioFile together with its context. For our demonstration, we’ve ready an audio file that gives a brief description of Baeldung. This file may be discovered within the src/check/sources/audio folder of the codebase.

Let’s see what we get as a response:

{
    "transcription": "Baeldung is a top-notch instructional platform that makes a speciality of Java, Spring, and associated applied sciences. It provides a wealth of tutorials, articles, and programs that assist builders grasp programming ideas. Known for its clear examples and sensible guides, Baeldung is a go-to useful resource for builders seeking to stage up their expertise."
}

As we will see, the API returns the proper transcription of the supplied audio file.

Notice how offering the context immediate helps the mannequin accurately transcribe the Baeldung title. Without this context, the Whisper mannequin transcribes the phrase as Baildung.

5. Conclusion

In this text, we’ve explored transcribing audio recordsdata with OpenAI in Spring AI.

We walked via the mandatory configuration and carried out an audio transcriber utilizing OpenAI’s Whisper speech-to-text mannequin. We additionally examined our utility and noticed how offering a context immediate improves the accuracy of the generated transcription, particularly for domain-specific names.

As at all times, all of the code examples used on this article can be found over on GitHub.

The publish Transcribing Audio Files With OpenAI in Spring AI first appeared on Baeldung.

       

HI-FI News

by way of Baeldung https://ift.tt/Jf4CscF

May 7, 2025 at 09:06PM