Use temp file for whisper model download to avoid partial downloads + improve error check

This commit is contained in:
niksedk 2023-01-09 21:24:23 +01:00
parent 321f17ec08
commit 621239e518
4 changed files with 60 additions and 5 deletions

View File

@ -47,8 +47,9 @@ namespace Nikse.SubtitleEdit.Core.AudioToText
public Subtitle Generate(Engine engine, Subtitle input, bool usePostProcessing, bool addPeriods, bool mergeLines, bool fixCasing, bool fixShortDuration, bool splitLines)
{
var subtitle = new Subtitle();
foreach (var resultText in input.Paragraphs)
for (var index = 0; index < input.Paragraphs.Count; index++)
{
var resultText = input.Paragraphs[index];
if (usePostProcessing && engine == Engine.Vosk && TwoLetterLanguageCode == "en" && resultText.Text == "the" && resultText.EndTime.TotalSeconds - resultText.StartTime.TotalSeconds > 1)
{
continue;
@ -65,6 +66,12 @@ namespace Nikse.SubtitleEdit.Core.AudioToText
{
resultText.Text = resultText.Text.TrimEnd('.');
}
var next = input.GetParagraphOrDefault(index + 1);
if (next != null && Math.Abs(resultText.EndTime.TotalMilliseconds - next.StartTime.TotalMilliseconds) < 0.01)
{
next.StartTime.TotalMilliseconds++;
}
}
subtitle.Paragraphs.Add(resultText);

View File

@ -40,6 +40,8 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
private double _lastEstimatedMs = double.MaxValue;
private VideoInfo _videoInfo;
private readonly WavePeakData _wavePeaks;
public bool IncompleteModel { get; set; }
public string IncompleteModelName { get; set; }
public Subtitle TranscribedSubtitle { get; private set; }
@ -177,6 +179,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
FfmpegMediaInfo.Parse(_videoFileName).HasFrontCenterAudio(_audioTrackNumber);
_languageCode = GetLanguage(comboBoxLanguages.Text);
IncompleteModel = false;
ShowProgressBar();
if (_batchMode)
@ -233,6 +236,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
{
UpdateLog();
SeLogger.Error(textBoxLog.Text);
IncompleteModelName = comboBoxModels.Text;
}
else
{
@ -595,6 +599,11 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
return;
}
if (outLine.Data.Contains("not all tensors loaded from model file"))
{
IncompleteModel = true;
}
_outputText.Add(outLine.Data.Trim() + Environment.NewLine);
foreach (var line in outLine.Data.SplitToLines())
@ -819,7 +828,25 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
if (Configuration.Settings.Tools.WhisperUseCpp)
{
translateToEnglish += "--print-progress ";
if (!Configuration.Settings.Tools.WhisperExtraSettings.Contains("--print-progress"))
{
translateToEnglish += "--print-progress ";
}
if (!Configuration.Settings.Tools.WhisperExtraSettings.Contains("--max-len"))
{
var maxChars = (int)Math.Round(Configuration.Settings.General.SubtitleLineMaximumLength * 1.8, MidpointRounding.AwayFromZero);
if (language == "jp")
{
maxChars = Configuration.Settings.Tools.AudioToTextLineMaxCharsJp;
}
if (language == "cn")
{
maxChars = Configuration.Settings.Tools.AudioToTextLineMaxCharsCn;
}
translateToEnglish += $"--max-len {maxChars} ";
}
}
var outputSrt = string.Empty;

View File

@ -81,7 +81,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
WhisperHelper.GetWhisperModel().CreateModelFolder();
}
_downloadFileName = MakeDownloadFileName(LastDownloadedModel);
_downloadFileName = MakeDownloadFileName(LastDownloadedModel) + ".$$$";
using (var downloadStream = new FileStream(_downloadFileName, FileMode.Create, FileAccess.Write))
{
var downloadTask = httpClient.DownloadAsync(url, downloadStream, new Progress<float>((progress) =>
@ -135,16 +135,29 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
DialogResult = DialogResult.Cancel;
}
private void CompleteDownload(FileStream downloadStream)
private void CompleteDownload(Stream downloadStream)
{
if (downloadStream.Length == 0)
{
throw new Exception("No content downloaded - missing file or no internet connection!");
}
var newFileName = _downloadFileName.Replace(".$$$", string.Empty);
try
{
File.Delete(newFileName);
}
catch
{
// ignore
}
downloadStream.Flush();
downloadStream.Close();
Application.DoEvents();
File.Move(_downloadFileName, newFileName);
Cursor = Cursors.Default;
labelPleaseWait.Text = string.Empty;

View File

@ -34808,7 +34808,15 @@ namespace Nikse.SubtitleEdit.Forms
if (form.TranscribedSubtitle.Paragraphs.Count == 0)
{
MessageBox.Show("No text found!");
if (form.IncompleteModel)
{
MessageBox.Show($"No text found - model incomplete.{Environment.NewLine}Please re-download model: {form.IncompleteModelName}");
}
else
{
MessageBox.Show("No text found!");
}
return;
}