mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 11:12:36 +01:00
Refactor model fetching in AutoTranslate.cs
The process for downloading Ollama Models has been refactored for brevity and clarity. A private async method, 'GetModelsAsync', was introduced by extracting code from 'DownloadOllamaModelsAsync'. This change enhances code readability and makes possible errors easier to trace. Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
parent
c9e20b0aa9
commit
a9b7da3dc1
@ -1457,42 +1457,45 @@ namespace Nikse.SubtitleEdit.Forms.Translate
|
||||
|
||||
private async Task DownloadOllamaModelsAsync()
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
try
|
||||
{
|
||||
try
|
||||
var models = await GetModelsAsync(nikseComboBoxUrl.Text.Replace("generate", "tags"));
|
||||
if (models.Count > 0)
|
||||
{
|
||||
Configuration.Settings.Tools.OllamaModels = string.Join(",", models);
|
||||
FillOllamaModels(models.ToArray());
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
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)
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
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;
|
||||
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();
|
||||
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());
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
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");
|
||||
return models;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user