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
7.2 KiB155 linesraw
1using System.Globalization;
2using System.Reflection;
3using Whisper;
4
5namespace TranscribeCS
6{
7	sealed record class CommandLineArgs
8	{
9		public int n_threads = Environment.ProcessorCount;
10		public int offset_t_ms = 0;
11		public int offset_n = 0;
12		public int duration_ms = 0;
13		public int max_context = -1;
14		public int max_len = 0;
15
16		public float word_thold = 0.01f;
17
18		public bool speed_up = false;
19		public bool translate = false;
20		public bool diarize = false;
21		public bool output_txt = false;
22		public bool output_vtt = false;
23		public bool output_srt = false;
24		public bool print_special = false;
25		public bool print_progress = false;
26		public bool print_colors = true;
27		public bool no_timestamps = false;
28		public int[]? prompt = null;
29
30		public eLanguage language = eLanguage.English;
31		public string model = string.Empty;
32		public readonly List<string> fileNames = new List<string>();
33
34		const bool output_wts = false;
35		public void apply( ref Parameters p )
36		{
37			p.setFlag( eFullParamsFlags.PrintRealtime, false );
38			p.setFlag( eFullParamsFlags.PrintProgress, print_progress );
39			p.setFlag( eFullParamsFlags.PrintTimestamps, !no_timestamps );
40			p.setFlag( eFullParamsFlags.PrintSpecial, print_special );
41			p.setFlag( eFullParamsFlags.Translate, translate );
42			p.language = language;
43			p.cpuThreads = n_threads;
44			if( max_context >= 0 )
45				p.n_max_text_ctx = max_context;
46			p.offset_ms = offset_t_ms;
47			p.duration_ms = duration_ms;
48			p.setFlag( eFullParamsFlags.TokenTimestamps, output_wts || max_len > 0 );
49			p.thold_pt = word_thold;
50			p.max_len = output_wts && max_len == 0 ? 60 : max_len;
51			p.setFlag( eFullParamsFlags.SpeedupAudio, speed_up );
52		}
53
54		public eResultFlags resultFlags()
55		{
56			eResultFlags flags = eResultFlags.None;
57			bool wts = output_wts || max_len > 0;
58			if( !no_timestamps || wts )
59				flags |= eResultFlags.Timestamps;
60			if( wts || print_colors )
61				flags |= eResultFlags.Tokens;
62			return flags;
63		}
64
65		static eLanguage parseLanguage( string lang ) =>
66			Library.languageFromCode( lang ) ?? throw new ArgumentException( $"Unknown language code \"{lang}\"" );
67
68		public CommandLineArgs( string[] argv )
69		{
70			for( int i = 0; i < argv.Length; i++ )
71			{
72				string arg = argv[ i ];
73				if( arg[ 0 ] != '-' )
74				{
75					fileNames.Add( arg );
76					continue;
77				}
78				if( arg == "-h" || arg == "--help" )
79				{
80					printUsage();
81					throw new OperationCanceledException();
82				}
83				else if( arg == "-t" || arg == "--threads" ) n_threads = int.Parse( argv[ ++i ] );
84				else if( arg == "-ot" || arg == "--offset-t" ) offset_t_ms = int.Parse( argv[ ++i ] );
85				else if( arg == "-on" || arg == "--offset-n" ) offset_n = int.Parse( argv[ ++i ] );
86				else if( arg == "-d" || arg == "--duration" ) duration_ms = int.Parse( argv[ ++i ] );
87				else if( arg == "-mc" || arg == "--max-context" ) max_context = int.Parse( argv[ ++i ] );
88				else if( arg == "-ml" || arg == "--max-len" ) max_len = int.Parse( argv[ ++i ] );
89				else if( arg == "-wt" || arg == "--word-thold" ) word_thold = float.Parse( argv[ ++i ], CultureInfo.InvariantCulture );
90				else if( arg == "-su" || arg == "--speed-up" ) speed_up = true;
91				else if( arg == "-tr" || arg == "--translate" ) translate = true;
92				else if( arg == "-di" || arg == "--diarize" ) diarize = true;
93				else if( arg == "-otxt" || arg == "--output-txt" ) output_txt = true;
94				else if( arg == "-ovtt" || arg == "--output-vtt" ) output_vtt = true;
95				else if( arg == "-osrt" || arg == "--output-srt" ) output_srt = true;
96				else if( arg == "-ps" || arg == "--print-special" ) print_special = true;
97				else if( arg == "-nc" || arg == "--no-colors" ) print_colors = false;
98				else if( arg == "-pp" || arg == "--print-progress" ) print_progress = true;
99				else if( arg == "-nt" || arg == "--no-timestamps" ) no_timestamps = true;
100				else if( arg == "-l" || arg == "--language" ) language = parseLanguage( argv[ ++i ] );
101				else if( arg == "--prompt" ) prompt = parsePrompt( argv[ ++i ] );
102				else if( arg == "-m" || arg == "--model" ) model = argv[ ++i ];
103				else if( arg == "-f" || arg == "--file" ) fileNames.Add( argv[ ++i ] );
104				else
105					throw new ArgumentException( $"Unknown argument: \"{arg}\"" );
106			}
107			if( string.IsNullOrWhiteSpace( model ) )
108				throw new ArgumentException( "The model file is not provided in the arguments" );
109			if( !File.Exists( model ) )
110				throw new FileNotFoundException( "Model not found", model );
111			if( fileNames.Count <= 0 )
112				throw new ArgumentException( "Please supply at least 1 input audio file to process" );
113		}
114
115		static string cstr( bool b ) => b.ToString();
116
117		static int[]? parsePrompt( string str )
118		{
119			if( string.IsNullOrWhiteSpace( str ) )
120				return null;
121			// TODO: expose whisper_tokenize function, as a method of iModel COM interface
122			throw new NotImplementedException();
123		}
124
125		void printUsage()
126		{
127			Console.WriteLine();
128
129			Console.WriteLine( "usage: {0} [options] file0.mp3 file1.wma ...", Path.GetFileName( Assembly.GetExecutingAssembly().Location ) );
130			Console.WriteLine();
131			Console.WriteLine( "options:" );
132			Console.WriteLine( "  -h,       --help          [default] show this help message and exit" );
133			Console.WriteLine( "  -t N,     --threads N     [{0,-7:D}] number of threads to use during computation", n_threads );
134			Console.WriteLine( "  -ot N,    --offset-t N    [{0,-7:D}] time offset in milliseconds", offset_t_ms );
135			Console.WriteLine( "  -on N,    --offset-n N    [{0,-7:D}] segment index offset", offset_n );
136			Console.WriteLine( "  -d  N,    --duration N    [{0,-7:D}] duration of audio to process in milliseconds", duration_ms );
137			Console.WriteLine( "  -mc N,    --max-context N [{0,-7:D}] maximum number of text context tokens to store", max_context );
138			Console.WriteLine( "  -ml N,    --max-len N     [{0,-7:D}] maximum segment length in characters", max_len );
139			Console.WriteLine( "  -wt N,    --word-thold N  [{0,-7:F2}] word timestamp probability threshold", word_thold );
140			Console.WriteLine( "  -su,      --speed-up      [{0,-7}] speed up audio by x2 (reduced accuracy)", cstr( speed_up ) );
141			Console.WriteLine( "  -tr,      --translate     [{0,-7}] translate from source language to english", cstr( translate ) );
142			Console.WriteLine( "  -di,      --diarize       [{0,-7}] stereo audio diarization", cstr( diarize ) );
143			Console.WriteLine( "  -otxt,    --output-txt    [{0,-7}] output result in a text file", cstr( output_txt ) );
144			Console.WriteLine( "  -ovtt,    --output-vtt    [{0,-7}] output result in a vtt file", cstr( output_vtt ) );
145			Console.WriteLine( "  -osrt,    --output-srt    [{0,-7}] output result in a srt file", cstr( output_srt ) );
146			Console.WriteLine( "  -ps,      --print-special [{0,-7}] print special tokens", cstr( print_special ) );
147			Console.WriteLine( "  -nc,      --no-colors     [{0,-7}] do not print colors", cstr( !print_colors ) );
148			Console.WriteLine( "  -nt,      --no-timestamps [{0,-7}] do not print timestamps", cstr( no_timestamps ) );
149			Console.WriteLine( "  -l LANG,  --language LANG [{0,-7}] spoken language", language.getCode() );
150			Console.WriteLine( "            --prompt PROMPT [       ] initial prompt" );
151			Console.WriteLine( "  -m FNAME, --model FNAME   [{0,-7}] model path", model );
152			Console.WriteLine( "  -f FNAME, --file FNAME    [{0,-7}] path of the input audio file", "" );
153		}
154	}
155}