Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to how WCR APIs show up in the app #194

Merged
merged 20 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AIDevGallery.SourceGenerator/Models/HardwareAccelerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ internal enum HardwareAccelerator
{
CPU,
DML,
QNN
QNN,
WCRAPI
nmetulev marked this conversation as resolved.
Show resolved Hide resolved
}
39 changes: 33 additions & 6 deletions AIDevGallery.SourceGenerator/SamplesSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void ExecuteSharedCodeEnumGeneration(SourceProductionContext context, Im
{
var filePath = type!.Locations[0].SourceTree?.FilePath;

if (filePath != null)
if (filePath != null && !filePath.Contains(@"\obj\"))
{
if (!filePaths.Contains(filePath))
{
Expand All @@ -66,7 +66,15 @@ private void ExecuteSharedCodeEnumGeneration(SourceProductionContext context, Im
foreach (var filePath in filePaths)
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
if (File.Exists(Path.ChangeExtension(filePath, ".xaml")))
var extension = Path.GetExtension(filePath);
var filePathWithoutExtension = filePath.Substring(0, filePath.Length - extension.Length);
if (fileName.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase) && File.Exists(filePathWithoutExtension))
{
fileName = Path.GetFileNameWithoutExtension(filePathWithoutExtension);
sourceBuilder.AppendLine($" {fileName}Cs,");
sourceBuilder.AppendLine($" {fileName}Xaml,");
}
else if (File.Exists(Path.ChangeExtension(filePath, ".xaml")))
{
sourceBuilder.AppendLine($" {fileName}Cs,");
nmetulev marked this conversation as resolved.
Show resolved Hide resolved
sourceBuilder.AppendLine($" {fileName}Xaml,");
Expand All @@ -89,14 +97,22 @@ private void ExecuteSharedCodeEnumGeneration(SourceProductionContext context, Im
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
var filePathXaml = Path.ChangeExtension(filePath, ".xaml");
if (File.Exists(filePathXaml))
var extension = Path.GetExtension(filePath);
var filePathWithoutExtension = filePath.Substring(0, filePath.Length - extension.Length);
if (fileName.EndsWith(".xaml", StringComparison.InvariantCultureIgnoreCase) && File.Exists(filePathWithoutExtension))
{
fileName = Path.GetFileNameWithoutExtension(fileName);
sourceBuilder.AppendLine($" SharedCodeEnum.{fileName}Cs => \"{fileName}.xaml.cs\",");
sourceBuilder.AppendLine($" SharedCodeEnum.{fileName}Xaml => \"{fileName}.xaml\",");
}
else if (File.Exists(filePathXaml))
{
sourceBuilder.AppendLine($" SharedCodeEnum.{Path.GetFileNameWithoutExtension(filePath)}Xaml => \"{Path.GetFileName(filePathXaml)}\",");
sourceBuilder.AppendLine($" SharedCodeEnum.{Path.GetFileNameWithoutExtension(filePath)}Cs => \"{Path.GetFileName(filePath)}\",");
sourceBuilder.AppendLine($" SharedCodeEnum.{fileName}Xaml => \"{Path.GetFileName(filePathXaml)}\",");
sourceBuilder.AppendLine($" SharedCodeEnum.{fileName}Cs => \"{Path.GetFileName(filePath)}\",");
}
else
{
sourceBuilder.AppendLine($" SharedCodeEnum.{Path.GetFileNameWithoutExtension(filePath)} => \"{Path.GetFileName(filePath)}\",");
sourceBuilder.AppendLine($" SharedCodeEnum.{fileName} => \"{Path.GetFileName(filePath)}\",");
}
}

Expand All @@ -111,6 +127,17 @@ private void ExecuteSharedCodeEnumGeneration(SourceProductionContext context, Im
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
var filePathXaml = Path.ChangeExtension(filePath, ".xaml");
var extension = Path.GetExtension(filePath);
var filePathWithoutExtension = filePath.Substring(0, filePath.Length - extension.Length);

// handle .xaml.cs files
if (File.Exists(filePathWithoutExtension))
{
filePathXaml = filePathWithoutExtension;
fileName = Path.GetFileNameWithoutExtension(fileName);
}


if (File.Exists(filePathXaml))
{
var fileContentXaml = XamlSourceCleanUp(File.ReadAllText(filePathXaml));
Expand Down
22 changes: 19 additions & 3 deletions AIDevGallery/Controls/ModelSelectionControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
ToolTipService.ToolTip="More info">
<Button.Flyout>
<Flyout ShouldConstrainToRootBounds="False">
<TextBlock Text="{x:Bind utils:AppUtils.GetHardwareAcceleratorDescription((models:HardwareAccelerator))}" TextWrapping="Wrap" />
<TextBlock Text="{x:Bind utils:AppUtils.GetHardwareAcceleratorDescription((models:HardwareAccelerator))}" TextWrapping="Wrap" MaxWidth="360" />
</Flyout>
</Button.Flyout>
</Button>
Expand Down Expand Up @@ -170,11 +170,19 @@
Click="ModelCard_Click"
Icon="{ui:FontIcon Glyph=&#xE8A5;}"
Tag="{x:Bind ModelDetails}"
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource NotZeroToVisibilityConverter}}"
Text="View model card" />
<MenuFlyoutItem
Click="ApiDocumentation_Click"
Icon="{ui:FontIcon Glyph=&#xE8A5;}"
Tag="{x:Bind ModelDetails}"
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource ZeroToVisibilityConverter}}"
Text="View API documentation" />
<MenuFlyoutItem
Click="ViewLicense_Click"
Icon="{ui:FontIcon Glyph=&#xE82D;}"
Tag="{x:Bind ModelDetails}"
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource NotZeroToVisibilityConverter}}"
Text="View license" />
<MenuFlyoutSeparator
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource NotZeroToVisibilityConverter}}"/>
Expand Down Expand Up @@ -265,7 +273,7 @@
ToolTipService.ToolTip="More info">
<Button.Flyout>
<Flyout ShouldConstrainToRootBounds="False">
<TextBlock Text="{x:Bind utils:AppUtils.GetHardwareAcceleratorDescription((models:HardwareAccelerator))}" TextWrapping="Wrap" />
<TextBlock Text="{x:Bind utils:AppUtils.GetHardwareAcceleratorDescription((models:HardwareAccelerator))}" TextWrapping="Wrap" MaxWidth="360" />
</Flyout>
</Button.Flyout>
</Button>
Expand Down Expand Up @@ -434,7 +442,7 @@
ToolTipService.ToolTip="More info">
<Button.Flyout>
<Flyout ShouldConstrainToRootBounds="False">
<TextBlock Text="{x:Bind utils:AppUtils.GetHardwareAcceleratorDescription((models:HardwareAccelerator))}" TextWrapping="Wrap" />
<TextBlock Text="{x:Bind utils:AppUtils.GetHardwareAcceleratorDescription((models:HardwareAccelerator))}" TextWrapping="Wrap" MaxWidth="360" />
</Flyout>
</Button.Flyout>
</Button>
Expand Down Expand Up @@ -528,11 +536,19 @@
Click="ModelCard_Click"
Icon="{ui:FontIcon Glyph=&#xE8A5;}"
Tag="{x:Bind ModelDetails}"
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource NotZeroToVisibilityConverter}}"
Text="View model card" />
<MenuFlyoutItem
Click="ApiDocumentation_Click"
Icon="{ui:FontIcon Glyph=&#xE8A5;}"
Tag="{x:Bind ModelDetails}"
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource ZeroToVisibilityConverter}}"
Text="View API documentation" />
<MenuFlyoutItem
Click="ViewLicense_Click"
Icon="{ui:FontIcon Glyph=&#xE82D;}"
Tag="{x:Bind ModelDetails}"
Visibility="{x:Bind ModelDetails.Size, Converter={StaticResource NotZeroToVisibilityConverter}}"
Text="View license" />
</MenuFlyout>
</Button.Flyout>
Expand Down
25 changes: 23 additions & 2 deletions AIDevGallery/Controls/ModelSelectionControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,22 @@ private void PopulateModelDetailsLists()

if (modelDetails.Compatibility.CompatibilityState == ModelCompatibilityState.Compatible)
{
AvailableModels.Add(new AvailableModel(modelDetails));
if (modelDetails.HardwareAccelerators.Contains(HardwareAccelerator.WCRAPI))
{
// insert APIs on top
AvailableModels.Insert(0, new AvailableModel(modelDetails));
}
else
{
AvailableModels.Add(new AvailableModel(modelDetails));
}
}
else
{
if (model.Size == 0)
{
UnavailableModels.Add(new BaseModel(modelDetails));
// insert APIs on top
UnavailableModels.Insert(0, new BaseModel(modelDetails));
}
else
{
Expand Down Expand Up @@ -352,6 +361,18 @@ private void ModelCard_Click(object sender, RoutedEventArgs e)
}
}

private void ApiDocumentation_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyoutItem btn && btn.Tag is ModelDetails details)
{
// we are in the sample view, open in app modelcard
if (ModelCardVisibility == Visibility.Visible)
{
App.MainWindow.Navigate("apis", details);
}
}
}

private void CopyModelPath_Click(object sender, RoutedEventArgs e)
{
if (sender is MenuFlyoutItem btn && btn.Tag is ModelDetails details)
Expand Down
2 changes: 1 addition & 1 deletion AIDevGallery/Helpers/ModelDetailsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static ModelDetails GetModelDetailsFromApiDefinition(ModelType modelType,
Id = apiDefinition.Id,
Icon = apiDefinition.Icon,
Name = apiDefinition.Name,
HardwareAccelerators = [HardwareAccelerator.QNN],
HardwareAccelerators = [HardwareAccelerator.WCRAPI],
IsUserAdded = false,
SupportedOnQualcomm = true,
ReadmeUrl = apiDefinition.ReadmeUrl,
Expand Down
2 changes: 1 addition & 1 deletion AIDevGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void Navigate(Type page, object? param = null)
if (page == typeof(APISelectionPage) && NavFrame.Content is APISelectionPage apiPage && param != null)
{
// No need to navigate to the APISelectionPage again, we just want to navigate to the right subpage
apiPage.SetSelectedAPIInMenu((ModelType)param);
apiPage.SetSelectedApiInMenu((ModelType)param);
}
else
{
Expand Down
19 changes: 17 additions & 2 deletions AIDevGallery/Models/ModelCompatibility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using AIDevGallery.Utils;
using System;
using System.Linq;

namespace AIDevGallery.Models;

Expand All @@ -20,9 +21,23 @@ private ModelCompatibility()
public static ModelCompatibility GetModelCompatibility(ModelDetails modelDetails)
{
string description = string.Empty;

ModelCompatibilityState compatibility;
if (modelDetails.HardwareAccelerators.Contains(HardwareAccelerator.CPU) ||

// check if WCR API
if (modelDetails.Url.StartsWith("file://", StringComparison.InvariantCultureIgnoreCase))
nmetulev marked this conversation as resolved.
Show resolved Hide resolved
{
if (Samples.ModelTypeHelpers.ApiDefinitionDetails.Any(md => md.Value.Id == modelDetails.Id) &&
WcrCompatibilityChecker.GetApiAvailability(Samples.ModelTypeHelpers.ApiDefinitionDetails.FirstOrDefault(md => md.Value.Id == modelDetails.Id).Key) != WcrApiAvailability.NotSupported)
{
compatibility = ModelCompatibilityState.Compatible;
}
else
{
compatibility = ModelCompatibilityState.NotCompatible;
description = "This Windows Copilot Runtime API requires a Copilot+ PC and a Windows 11 Insider Preview Build 26120.3073 (Dev and Beta Channels).";
}
}
else if (modelDetails.HardwareAccelerators.Contains(HardwareAccelerator.CPU) ||
(modelDetails.HardwareAccelerators.Contains(HardwareAccelerator.QNN) && DeviceUtils.IsArm64()))
{
compatibility = ModelCompatibilityState.Compatible;
Expand Down
3 changes: 2 additions & 1 deletion AIDevGallery/Models/Samples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ internal enum HardwareAccelerator
{
CPU,
DML,
QNN
QNN,
WCRAPI
}

#pragma warning restore SA1402 // File may only contain a single type
Expand Down
12 changes: 10 additions & 2 deletions AIDevGallery/Pages/APISelectionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using AIDevGallery.Telemetry.Events;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AIDevGallery.Pages;

Expand All @@ -25,7 +27,13 @@ protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter is ModelType type)
{
SetSelectedAPIInMenu(type);
SetSelectedApiInMenu(type);
}
else if (e.Parameter is ModelDetails details &&
details.Url.StartsWith("file://", StringComparison.InvariantCultureIgnoreCase) &&
nmetulev marked this conversation as resolved.
Show resolved Hide resolved
ModelTypeHelpers.ApiDefinitionDetails.Any(md => md.Value.Id == details.Id))
{
SetSelectedApiInMenu(ModelTypeHelpers.ApiDefinitionDetails.FirstOrDefault(md => md.Value.Id == details.Id).Key);
}
else
{
Expand Down Expand Up @@ -63,7 +71,7 @@ private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelec
}
}

public void SetSelectedAPIInMenu(ModelType selectedType)
public void SetSelectedApiInMenu(ModelType selectedType)
{
foreach (var item in NavView.MenuItems)
{
Expand Down
3 changes: 1 addition & 2 deletions AIDevGallery/Pages/ScenarioSelectionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ internal record FilterRecord(string? Tag, string Text);
new(null, "All" ),
new("npu", "NPU" ),
new("gpu", "GPU" ),

// new("wcr-api", "WCR API" )
new("wcr-api", "WCR API" )
];

private static LastInternalNavigation? lastInternalNavigation;
Expand Down
10 changes: 8 additions & 2 deletions AIDevGallery/ProjectGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,16 @@ private string CleanXamlSource(string xamlCode, string newNamespace, out string
if (match.Success)
{
var oldClassFullName = match.Groups[1].Value;
_ = oldClassFullName[..oldClassFullName.LastIndexOf('.')];
className = oldClassFullName[(oldClassFullName.LastIndexOf('.') + 1)..];

xamlCode = xamlCode.Replace(match.Value, @$"x:Class=""{newNamespace}.Sample""");
if (oldClassFullName.Contains(".SharedCode."))
{
xamlCode = xamlCode.Replace(match.Value, @$"x:Class=""{newNamespace}.{className}""");
nmetulev marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
xamlCode = xamlCode.Replace(match.Value, @$"x:Class=""{newNamespace}.Sample""");
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ internal enum HardwareAccelerator
{
CPU,
DML,
QNN
QNN,
WCRAPI
}
Loading