Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
jayeshp19 committed Nov 27, 2024
2 parents fe3e1fd + 415b07a commit b5c5cb8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-kangaroos-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-plugins-deepgram": patch
---

Added support for custom deepgram base url
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from .models import DeepgramLanguages, DeepgramModels

BASE_URL = "https://api.deepgram.com/v1/listen"
BASE_URL_WS = "wss://api.deepgram.com/v1/listen"


# This is the magic number during testing that we use to determine if a frame is loud enough
Expand Down Expand Up @@ -126,6 +125,7 @@ def __init__(
profanity_filter: bool = False,
api_key: str | None = None,
http_session: aiohttp.ClientSession | None = None,
base_url: str = BASE_URL,
energy_filter: AudioEnergyFilter | bool = False,
) -> None:
"""
Expand All @@ -140,6 +140,7 @@ def __init__(
streaming=True, interim_results=interim_results
)
)
self._base_url = base_url

api_key = api_key or os.environ.get("DEEPGRAM_API_KEY")
if api_key is None:
Expand Down Expand Up @@ -210,7 +211,7 @@ async def _recognize_impl(

try:
async with self._ensure_session().post(
url=_to_deepgram_url(recognize_config),
url=_to_deepgram_url(recognize_config, self._base_url, websocket=False),
data=rtc.combine_audio_frames(buffer).to_wav_bytes(),
headers={
"Authorization": f"Token {self._api_key}",
Expand Down Expand Up @@ -253,6 +254,7 @@ def stream(
opts=config,
api_key=self._api_key,
http_session=self._ensure_session(),
base_url=self._base_url,
)
self._active_streams.add(stream)
return stream
Expand Down Expand Up @@ -290,6 +292,7 @@ def __init__(
conn_options: APIConnectOptions,
api_key: str,
http_session: aiohttp.ClientSession,
base_url: str,
) -> None:
super().__init__(
stt=stt, conn_options=conn_options, sample_rate=opts.sample_rate
Expand All @@ -301,6 +304,7 @@ def __init__(
self._opts = opts
self._api_key = api_key
self._session = http_session
self._base_url = base_url
self._speaking = False
self._audio_duration_collector = PeriodicCollector(
callback=self._on_audio_duration_report,
Expand Down Expand Up @@ -695,7 +699,7 @@ def prerecorded_transcription_to_speech_event(
)


def _to_deepgram_url(opts: dict, *, websocket: bool = False) -> str:
def _to_deepgram_url(opts: dict, base_url: str, *, websocket: bool) -> str:
if opts.get("keywords"):
# convert keywords to a list of "keyword:intensifier"
opts["keywords"] = [
Expand All @@ -704,5 +708,11 @@ def _to_deepgram_url(opts: dict, *, websocket: bool = False) -> str:

# lowercase bools
opts = {k: str(v).lower() if isinstance(v, bool) else v for k, v in opts.items()}
base_url = BASE_URL_WS if websocket else BASE_URL

if websocket and base_url.startswith("http"):
base_url = base_url.replace("http", "ws", 1)

elif not websocket and base_url.startswith("ws"):
base_url = base_url.replace("ws", "http", 1)

return f"{base_url}?{urlencode(opts, doseq=True)}"

0 comments on commit b5c5cb8

Please sign in to comment.