Skip to content

Commit

Permalink
Merge pull request #803 from ftnext/tweak-groq-code
Browse files Browse the repository at this point in the history
Tweak Groq code
  • Loading branch information
ftnext authored Dec 8, 2024
2 parents d42245b + d95f8ba commit 11ee180
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rstcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
python -m pip install rstcheck
- name: Run rstcheck
run: |
python -m rstcheck README.rst reference/*.rst
python -m rstcheck --ignore-directives autofunction README.rst reference/*.rst
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ Groq Whisper API (for Groq Whisper API users)

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

If not installed, everything in the library will still work, except calling ``recognizer_instance.recognize_groq`` will raise an ``RequestError``.

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``

Troubleshooting
---------------

Expand Down
5 changes: 5 additions & 0 deletions reference/library-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,11 @@ 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.

``recognizer_instance.recognize_groq(audio_data: AudioData, model = "whisper-large-v3-turbo", **kwargs)``
---------------------------------------------------------------------------------------------------------

.. autofunction:: speech_recognition.recognizers.groq.recognize

``AudioSource``
---------------

Expand Down
2 changes: 1 addition & 1 deletion speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,7 @@ def flush(self, *args, **kwargs):
Recognizer.recognize_google = google.recognize_legacy
Recognizer.recognize_openai = openai.recognize
Recognizer.recognize_whisper_api = openai.recognize # Deprecated
Recognizer.recognize_groq = groq.recognize_groq
Recognizer.recognize_groq = groq.recognize


# ===============================
Expand Down
11 changes: 4 additions & 7 deletions speech_recognition/recognizers/groq.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import os
from typing import Literal, TypedDict

from typing_extensions import Unpack

from speech_recognition.audio import AudioData
Expand All @@ -28,7 +28,7 @@ class GroqOptionalParameters(TypedDict):
language: str


def recognize_groq(
def recognize(
recognizer,
audio_data: "AudioData",
*,
Expand All @@ -42,11 +42,8 @@ def recognize_groq(
Detail: https://console.groq.com/docs/speech-text
Raises a ``speech_recognition.exceptions.SetupError`` exception if there are any issues with the groq installation, or the environment variable is missing.
Set environment variable ``GROQ_API_KEY``; otherwise groq library will raise a ``groq.GroqError``.
"""
if os.environ.get("GROQ_API_KEY") is None:
raise SetupError("Set environment variable ``GROQ_API_KEY``")

try:
import groq
except ImportError:
Expand All @@ -55,4 +52,4 @@ def recognize_groq(
)

recognizer = OpenAICompatibleRecognizer(groq.Groq())
return recognizer.recognize(audio_data, model)
return recognizer.recognize(audio_data, model, **kwargs)
6 changes: 4 additions & 2 deletions tests/recognizers/test_groq.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def test_transcribe_with_groq_whisper(respx_mock, monkeypatch):
monkeypatch.setenv("GROQ_API_KEY", "gsk_grok_api_key")

respx_mock.post(
"https://api.groq.com/openai/v1/audio/transcriptions"
"https://api.groq.com/openai/v1/audio/transcriptions",
headers__contains={"Authorization": "Bearer gsk_grok_api_key"},
data__contains={"model": "whisper-large-v3"},
).mock(
return_value=httpx.Response(
200,
Expand All @@ -26,7 +28,7 @@ def test_transcribe_with_groq_whisper(respx_mock, monkeypatch):
audio_data = MagicMock(spec=AudioData)
audio_data.get_wav_data.return_value = b"audio_data"

actual = groq.recognize_groq(
actual = groq.recognize(
MagicMock(spec=Recognizer), audio_data, model="whisper-large-v3"
)

Expand Down

0 comments on commit 11ee180

Please sign in to comment.