Transforming Audio to Text with OpenAI’s Speech-to-Text API: A Practical Step by Step Guide.

Transforming Audio to Text with OpenAI’s Speech-to-Text API: A Practical Step by Step Guide.

Transforming Audio to Text with OpenAI’s Speech-to-Text API: A Practical Step by Step Guide. Convert Zoom and WebEx and any Audio File!

Also Translate information to and from English!

Introduction

OpenAI’s Speech-to-Text API, leveraging the cutting-edge Whisper mannequin, is a flexible instrument for transcribing and translating audio into textual content. This article delves into the API’s capabilities, specializing in its transcriptions and translations endpoints. With help for a myriad of languages and a simple Python implementation, this API is a game-changer for builders and content material creators alike.

Quickstart: Transcribing Audio

Basic Transcription

The API’s transcription characteristic is easy to make use of. Here’s a primary instance in Python:

!pip set up openai
import os

# Prompt for the API key
api_key = enter("Enter your OpenAI API key: ")

# Set the surroundings variable
os.environ["OPENAI_API_KEY"] = api_key

Now name the Whisper APIs and you’ll translate ANY audio file. Here is the hyperlink to the audio file that I used: https://github.com/fenago/datasets/uncooked/fundamental/18-13-52.m4a

from openai import OpenAI
consumer = OpenAI()

audio_file = open("/content material/18-13-52.m4a", "rb")
transcript = consumer.audio.transcriptions.create(
mannequin="whisper-1",
file=audio_file
)

print(transcript)

This script opens an audio file and requests a transcription, which is returned as JSON containing the transcribed textual content.

Advanced Options

You can customise the transcription request with further parameters. For occasion, to specify the response format as plain textual content, modify the request like so:

audio_file = open("/content material/18-13-52.m4a", "rb")
transcript = consumer.audio.transcriptions.create(
mannequin="whisper-1",
file=audio_file,
response_format="textual content"
)

Translating Audio

The API additionally presents a translation endpoint, which transcribes audio in any supported language into English.

audio_file= open("/path/to/file/spanish.mp3", "rb")
transcript = consumer.audio.translations.create(
mannequin="whisper-1",
file=audio_file
)

Supported Languages

The API helps transcription and translation in quite a few languages, together with however not restricted to Arabic, Chinese, French, German, Spanish, and extra. While skilled on 98 languages, the listed ones exceed the <50% phrase error charge, making certain larger accuracy.

Handling Longer Inputs

For audio information bigger than 25 MB, it’s really useful to separate them into smaller segments utilizing instruments like PyDub:

from pydub import AudioPhase

music = AudioPhase.from_mp3("long_audio.mp3")
ten_minutes = 10 * 60 * 1000
first_10_minutes = music[:ten_minutes]
first_10_minutes.export("segment1.mp3", format="mp3")

Utilizing Prompts for Better Accuracy

Prompts can improve the standard of transcripts, particularly for correcting particular phrases or preserving context in break up information. The mannequin considers the ultimate 224 tokens of a immediate and makes use of related context to enhance transcription accuracy.

Conclusion

OpenAI’s Speech-to-Text API presents highly effective and versatile capabilities for audio transcription and translation. By integrating this API into your functions, you’ll be able to harness the ability of OpenAI’s Whisper mannequin to unlock new potentialities in audio processing.

Addendum: Enhancing Transcription Accuracy with OpenAI’s Whisper and GPT-4

Introduction

While OpenAI’s Whisper API excels in speech-to-text transcription, it faces challenges with unusual phrases or acronyms. This addendum introduces strategies to boost the reliability of Whisper, significantly in dealing with specialised vocabulary.

Using the Prompt Parameter in Whisper

Method Overview

The Whisper API permits for an non-obligatory immediate parameter. This methodology includes passing a dictionary of right spellings to information the transcription.

Example

def transcribe(filepath):
return consumer.audio.transcriptions.create(
mannequin="whisper-1",
file=open(filepath, "rb"),
immediate="ZyntriQix, Digique Plus, CynapseFive, VortiQore V8, EchoNix Array, OrbitalLink Seven, DigiFractal Matrix, PULSE, RAPT, B.R.I.C.Okay., Q.U.A.R.T.Z., F.L.I.N.T."
)

transcribed_text = transcribe("audio_file_path.mp3")

Limitations

This approach is constrained to the primary 244 tokens of the immediate, making it appropriate for small lists of specialised phrases.

Post-Processing with GPT-4

Method Overview

A extra scalable resolution includes a post-processing step with GPT-4, which has a bigger context window and higher instruction-following capabilities.

Implementation

  1. Define the System Prompt: Create a immediate that instructs GPT-4 to right spelling discrepancies.
system_prompt = "You are a useful assistant for the corporate ZyntriQix. Correct any spelling discrepancies within the transcribed textual content, specializing in the next product names: ZyntriQix, Digique Plus, CynapseFive, VortiQore V8, EchoNix Array, OrbitalLink Seven, DigiFractal Matrix, PULSE, RAPT, B.R.I.C.Okay., Q.U.A.R.T.Z., F.L.I.N.T."
  1. Generate Corrected Transcript: Use GPT-4 to refine the transcription.
def generate_corrected_transcript(temperature, system_prompt, audio_file):
original_transcript = transcribe(audio_file)
response = consumer.chat.completions.create(
mannequin="gpt-4",
temperature=temperature,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": original_transcript}
]
)
return response['choices'][0]['message']['content']

corrected_text = generate_corrected_transcript(0, system_prompt, "audio_file_path.mp3")

Advantages

  • Scalability: GPT-4’s bigger context window makes it extra appropriate for intensive and complicated vocabularies.
  • Reliability: GPT-4’s superior instruction-following capabilities guarantee extra correct corrections in comparison with Whisper’s immediate parameter.

Conclusion

By combining Whisper’s transcription capabilities with GPT-4’s subtle instruction-following and contextual understanding, we will considerably improve the accuracy of transcriptions, particularly when coping with specialised or unusual vocabulary. This hybrid strategy presents a sturdy resolution for a variety of transcription wants.

HI-FI News

through Artificial Intelligence on Medium https://ift.tt/5LM3CI1

November 19, 2023 at 12:33AM