Skip to content

Commit d086d8b

Browse files
committed
First Changes for Whisper
1 parent 43f3ad3 commit d086d8b

12 files changed

Lines changed: 224 additions & 21 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "8.0.201"
3+
"version": "8.0"
44
}
55
}

ClassTranscribeServer/global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"sdk": {
3-
"version": "8.0.401"
3+
"version": "8.0"
44
}
55
}

PythonRpcServer/server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ def LogWorker(logId, worker):
4141

4242

4343
class PythonServerServicer(ct_pb2_grpc.PythonServerServicer):
44+
def CaptionRPC(self, request, context):
45+
#See CaptionRequest
46+
print( f"CaptionRPC({request.logId};{request.refId};{request.filePath};{request.phraseHints};{request.courseHints};{request.outputLanguages})")
47+
kalturaprovider = KalturaProvider()
48+
result = LogWorker(f"CaptionRPC({request.filePath})", lambda: kalturaprovider.getCaptions(request.refId))
49+
return ct_pb2.JsonString(json = result)
50+
51+
52+
4453
def GetScenesRPC(self, request, context):
4554
raise NotImplementedError('Implementation now in pyapi')
4655
# res = scenedetector.find_scenes(request.filePath)

TaskEngine/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public static void SetupServices()
8181
.AddSingleton<DownloadPlaylistInfoTask>()
8282
.AddSingleton<DownloadMediaTask>()
8383
.AddSingleton<ConvertVideoToWavTask>()
84-
.AddSingleton<TranscriptionTask>()
84+
.AddSingleton<LocalTranscriptionTask>()
85+
.AddSingleton<AzureTranscriptionTask>()
8586
.AddSingleton<QueueAwakerTask>()
8687
// .AddSingleton<GenerateVTTFileTask>()
8788
.AddSingleton<RpcClient>()
@@ -175,7 +176,7 @@ static void createTaskQueues() {
175176
// Transcription Related
176177
_logger.LogInformation($"Creating TranscriptionTask consumers. Concurrency={concurrent_transcriptions} ");
177178

178-
_serviceProvider.GetService<TranscriptionTask>().Consume(concurrent_transcriptions);
179+
_serviceProvider.GetService<LocalTranscriptionTask>().Consume(concurrent_transcriptions);
179180

180181
// no more! - _serviceProvider.GetService<GenerateVTTFileTask>().Consume(concurrent_transcriptions);
181182

TaskEngine/Tasks/TranscriptionTask.cs renamed to TaskEngine/Tasks/AzureTranscriptionTask.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ namespace TaskEngine.Tasks
2121
/// This task produces the transcriptions for a Video item.
2222
/// </summary>
2323
[SuppressMessage("Microsoft.Performance", "CA1812:MarkMembersAsStatic")] // This class is never directly instantiated
24-
class TranscriptionTask : RabbitMQTask<string>
24+
class AzureTranscriptionTask : RabbitMQTask<string>
2525
{
2626

2727
private readonly MSTranscriptionService _msTranscriptionService;
2828
// nope private readonly GenerateVTTFileTask _generateVTTFileTask;
2929
private readonly CaptionQueries _captionQueries;
3030

3131

32-
public TranscriptionTask(RabbitMQConnection rabbitMQ, MSTranscriptionService msTranscriptionService,
32+
public AzureTranscriptionTask(RabbitMQConnection rabbitMQ, MSTranscriptionService msTranscriptionService,
3333
// GenerateVTTFileTask generateVTTFileTask,
34-
ILogger<TranscriptionTask> logger, CaptionQueries captionQueries)
34+
ILogger<AzureTranscriptionTask> logger, CaptionQueries captionQueries)
3535
: base(rabbitMQ, TaskType.TranscribeVideo, logger)
3636
{
3737
_msTranscriptionService = msTranscriptionService;

TaskEngine/Tasks/ConvertVideoToWavTask.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ namespace TaskEngine.Tasks
2121
class ConvertVideoToWavTask : RabbitMQTask<string>
2222
{
2323
private readonly RpcClient _rpcClient;
24-
private readonly TranscriptionTask _transcriptionTask;
24+
private readonly LocalTranscriptionTask _localTranscriptionTask;
2525

26-
public ConvertVideoToWavTask(RabbitMQConnection rabbitMQ, RpcClient rpcClient, TranscriptionTask transcriptionTask, ILogger<ConvertVideoToWavTask> logger)
26+
public ConvertVideoToWavTask(RabbitMQConnection rabbitMQ, RpcClient rpcClient, LocalTranscriptionTask localTranscriptionTask, ILogger<ConvertVideoToWavTask> logger)
2727
: base(rabbitMQ, TaskType.ConvertMedia, logger)
2828
{
2929
_rpcClient = rpcClient;
30-
_transcriptionTask = transcriptionTask;
30+
_localTranscriptionTask = localTranscriptionTask;
3131
}
3232

3333
protected override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
@@ -72,11 +72,10 @@ private async Task OldOnConsumeNotUsed(string videoId)
7272
videoLatest.Audio = fileRecord;
7373
await _context.SaveChangesAsync();
7474

75-
7675
// If no transcriptions present, produce transcriptions.
7776
if (!videoLatest.Transcriptions.Any())
7877
{
79-
_transcriptionTask.Publish(videoLatest.Id);
78+
_localTranscriptionTask.Publish(videoLatest.Id);
8079
}
8180
}
8281
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Logging;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using Grpc.Core;
9+
using Newtonsoft.Json.Linq;
10+
11+
12+
using ClassTranscribeDatabase;
13+
using ClassTranscribeDatabase.Models;
14+
using ClassTranscribeDatabase.Services;
15+
16+
using static ClassTranscribeDatabase.CommonUtils;
17+
18+
#pragma warning disable CA2007
19+
// https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2007
20+
// We are okay awaiting on a task in the same thread
21+
22+
namespace TaskEngine.Tasks
23+
{
24+
/// <summary>
25+
/// This task produces the transcriptions for a Video item.
26+
/// </summary>
27+
[SuppressMessage("Microsoft.Performance", "CA1812:MarkMembersAsStatic")] // This class is never directly instantiated
28+
class LocalTranscriptionTask : RabbitMQTask<string>
29+
{
30+
31+
private readonly CaptionQueries _captionQueries;
32+
private readonly RpcClient _rpcClient;
33+
34+
35+
public LocalTranscriptionTask(RabbitMQConnection rabbitMQ,
36+
RpcClient rpcClient,
37+
// GenerateVTTFileTask generateVTTFileTask,
38+
ILogger<LocalTranscriptionTask> logger, CaptionQueries captionQueries)
39+
: base(rabbitMQ, TaskType.TranscribeVideo, logger)
40+
{
41+
_rpcClient = rpcClient;
42+
_captionQueries = captionQueries;
43+
}
44+
45+
protected async override Task OnConsume(string videoId, TaskParameters taskParameters, ClientActiveTasks cleanup)
46+
{
47+
RegisterTask(cleanup, videoId); // may throw AlreadyInProgress exception
48+
49+
const string SOURCEINTERNALREF= "ClassTranscribe/Local"; // Do not change me; this is a key inside the database
50+
// to indicate the source of the captions was this code
51+
52+
53+
using (var _context = CTDbContext.CreateDbContext())
54+
{
55+
56+
// TODO: taskParameters.Force should wipe all captions and reset the Transcription Status
57+
58+
Video video = await _context.Videos.Include(v => v.Video1).Where(v => v.Id == videoId).FirstAsync();
59+
// ! Note the 'Include' ; we don't build the whole tree of related Entities
60+
61+
if (video.TranscriptionStatus == Video.TranscriptionStatusMessages.NOERROR)
62+
{
63+
GetLogger().LogInformation($"{videoId}:Skipping Transcribing of- already complete");
64+
return;
65+
}
66+
var medias = await _context.Medias.Include(m=>m.Playlist).Where(m=>m.VideoId == videoId && m.Playlist != null).ToListAsync();
67+
if(medias.Count == 0) {
68+
GetLogger().LogInformation($"{videoId}:Skipping Transcribing - no media / playlist cares about this video");
69+
return;
70+
}
71+
72+
GetLogger().LogInformation($"{videoId}: Has new Phrase Hints: {video.HasPhraseHints()}");
73+
74+
string phraseHints = "";
75+
if (video.HasPhraseHints()) {
76+
var data = await _context.TextData.FindAsync(video.PhraseHintsDataId);
77+
phraseHints = data.Text;
78+
} else
79+
{ // deprecated
80+
phraseHints = video.PhraseHints ?? "";
81+
}
82+
83+
GetLogger().LogInformation($"{videoId}:Using Phrase Hints length = {phraseHints.Length}");
84+
// GetKey can throw if the video.Id is currently being transcribed
85+
// However registerTask should have already detected that
86+
var key = TaskEngineGlobals.KeyProvider.GetKey(video.Id);
87+
88+
video.TranscribingAttempts += 10;
89+
await _context.SaveChangesAsync();
90+
GetLogger().LogInformation($"{videoId}: Updated TranscribingAttempts = {video.TranscribingAttempts}");
91+
try
92+
{
93+
94+
GetLogger().LogInformation($"{videoId}: Calling RecognitionWithVideoStreamAsync");
95+
96+
var request = new CTGrpc.CaptionRequest
97+
{
98+
LogId = videoId,
99+
FilePath = video.Video1.VMPath,
100+
PhraseHints = phraseHints,
101+
CourseHints = "",
102+
OutputLanguages = "en"
103+
};
104+
var jsonString = "";
105+
try {
106+
jsonString = (await _rpcClient.PythonServerClient.CaptionRPCAsync(request)).Json;
107+
}
108+
catch (RpcException e)
109+
{
110+
if (e.Status.StatusCode == StatusCode.InvalidArgument)
111+
{
112+
GetLogger().LogError($"CaptionRPC=({videoId}):{e.Message}");
113+
}
114+
return;
115+
} finally {
116+
GetLogger().LogInformation($"{videoId} Caption - rpc complete");
117+
TaskEngineGlobals.KeyProvider.ReleaseKey(key, video.Id);
118+
}
119+
JArray jArray = JArray.Parse(jsonString);
120+
121+
foreach (var captionsInLanguage in jArray)
122+
{
123+
var theLanguage = captionsInLanguage["Lang"].ToString(Newtonsoft.Json.Formatting.None);
124+
var theCaptionsAsJson = captionsInLanguage["Captions"];
125+
126+
var theCaptions = new List<Caption>();
127+
int cueCount = 0;
128+
// Fix the next line of code
129+
130+
foreach (var jsonCue in theCaptionsAsJson) {
131+
var caption = new Caption() {
132+
Index = cueCount ++,
133+
Begin = TimeSpan.Parse(jsonCue["start"].ToString(Newtonsoft.Json.Formatting.None)),
134+
End = TimeSpan.Parse(jsonCue["end"].ToString(Newtonsoft.Json.Formatting.None)) ,
135+
Text = jsonCue["text"] .ToString(Newtonsoft.Json.Formatting.None)
136+
};
137+
138+
theCaptions.Add(caption);
139+
}
140+
if (theCaptions.Count > 0)
141+
{
142+
143+
var t = _context.Transcriptions.SingleOrDefault(t => t.VideoId == video.Id && t.SourceInternalRef == SOURCEINTERNALREF && t.Language == theLanguage && t.TranscriptionType == TranscriptionType.Caption);
144+
GetLogger().LogInformation($"Find Existing Transcriptions null={t == null}");
145+
// Did we get the default or an existing Transcription entity?
146+
if (t == null)
147+
{
148+
t = new Transcription()
149+
{
150+
TranscriptionType = TranscriptionType.Caption,
151+
Captions = theCaptions,
152+
Language = theLanguage,
153+
VideoId = video.Id,
154+
Label = $"{theLanguage} (ClassTranscribe)",
155+
SourceInternalRef = SOURCEINTERNALREF, //
156+
SourceLabel = "ClassTranscribe (Local" + (phraseHints.Length>0 ?" with phrase hints)" : ")")
157+
};
158+
_context.Add(t);
159+
}
160+
else
161+
{
162+
t.Captions.AddRange(theCaptions);
163+
}
164+
}
165+
}
166+
167+
video.TranscriptionStatus = "NoError";
168+
// video.JsonMetadata["LastSuccessfulTime"] = result.LastSuccessTime.ToString();
169+
170+
// GetLogger().LogInformation($"{videoId}: Saving captions Code={result.ErrorCode}. LastSuccessTime={result.LastSuccessTime}");
171+
await _context.SaveChangesAsync();
172+
}
173+
catch (Exception ex)
174+
{
175+
GetLogger().LogError(ex, $"{videoId}: Transcription Exception:${ex.StackTrace}");
176+
video.TranscribingAttempts += 1000;
177+
await _context.SaveChangesAsync();
178+
throw;
179+
}
180+
181+
}
182+
}
183+
184+
}
185+
}

TaskEngine/Tasks/QueueAwakerTask.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class QueueAwakerTask : RabbitMQTask<JObject>
2222
private readonly DownloadPlaylistInfoTask _downloadPlaylistInfoTask;
2323
private readonly DownloadMediaTask _downloadMediaTask;
2424
// private readonly ConvertVideoToWavTask _convertVideoToWavTask;
25-
private readonly TranscriptionTask _transcriptionTask;
25+
private readonly LocalTranscriptionTask _transcriptionTask;
2626
// nope private readonly GenerateVTTFileTask _generateVTTFileTask;
2727
private readonly ProcessVideoTask _processVideoTask;
2828
private readonly SceneDetectionTask _sceneDetectionTask;
@@ -39,7 +39,7 @@ public QueueAwakerTask() { }
3939

4040
public QueueAwakerTask(RabbitMQConnection rabbitMQ, DownloadPlaylistInfoTask downloadPlaylistInfoTask,
4141
DownloadMediaTask downloadMediaTask,
42-
TranscriptionTask transcriptionTask, ProcessVideoTask processVideoTask,
42+
LocalTranscriptionTask transcriptionTask, ProcessVideoTask processVideoTask,
4343
// GenerateVTTFileTask generateVTTFileTask,
4444
SceneDetectionTask sceneDetectionTask,
4545
CreateBoxTokenTask createBoxTokenTask,// UpdateBoxTokenTask updateBoxTokenTask,

TaskEngine/Tasks/SceneDetectionTask.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ namespace TaskEngine.Tasks
1919
class SceneDetectionTask : RabbitMQTask<string>
2020
{
2121
private readonly RpcClient _rpcClient;
22-
private readonly TranscriptionTask _transcriptionTask;
22+
private readonly LocalTranscriptionTask _transcriptionTask;
2323

24-
public SceneDetectionTask(RabbitMQConnection rabbitMQ,TranscriptionTask transcriptionTask, RpcClient rpcClient, ILogger<SceneDetectionTask> logger)
24+
public SceneDetectionTask(RabbitMQConnection rabbitMQ,LocalTranscriptionTask localTanscriptionTask, RpcClient rpcClient, ILogger<SceneDetectionTask> logger)
2525
: base(rabbitMQ, TaskType.SceneDetection, logger)
2626
{
2727
_rpcClient = rpcClient;
28-
_transcriptionTask = transcriptionTask;
28+
_transcriptionTask = localTanscriptionTask;
2929
}
3030
/// <summary>Extracts scene information for a video.
3131
/// Beware: It is possible to start another scene task while the first one is still running</summary>

TaskEngine/TempCode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class TempCode
2424
private readonly PythonCrawlerTask _pythonCrawlerTask;
2525
private readonly ProcessVideoTask _processVideoTask;
2626
// private readonly GenerateVTTFileTask _generateVTTFileTask;
27-
private readonly TranscriptionTask _transcriptionTask;
27+
private readonly LocalTranscriptionTask _transcriptionTask;
2828
private readonly ConvertVideoToWavTask _convertVideoToWavTask;
2929
private readonly DownloadMediaTask _downloadMediaTask;
3030
private readonly DownloadPlaylistInfoTask _downloadPlaylistInfoTask;
@@ -34,7 +34,7 @@ class TempCode
3434

3535
public TempCode(CTDbContext c, CreateBoxTokenTask createBoxTokenTask, //UpdateBoxTokenTask updateBoxTokenTask,
3636
SceneDetectionTask ePubGeneratorTask, ProcessVideoTask processVideoTask,
37-
TranscriptionTask transcriptionTask, ConvertVideoToWavTask convertVideoToWavTask, DownloadMediaTask downloadMediaTask,
37+
LocalTranscriptionTask localTranscriptionTask, ConvertVideoToWavTask convertVideoToWavTask, DownloadMediaTask downloadMediaTask,
3838
DownloadPlaylistInfoTask downloadPlaylistInfoTask, QueueAwakerTask queueAwakerTask,
3939
CleanUpElasticIndexTask cleanUpElasticIndexTask, RpcClient rpcClient,
4040
PythonCrawlerTask pythonCrawlerTask)
@@ -45,7 +45,7 @@ public TempCode(CTDbContext c, CreateBoxTokenTask createBoxTokenTask, //UpdateBo
4545
_sceneDetectionTask = ePubGeneratorTask;
4646
_processVideoTask = processVideoTask;
4747
// _generateVTTFileTask = generateVTTFileTask;
48-
_transcriptionTask = transcriptionTask;
48+
_transcriptionTask = localTranscriptionTask;
4949
_convertVideoToWavTask = convertVideoToWavTask;
5050
_downloadMediaTask = downloadMediaTask;
5151
_downloadPlaylistInfoTask = downloadPlaylistInfoTask;

0 commit comments

Comments
 (0)