-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 使用 Azure OpenAI 进行翻译 * add: add warning message if the output was filtered
- Loading branch information
Showing
11 changed files
with
402 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
MisakaTranslator-WPF/SettingsPages/TranslatorPages/AzureOpenAITransSettingsPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
69 changes: 69 additions & 0 deletions
69
MisakaTranslator-WPF/SettingsPages/TranslatorPages/AzureOpenAITransSettingsPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.