Skip to content
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
</p>
<p align="center"><b>EN</b> | <a href="docs/README-RU.md"><b>RU</b></a></p>
<p align="center">Advanced screen translator. <b>Translumo</b> is able to detect and translate appearing in the selected area text in real-time (e.g. subtitles).</p>

<h1>My Edition</h1>
<li>Add a ollama enum type and a text box UI to input local ollama model name</li>
<li>Modify Translator.cs, TranslatorFactory.cs, TranslationConfiguration.cs, LanguagesSettingsView.xaml, lang.en-US.xaml and lang.ru-RU.xaml </li>
<li>Add LlamaTranslator.cs, LlamaContainer.cs</li>
<img src="https://github.com/WaterS-MoYu/Translumollama/blob/master/docs/AddtextboxUI.png">

<h1>Main features</h1>
<ul>
<li><b>High text recognition precision</b></li>
Expand Down
Binary file added docs/AddtextboxUI.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class TranslationConfiguration : BindableBase
{
TranslateFromLang = Languages.English,
TranslateToLang = Languages.Russian,
OllamaModel = "llama3",
ProxySettings = new List<Proxy>()
};

Expand Down Expand Up @@ -40,6 +41,15 @@ public Translators Translator
}
}

public string OllamaModel
{
get => _ollamaModel;
set
{
SetProperty(ref _ollamaModel, value);
}
}

public List<Proxy> ProxySettings
{
get => _proxySettings;
Expand All @@ -52,6 +62,7 @@ public List<Proxy> ProxySettings
private Languages _translateFromLang;
private Languages _translateToLang;
private Translators _translator;
private string _ollamaModel;
private List<Proxy> _proxySettings = new List<Proxy>();
}
}
18 changes: 18 additions & 0 deletions src/Translumo.Translation/Llama/LlamaContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text;
using System.Threading.Tasks;
using Translumo.Translation.Configuration;
using Translumo.Utils.Http;

namespace Translumo.Translation.Llama
{
public sealed class LlamaContainer : TranslationContainer
{
public LlamaContainer(Proxy proxy = null, bool isPrimary = false) : base(proxy, isPrimary)
{
}
}
}
82 changes: 82 additions & 0 deletions src/Translumo.Translation/Llama/LlamaTranslator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Transactions;
using System.Web;
using Microsoft.Extensions.Logging;
using Translumo.Infrastructure.Constants;
using Translumo.Infrastructure.Language;
using Translumo.Translation.Configuration;
using Translumo.Translation.Exceptions;
using Translumo.Utils.Http;

namespace Translumo.Translation.Llama
{
public class LlamaTranslator : BaseTranslator<LlamaContainer>
{
private const string LLAMA_API_URL = "http://localhost:11434/api/generate";
private readonly HttpClient _httpClient;
private string _modelName;

public LlamaTranslator(TranslationConfiguration translationConfiguration, LanguageService languageService, ILogger logger)
: base(translationConfiguration, languageService, logger)
{
_httpClient = new HttpClient();
_modelName = translationConfiguration.OllamaModel;
}

public override Task<string> TranslateTextAsync(string sourceText)
{
//TODO: Temp implementation for specific lang
if (TargetLangDescriptor.Language == Languages.PortugueseBrazil)
{
throw new TransactionException("Llama translate is unavailable for this language");
}

return base.TranslateTextAsync(sourceText);
}



protected override async Task<string> TranslateTextInternal(LlamaContainer container, string sourceText)
{
var requestBody = new
{
model = _modelName, // 你可以改成本地安装的模型,如 "llama2"、"mistral"
prompt = $"Translate this text to {TargetLangDescriptor.Language.ToString()}: {sourceText}",
stream = false // 设置 stream=false 以同步返回完整翻译结果
};

// 序列化 JSON
var content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");

// 发送 HTTP 请求到本地 Llama 服务器
HttpResponseMessage response = await _httpClient.PostAsync(LLAMA_API_URL, content);

if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();

// 解析 JSON 响应
using JsonDocument jsonDoc = JsonDocument.Parse(responseBody);
string translatedText = jsonDoc.RootElement.GetProperty("response").GetString();

return translatedText;
}

throw new TranslationException($"Llama translation failed. Status Code: {response.StatusCode}");
}

protected override IList<LlamaContainer> CreateContainers(TranslationConfiguration configuration)
{
var result = configuration.ProxySettings.Select(proxy => new LlamaContainer(proxy)).ToList();
result.Add(new LlamaContainer(isPrimary: true));

return result;
}
}
}
3 changes: 3 additions & 0 deletions src/Translumo.Translation/TranslatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Translumo.Translation.Configuration;
using Translumo.Translation.Deepl;
using Translumo.Translation.Google;
using Translumo.Translation.Llama;
using Translumo.Translation.Papago;
using Translumo.Translation.Yandex;

Expand Down Expand Up @@ -35,6 +36,8 @@ public ITranslator CreateTranslator(TranslationConfiguration translatorConfigura
return new PapagoTranslator(translatorConfiguration, _languageService, _logger);
case Translators.Google:
return new GoogleTranslator(translatorConfiguration, _languageService, _logger);
case Translators.LocalOllama:
return new LlamaTranslator(translatorConfiguration, _languageService, _logger);
default:
throw new NotSupportedException();
}
Expand Down
4 changes: 3 additions & 1 deletion src/Translumo.Translation/Translators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public enum Translators : byte

Google = 2,

Papago = 3
Papago = 3,

LocalOllama = 4
}
}
4 changes: 4 additions & 0 deletions src/Translumo/MVVM/Views/LanguagesSettingsView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
<ComboBox Width="130" Style="{DynamicResource MaterialDesignComboBox}" ItemsSource="{Binding Source={StaticResource dataFromEnum}}" SelectedItem="{Binding Path=Model.Translator}"></ComboBox>
<Label HorizontalAlignment="Stretch" Content="{DynamicResource Str.LangSettings.Translator}" Style="{StaticResource ControlCaptionLabel}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="130" Style="{DynamicResource MaterialDesignTextBox}" Text="{Binding Path=Model.OllamaModel, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<Label HorizontalAlignment="Stretch" Content="{DynamicResource Str.LangSettings.OllamaModel}" Style="{StaticResource ControlCaptionLabel}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<ComboBox Width="130" Style="{DynamicResource MaterialDesignComboBox}" ItemsSource="{Binding Source={StaticResource ttsSystems}}" SelectedItem="{Binding Path=TtsSystem}"></ComboBox>
<Label HorizontalAlignment="Stretch" Content="{DynamicResource Str.LangSettings.TtsSystem}" Style="{StaticResource ControlCaptionLabel}"/>
Expand Down
1 change: 1 addition & 0 deletions src/Translumo/Resources/Localization/lang.en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<system:String x:Key="Str.LangSettings.SourceLang">Source language</system:String>
<system:String x:Key="Str.LangSettings.TranslationLang">Translation language</system:String>
<system:String x:Key="Str.LangSettings.Translator">Translator</system:String>
<system:String x:Key="Str.LangSettings.OllamaModel">OllamaModel</system:String>
<system:String x:Key="Str.LangSettings.TtsSystem">Text to speech system (experimental feature)</system:String>

<!--Hotkeys descriptions-->
Expand Down
1 change: 1 addition & 0 deletions src/Translumo/Resources/Localization/lang.ru-RU.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<system:String x:Key="Str.LangSettings.SourceLang">Исходный язык</system:String>
<system:String x:Key="Str.LangSettings.TranslationLang">Язык перевода</system:String>
<system:String x:Key="Str.LangSettings.Translator">Переводчик</system:String>
<system:String x:Key="Str.LangSettings.OllamaModel">OllamaModel</system:String>
<system:String x:Key="Str.LangSettings.TtsSystem">Синтез речи TTS (предварительная версия)</system:String>

<!--Hotkeys descriptions-->
Expand Down