Skip to content

Commit

Permalink
feat: 使用 Azure OpenAI 进行翻译 (#340)
Browse files Browse the repository at this point in the history
* feat: 使用 Azure OpenAI 进行翻译

* add: add warning message if the output was filtered
  • Loading branch information
dogdie233 authored Nov 2, 2024
1 parent d91e66c commit d2d7841
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 0 deletions.
14 changes: 14 additions & 0 deletions MisakaTranslator-WPF/IAppSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ string ChatGPTapiUrl
set;
}

[Option(Alias = "AzureOpenAITranslator.apiKey", DefaultValue = "")]
string AzureOpenAIApiKey
{
get;
set;
}

[Option(Alias = "AzureOpenAITranslator.apiUrl", DefaultValue = "https://XXX.openai.azure.com/openai/deployments/YYY/chat/completions")]
string AzureOpenAIApiUrl
{
get;
set;
}

[Option(Alias = "XiaoniuTranslator.xiaoniuApiKey", DefaultValue = "")]
string xiaoniuApiKey
{
Expand Down
8 changes: 8 additions & 0 deletions MisakaTranslator-WPF/MisakaTranslator-WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@
<PackageReference Include="TesseractOCR" Version="5.2.14" Aliases="Tesseract" />
</ItemGroup>

<ItemGroup>
<Page Update="SettingsPages\TranslatorPages\AzureOpenAITransSettingsPage.xaml">
<Generator>MSBuild:Compile</Generator>
<XamlRuntime>Wpf</XamlRuntime>
<SubType>Designer</SubType>
</Page>
</ItemGroup>

<!-- Workaround for Desktop Bridge -->
<Target Name="BeforeGenerateCurrentProjectAppxManifest" BeforeTargets="_GenerateCurrentProjectAppxManifest">
<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<Page x:Class="MisakaTranslator_WPF.SettingsPages.TranslatorPages.AzureOpenAITransSettingsPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:hc="https://handyorg.github.io/handycontrol"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MisakaTranslator_WPF.SettingsPages.TranslatorPages"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="594"
Title="AzureOpenAITransSettingsPage">

<Grid>
<hc:ScrollViewer HorizontalScrollBarVisibility="Disabled">
<StackPanel Background="White">
<TextBlock Text="{StaticResource AzureOpenAITransSettingsPage_Introduce}" FontSize="16" Margin="10" TextWrapping="WrapWithOverflow"/>
<StackPanel Margin="20,20,20,0">
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.ColumnSpan="2">
<hc:TextBox hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="{StaticResource AzureOpenAITransSettingsPage_secretKey}" x:Name="AzureOpenAITransSecretKeyBox" hc:InfoElement.ShowClearButton="True" Margin="0,0,132,0"/>
<!--Button Content="{StaticResource API_authTestBtn}" Margin="432,0,0,0" Width="122" Click="AuthTestBtn_Click" HorizontalAlignment="Left" /-->
</Grid>
</Grid>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.ColumnSpan="2">
<hc:TextBox hc:TitleElement.TitlePlacement="Left" hc:TitleElement.Title="{StaticResource AzureOpenAITransSettingsPage_apiUrl}" x:Name="AzureOpenAITransUrlBox" hc:InfoElement.ShowClearButton="True"
Text="https://XXX.openai.azure.com/openai/deployments/YYY/chat/completions" Margin="0,0,132,0"/>
<Button Content="{StaticResource API_authTestBtn}" Margin="0,0,0,0" Width="122" Click="AuthTestBtn_Click" HorizontalAlignment="Right" />
</Grid>
</Grid>
<hc:Divider/>
<hc:TextBox hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="{StaticResource API_testSrcText}" Name="TestSrcText" hc:InfoElement.ShowClearButton="True" Text="本ソフトウェアのご利用を歓迎します。"/>
<Grid Margin="0,10,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<hc:TextBox hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="{StaticResource API_testSrcLang}" Name="TestSrcLang" hc:InfoElement.ShowClearButton="True" Text="jp" TextAlignment="Center"/>
<hc:TextBox Grid.Column="2" hc:InfoElement.TitlePlacement="Left" hc:InfoElement.Title="{StaticResource API_testDstLang}" Name="TestDstLang" hc:InfoElement.ShowClearButton="True" Text="zh" TextAlignment="Center"/>
<Button Grid.Column="3" Content="{StaticResource API_testTransBtn}" Margin="10,0,0,0" Width="122" Click="TransTestBtn_Click" ></Button>
</Grid>
<StackPanel Margin="0,10,0,0" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="{StaticResource API_applyBtn}" Width="122" Click="ApplyBtn_Click" HorizontalAlignment="Left" />
<Button Content="{StaticResource API_docBtn}" Margin="10,0,0,0" Width="225" Click="DocBtn_Click" HorizontalAlignment="Left" ></Button>
<!--<Button Content="{StaticResource API_billBtn}" Margin="10,0,0,0" Width="122" Click="BillBtn_Click" HorizontalAlignment="Right" ></Button>!-->
</StackPanel>
</StackPanel>
</StackPanel>
</hc:ScrollViewer>
<StackPanel hc:Growl.GrowlParent="True" HorizontalAlignment="Right" Margin="15"/>

</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using System.Windows;
using System.Windows.Controls;
using TranslatorLibrary;

namespace MisakaTranslator_WPF.SettingsPages.TranslatorPages
{
/// <summary>
/// AzureOpenAITransSettingsPage.xaml 的交互逻辑
/// </summary>
public partial class AzureOpenAITransSettingsPage : Page
{
public AzureOpenAITransSettingsPage()
{
InitializeComponent();
AzureOpenAITransSecretKeyBox.Text = Common.appSettings.AzureOpenAIApiKey;
AzureOpenAITransUrlBox.Text = Common.appSettings.AzureOpenAIApiUrl;
}

private async void AuthTestBtn_Click(object sender, RoutedEventArgs e)
{
Common.appSettings.AzureOpenAIApiKey = AzureOpenAITransSecretKeyBox.Text;
Common.appSettings.AzureOpenAIApiUrl = AzureOpenAITransUrlBox.Text;

ITranslator trans = new AzureOpenAITranslator();
trans.TranslatorInit(AzureOpenAITransSecretKeyBox.Text, AzureOpenAITransUrlBox.Text);

if (await trans.TranslateAsync("apple", "zh", "en") != null)
{
HandyControl.Controls.Growl.Success($"Azure OpenAI {Application.Current.Resources["APITest_Success_Hint"]}");
}
else
{
HandyControl.Controls.Growl.Error($"Azure OpenAI {Application.Current.Resources["APITest_Error_Hint"]}\n{trans.GetLastError()}");
}
}

private void ApplyBtn_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("https://azure.microsoft.com/en-us/solutions/ai/");
}

private void DocBtn_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("https://learn.microsoft.com/en-us/azure/ai-services/cognitive-services-support-options");
}

private void BillBtn_Click(object sender, RoutedEventArgs e)
{
// System.Diagnostics.Process.Start(ChatGPTTranslator.BILL_URL);
}

private async void TransTestBtn_Click(object sender, RoutedEventArgs e)
{
ITranslator trans = new AzureOpenAITranslator();
trans.TranslatorInit(AzureOpenAITransSecretKeyBox.Text, AzureOpenAITransUrlBox.Text);
string res = await trans.TranslateAsync(TestSrcText.Text, TestDstLang.Text, TestSrcLang.Text);

if (res != null)
{
HandyControl.Controls.MessageBox.Show(res, Application.Current.Resources["MessageBox_Result"].ToString());
}
else
{
HandyControl.Controls.Growl.Error(
$"Azure OpenAI {Application.Current.Resources["APITest_Error_Hint"]}\n{trans.GetLastError()}");
}
}
}
}
1 change: 1 addition & 0 deletions MisakaTranslator-WPF/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<hc:SideMenuItem Header="{StaticResource SettingsMenu_TranslatorSettings_Dreye}" Selected="Item_Dreye_Selected"/>
<hc:SideMenuItem Header="{StaticResource SettingsMenu_TranslatorSettings_DeepL}" Selected="Item_DeepLTrans_Selected"/>
<hc:SideMenuItem Header="{StaticResource SettingsMenu_TranslatorSettings_ChatGPT}" Selected="Item_ChatGPTTrans_Selected"/>
<hc:SideMenuItem Header="{StaticResource SettingsMenu_TranslatorSettings_AzureOpenAI}" Selected="Item_AzureOpenAITrans_Selected"/>
<hc:SideMenuItem Header="{StaticResource SettingsMenu_TranslatorSettings_IBM}" Selected="Item_IBM_Selected"/>
<hc:SideMenuItem Header="{StaticResource SettingsMenu_TranslatorSettings_Yandex}" Selected="Item_Yandex_Selected"/>
</hc:SideMenuItem>
Expand Down
5 changes: 5 additions & 0 deletions MisakaTranslator-WPF/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ private void Item_ChatGPTTrans_Selected(object sender, RoutedEventArgs e)
this.SettingFrame.Navigate(new Uri("SettingsPages/TranslatorPages/ChatGPTTransSettingsPage.xaml", UriKind.Relative));
}

private void Item_AzureOpenAITrans_Selected(object sender, RoutedEventArgs e)
{
this.SettingFrame.Navigate(new Uri("SettingsPages/TranslatorPages/AzureOpenAITransSettingsPage.xaml", UriKind.Relative));
}

private void Item_FYJTrans_Selected(object sender, RoutedEventArgs e)
{
this.SettingFrame.Navigate(new Uri("SettingsPages/TranslatorPages/TencentFYJTransSettingsPage.xaml", UriKind.Relative));
Expand Down
4 changes: 4 additions & 0 deletions MisakaTranslator-WPF/TranslateWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ public static ITranslator TranslatorAuto(string translator)
ChatGPTTranslator chatgpt = new ChatGPTTranslator();
chatgpt.TranslatorInit(Common.appSettings.ChatGPTapiKey, Common.appSettings.ChatGPTapiUrl);
return chatgpt;
case nameof(AzureOpenAITranslator):
AzureOpenAITranslator azureOpenAI = new AzureOpenAITranslator();
azureOpenAI.TranslatorInit(Common.appSettings.AzureOpenAIApiKey, Common.appSettings.AzureOpenAIApiUrl);
return azureOpenAI;
case "ArtificialTranslator":
ArtificialTranslator at = new ArtificialTranslator();
at.TranslatorInit(Common.appSettings.ArtificialPatchPath);
Expand Down
5 changes: 5 additions & 0 deletions MisakaTranslator-WPF/lang/en-US.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
<sys:String x:Key="SettingsMenu_DictSettings_xxgrz">小学馆日中 Shogakukan ja-zh dictionary</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_DeepL">DeepL</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_ChatGPT">ChatGPT</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_AzureOpenAI">Azure OpenAI</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_IBM">IBM</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_Yandex">Yandex</sys:String>

Expand Down Expand Up @@ -97,6 +98,10 @@
<sys:String x:Key="ChatGPTTransSettingsPage_secretKey">ChatGPT authentication key</sys:String>
<sys:String x:Key="ChatGPTTransSettingsPage_apiUrl">ChatGPT API URL</sys:String>

<sys:String x:Key="AzureOpenAITransSettingsPage_Introduce">Azure OpenAI (network required) provides AI Translat, basically consistent with ChatGPT</sys:String>
<sys:String x:Key="AzureOpenAITransSettingsPage_secretKey">Deployment Key</sys:String>
<sys:String x:Key="AzureOpenAITransSettingsPage_apiUrl">Deployment target URL</sys:String>


<sys:String x:Key="TencentOldTransSettingsPage_Introduce">Tencent TMT API (腾讯云翻译, network required) supports multilingual translation with better support for Japanese and Chinese. You need to apply for the API (on the website in Chinese), get access to the SecretId and SecretKey, and then fill in below before testing. If the API is tested as invalid, you can refer to official Tencent documentations with the error codes. Previous version of API is likely to incur charges, &quot;Check API quota&quot; will lead you to the dashboard of Tencent TMT.</sys:String>
<sys:String x:Key="TencentOldTransSettingsPage_SecretId">SecretId</sys:String>
Expand Down
5 changes: 5 additions & 0 deletions MisakaTranslator-WPF/lang/zh-CN.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<sys:String x:Key="SettingsMenu_DictSettings_xxgrz">小学馆日中</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_DeepL">DeepL</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_ChatGPT">ChatGPT</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_AzureOpenAI">Azure OpenAI</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_IBM">IBM</sys:String>
<sys:String x:Key="SettingsMenu_TranslatorSettings_Yandex">Yandex</sys:String>

Expand Down Expand Up @@ -95,6 +96,10 @@
<sys:String x:Key="ChatGPTTransSettingsPage_secretKey">ChatGPT API 密钥</sys:String>
<sys:String x:Key="ChatGPTTransSettingsPage_apiUrl">ChatGPT API URL</sys:String>

<sys:String x:Key="AzureOpenAITransSettingsPage_Introduce">Azure OpenAI人工智能翻译,基本和ChatGPT一致</sys:String>
<sys:String x:Key="AzureOpenAITransSettingsPage_secretKey">Deployment 密钥</sys:String>
<sys:String x:Key="AzureOpenAITransSettingsPage_apiUrl">Deployment 目标URL</sys:String>

<sys:String x:Key="TencentOldTransSettingsPage_Introduce">腾讯翻译(私人)API支持多种语言互译,对日语、汉语的Galgame互译支持较好,需要联网使用;您需要先申请该API并创建一个密钥,获取SecretId和SecretKey后填入下方相应位置并进行测试认证;如果认证出现错误,请根据错误码至腾讯翻译API官方文档获取解决办法;腾讯旧版翻译API部分版本可能会产生某些费用,您可以使用额度查询按钮进入控制台确认。</sys:String>
<sys:String x:Key="TencentOldTransSettingsPage_SecretId">腾讯云API SecretId</sys:String>
<sys:String x:Key="TencentOldTransSettingsPage_SecretKey">腾讯云API SecretKey</sys:String>
Expand Down
Loading

0 comments on commit d2d7841

Please sign in to comment.