Skip to content

Commit

Permalink
Merge pull request #43 from microsoft/bsonnino/fix-generated-crash
Browse files Browse the repository at this point in the history
Fix crash when running and exiting the generated project
  • Loading branch information
nmetulev authored Dec 13, 2024
2 parents 3d80e46 + bf1b176 commit 8e28945
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
8 changes: 8 additions & 0 deletions AIDevGallery/ProjectGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,11 @@ private async Task<string> AddFilesFromSampleAsync(
}
}

if (sample.SharedCode.Contains(SharedCodeEnum.GenAIModel))
{
cleanCsSource = RegexInitializeComponent().Replace(cleanCsSource, $"$1this.InitializeComponent();$1GenAIModel.InitializeGenAI();");
}

await File.WriteAllTextAsync(Path.Join(outputPath, $"{className}.xaml.cs"), cleanCsSource, cancellationToken);
}

Expand All @@ -569,6 +574,9 @@ private async Task<string> AddFilesFromSampleAsync(
[GeneratedRegex(@"[\r\n][\s]*return Task.CompletedTask;")]
private static partial Regex RegexReturnTaskCompletedTask();

[GeneratedRegex(@"(\s*)this.InitializeComponent\(\);")]
private static partial Regex RegexInitializeComponent();

private string CleanXamlSource(string xamlCode, string newNamespace, out string className)
{
var match = XClass().Match(xamlCode);
Expand Down
7 changes: 7 additions & 0 deletions AIDevGallery/Samples/SharedCode/GenAIModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ internal class GenAIModel : IChatClient, IDisposable
private Tokenizer? _tokenizer;
private LlmPromptTemplate? _template;
private static readonly SemaphoreSlim _createSemaphore = new(1, 1);
private static OgaHandle? _ogaHandle;

public static ChatOptions GetDefaultChatOptions()
{
Expand Down Expand Up @@ -84,6 +85,11 @@ private GenAIModel(string modelDir)
return model;
}

public static void InitializeGenAI()
{
_ogaHandle = new OgaHandle();
}

[MemberNotNullWhen(true, nameof(_model), nameof(_tokenizer))]
public bool IsReady => _model != null && _tokenizer != null;

Expand All @@ -93,6 +99,7 @@ public void Dispose()
{
_model?.Dispose();
_tokenizer?.Dispose();
_ogaHandle?.Dispose();
}

private string GetPrompt(IEnumerable<ChatMessage> history)
Expand Down
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
<PackageVersion Include="CommunityToolkit.WinUI.Converters" Version="8.1.240916" />
<PackageVersion Include="CommunityToolkit.WinUI.UI.Controls.Markdown" Version="7.1.2" />
<PackageVersion Include="Microsoft.Build" Version="17.12.6" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.20.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime.DirectML" Version="1.20.1" />
<PackageVersion Include="Microsoft.ML.OnnxRuntime.Extensions" Version="0.13.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.5.0" />
<PackageVersion Include="Microsoft.ML.OnnxRuntimeGenAI.DirectML" Version="0.5.2" />
<PackageVersion Include="Microsoft.WindowsAppSDK" Version="1.6.241114003" />
<PackageVersion Include="CommunityToolkit.WinUI.Animations" Version="8.1.240916" />
<PackageVersion Include="CommunityToolkit.WinUI.Extensions" Version="8.1.240916" />
Expand Down

0 comments on commit 8e28945

Please sign in to comment.