Skip to content

Commit b8060d7

Browse files
committed
TextSummary example
1 parent 09c6bd6 commit b8060d7

20 files changed

+1444
-88
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<Application x:Class="TensorStack.Example.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:System="clr-namespace:System;assembly=mscorlib"
5+
xmlns:local="clr-namespace:TensorStack.Example"
6+
xmlns:CommonControls="clr-namespace:TensorStack.WPF.Controls;assembly=TensorStack.WPF"
7+
xmlns:TextGeneration="clr-namespace:TensorStack.TextGeneration.Common;assembly=TensorStack.TextGeneration"
8+
ShutdownMode="OnMainWindowClose">
9+
<Application.Resources>
10+
11+
12+
<ResourceDictionary>
13+
14+
<!--TensorStack.Example-->
15+
16+
<ObjectDataProvider x:Key="EarlyStopping" MethodName="GetValues" ObjectType="{x:Type System:Enum}">
17+
<ObjectDataProvider.MethodParameters>
18+
<x:Type TypeName="TextGeneration:EarlyStopping"/>
19+
</ObjectDataProvider.MethodParameters>
20+
</ObjectDataProvider>
21+
22+
23+
<Style x:Key="ImageDropZoneStyle" TargetType="{x:Type Border}">
24+
<Setter Property="AllowDrop" Value="False"/>
25+
<Setter Property="BorderBrush" Value="Transparent"/>
26+
<Setter Property="BorderThickness" Value="1"/>
27+
<Style.Triggers>
28+
<MultiDataTrigger>
29+
<MultiDataTrigger.Conditions>
30+
<Condition Binding="{Binding IsDragDrop, RelativeSource={RelativeSource AncestorType=CommonControls:ViewControl}}" Value="True" />
31+
<Condition Binding="{Binding DragDropType, RelativeSource={RelativeSource AncestorType=CommonControls:ViewControl}}" Value="Image" />
32+
</MultiDataTrigger.Conditions>
33+
<MultiDataTrigger.Setters>
34+
<Setter Property="AllowDrop" Value="True"/>
35+
<Setter Property="BorderBrush" Value="{StaticResource AccentColour2}"/>
36+
</MultiDataTrigger.Setters>
37+
</MultiDataTrigger>
38+
</Style.Triggers>
39+
</Style>
40+
41+
42+
<Style x:Key="VideoDropZoneStyle" TargetType="{x:Type Border}">
43+
<Setter Property="AllowDrop" Value="False"/>
44+
<Setter Property="BorderBrush" Value="Transparent"/>
45+
<Setter Property="BorderThickness" Value="1"/>
46+
<Style.Triggers>
47+
<MultiDataTrigger>
48+
<MultiDataTrigger.Conditions>
49+
<Condition Binding="{Binding IsDragDrop, RelativeSource={RelativeSource AncestorType=CommonControls:ViewControl}}" Value="True" />
50+
<Condition Binding="{Binding DragDropType, RelativeSource={RelativeSource AncestorType=CommonControls:ViewControl}}" Value="Video" />
51+
</MultiDataTrigger.Conditions>
52+
<MultiDataTrigger.Setters>
53+
<Setter Property="AllowDrop" Value="True"/>
54+
<Setter Property="BorderBrush" Value="{StaticResource AccentColour2}"/>
55+
</MultiDataTrigger.Setters>
56+
</MultiDataTrigger>
57+
</Style.Triggers>
58+
</Style>
59+
60+
<!--TensorStack.WPF-->
61+
<ResourceDictionary.MergedDictionaries>
62+
<ResourceDictionary Source="pack://application:,,,/TensorStack.WPF;component/ThemeDefault.xaml" />
63+
</ResourceDictionary.MergedDictionaries>
64+
65+
</ResourceDictionary>
66+
67+
68+
</Application.Resources>
69+
</Application>
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using System;
4+
using System.Threading.Tasks;
5+
using System.Windows;
6+
using System.Windows.Threading;
7+
using TensorStack.Example.Services;
8+
using TensorStack.WPF;
9+
10+
namespace TensorStack.Example
11+
{
12+
/// <summary>
13+
/// Interaction logic for App.xaml
14+
/// </summary>
15+
public partial class App : Application
16+
{
17+
private readonly IHost _appHost;
18+
19+
public App()
20+
{
21+
RegisterExceptionHandlers();
22+
23+
var builder = Host.CreateApplicationBuilder();
24+
25+
var configuration = Json.Load<Settings>("Settings.json");
26+
configuration.Initialize();
27+
28+
// Add WPFCommon
29+
builder.Services.AddWPFCommon<MainWindow, Settings>(configuration);
30+
31+
// builder.Services.AddSingleton<IMediaService, MediaService>();
32+
builder.Services.AddSingleton<ITextService, TextService>();
33+
34+
_appHost = builder.Build();
35+
36+
// Initialize WPFCommon
37+
_appHost.Services.UseWPFCommon();
38+
}
39+
40+
41+
/// <summary>
42+
/// Application startup.
43+
/// </summary>
44+
/// <returns>Task.</returns>
45+
private Task AppStartup()
46+
{
47+
MainWindow = _appHost.Services.GetMainWindow();
48+
MainWindow.Show();
49+
return Task.CompletedTask;
50+
}
51+
52+
53+
/// <summary>
54+
/// Application shutdown.
55+
/// </summary>
56+
private async Task AppShutdown()
57+
{
58+
using (_appHost)
59+
{
60+
await _appHost.StopAsync();
61+
DeregisterExceptionHandlers();
62+
}
63+
}
64+
65+
66+
/// <summary>
67+
/// Raises the <see cref="E:System.Windows.Application.Startup" /> event.
68+
/// </summary>
69+
/// <param name="e">A <see cref="T:System.Windows.StartupEventArgs" /> that contains the event data.</param>
70+
protected override async void OnStartup(StartupEventArgs e)
71+
{
72+
await AppStartup();
73+
base.OnStartup(e);
74+
}
75+
76+
77+
/// <summary>
78+
/// Raises the <see cref="E:System.Windows.Application.SessionEnding" /> event.
79+
/// </summary>
80+
/// <param name="e">A <see cref="T:System.Windows.SessionEndingCancelEventArgs" /> that contains the event data.</param>
81+
protected override async void OnSessionEnding(SessionEndingCancelEventArgs e)
82+
{
83+
await AppShutdown();
84+
base.OnSessionEnding(e);
85+
}
86+
87+
88+
/// <summary>
89+
/// Raises the <see cref="E:System.Windows.Application.Exit" /> event.
90+
/// </summary>
91+
/// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param>
92+
protected async override void OnExit(ExitEventArgs e)
93+
{
94+
await AppShutdown();
95+
base.OnExit(e);
96+
}
97+
98+
99+
/// <summary>
100+
/// Registers the exception handlers.
101+
/// </summary>
102+
private void RegisterExceptionHandlers()
103+
{
104+
DispatcherUnhandledException += OnDispatcherException;
105+
AppDomain.CurrentDomain.UnhandledException += OnAppDomainException;
106+
TaskScheduler.UnobservedTaskException += OnTaskSchedulerException;
107+
}
108+
109+
110+
/// <summary>
111+
/// Deregisters the exception handlers.
112+
/// </summary>
113+
private void DeregisterExceptionHandlers()
114+
{
115+
DispatcherUnhandledException -= OnDispatcherException;
116+
AppDomain.CurrentDomain.UnhandledException -= OnAppDomainException;
117+
TaskScheduler.UnobservedTaskException -= OnTaskSchedulerException;
118+
}
119+
120+
121+
/// <summary>
122+
/// Handles the <see cref="E:DispatcherException" /> event.
123+
/// </summary>
124+
/// <param name="sender">The sender.</param>
125+
/// <param name="e">The <see cref="DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
126+
private void OnDispatcherException(object sender, DispatcherUnhandledExceptionEventArgs e)
127+
{
128+
ShowExceptionMessage(e.Exception);
129+
130+
// Prevent application from crashing
131+
e.Handled = true;
132+
}
133+
134+
135+
/// <summary>
136+
/// Handles the <see cref="E:AppDomainException" /> event.
137+
/// </summary>
138+
/// <param name="sender">The sender.</param>
139+
/// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param>
140+
private void OnAppDomainException(object sender, UnhandledExceptionEventArgs e)
141+
{
142+
if (e.ExceptionObject is Exception ex)
143+
{
144+
ShowExceptionMessage(ex);
145+
}
146+
}
147+
148+
149+
/// <summary>
150+
/// Handles the <see cref="E:TaskSchedulerException" /> event.
151+
/// </summary>
152+
/// <param name="sender">The sender.</param>
153+
/// <param name="e">The <see cref="UnobservedTaskExceptionEventArgs"/> instance containing the event data.</param>
154+
private void OnTaskSchedulerException(object sender, UnobservedTaskExceptionEventArgs e)
155+
{
156+
ShowExceptionMessage(e.Exception);
157+
158+
// Prevent application from crashing
159+
e.SetObserved();
160+
}
161+
162+
163+
private void ShowExceptionMessage(Exception ex)
164+
{
165+
MessageBox.Show($"An unexpected error occurred:\n{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
166+
}
167+
}
168+
}
169+
170+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using TensorStack.TextGeneration.Common;
2+
3+
namespace TensorStack.Example.Common
4+
{
5+
public interface ITransformerRequest
6+
{
7+
int Beams { get; set; }
8+
int DiversityLength { get; set; }
9+
EarlyStopping EarlyStopping { get; set; }
10+
float LengthPenalty { get; set; }
11+
int MaxLength { get; set; }
12+
int MinLength { get; set; }
13+
int NoRepeatNgramSize { get; set; }
14+
string Prompt { get; set; }
15+
int Seed { get; set; }
16+
float Temperature { get; set; }
17+
int TopK { get; set; }
18+
float TopP { get; set; }
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using TensorStack.WPF;
2+
3+
namespace TensorStack.Example.Common
4+
{
5+
public class TextModel : BaseModel
6+
{
7+
public int Id { get; init; }
8+
public string Name { get; init; }
9+
public bool IsDefault { get; set; }
10+
public TextModelType Type { get; init; }
11+
public string Version { get; set; }
12+
public int MinLength { get; init; }
13+
public int MaxLength { get; init; }
14+
public string[] Prefixes { get; init; }
15+
public string Path { get; set; }
16+
public string[] UrlPaths { get; set; }
17+
}
18+
19+
20+
public enum TextModelType
21+
{
22+
Summary = 0,
23+
Phi3 = 1
24+
}
25+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System.IO;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
using System.Threading.Tasks;
5+
6+
namespace TensorStack.Example
7+
{
8+
public static class Json
9+
{
10+
public readonly static JsonSerializerOptions DefaultOptions;
11+
12+
static Json()
13+
{
14+
DefaultOptions = new JsonSerializerOptions
15+
{
16+
WriteIndented = true,
17+
Converters = { new JsonStringEnumConverter() }
18+
};
19+
}
20+
21+
22+
public static T Load<T>(string filePath) where T : class
23+
{
24+
try
25+
{
26+
using (var jsonReader = File.OpenRead(filePath))
27+
{
28+
return JsonSerializer.Deserialize<T>(jsonReader, DefaultOptions);
29+
}
30+
}
31+
catch (System.Exception)
32+
{
33+
return default;
34+
}
35+
36+
}
37+
38+
39+
public static async Task<T> LoadAsync<T>(string filePath) where T : class
40+
{
41+
try
42+
{
43+
using (var jsonReader = File.OpenRead(filePath))
44+
{
45+
return await JsonSerializer.DeserializeAsync<T>(jsonReader, DefaultOptions);
46+
}
47+
}
48+
catch (System.Exception)
49+
{
50+
return default;
51+
}
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)