8000 feat: config.toml for ASR and TTS by rachwalk · Pull Request #546 · RobotecAI/rai · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: config.toml for ASR and TTS #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ repos:
hooks:
- id: ruff
args: [--extend-select, "I,RUF022", --fix, --ignore, E731]
exclude: src/rai_core/rai/frontend/configurator.py
- id: ruff-format
14 changes: 9 additions & 5 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ region_name = "us-east-1"
simple_model = "gpt-4o-mini"
complex_model = "gpt-4o"
embeddings_model = "text-embedding-ada-002"
base_url = "https://api.openai.com/v1/" # for openai compatible apis
base_url = "https://api.openai.com/v1/"

[ollama]
simple_model = "llama3.2"
Expand All @@ -34,14 +34,18 @@ host = "https://api.smith.langchain.com"

[asr]
recording_device_name = "default"
vendor = "whisper"
transcription_model = "LocalWhisper"
language = "en"
vad_model = "SileroVAD"
silence_grace_period = 0.3
use_wake_word = false
vad_threshold = 0.3
use_wake_word = false
wake_word_model = ""
wake_word_threshold = 0.5
wake_word_model_name = ""
transcription_model_name = "tiny"

[tts]
vendor = "elevenlabs"
keep_speaker_busy = false
vendor = "ElevenLabs"
voice = ""
speaker_device_name = "default"
23 changes: 23 additions & 0 deletions src/rai_asr/rai_asr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,26 @@
"""RAI ASR package."""

__version__ = "0.1.0"


from rai_asr.agents.asr_agent import SpeechRecognitionAgent
from rai_asr.agents.initialization import (
TRANSCRIBE_MODELS,
ASRAgentConfig,
MicrophoneConfig,
TranscribeConfig,
VADConfig,
WWConfig,
load_config,
)

__all__ = [
"TRANSCRIBE_MODELS",
"ASRAgentConfig",
"MicrophoneConfig",
"SpeechRecognitionAgent",
"TranscribeConfig",
"VADConfig",
"WWConfig",
"load_config",
]
16 changes: 16 additions & 0 deletions src/rai_asr/rai_asr/agents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,23 @@
# limitations under the License.

from rai_asr.agents.asr_agent import SpeechRecognitionAgent
from rai_asr.agents.initialization import (
TRANSCRIBE_MODELS,
ASRAgentConfig,
MicrophoneConfig,
TranscribeConfig,
VADConfig,
WWConfig,
load_config,
)

__all__ = [
"TRANSCRIBE_MODELS",
"ASRAgentConfig",
"MicrophoneConfig",
"SpeechRecognitionAgent",
"TranscribeConfig",
"VADConfig",
"WWConfig",
"load_config",
]
56 changes: 56 additions & 0 deletions src/rai_asr/rai_asr/agents/asr_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
SoundDeviceConnector,
SoundDeviceMessage,
)
from typing_extensions import Self

from rai_asr.models import BaseTranscriptionModel, BaseVoiceDetectionModel

from .initialization import load_config


class ThreadData(TypedDict):
thread: Thread
Expand Down Expand Up @@ -109,6 +112,59 @@ def __init__(
self.transcription_buffers: dict[str, list[NDArray]] = {}
self.is_playing = True

@classmethod
def from_config(cls, cfg_path: Optional[str] = None) -> Self:
cfg = load_config(cfg_path)
microphone_configuration = SoundDeviceConfig(
stream=True,
channels=1,
device_name=cfg.microphone.device_name,
block_size=1280,
consumer_sampling_rate=16000,
dtype="int16",
device_number=None,
is_input=True,
is_output=False,
)
match cfg.transcribe.model_type:
case "LocalWhisper (Free)":
from rai_asr.models import LocalWhisper

model = LocalWhisper(
cfg.transcribe.model_name, 16000, language=cfg.transcribe.language
)
case "FasterWhisper (Free)":
from rai_asr.models import FasterWhisper

model = FasterWhisper(
cfg.transcribe.model_name, 16000, language=cfg.transcribe.language
)
case "OpenAI (Cloud)":
from rai_asr.models import OpenAIWhisper

model = OpenAIWhisper(
cfg.transcribe.model_name, 16000, language=cfg.transcribe.language
)
case _:
raise ValueError(f"Unknown model name f{cfg.transcribe.model_name}")

match cfg.voice_activity_detection.model_name:
case "SileroVAD":
from rai_asr.models import SileroVAD

vad = SileroVAD(16000, cfg.voice_activity_detection.threshold)

agent = cls(microphone_configuration, "rai_auto_asr_agent", model, vad)
if cfg.wakeword.is_used:
match cfg.wakeword.model_type:
case "OpenWakeWord":
from rai_asr.models import OpenWakeWord

agent.add_detection_model(
OpenWakeWord(cfg.wakeword.model_name, cfg.wakeword.threshold)
)
return agent

def __call__(self):
self.run()

Expand Down
92 changes: 92 additions & 0 deletions src/rai_asr/rai_asr/agents/initialization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright (C) 2025 Robotec.AI
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 dataclasses import dataclass
from typing import Literal, Optional

import tomli


@dataclass
class VADConfig:
model_name: Literal["SileroVAD"] = "SileroVAD"
threshold: float = 0.5
silence_grace_period: float = 0.3


@dataclass
class WWConfig:
model_name: str = "hey jarvis"
model_type: Literal["OpenWakeWord"] = "OpenWakeWord"
threshold: float = 0.01
is_used: bool = False


TRANSCRIBE_MODELS = ["LocalWhisper", "FasterWhisper", "OpenAI"]


@dataclass
class TranscribeConfig:
model_type: str = TRANSCRIBE_MODELS[0]
model_name: str = "tiny"
language: str = "en"

def __post_init__(self):
if self.model_type not in TRANSCRIBE_MODELS:
raise ValueError(
f"unknown model_type: {self.model_type}. Must be one of {TRANSCRIBE_MODELS}"
)


@dataclass
class MicrophoneConfig:
device_name: str


@dataclass
class ASRAgentConfig:
voice_activity_detection: VADConfig
wakeword: WWConfig
transcribe: TranscribeConfig
microphone: MicrophoneConfig


def load_config(config_path: Optional[str] = None) -> ASRAgentConfig:
if config_path is None:
with open("config.toml", "rb") as f:
config_dict = tomli.load(f)
else:
with open(config_path, "rb") as f:
config_dict = tomli.load(f)
return ASRAgentConfig(
voice_activity_detection=VADConfig(
model_name=config_dict["asr"]["vad_model"],
threshold=config_dict["asr"]["vad_threshold"],
silence_grace_period=config_dict["asr"]["silence_grace_period"],
), D1A4
wakeword=WWConfig(
model_type=config_dict["asr"]["wake_word_model"],
model_name=config_dict["asr"]["wake_word_model_name"],
threshold=config_dict["asr"]["wake_word_threshold"],
is_used=config_dict["asr"]["use_wake_word"],
),
transcribe=TranscribeConfig(
model_type=config_dict["asr"]["transcription_model"],
model_name=config_dict["asr"]["transcription_model_name"],
language=config_dict["asr"]["language"],
),
microphone=MicrophoneConfig(
device_name=config_dict["asr"]["recording_device_name"],
),
)
3 changes: 2 additions & 1 deletion src/rai_asr/rai_asr/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# limitations under the License.

from rai_asr.models.base import BaseTranscriptionModel, BaseVoiceDetectionModel
from rai_asr.models.local_whisper import LocalWhisper
from rai_asr.models.local_whisper import FasterWhisper, LocalWhisper
from rai_asr.models.open_ai_whisper import OpenAIWhisper
from rai_asr.models.open_wake_word import OpenWakeWord
from rai_asr.models.silero_vad import SileroVAD

__all__ = [
"BaseTranscriptionModel",
"BaseVoiceDetectionModel",
"FasterWhisper",
"LocalWhisper",
"OpenAIWhisper",
"OpenWakeWord",
Expand Down
Loading
0