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

KonstantinUX bugfix, microphone C# example3cff5d2

master
6.7 KiB147 linesraw
1using System.Globalization;
2using System.Reflection;
3using Whisper;
4
5namespace MicrophoneCS
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 print_special = false;
23		public bool print_progress = false;
24		public bool print_colors = true;
25		public bool no_timestamps = false;
26		public int[]? prompt = null;
27		public int captureDeviceIndex = 0;
28
29		public eLanguage language = eLanguage.English;
30		public string model = string.Empty;
31
32		const bool output_wts = false;
33		public bool listDevices = false;
34
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 == "-h" || arg == "--help" )
74				{
75					printUsage();
76					throw new OperationCanceledException();
77				}
78				else if( arg == "-c" || arg == "--capture" ) captureDeviceIndex = int.Parse( argv[ ++i ] );
79				else if( arg == "-ld" || arg == "--list-devices" ) listDevices = true;
80				else if( arg == "-t" || arg == "--threads" ) n_threads = int.Parse( argv[ ++i ] );
81				else if( arg == "-ot" || arg == "--offset-t" ) offset_t_ms = int.Parse( argv[ ++i ] );
82				else if( arg == "-on" || arg == "--offset-n" ) offset_n = int.Parse( argv[ ++i ] );
83				else if( arg == "-d" || arg == "--duration" ) duration_ms = int.Parse( argv[ ++i ] );
84				else if( arg == "-mc" || arg == "--max-context" ) max_context = int.Parse( argv[ ++i ] );
85				else if( arg == "-ml" || arg == "--max-len" ) max_len = int.Parse( argv[ ++i ] );
86				else if( arg == "-wt" || arg == "--word-thold" ) word_thold = float.Parse( argv[ ++i ], CultureInfo.InvariantCulture );
87				else if( arg == "-su" || arg == "--speed-up" ) speed_up = true;
88				else if( arg == "-tr" || arg == "--translate" ) translate = true;
89				else if( arg == "-di" || arg == "--diarize" ) diarize = true;
90				else if( arg == "-otxt" || arg == "--output-txt" ) output_txt = true;
91				else if( arg == "-ps" || arg == "--print-special" ) print_special = true;
92				else if( arg == "-nc" || arg == "--no-colors" ) print_colors = false;
93				else if( arg == "-pp" || arg == "--print-progress" ) print_progress = true;
94				else if( arg == "-nt" || arg == "--no-timestamps" ) no_timestamps = true;
95				else if( arg == "-l" || arg == "--language" ) language = parseLanguage( argv[ ++i ] );
96				else if( arg == "--prompt" ) prompt = parsePrompt( argv[ ++i ] );
97				else if( arg == "-m" || arg == "--model" ) model = argv[ ++i ];
98				else
99					throw new ArgumentException( $"Unknown argument: \"{arg}\"" );
100			}
101			if( listDevices )
102				return;
103			if( string.IsNullOrWhiteSpace( model ) )
104				throw new ArgumentException( "The model file is not provided in the arguments" );
105			if( !File.Exists( model ) )
106				throw new FileNotFoundException( "Model not found", model );
107		}
108
109		static string cstr( bool b ) => b.ToString();
110
111		static int[]? parsePrompt( string str )
112		{
113			if( string.IsNullOrWhiteSpace( str ) )
114				return null;
115			// TODO: expose whisper_tokenize function, as a method of iModel COM interface
116			throw new NotImplementedException();
117		}
118
119		void printUsage()
120		{
121			Console.WriteLine();
122
123			Console.WriteLine( "usage: {0} [options] file0.mp3 file1.wma ...", Path.GetFileName( Assembly.GetExecutingAssembly().Location ) );
124			Console.WriteLine();
125			Console.WriteLine( "options:" );
126			Console.WriteLine( "  -h,       --help          [default] show this help message and exit" );
127			Console.WriteLine( "  -t N,     --threads N     [{0,-7:D}] number of threads to use during computation", n_threads );
128			Console.WriteLine( "  -ot N,    --offset-t N    [{0,-7:D}] time offset in milliseconds", offset_t_ms );
129			Console.WriteLine( "  -on N,    --offset-n N    [{0,-7:D}] segment index offset", offset_n );
130			Console.WriteLine( "  -d  N,    --duration N    [{0,-7:D}] duration of audio to process in milliseconds", duration_ms );
131			Console.WriteLine( "  -mc N,    --max-context N [{0,-7:D}] maximum number of text context tokens to store", max_context );
132			Console.WriteLine( "  -ml N,    --max-len N     [{0,-7:D}] maximum segment length in characters", max_len );
133			Console.WriteLine( "  -wt N,    --word-thold N  [{0,-7:F2}] word timestamp probability threshold", word_thold );
134			Console.WriteLine( "  -su,      --speed-up      [{0,-7}] speed up audio by x2 (reduced accuracy)", cstr( speed_up ) );
135			Console.WriteLine( "  -tr,      --translate     [{0,-7}] translate from source language to english", cstr( translate ) );
136			Console.WriteLine( "  -di,      --diarize       [{0,-7}] stereo audio diarization", cstr( diarize ) );
137			Console.WriteLine( "  -otxt,    --output-txt    [{0,-7}] output result in a text file", cstr( output_txt ) );
138			Console.WriteLine( "  -ps,      --print-special [{0,-7}] print special tokens", cstr( print_special ) );
139			Console.WriteLine( "  -nc,      --no-colors     [{0,-7}] do not print colors", cstr( !print_colors ) );
140			Console.WriteLine( "  -nt,      --no-timestamps [{0,-7}] do not print timestamps", cstr( no_timestamps ) );
141			Console.WriteLine( "  -l LANG,  --language LANG [{0,-7}] spoken language", language.getCode() );
142			Console.WriteLine( "            --prompt PROMPT [       ] initial prompt" );
143			Console.WriteLine( "  -m FNAME, --model FNAME   [{0,-7}] model path", model );
144			Console.WriteLine( "  -f FNAME, --file FNAME    [{0,-7}] path of the input audio file", "" );
145		}
146	}
147}