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

KonstantinC# console example, diarize feature850bf49

master
2.9 KiB99 linesraw
1using System.Globalization;
2using Whisper;
3
4namespace TranscribeCS
5{
6	/// <summary>Implementation of Callbacks abstract class, to print these segments as soon as they’re produced by the library.</summary>
7	sealed class Transcribe: Callbacks
8	{
9		readonly CommandLineArgs args;
10		readonly eResultFlags resultFlags;
11
12		public Transcribe( CommandLineArgs args )
13		{
14			this.args = args;
15			resultFlags = args.resultFlags();
16			Console.OutputEncoding = System.Text.Encoding.UTF8;
17		}
18
19		// Terminal color map. 10 colors grouped in ranges [0.0, 0.1, ..., 0.9]
20		// Lowest is red, middle is yellow, highest is green.
21		readonly string[] k_colors = new string[]
22		{
23			"\x1B[38;5;196m", "\x1B[38;5;202m", "\x1B[38;5;208m", "\x1B[38;5;214m", "\x1B[38;5;220m",
24			"\x1B[38;5;226m", "\x1B[38;5;190m", "\x1B[38;5;154m", "\x1B[38;5;118m", "\x1B[38;5;82m"
25		};
26
27		int colorIndex( in sToken tok )
28		{
29			float p = tok.probability;
30			float p3 = p * p * p;
31			int col = (int)( p3 * k_colors.Length );
32			col = Math.Clamp( col, 0, k_colors.Length - 1 );
33			return col;
34		}
35
36		public static string printTime( TimeSpan ts ) =>
37			ts.ToString( "hh':'mm':'ss'.'fff", CultureInfo.InvariantCulture );
38		public static string printTimeWithComma( TimeSpan ts ) =>
39			ts.ToString( "hh':'mm':'ss','fff", CultureInfo.InvariantCulture );
40
41		protected override void onNewSegment( Context sender, int countNew )
42		{
43			TranscribeResult res = sender.results( resultFlags );
44			ReadOnlySpan<sToken> tokens = res.tokens;
45
46			int s0 = res.segments.Length - countNew;
47			if( s0 == 0 )
48				Console.WriteLine();
49
50			for( int i = s0; i < res.segments.Length; i++ )
51			{
52				sSegment seg = res.segments[ i ];
53
54				if( args.no_timestamps )
55				{
56					if( args.print_colors && AnsiCodes.enabled )
57					{
58						foreach( sToken tok in res.getTokens( seg ) )
59						{
60							if( !args.print_special && tok.hasFlag( eTokenFlags.Special ) )
61								continue;
62							Console.Write( "{0}{1}{2}", k_colors[ colorIndex( tok ) ], tok.text, "\x1B[0m" );
63						}
64					}
65					else
66						Console.Write( seg.text );
67					Console.Out.Flush();
68					continue;
69				}
70
71				string speaker = "";
72				if( args.diarize )
73				{
74					speaker = sender.detectSpeaker( seg.time ) switch
75					{
76						eSpeakerChannel.Unsure => "(speaker ?)",
77						eSpeakerChannel.Left => "(speaker 0)",
78						eSpeakerChannel.Right => "(speaker 1)",
79						_ => ""
80					};
81				}
82
83				if( args.print_colors && AnsiCodes.enabled )
84				{
85					Console.Write( "[{0} --> {1}] {2} ", printTime( seg.time.begin ), printTime( seg.time.end ), speaker );
86					foreach( sToken tok in res.getTokens( seg ) )
87					{
88						if( !args.print_special && tok.hasFlag( eTokenFlags.Special ) )
89							continue;
90						Console.Write( "{0}{1}{2}", k_colors[ colorIndex( tok ) ], tok.text, "\x1B[0m" );
91					}
92					Console.WriteLine();
93				}
94				else
95					Console.WriteLine( "[{0} --> {1}] {2} {3}", printTime( seg.time.begin ), printTime( seg.time.end ), speaker, seg.text );
96			}
97		}
98	}
99}