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