This commit is contained in:
Nikolaj Olsson 2024-06-18 19:28:34 +02:00
parent 6759e1fa5c
commit c695ed7b35

View File

@ -6,6 +6,7 @@ using Nikse.SubtitleEdit.Forms.Options;
using Nikse.SubtitleEdit.Logic; using Nikse.SubtitleEdit.Logic;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
@ -34,6 +35,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
private bool _breakTranslation; private bool _breakTranslation;
private bool _translationInProgress; private bool _translationInProgress;
private bool _singleLineMode; private bool _singleLineMode;
private bool _ollamaModelsDone;
private CancellationTokenSource _cancellationTokenSource; private CancellationTokenSource _cancellationTokenSource;
public AutoTranslate(Subtitle subtitle, Subtitle targetLines, string title, Encoding encoding) public AutoTranslate(Subtitle subtitle, Subtitle targetLines, string title, Encoding encoding)
@ -1168,6 +1170,60 @@ namespace Nikse.SubtitleEdit.Forms.Translate
{ {
SetAutoTranslatorEngine(); SetAutoTranslatorEngine();
SetupLanguageSettings(); SetupLanguageSettings();
if (GetCurrentEngine().GetType() == typeof(OllamaTranslate))
{
var bw = new BackgroundWorker();
bw.DoWork += (o, args) =>
{
using (var httpClient = new HttpClient())
{
try
{
var url = nikseComboBoxUrl.Text.Replace("generate", "tags");
var resultTask = httpClient.GetAsync(new Uri(url));
var result = resultTask.Result;
var bytes = result.Content.ReadAsByteArrayAsync().Result;
if (result.IsSuccessStatusCode)
{
_ollamaModelsDone = true;
var parser = new SeJsonParser();
var resultJson = Encoding.UTF8.GetString(bytes);
var names = parser.GetAllTagsByNameAsStrings(resultJson, "name");
args.Result = names;
}
}
catch
{
// ignore
}
}
};
bw.RunWorkerCompleted += (o, args) =>
{
if (args.Result is List<string> names)
{
var models = Configuration.Settings.Tools.OllamaModels.Split(',').ToList();
var newModels = false;
foreach (var name in names.OrderByDescending(p => p))
{
if (!models.Contains(name))
{
models.Insert(0, name);
newModels = true;
}
}
if (newModels)
{
Configuration.Settings.Tools.OllamaModels = string.Join(",", models);
FillOllamaModels(models);
}
}
};
bw.RunWorkerAsync();
}
} }
private void nikseComboBoxUrl_SelectedIndexChanged(object sender, EventArgs e) private void nikseComboBoxUrl_SelectedIndexChanged(object sender, EventArgs e)
@ -1460,21 +1516,21 @@ namespace Nikse.SubtitleEdit.Forms.Translate
var parser = new SeJsonParser(); var parser = new SeJsonParser();
var resultJson = Encoding.UTF8.GetString(bytes); var resultJson = Encoding.UTF8.GetString(bytes);
var names = parser.GetAllTagsByNameAsStrings(resultJson, "name"); var names = parser.GetAllTagsByNameAsStrings(resultJson, "name");
var newModels = false;
foreach (var name in names.OrderByDescending(p => p)) foreach (var name in names.OrderByDescending(p => p))
{ {
models.Remove(name); if (!models.Contains(name))
models.Insert(0, name); {
models.Insert(0, name);
newModels = true;
}
} }
Configuration.Settings.Tools.OllamaModels = string.Join(",", models); if (newModels)
var v = comboBoxFormality.Text;
comboBoxFormality.Items.Clear();
foreach (var model in models)
{ {
comboBoxFormality.Items.Add(model); Configuration.Settings.Tools.OllamaModels = string.Join(",", models);
FillOllamaModels(models);
} }
comboBoxFormality.Text = v;
} }
} }
catch (Exception exception) catch (Exception exception)
@ -1485,6 +1541,23 @@ namespace Nikse.SubtitleEdit.Forms.Translate
} }
} }
private void FillOllamaModels(List<string> models)
{
if (!(GetCurrentEngine() is OllamaTranslate))
{
return;
}
var v = comboBoxFormality.Text;
comboBoxFormality.Items.Clear();
foreach (var model in models)
{
comboBoxFormality.Items.Add(model);
}
comboBoxFormality.Text = v;
}
private void findModelsToolStripMenuItem_Click(object sender, EventArgs e) private void findModelsToolStripMenuItem_Click(object sender, EventArgs e)
{ {
UiUtil.OpenUrl("https://ollama.com/library"); UiUtil.OpenUrl("https://ollama.com/library");