Skip to content

Commit

Permalink
Merge pull request #804 from ftnext/remove-openai-api-key
Browse files Browse the repository at this point in the history
Remove `api_key` parameter for OpenAI client
  • Loading branch information
ftnext authored Dec 8, 2024
2 parents 11ee180 + b25fd60 commit e13ed63
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 25 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ jobs:
- name: Install Python dependencies (Ubuntu, <=3.12)
if: matrix.os == 'ubuntu-latest' && matrix.python-version != '3.13'
run: |
python -m pip install .[dev,audio,pocketsphinx,whisper-local,whisper-api,groq]
python -m pip install .[dev,audio,pocketsphinx,whisper-local,openai,groq]
- name: Install Python dependencies (Ubuntu, 3.13)
if: matrix.os == 'ubuntu-latest' && matrix.python-version == '3.13'
run: |
python -m pip install standard-aifc setuptools
python -m pip install --no-build-isolation .[dev,audio,pocketsphinx,whisper-api,groq]
python -m pip install --no-build-isolation .[dev,audio,pocketsphinx,openai,groq]
- name: Install Python dependencies (Windows)
if: matrix.os == 'windows-latest'
run: |
python -m pip install .[dev,whisper-local,whisper-api,groq]
python -m pip install .[dev,whisper-local,openai,groq]
- name: Test with unittest
run: |
pytest --doctest-modules -v speech_recognition/recognizers/ tests/
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ OpenAI Whisper API (for OpenAI Whisper API users)

The library `openai <https://pypi.org/project/openai/>`__ is **required if and only if you want to use OpenAI Whisper API** (``recognizer_instance.recognize_openai``).

If not installed, everything in the library will still work, except calling ``recognizer_instance.recognize_openai`` will raise an ``RequestError``.
You can install it with ``python3 -m pip install SpeechRecognition[openai]``.

You can install it with ``python3 -m pip install SpeechRecognition[whisper-api]``.
Please set the environment variable ``OPENAI_API_KEY`` before calling ``recognizer_instance.recognize_openai``.

Groq Whisper API (for Groq Whisper API users)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -189,7 +189,7 @@ The library `groq <https://pypi.org/project/groq/>`__ is **required if and only

You can install it with ``python3 -m pip install SpeechRecognition[groq]``.

Please set the environment variable ``GROQ_API_KEY`` before calling ``recognizer_instance.recognize_groq``
Please set the environment variable ``GROQ_API_KEY`` before calling ``recognizer_instance.recognize_groq``.

Troubleshooting
---------------
Expand Down
7 changes: 5 additions & 2 deletions examples/microphone_recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# NOTE: this example requires PyAudio because it uses the Microphone class

import os

import speech_recognition as sr

# obtain audio from the microphone
Expand Down Expand Up @@ -95,7 +97,8 @@

# recognize speech using Whisper API
OPENAI_API_KEY = "INSERT OPENAI API KEY HERE"
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
try:
print(f"Whisper API thinks you said {r.recognize_openai(audio, api_key=OPENAI_API_KEY)}")
print(f"OpenAI Whisper API thinks you said {r.recognize_openai(audio)}")
except sr.RequestError as e:
print(f"Could not request results from Whisper API; {e}")
print(f"Could not request results from OpenAI Whisper API; {e}")
12 changes: 3 additions & 9 deletions reference/library-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,10 @@ You can translate the result to english with Whisper by passing translate=True

Other values are passed directly to whisper. See https://github.com/openai/whisper/blob/main/whisper/transcribe.py for all options

``recognizer_instance.recognize_openai(audio_data: AudioData, model: str = "whisper-1", api_key: str | None = None)``
---------------------------------------------------------------------------------------------------------------------

Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the OpenAI Whisper API.

This function requires an OpenAI account; visit https://platform.openai.com/signup, then generate API Key in `User settings <https://platform.openai.com/account/api-keys>`__.

Detail: https://platform.openai.com/docs/guides/speech-to-text
``recognizer_instance.recognize_openai(audio_data: AudioData, model = "whisper-1", **kwargs)``
----------------------------------------------------------------------------------------------

Raises a ``speech_recognition.exceptions.SetupError`` exception if there are any issues with the openai installation, or the environment variable is missing.
.. autofunction:: speech_recognition.recognizers.openai.recognize

``recognizer_instance.recognize_groq(audio_data: AudioData, model = "whisper-large-v3-turbo", **kwargs)``
---------------------------------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pocketsphinx =
whisper-local =
openai-whisper
soundfile
whisper-api =
openai =
openai
httpx < 0.28
groq =
Expand Down
9 changes: 2 additions & 7 deletions speech_recognition/recognizers/openai.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
from typing import Literal

from typing_extensions import Unpack
Expand Down Expand Up @@ -34,7 +33,6 @@ def recognize(
audio_data: "AudioData",
*,
model: WhisperModel = "whisper-1",
api_key: str | None = None,
**kwargs: Unpack[OpenAIOptionalParameters],
) -> str:
"""
Expand All @@ -44,17 +42,14 @@ def recognize(
Detail: https://platform.openai.com/docs/guides/speech-to-text
Raises a ``speech_recognition.exceptions.SetupError`` exception if there are any issues with the openai installation, or the environment variable is missing.
Set environment variable ``OPENAI_API_KEY``; otherwise openai library will raise a ``openai.OpenAIError``.
"""
if api_key is None and os.environ.get("OPENAI_API_KEY") is None:
raise SetupError("Set environment variable ``OPENAI_API_KEY``")

try:
import openai
except ImportError:
raise SetupError(
"missing openai module: ensure that openai is set up correctly."
)

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

0 comments on commit e13ed63

Please sign in to comment.