Merge branch 'main' into ayo-logging-fixes

This commit is contained in:
Alex Young 2024-04-17 20:47:55 +01:00
commit 2ba48bcbf4
4 changed files with 36 additions and 43 deletions

View file

@ -183,7 +183,7 @@ online.init() # refresh if you're going to re-use the object for the next audio
### Server -- real-time from mic ### Server -- real-time from mic
`whisper_online_server.py` has the same model options as `whisper_online.py`, plus `--host` and `--port` of the TCP connection. See help message (`-h` option). `whisper_online_server.py` has the same model options as `whisper_online.py`, plus `--host` and `--port` of the TCP connection and the `--warmup-file`. See the help message (`-h` option).
Client example: Client example:

View file

@ -2,8 +2,6 @@
"""Functions for sending and receiving individual lines of text over a socket. """Functions for sending and receiving individual lines of text over a socket.
Used by marian-server-server.py to communicate with the Marian worker.
A line is transmitted using one or more fixed-size packets of UTF-8 bytes A line is transmitted using one or more fixed-size packets of UTF-8 bytes
containing: containing:
@ -11,6 +9,7 @@ containing:
- Zero or more \0 bytes as required to pad the packet to PACKET_SIZE - Zero or more \0 bytes as required to pad the packet to PACKET_SIZE
Originally from the UEDIN team of the ELITR project.
""" """
PACKET_SIZE = 65536 PACKET_SIZE = 65536

View file

@ -559,7 +559,7 @@ def add_shared_args(parser):
def asr_factory(args, logfile=sys.stderr): def asr_factory(args, logfile=sys.stderr):
""" """
Creates and configures an ASR instance based on the specified backend and arguments. Creates and configures an ASR and ASR Online instance based on the specified backend and arguments.
""" """
backend = args.backend backend = args.backend
if backend == "openai-api": if backend == "openai-api":
@ -584,8 +584,23 @@ def asr_factory(args, logfile=sys.stderr):
logging.info("Setting VAD filter") logging.info("Setting VAD filter")
asr.use_vad() asr.use_vad()
return asr language = args.lan
if args.task == "translate":
asr.set_translate_task()
tgt_language = "en" # Whisper translates into English
else:
tgt_language = language # Whisper transcribes in this language
# Create the tokenizer
if args.buffer_trimming == "sentence":
tokenizer = create_tokenizer(tgt_language)
else:
tokenizer = None
# Create the OnlineASRProcessor
online = OnlineASRProcessor(asr,tokenizer,logfile=logfile,buffer_trimming=(args.buffer_trimming, args.buffer_trimming_sec))
return asr, online
## main: ## main:
if __name__ == "__main__": if __name__ == "__main__":
@ -613,27 +628,13 @@ if __name__ == "__main__":
duration = len(load_audio(audio_path))/SAMPLING_RATE duration = len(load_audio(audio_path))/SAMPLING_RATE
logging.info("Audio duration is: %2.2f seconds" % duration) logging.info("Audio duration is: %2.2f seconds" % duration)
asr = asr_factory(args, logfile=logfile) asr, online = asr_factory(args, logfile=logfile)
language = args.lan
if args.task == "translate":
asr.set_translate_task()
tgt_language = "en" # Whisper translates into English
else:
tgt_language = language # Whisper transcribes in this language
min_chunk = args.min_chunk_size min_chunk = args.min_chunk_size
if args.buffer_trimming == "sentence":
tokenizer = create_tokenizer(tgt_language)
else:
tokenizer = None
online = OnlineASRProcessor(asr,tokenizer,logfile=logfile,buffer_trimming=(args.buffer_trimming, args.buffer_trimming_sec))
# load the audio into the LRU cache before we start the timer # load the audio into the LRU cache before we start the timer
a = load_audio_chunk(audio_path,0,1) a = load_audio_chunk(audio_path,0,1)
# warm up the ASR, because the very first transcribe takes much more time than the other # warm up the ASR because the very first transcribe takes much more time than the other
asr.transcribe(a) asr.transcribe(a)
beg = args.start_at beg = args.start_at

View file

@ -12,6 +12,8 @@ parser = argparse.ArgumentParser()
# server options # server options
parser.add_argument("--host", type=str, default='localhost') parser.add_argument("--host", type=str, default='localhost')
parser.add_argument("--port", type=int, default=43007) parser.add_argument("--port", type=int, default=43007)
parser.add_argument("--warmup-file", type=str, dest="warmup_file",
help="The path to a speech audio wav file to warm up Whisper so that the very first chunk processing is fast. It can be e.g. https://github.com/ggerganov/whisper.cpp/raw/master/samples/jfk.wav .")
parser.add_argument("-l", "--log-level", dest="log_level", parser.add_argument("-l", "--log-level", dest="log_level",
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
@ -33,37 +35,28 @@ SAMPLING_RATE = 16000
size = args.model size = args.model
language = args.lan language = args.lan
asr, online = asr_factory(args)
asr = asr_factory(args)
if args.task == "translate":
asr.set_translate_task()
tgt_language = "en"
else:
tgt_language = language
min_chunk = args.min_chunk_size min_chunk = args.min_chunk_size
if args.buffer_trimming == "sentence": if args.buffer_trimming == "sentence":
tokenizer = create_tokenizer(tgt_language) tokenizer = create_tokenizer(tgt_language)
else: else:
tokenizer = None tokenizer = None
online = OnlineASRProcessor(asr,tokenizer,buffer_trimming=(args.buffer_trimming, args.buffer_trimming_sec)) online = OnlineASRProcessor(asr,tokenizer,buffer_trimming=(args.buffer_trimming, args.buffer_trimming_sec))
# warm up the ASR because the very first transcribe takes more time than the others.
# Test results in https://github.com/ufal/whisper_streaming/pull/81
demo_audio_path = "cs-maji-2.16k.wav" msg = "Whisper is not warmed up. The first chunk processing may take longer."
if os.path.exists(demo_audio_path): if args.warmup_file:
# load the audio into the LRU cache before we start the timer if os.path.isfile(args.warmup_file):
logging.debug(f"Warming up on {demo_audio_path}") a = load_audio_chunk(args.warmup_file,0,1)
a = load_audio_chunk(demo_audio_path,0,1) asr.transcribe(a)
print("INFO: Whisper is warmed up.",file=sys.stderr)
# TODO: it should be tested whether it's meaningful else:
# warm up the ASR, because the very first transcribe takes much more time than the other print("WARNING: The warm up file is not available. "+msg,file=sys.stderr)
asr.transcribe(a)
logging.debug("Whisper is warmed up")
else: else:
logging.debug("Whisper is not warmed up") print("WARNING: " + msg, file=sys.stderr)
######### Server objects ######### Server objects