Skip to content

Commit

Permalink
Implement UI & Model download
Browse files Browse the repository at this point in the history
  • Loading branch information
sappho192 committed Jan 25, 2024
1 parent 44416d9 commit c43169f
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 20 deletions.
Binary file added onnx_project/dotnet/GuiExample/7z.dll
Binary file not shown.
12 changes: 12 additions & 0 deletions onnx_project/dotnet/GuiExample/GuiExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@
</PropertyGroup>

<ItemGroup>
<None Remove="7z.dll" />
</ItemGroup>

<ItemGroup>
<Content Include="7z.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Downloader" Version="3.0.6" />
<PackageReference Include="Squid-Box.SevenZipSharp" Version="1.6.1.23" />
<PackageReference Include="WPF-UI" Version="2.1.0" />
</ItemGroup>

Expand Down
41 changes: 30 additions & 11 deletions onnx_project/dotnet/GuiExample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
xmlns:local="clr-namespace:GuiExample"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
mc:Ignorable="d"
Title="GUI Example of FF14 Ja→Ko Translator" Height="400" Width="600">
Title="GUI Example of FF14 Ja→Ko Translator" Width="600" Height="500"
MinWidth="600" MinHeight="500">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="AUTO"/>
Expand All @@ -15,23 +16,37 @@
<Grid Grid.Row="0" Margin="4">
<StackPanel Orientation="Vertical">
<Expander Header="Model Configuration" IsExpanded="True" Margin="4">
<StackPanel Orientation="Horizontal">
<ui:Button Content="Download"/>
<Border Width="24"/>
<Button Content="Load Model"/>
<StackPanel Orientation="Vertical">
<ui:CardControl Header="Directory path" Margin="4">
<StackPanel Orientation="Horizontal">
<ui:TextBox Name="tbModelDirPath" Width="350"/>
<Border Width="4"/>
<ui:Button Name="btSearchModelDirPath" Click="btSearchModelDirPath_Click" Icon="Search24"/>
</StackPanel>
</ui:CardControl>
<ui:Card Margin="4">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Border Width="24"/>
<ui:Button Name="btLoadModel" Click="btLoadModel_Click" Content="Load Model"/>
<Border Width="24"/>
<ui:Button Name="btExtractModel" Click="btExtractModel_Click" Content="Extract"/>
<Border Width="24"/>
<ui:Button Name="btDownloadModel" Click="btDownloadModel_Click" Content="Download"/>
</StackPanel>
</ui:Card>
</StackPanel>
</Expander>
<ui:Card Margin="4">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Input 1"/>
<Button Name="btInputText1" Click="btInputText1_Click" Content="Example 1"/>
<Border Width="8"/>
<Button Content="Input 2"/>
<Button Name="btInputText2" Click="btInputText2_Click" Content="Example 2"/>
<Border Width="8"/>
<Button Content="Input 3"/>
<Button Name="btInputText3" Click="btInputText3_Click" Content="Example 3"/>
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Translate"/>
<Button Name="btTranslate" Click="btTranslate_Click" Content="Translate"/>
</StackPanel>
</Grid>
</ui:Card>
Expand All @@ -42,8 +57,12 @@
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBox DockPanel.Dock="Left" Text="src" Margin="4"/>
<TextBox Grid.Column="1" DockPanel.Dock="Right" Text="dst" Margin="4"/>
<ui:TextBox Name="tbSrcText" TextWrapping="Wrap" Grid.Column="0"
DockPanel.Dock="Left" Text="" Margin="4" PlaceholderText="Source text"/>
<ui:TextBox Name="tbDstText" IsReadOnly="True" TextWrapping="Wrap" Grid.Column="1"
DockPanel.Dock="Right" Text="" Margin="4" PlaceholderText="Translated text" />
</Grid>
<ui:Snackbar Grid.Row="1" Name="sbSnackbar" Appearance="Secondary" Timeout="2800">
</ui:Snackbar>
</Grid>
</Window>
194 changes: 185 additions & 9 deletions onnx_project/dotnet/GuiExample/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Text;
using Microsoft.Win32;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Downloader;
using System.Net;
using System.ComponentModel;
using System.IO;
using System.Security.Cryptography;
using SevenZip;

namespace GuiExample
{
Expand All @@ -16,9 +14,187 @@ namespace GuiExample
/// </summary>
public partial class MainWindow : Window
{
private readonly DownloadService downloader = new(new DownloadConfiguration
{
RequestConfiguration =
{
Proxy = WebRequest.GetSystemWebProxy()
}
});

public MainWindow()
{
InitializeComponent();
InitializeModelDirPath();
InitDownloader();
SevenZip.SevenZipBase.SetLibraryPath(
Path.Combine(Directory.GetCurrentDirectory(), "7z.dll"));
}

private void InitDownloader()
{
downloader.DownloadStarted += Downloader_DownloadStarted;
downloader.DownloadProgressChanged += Downloader_DownloadProgressChanged;
downloader.DownloadFileCompleted += Downloader_DownloadFileCompleted;
}

private void Downloader_DownloadFileCompleted(object? sender, AsyncCompletedEventArgs e)
{
Application.Current.Dispatcher.Invoke(() =>
{
btDownloadModel.IsEnabled = true;
sbSnackbar.Show("Download completed. Extracting the model file...");
});
ExtractModelArchive();
}

private void ExtractModelArchive()
{
string modelDirPath = "";
Application.Current.Dispatcher.Invoke(() =>
{
modelDirPath = tbModelDirPath.Text;
});

var filePath = $"{modelDirPath}\\onnx_model.7z";
var extractor = new SevenZipExtractor(filePath);
extractor.ExtractionFinished += (sender, args) =>
{
Application.Current.Dispatcher.Invoke(() =>
{
sbSnackbar.Show("Extracted the model file.");
});
};
extractor.ExtractArchive(tbModelDirPath.Text);

// 74cf3e47d445dc308130e425d88640d5
//using var md5 = MD5.Create();
//using var stream = File.OpenRead(filePath);
//var hash = md5.ComputeHash(stream);
//Console.WriteLine($"MD5 checksum: {BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant()}");
}

private void Downloader_DownloadProgressChanged(object? sender, Downloader.DownloadProgressChangedEventArgs e)
{
//Application.Current.Dispatcher.Invoke(() =>
//{
// lbDownloadProgress.Content = $"{e.ProgressPercentage:F2}%";
// lbSpeedProgress.Content = $"{e.AverageBytesPerSecondSpeed}B/s";
//});
}

private void Downloader_DownloadStarted(object? sender, DownloadStartedEventArgs e)
{
sbSnackbar.Show("Downloading the model file...");
}

private void InitializeModelDirPath()
{
// Get current absolute directory path
string currentDirPath = Directory.GetCurrentDirectory();
// Make Onnx model directory path
string onnxDirPath = Path.Combine(currentDirPath, "onnx");
tbModelDirPath.Text = onnxDirPath;
}

private void btLoadModel_Click(object sender, RoutedEventArgs e)
{
(var allFileExist, var message) = checkModelFiles();
if (!allFileExist)
{// "Please download the model first."
sbSnackbar.Show(message);
}
else
{
// Load model

sbSnackbar.Show("Model loaded.");
}
}

private async void btDownloadModel_Click(object sender, RoutedEventArgs e)
{
btDownloadModel.IsEnabled = false;

await DownloadModel();
}

private async Task DownloadModel()
{
var modelDirPath = tbModelDirPath.Text;
var url = "https://github.com/sappho192/ffxiv-ja-ko-translator/releases/download/0.2.1/onnx_model.7z";
// Get current directoryinfo
var directoryInfo = new DirectoryInfo(modelDirPath);
await downloader.DownloadFileTaskAsync(url, directoryInfo);
}

private void btInputText1_Click(object sender, RoutedEventArgs e)
{
tbSrcText.Text = "ギルガメッシュ討伐戦に行ってきます。一緒に行きましょうか?";
tbDstText.Text = "";
}

private void btInputText2_Click(object sender, RoutedEventArgs e)
{
tbSrcText.Text = "絶アルテマウェポン破壊作戦をクリアした事ありますか?";
tbDstText.Text = "";
}

private void btInputText3_Click(object sender, RoutedEventArgs e)
{
tbSrcText.Text = "美容師の呼び鈴を使って髪方を変えますよ。";
tbDstText.Text = "";
}

private void btTranslate_Click(object sender, RoutedEventArgs e)
{
//sbSnackbar.Show();
}

private (bool, string) checkModelFiles()
{
// Check onnx directory
string onnxDirPath = tbModelDirPath.Text;
if (!Directory.Exists(onnxDirPath))
{
return (false, "The path doesn't exist.");
}
// Check onnx model files
string[] onnxModelFiles = [
"encoder_model.onnx",
"decoder_model_merged.onnx",
//"tokenizer.json",
//"vocab.txt"
];
foreach (string onnxModelFile in onnxModelFiles)
{
string onnxModelFilePath = Path.Combine(onnxDirPath, onnxModelFile);
if (!File.Exists(onnxModelFilePath))
{
return (false, $"Path doesn't exist: {onnxModelFilePath}");
}
}

return (true, string.Empty);
}

private void btSearchModelDirPath_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFolderDialog
{
Multiselect = false,
Title = "Select ONNX model directory",
InitialDirectory = tbModelDirPath.Text
};
if (dialog.ShowDialog() == true)
{
tbModelDirPath.Text = dialog.FolderName;
}
}

private void btExtractModel_Click(object sender, RoutedEventArgs e)
{
ExtractModelArchive();
}
}
}

0 comments on commit c43169f

Please sign in to comment.