Skip to content

Commit

Permalink
handle stt exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
theomonnom committed Oct 29, 2024
1 parent 1d745ce commit ce28f20
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 13 deletions.
31 changes: 24 additions & 7 deletions livekit-plugins/livekit-plugins-clova/livekit/plugins/clova/stt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,27 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import io
import json
import asyncio
import os
import time
import wave
from typing import Optional, Union

import aiohttp
from livekit.agents import stt, utils

from livekit.agents import (
APIConnectionError,

Check failure on line 28 in livekit-plugins/livekit-plugins-clova/livekit/plugins/clova/stt.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F401)

livekit-plugins/livekit-plugins-clova/livekit/plugins/clova/stt.py:28:5: F401 `livekit.agents.APIConnectionError` imported but unused
APIStatusError,
APITimeoutError,
stt,
utils,
)

from livekit.agents.stt import SpeechEventType, STTCapabilities
from livekit.agents.utils import AudioBuffer, merge_frames
from livekit.plugins.clova.constants import CLOVA_INPUT_SAMPLE_RATE
Expand Down Expand Up @@ -115,16 +127,21 @@ async def _recognize_impl(
)
logger.info(f"final event: {response_data}")
return self._transcription_to_speech_event(text=text)
except Exception as ex:
logger.error(f"{ex}")
return self._transcription_to_speech_event(
event_type=stt.SpeechEventType.FINAL_TRANSCRIPT, text=""
)

except asyncio.TimeoutError as e:
raise APITimeoutError() from e
except aiohttp.ClientResponseError as e:
raise APIStatusError(
message=e.message,
status_code=e.status,
request_id=None,
body=None,
) from e

def _transcription_to_speech_event(
self,
event_type: SpeechEventType = stt.SpeechEventType.INTERIM_TRANSCRIPT,
text: str = None,
text: str | None = None,
) -> stt.SpeechEvent:
return stt.SpeechEvent(
type=event_type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@
from typing import AsyncIterable, List, Union

from livekit import agents, rtc
from livekit.agents import stt, utils

from livekit.agents import (
APIConnectionError,
APIStatusError,
APITimeoutError,
stt,
utils,
)

from google.auth import default as gauth_default
from google.auth.exceptions import DefaultCredentialsError
Expand Down Expand Up @@ -165,12 +172,27 @@ async def _recognize_impl(
language_codes=config.languages,
)

raw = await self._ensure_client().recognize(
cloud_speech.RecognizeRequest(
recognizer=self._recognizer, config=config, content=frame.data.tobytes()
try:
raw = await self._ensure_client().recognize(
cloud_speech.RecognizeRequest(
recognizer=self._recognizer,
config=config,
content=frame.data.tobytes(),
)
)

return _recognize_response_to_speech_event(raw)
except DeadlineExceeded:

Check failure on line 185 in livekit-plugins/livekit-plugins-google/livekit/plugins/google/stt.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F821)

livekit-plugins/livekit-plugins-google/livekit/plugins/google/stt.py:185:16: F821 Undefined name `DeadlineExceeded`
raise APITimeoutError()
except GoogleAPICallError as e:

Check failure on line 187 in livekit-plugins/livekit-plugins-google/livekit/plugins/google/stt.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F821)

livekit-plugins/livekit-plugins-google/livekit/plugins/google/stt.py:187:16: F821 Undefined name `GoogleAPICallError`
raise APIStatusError(
e.message,
status_code=e.code or -1,
request_id=None,
body=None,
)
)
return _recognize_response_to_speech_event(raw)
except Exception as e:
raise APIConnectionError() from e

def stream(
self, *, language: SpeechLanguages | str | None = None
Expand Down

0 comments on commit ce28f20

Please sign in to comment.