Skip to content
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

feat/transcribe_script #5

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 6 additions & 3 deletions ovos_stt_plugin_fasterwhisper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def __init__(self, *args, **kwargs):
self.compute_type = self.config.get("compute_type", "int8")
self.use_cuda = self.config.get("use_cuda", False)
self.cpu_threads = self.config.get("cpu_threads", 4)

self.vad_filter = self.config.get("vad_filter", False)
if self.use_cuda:
device = "cuda"
else:
Expand All @@ -203,8 +203,11 @@ def audiodata2array(audio_data):

def execute(self, audio, language=None):
lang = language or self.lang
segments, _ = self.engine.transcribe(self.audiodata2array(audio), beam_size=self.beam_size,
condition_on_previous_text=False, language=lang.split("-")[0].lower())
segments, _ = self.engine.transcribe(self.audiodata2array(audio),
beam_size=self.beam_size,
vad_filter=self.vad_filter,
condition_on_previous_text=False,
language=lang.split("-")[0].lower())
# segments is an iterator, transcription only happens here
transcription = "".join(segment.text for segment in segments).strip()
return transcription
Expand Down
63 changes: 63 additions & 0 deletions ovos_stt_plugin_fasterwhisper/transcribe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import click
import os
from speech_recognition import Recognizer, AudioFile

from ovos_stt_plugin_fasterwhisper import FasterWhisperSTT


@click.command()
@click.option("--path")
@click.option("--lang", default="en-us")
@click.option("--model", default="base")
@click.option("--format", default="wav")
@click.option("--beam", default=5)
@click.option("--cuda", default=False)
@click.option("--compute", default="int8")
@click.option("--vad", default=False)
def transcribe(path: str, lang: str, model: str, format: str, beam: int, cuda: bool, compute: str, vad: bool):
config = {
"lang": lang,
"model": model,
"beam_size": beam,
"use_cuda": cuda,
"compute_type": compute,
"vad_filter": vad
}

b = FasterWhisperSTT(config=config)

if os.path.isfile(path):
try:
with AudioFile(path) as source:
try:
audio = Recognizer().record(source)
t = b.execute(audio, language="en")
print(t)
with open(path.replace(f'.{format}', '.txt'), "w") as f:
f.write(t)
except:
print("failed to transcribe file")
except:
print("failed to open file", f)
elif os.path.isdir(path):
for root, folder, files in os.walk(path):
for f in files:
if f.endswith(".wav") and not os.path.isfile(f"{root}/{f.replace(f'.{format}', '.txt')}"):
print(root, f)
try:
with AudioFile(f"{root}/{f}") as source:
try:
audio = Recognizer().record(source)
t = b.execute(audio, language="en")
print(t)
with open(f"{root}/{f.replace(f'.{format}', '.txt')}", "w") as f:
f.write(t)
except:
print("failed to transcribe file")
except:
print("failed to open file", f)
continue


if __name__ == "__main__":
transcribe()
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,9 @@ def required(requirements_file):
keywords='mycroft ovos plugin stt',
entry_points={'mycroft.plugin.stt': PLUGIN_ENTRY_POINT,
'mycroft.plugin.stt.config': CONFIG_ENTRY_POINT,
'neon.plugin.audio': LANG_PLUGIN_ENTRY_POINT}
'neon.plugin.audio': LANG_PLUGIN_ENTRY_POINT,
'console_scripts': [
'fw-transcribe=ovos_stt_plugin_fasterwhisper.transcribe:transcribe'
]
}
)