Skip to content

Commit ad02846

Browse files
committed
Update LocalTranscriptionTask.cs
1 parent 1e5b6b6 commit ad02846

1 file changed

Lines changed: 49 additions & 47 deletions

File tree

TaskEngine/Tasks/LocalTranscriptionTask.cs

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -93,81 +93,83 @@ protected async override Task OnConsume(string videoId, TaskParameters taskParam
9393

9494
GetLogger().LogInformation($"{videoId}: Calling RecognitionWithVideoStreamAsync");
9595

96-
var request = new CTGrpc.CaptionRequest
96+
var request = new CTGrpc.TranscriptionRequest
9797
{
9898
LogId = videoId,
9999
FilePath = video.Video1.VMPath,
100-
PhraseHints = phraseHints,
101-
CourseHints = "",
102-
OutputLanguages = "en"
100+
Model = "en",
101+
Language = "en"
102+
// PhraseHints = phraseHints,
103+
// CourseHints = "",
104+
// OutputLanguages = "en"
103105
};
104106
var jsonString = "";
105107
try {
106-
jsonString = (await _rpcClient.PythonServerClient.CaptionRPCAsync(request)).Json;
108+
jsonString = (await _rpcClient.PythonServerClient.TranscribeAudioRPCAsync(request)).Json;
107109
}
108110
catch (RpcException e)
109111
{
110112
if (e.Status.StatusCode == StatusCode.InvalidArgument)
111113
{
112-
GetLogger().LogError($"CaptionRPC=({videoId}):{e.Message}");
114+
GetLogger().LogError($"TranscribeAudioRPCAsync=({videoId}):{e.Message}");
113115
}
114116
return;
115117
} finally {
116-
GetLogger().LogInformation($"{videoId} Caption - rpc complete");
118+
GetLogger().LogInformation($"{videoId} Transcribe - rpc complete");
117119
TaskEngineGlobals.KeyProvider.ReleaseKey(key, video.Id);
118120
}
119-
JArray jArray = JArray.Parse(jsonString);
121+
122+
JObject jObject = JObject.Parse(jsonString);
123+
// JArray jArray = JArray.Parse(jsonString);
124+
var theLanguage = jObject["result"]["language"].ToString(Newtonsoft.Json.Formatting.None);
125+
var theCaptionsAsJson = jObject["transcription"];
120126

121-
foreach (var captionsInLanguage in jArray)
127+
var theCaptions = new List<Caption>();
128+
int cueCount = 0;
129+
130+
foreach (var jsonCue in theCaptionsAsJson) {
131+
var caption = new Caption() {
132+
Index = cueCount ++,
133+
Begin = TimeSpan.Parse(jsonCue["timestamps"]["from"].ToString(Newtonsoft.Json.Formatting.None)),
134+
End = TimeSpan.Parse(jsonCue["timestamps"]["to"].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)
122141
{
123-
var theLanguage = captionsInLanguage["Lang"].ToString(Newtonsoft.Json.Formatting.None);
124-
var theCaptionsAsJson = captionsInLanguage["Captions"];
142+
GetLogger().LogInformation($"{videoId}: Created {theCaptions.Count} captions objects");
125143

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)
144+
var t = _context.Transcriptions.SingleOrDefault(t => t.VideoId == video.Id && t.SourceInternalRef == SOURCEINTERNALREF && t.Language == theLanguage && t.TranscriptionType == TranscriptionType.Caption);
145+
GetLogger().LogInformation($"Find Existing Transcriptions null={t == null}");
146+
// Did we get the default or an existing Transcription entity?
147+
if (t == null)
148+
{
149+
t = new Transcription()
150+
{
151+
TranscriptionType = TranscriptionType.Caption,
152+
Captions = theCaptions,
153+
Language = theLanguage,
154+
VideoId = video.Id,
155+
Label = $"{theLanguage} (ClassTranscribe)",
156+
SourceInternalRef = SOURCEINTERNALREF, //
157+
SourceLabel = "ClassTranscribe (Local" + (phraseHints.Length>0 ?" with phrase hints)" : ")")
158+
// Todo store the entire Whisper result here
136159
};
137-
138-
theCaptions.Add(caption);
160+
_context.Add(t);
139161
}
140-
if (theCaptions.Count > 0)
162+
else
141163
{
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+
t.Captions.AddRange(theCaptions);
164165
}
165166
}
167+
166168

167169
video.TranscriptionStatus = "NoError";
168170
// video.JsonMetadata["LastSuccessfulTime"] = result.LastSuccessTime.ToString();
169171

170-
// GetLogger().LogInformation($"{videoId}: Saving captions Code={result.ErrorCode}. LastSuccessTime={result.LastSuccessTime}");
172+
GetLogger().LogInformation($"{videoId}: Saving captions");
171173
await _context.SaveChangesAsync();
172174
}
173175
catch (Exception ex)

0 commit comments

Comments
 (0)