Skip to content

Commit

Permalink
[feat] Partially support OpenAI's optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ftnext committed Dec 7, 2024
1 parent ee84956 commit bbf6753
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion speech_recognition/recognizers/whisper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import os
from typing import Literal

from typing_extensions import Unpack

from speech_recognition.audio import AudioData
from speech_recognition.exceptions import SetupError
from speech_recognition.recognizers.whisper_api import (
Expand All @@ -13,12 +15,27 @@
WhisperModel = Literal["whisper-1"]


class OpenAIOptionalParameters:
"""OpenAI speech transcription's optional parameters.
https://platform.openai.com/docs/api-reference/audio/createTranscription
"""

language: str
prompt: str
# TODO Add support `Literal["text", "srt", "verbose_json", "vtt"]`
response_format: Literal["json"]
temperature: float
# timestamp_granularities # TODO support


def recognize_whisper_api(
recognizer,
audio_data: "AudioData",
*,
model: WhisperModel = "whisper-1",
api_key: str | None = None,
**kwargs: Unpack[OpenAIOptionalParameters],
) -> str:
"""
Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the OpenAI Whisper API.
Expand All @@ -40,4 +57,4 @@ def recognize_whisper_api(
)

recognizer = OpenAICompatibleRecognizer(openai.OpenAI(api_key=api_key))
return recognizer.recognize(audio_data, model)
return recognizer.recognize(audio_data, model, **kwargs)

0 comments on commit bbf6753

Please sign in to comment.