Merge pull request #8553 from ivandrofly/auto-translate

Auto translate
This commit is contained in:
Nikolaj Olsson 2024-06-23 11:38:19 +02:00 committed by GitHub
commit fdc0d915ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 35 deletions

View File

@ -17,6 +17,8 @@
{
components.Dispose();
}
_httpClient.Dispose();
base.Dispose(disposing);
}

View File

@ -1172,9 +1172,7 @@ namespace Nikse.SubtitleEdit.Forms.Translate
if (GetCurrentEngine().GetType() == typeof(OllamaTranslate))
{
nikseComboBoxEngine.Enabled = false;
await DownloadOllamaModelsAsync().ConfigureAwait(true);
nikseComboBoxEngine.Enabled = true;
await DownloadOllamaModelsAsync();
}
}
@ -1453,50 +1451,58 @@ namespace Nikse.SubtitleEdit.Forms.Translate
private async void UpdateLocalModelsToolStripMenuItem_Click(object sender, EventArgs e)
{
await DownloadOllamaModelsAsync();
await DownloadOllamaModelsAsync(shouldNotifyOnError: true);
}
private async Task DownloadOllamaModelsAsync()
private async Task DownloadOllamaModelsAsync(bool shouldNotifyOnError = false)
{
using (var httpClient = new HttpClient())
try
{
try
var models = await GetModelsAsync(nikseComboBoxUrl.Text.Replace("generate", "tags")).ConfigureAwait(true);
if (models.Count > 0)
{
var url = nikseComboBoxUrl.Text.Replace("generate", "tags");
var result = await httpClient.GetAsync(new Uri(url)).ConfigureAwait(true);
var bytes = await result.Content.ReadAsByteArrayAsync().ConfigureAwait(true);
if (!result.IsSuccessStatusCode)
{
return;
}
var parser = new SeJsonParser();
var resultJson = Encoding.UTF8.GetString(bytes);
var names = parser.GetAllTagsByNameAsStrings(resultJson, "name");
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.ToArray());
}
FillOllamaModels(models.ToArray());
}
catch (Exception exception)
}
catch (Exception exception)
{
if (shouldNotifyOnError)
{
MessageBox.Show("Unable to get ollama models - is ollama running?" + Environment.NewLine + exception.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
SeLogger.Error(exception, "Unable to get ollama models");
}
}
async Task<List<string>> GetModelsAsync(string url)
{
var result = await GetOllamaClient().GetAsync(new Uri(url)).ConfigureAwait(false);
var bytes = await result.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
if (!result.IsSuccessStatusCode)
{
return new List<string>();
}
var parser = new SeJsonParser();
var resultJson = Encoding.UTF8.GetString(bytes);
var names = parser.GetAllTagsByNameAsStrings(resultJson, "name");
var models = Configuration.Settings.Tools.OllamaModels.Split(',').ToList();
foreach (var name in names.OrderByDescending(name => name))
{
if (!models.Contains(name))
{
models.Insert(0, name);
}
}
Configuration.Settings.Tools.OllamaModels = string.Join(",", models);
return models;
}
}
private HttpClient _httpClient;
private HttpClient GetOllamaClient() => _httpClient ?? (_httpClient = new HttpClient());
private void FillOllamaModels(string[] models)
{
if (!(GetCurrentEngine() is OllamaTranslate))