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

KonstantinBugfix, incorrect output of command-line examples when launched with multiple input files3ba8e63

master
3.0 KiB104 linesraw
1using Whisper;
2
3namespace TranscribeCS
4{
5	static class Program
6	{
7		static readonly bool streamAudio = true;
8
9		static int Main( string[] args )
10		{
11			try
12			{
13				CommandLineArgs cla;
14				try
15				{
16					cla = new CommandLineArgs( args );
17				}
18				catch( OperationCanceledException )
19				{
20					return 1;
21				}
22				const eLoggerFlags loggerFlags = eLoggerFlags.UseStandardError | eLoggerFlags.SkipFormatMessage;
23				Library.setLogSink( eLogLevel.Debug, loggerFlags );
24
25				using iModel model = Library.loadModel( cla.model );
26				using Context context = model.createContext();
27				cla.apply( ref context.parameters );
28				// When there're multiple input files, assuming they're independent clips
29				context.parameters.setFlag( eFullParamsFlags.NoContext, true );
30				using iMediaFoundation mf = Library.initMediaFoundation();
31				Transcribe transcribe = new Transcribe( cla );
32
33				foreach( string audioFile in cla.fileNames )
34				{
35					if( streamAudio )
36					{
37						using iAudioReader reader = mf.openAudioFile( audioFile, cla.diarize );
38						context.runFull( reader, transcribe, null, cla.prompt );
39					}
40					else
41					{
42						using iAudioBuffer buffer = mf.loadAudioFile( audioFile, cla.diarize );
43						context.runFull( buffer, transcribe, cla.prompt );
44					}
45					// When asked to, produce these text files
46					if( cla.output_txt )
47						writeTextFile( context, audioFile );
48					if( cla.output_srt )
49						writeSubRip( context, audioFile, cla );
50					if( cla.output_vtt )
51						writeWebVTT( context, audioFile );
52				}
53
54				context.timingsPrint();
55				return 0;
56			}
57			catch( Exception ex )
58			{
59				Console.WriteLine( ex.Message );
60				return ex.HResult;
61			}
62		}
63
64		static void writeTextFile( Context context, string audioPath )
65		{
66			using var stream = File.CreateText( Path.ChangeExtension( audioPath, ".txt" ) );
67			foreach( sSegment seg in context.results().segments )
68				stream.WriteLine( seg.text );
69		}
70
71		static void writeSubRip( Context context, string audioPath, CommandLineArgs cliArgs )
72		{
73			using var stream = File.CreateText( Path.ChangeExtension( audioPath, ".srt" ) );
74			var segments = context.results( eResultFlags.Timestamps ).segments;
75
76			for( int i = 0; i < segments.Length; i++ )
77			{
78				stream.WriteLine( i + 1 + cliArgs.offset_n );
79				sSegment seg = segments[ i ];
80				string begin = Transcribe.printTimeWithComma( seg.time.begin );
81				string end = Transcribe.printTimeWithComma( seg.time.end );
82				stream.WriteLine( "{0} --> {1}", begin, end );
83				stream.WriteLine( seg.text );
84				stream.WriteLine();
85			}
86		}
87
88		static void writeWebVTT( Context context, string audioPath )
89		{
90			using var stream = File.CreateText( Path.ChangeExtension( audioPath, ".vtt" ) );
91			stream.WriteLine( "WEBVTT" );
92			stream.WriteLine();
93
94			foreach( sSegment seg in context.results( eResultFlags.Timestamps ).segments )
95			{
96				string begin = Transcribe.printTime( seg.time.begin );
97				string end = Transcribe.printTime( seg.time.end );
98				stream.WriteLine( "{0} --> {1}", begin, end );
99				stream.WriteLine( seg.text );
100				stream.WriteLine();
101			}
102		}
103	}
104}