-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement base class for asr models, add local whisper
- Loading branch information
1 parent
ddfd016
commit a157a01
Showing
4 changed files
with
177 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright (C) 2024 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 launch import LaunchDescription | ||
from launch.actions import DeclareLaunchArgument | ||
from launch.substitutions import LaunchConfiguration | ||
from launch_ros.actions import Node | ||
|
||
|
||
def generate_launch_description(): | ||
return LaunchDescription( | ||
[ | ||
DeclareLaunchArgument( | ||
"recording_device", | ||
default_value="0", | ||
description="Microphone device number. See available by running python -c 'import sounddevice as sd; print(sd.query_devices())'", | ||
), | ||
DeclareLaunchArgument( | ||
"language", | ||
default_value="en", | ||
description="Language code for the ASR model", | ||
), | ||
DeclareLaunchArgument( | ||
"model_name", | ||
default_value="base", | ||
description="Model name for the ASR model", | ||
), | ||
DeclareLaunchArgument( | ||
"model_vendor", | ||
default_value="whisper", | ||
description="Model vendor of the ASR", | ||
), | ||
DeclareLaunchArgument( | ||
"silence_grace_period", | ||
default_value="2.0", | ||
description="Grace period in seconds after silence to stop recording", | ||
), | ||
DeclareLaunchArgument( | ||
"sample_rate", | ||
default_value="0", | ||
description="Sample rate for audio capture (0 for auto-detect)", | ||
), | ||
Node( | ||
package="rai_asr", | ||
executable="asr_node", | ||
name="rai_asr", | ||
output="screen", | ||
emulate_tty=True, | ||
parameters=[ | ||
{ | ||
"language": LaunchConfiguration("language"), | ||
"model": LaunchConfiguration("model"), | ||
"silence_grace_period": LaunchConfiguration( | ||
"silence_grace_period" | ||
), | ||
"sample_rate": LaunchConfiguration("sample_rate"), | ||
} | ||
], | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import io | ||
import os | ||
from abc import abstractmethod | ||
from functools import partial | ||
|
||
import numpy as np | ||
import whisper | ||
from numpy.typing import NDArray | ||
from openai import OpenAI | ||
from scipy.io import wavfile | ||
from whisper.transcribe import transcribe | ||
|
||
|
||
class ASRModel: | ||
def __init__(self, model_name: str, sample_rate: int, language: str = "en"): | ||
self.model_name = model_name | ||
self.sample_rate = sample_rate | ||
self.language = language | ||
|
||
@abstractmethod | ||
def transcribe(self, data: NDArray[np.int16]) -> str: | ||
pass | ||
|
||
def __call__(self, data: NDArray[np.int16]) -> str: | ||
return self.transcribe(data) | ||
|
||
|
||
class OpenAIWhisper(ASRModel): | ||
def __init__(self, model_name: str, sample_rate: int, language: str = "en"): | ||
super().__init__(model_name, sample_rate, language) | ||
api_key = os.getenv("OPENAI_API_KEY") | ||
if api_key is None: | ||
raise ValueError("OPENAI_API_KEY environment variable is not set.") | ||
self.api_key = api_key | ||
self.openai_client = OpenAI() | ||
self.model = partial( | ||
self.openai_client.audio.transcriptions.create, | ||
model=self.model_name, | ||
) | ||
|
||
def transcribe(self, data: NDArray[np.int16]) -> str: | ||
with io.BytesIO() as temp_wav_buffer: | ||
wavfile.write(temp_wav_buffer, self.sample_rate, data) | ||
temp_wav_buffer.seek(0) | ||
temp_wav_buffer.name = "temp.wav" | ||
response = self.model(file=temp_wav_buffer, language=self.language) | ||
transcription = response.text | ||
return transcription | ||
|
||
|
||
class LocalWhisper(ASRModel): | ||
def __init__(self, model_name: str, sample_rate: int, language: str = "en"): | ||
super().__init__(model_name, sample_rate, language) | ||
self.whisper = whisper.load_model(self.model_name) | ||
|
||
def transcribe(self, data: NDArray[np.int16]) -> str: | ||
result = transcribe(self.whisper, data.astype(np.float32) / 32768.0) | ||
transcription = result["text"] | ||
return transcription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters