yum-archive/TaSTT-Whisper

High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model

git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper

KonstantinSource codes8c4603c

master
5.2 KiB101 linesraw
1#include "params.h"
2#include <algorithm>
3#include <thread>
4#include "miscUtils.h"
5
6whisper_params::whisper_params()
7{
8#ifdef _DEBUG
9	n_threads = 2;
10#else
11	n_threads = std::min( 4u, std::thread::hardware_concurrency() );
12#endif	
13}
14
15namespace
16{
17	const char* cstr( bool b )
18	{
19		return b ? "true" : "false";
20	}
21}
22
23void whisper_print_usage( int argc, wchar_t** argv, const whisper_params& params )
24{
25	fprintf( stderr, "\n" );
26	fprintf( stderr, "usage: %S [options] file0.wav file1.wav ...\n", argv[ 0 ] );
27	fprintf( stderr, "\n" );
28	fprintf( stderr, "options:\n" );
29	fprintf( stderr, "  -h,       --help          [default] show this help message and exit\n" );
30	fprintf( stderr, "  -t N,     --threads N     [%-7d] number of threads to use during computation\n", params.n_threads );
31	fprintf( stderr, "  -p N,     --processors N  [%-7d] number of processors to use during computation\n", params.n_processors );
32	fprintf( stderr, "  -ot N,    --offset-t N    [%-7d] time offset in milliseconds\n", params.offset_t_ms );
33	fprintf( stderr, "  -on N,    --offset-n N    [%-7d] segment index offset\n", params.offset_n );
34	fprintf( stderr, "  -d  N,    --duration N    [%-7d] duration of audio to process in milliseconds\n", params.duration_ms );
35	fprintf( stderr, "  -mc N,    --max-context N [%-7d] maximum number of text context tokens to store\n", params.max_context );
36	fprintf( stderr, "  -ml N,    --max-len N     [%-7d] maximum segment length in characters\n", params.max_len );
37	fprintf( stderr, "  -wt N,    --word-thold N  [%-7.2f] word timestamp probability threshold\n", params.word_thold );
38	fprintf( stderr, "  -su,      --speed-up      [%-7s] speed up audio by x2 (reduced accuracy)\n", cstr( params.speed_up ) );
39	fprintf( stderr, "  -tr,      --translate     [%-7s] translate from source language to english\n", cstr( params.translate ) );
40	fprintf( stderr, "  -di,      --diarize       [%-7s] stereo audio diarization\n", cstr( params.diarize ) );
41	fprintf( stderr, "  -otxt,    --output-txt    [%-7s] output result in a text file\n", cstr( params.output_txt ) );
42	fprintf( stderr, "  -ovtt,    --output-vtt    [%-7s] output result in a vtt file\n", cstr( params.output_vtt ) );
43	fprintf( stderr, "  -osrt,    --output-srt    [%-7s] output result in a srt file\n", cstr( params.output_srt ) );
44	fprintf( stderr, "  -owts,    --output-words  [%-7s] output script for generating karaoke video\n", cstr( params.output_wts ) );
45	fprintf( stderr, "  -ps,      --print-special [%-7s] print special tokens\n", cstr( params.print_special ) );
46	fprintf( stderr, "  -nc,      --no-colors     [%-7s] do not print colors\n", cstr( !params.print_colors ) );
47	fprintf( stderr, "  -nt,      --no-timestamps [%-7s] do not print timestamps\n", cstr( params.no_timestamps ) );
48	fprintf( stderr, "  -l LANG,  --language LANG [%-7s] spoken language\n", params.language.c_str() );
49	fprintf( stderr, "  -m FNAME, --model FNAME   [%-7S] model path\n", params.model.c_str() );
50	fprintf( stderr, "  -f FNAME, --file FNAME    [%-7s] path of the input audio file\n", "" );
51	fprintf( stderr, "\n" );
52}
53
54bool whisper_params::parse( int argc, wchar_t* argv[] )
55{
56	for( int i = 1; i < argc; i++ )
57	{
58		std::wstring arg = argv[ i ];
59
60		if( arg[ 0 ] != '-' )
61		{
62			fname_inp.push_back( arg );
63			continue;
64		}
65
66		if( arg == L"-h" || arg == L"--help" )
67		{
68			whisper_print_usage( argc, argv, *this );
69			return false;
70		}
71
72		else if( arg == L"-t" || arg == L"--threads" ) { n_threads = std::stoul( argv[ ++i ] ); }
73		else if( arg == L"-p" || arg == L"--processors" ) { n_processors = std::stoul( argv[ ++i ] ); }
74		else if( arg == L"-ot" || arg == L"--offset-t" ) { offset_t_ms = std::stoul( argv[ ++i ] ); }
75		else if( arg == L"-on" || arg == L"--offset-n" ) { offset_n = std::stoul( argv[ ++i ] ); }
76		else if( arg == L"-d" || arg == L"--duration" ) { duration_ms = std::stoul( argv[ ++i ] ); }
77		else if( arg == L"-mc" || arg == L"--max-context" ) { max_context = std::stoul( argv[ ++i ] ); }
78		else if( arg == L"-ml" || arg == L"--max-len" ) { max_len = std::stoul( argv[ ++i ] ); }
79		else if( arg == L"-wt" || arg == L"--word-thold" ) { word_thold = std::stof( argv[ ++i ] ); }
80		else if( arg == L"-su" || arg == L"--speed-up" ) { speed_up = true; }
81		else if( arg == L"-tr" || arg == L"--translate" ) { translate = true; }
82		else if( arg == L"-di" || arg == L"--diarize" ) { diarize = true; }
83		else if( arg == L"-otxt" || arg == L"--output-txt" ) { output_txt = true; }
84		else if( arg == L"-ovtt" || arg == L"--output-vtt" ) { output_vtt = true; }
85		else if( arg == L"-osrt" || arg == L"--output-srt" ) { output_srt = true; }
86		else if( arg == L"-owts" || arg == L"--output-words" ) { output_wts = true; }
87		else if( arg == L"-ps" || arg == L"--print-special" ) { print_special = true; }
88		else if( arg == L"-nc" || arg == L"--no-colors" ) { print_colors = false; }
89		else if( arg == L"-nt" || arg == L"--no-timestamps" ) { no_timestamps = true; }
90		else if( arg == L"-l" || arg == L"--language" ) { language = utf8( argv[ ++i ] ); }
91		else if( arg == L"-m" || arg == L"--model" ) { model = argv[ ++i ]; }
92		else if( arg == L"-f" || arg == L"--file" ) { fname_inp.push_back( argv[ ++i ] ); }
93		else
94		{
95			fprintf( stderr, "error: unknown argument: %S\n", arg.c_str() );
96			whisper_print_usage( argc, argv, *this );
97			return false;
98		}
99	}
100	return true;
101}