-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from BESSER-PEARL/speech2text
Speech2text
- Loading branch information
Showing
16 changed files
with
211 additions
and
7 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
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
Empty file.
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,54 @@ | ||
import io | ||
from typing import TYPE_CHECKING | ||
|
||
import librosa | ||
from transformers import AutoProcessor, TFAutoModelForSpeechSeq2Seq, logging | ||
|
||
from besser.bot import nlp | ||
from besser.bot.nlp.speech2text.speech2text import Speech2Text | ||
|
||
if TYPE_CHECKING: | ||
from besser.bot.nlp.nlp_engine import NLPEngine | ||
|
||
logging.set_verbosity_error() | ||
|
||
|
||
class HFSpeech2Text(Speech2Text): | ||
"""A Hugging Face Speech2Text. | ||
It loads a Speech2Text Hugging Face model to perform the Speech2Text task. | ||
.. warning:: | ||
Only tested with ``openai/whisper-*`` models | ||
Args: | ||
nlp_engine (NLPEngine): the NLPEngine that handles the NLP processes of the bot | ||
Attributes: | ||
_model_name (str): the Hugging Face model name | ||
_processor (): the model text processor | ||
_model (): the Speech2Text model | ||
_sampling_rate (int): the sampling rate of audio data, it must coincide with the sampling rate used to train the | ||
model | ||
_forced_decoder_ids (list): the decoder ids | ||
""" | ||
|
||
def __init__(self, nlp_engine: 'NLPEngine'): | ||
super().__init__(nlp_engine) | ||
self._model_name: str = self._nlp_engine.get_property(nlp.NLP_STT_HF_MODEL) | ||
self._processor = AutoProcessor.from_pretrained(self._model_name) | ||
self._model = TFAutoModelForSpeechSeq2Seq.from_pretrained(self._model_name) | ||
self._sampling_rate: int = 16000 | ||
# self.model.config.forced_decoder_ids = None | ||
self._forced_decoder_ids = self._processor.get_decoder_prompt_ids( | ||
language=self._nlp_engine.get_property(nlp.NLP_LANGUAGE), task="transcribe" | ||
) | ||
|
||
def speech2text(self, speech: bytes): | ||
wav_stream = io.BytesIO(speech) | ||
audio, sampling_rate = librosa.load(wav_stream, sr=self._sampling_rate) | ||
input_features = self._processor(audio, sampling_rate=self._sampling_rate, return_tensors="tf").input_features | ||
predicted_ids = self._model.generate(input_features, forced_decoder_ids=self._forced_decoder_ids) | ||
transcriptions = self._processor.batch_decode(predicted_ids, skip_special_tokens=True) | ||
return transcriptions[0] |
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,37 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from besser.bot.nlp.nlp_engine import NLPEngine | ||
|
||
|
||
class Speech2Text(ABC): | ||
"""The Speech2Text abstract class. | ||
The Speech2Text component, also known as STT, Automatic Speech Recognition or ASR, is in charge of converting spoken | ||
language or audio speech signals into written text. This task is called transcribing. | ||
We can use it in a chatbot to allow the users to send voice messages and transcribe them to written text so the bot | ||
can process them like regular text messages. | ||
Args: | ||
nlp_engine (NLPEngine): the NLPEngine that handles the NLP processes of the bot | ||
Attributes: | ||
_nlp_engine (): The NLPEngine that handles the NLP processes of the bot | ||
""" | ||
|
||
def __init__(self, nlp_engine: 'NLPEngine'): | ||
self._nlp_engine: 'NLPEngine' = nlp_engine | ||
|
||
@abstractmethod | ||
def speech2text(self, speech: bytes) -> str: | ||
"""Transcribe a voice audio into its corresponding text representation. | ||
Args: | ||
speech (bytes): the recorded voice that wants to be transcribed | ||
Returns: | ||
str: the speech transcription | ||
""" | ||
pass |
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
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
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
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 |
---|---|---|
|
@@ -18,3 +18,5 @@ nlp | |
nlp/number | ||
nlp/pipelines | ||
nlp/text_preprocessing | ||
nlp/hf_speech2text | ||
nlp/speech2text |
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,8 @@ | ||
hf_speech2text | ||
============== | ||
|
||
.. automodule:: besser.bot.nlp.speech2text.hf_speech2text | ||
:members: | ||
:private-members: | ||
:undoc-members: | ||
:show-inheritance: |
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,8 @@ | ||
speech2text | ||
=========== | ||
|
||
.. automodule:: besser.bot.nlp.speech2text.speech2text | ||
:members: | ||
:private-members: | ||
:undoc-members: | ||
:show-inheritance: |
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