mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 11:12:36 +01:00
commit
fdc0d915ba
2
src/ui/Forms/Translate/AutoTranslate.Designer.cs
generated
2
src/ui/Forms/Translate/AutoTranslate.Designer.cs
generated
@ -17,6 +17,8 @@
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
|
||||
_httpClient.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
|
@ -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))
|
||||
|
Loading…
Reference in New Issue
Block a user