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