Skip to content

Commit

Permalink
Merge pull request #3 from microsoft/nmetulev/fixes
Browse files Browse the repository at this point in the history
Small fixes all around
  • Loading branch information
nmetulev authored Nov 18, 2024
2 parents 0c00a61 + a547cc0 commit 917dd7f
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 51 deletions.
55 changes: 20 additions & 35 deletions AIDevGallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,48 +122,33 @@ private void GenerateSearchIndex()
foreach (Scenario scenario in category.Scenarios)
{
SearchIndex.Add(new SearchResult() { Label = scenario.Name, Icon = scenario.Icon!, Description = scenario.Description!, Tag = scenario });

// TODO
/*
foreach (ScenarioSample sample in scenario.Samples)
{
if (sample.ModelDetails != null)
{
if (!SearchIndex.Any(sr => sr.Label == sample.ModelDetails.Name))
{
if (sample.ModelDetails.Parent != null)
{
SearchIndex.Add(new SearchResult() { Label = sample.ModelDetails.Name, Description = sample.ModelDetails.Description, Tag = sample.ModelDetails.Parent });
}
}
}
}
*/
}
}

foreach (var sample in SampleDetails.Samples)
{
AddModelTypes(sample.Model1Types);
AddModelTypes(sample.Model2Types);
}
List<ModelType> rootModels = [.. ModelTypeHelpers.ModelGroupDetails.Keys];
rootModels.AddRange(ModelTypeHelpers.ModelFamilyDetails.Keys);

static void AddModelTypes(List<ModelType>? modelTypes)
foreach (var key in rootModels)
{
if (modelTypes == null)
if (ModelTypeHelpers.ParentMapping.TryGetValue(key, out List<ModelType>? innerItems))
{
return;
}

foreach (var modelType in modelTypes)
{
if (ModelTypeHelpers.ModelDetails.TryGetValue(modelType, out var modelDetails))
{
SearchIndex.Add(new SearchResult() { Label = modelDetails.Name, Description = modelDetails.Description, Tag = modelType });
}
else if (ModelTypeHelpers.ModelFamilyDetails.TryGetValue(modelType, out var modelFamily))
if (innerItems?.Count > 0)
{
SearchIndex.Add(new SearchResult() { Label = modelFamily.Name, Description = modelFamily.Description, Tag = modelType });
foreach (var childNavigationItem in innerItems)
{
if (ModelTypeHelpers.ModelGroupDetails.TryGetValue(childNavigationItem, out var modelGroup))
{
SearchIndex.Add(new SearchResult() { Label = modelGroup.Name, Icon = modelGroup.Icon, Description = modelGroup.Name!, Tag = childNavigationItem });
}
else if (ModelTypeHelpers.ModelFamilyDetails.TryGetValue(childNavigationItem, out var modelFamily))
{
SearchIndex.Add(new SearchResult() { Label = modelFamily.Name, Description = modelFamily.Description, Tag = childNavigationItem });
}
else if (ModelTypeHelpers.ApiDefinitionDetails.TryGetValue(childNavigationItem, out var apiDefinition))
{
SearchIndex.Add(new SearchResult() { Label = apiDefinition.Name, Icon = apiDefinition.Icon, Description = apiDefinition.Name!, Tag = childNavigationItem });
}
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions AIDevGallery/Controls/DownloadProgressList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
VerticalAlignment="Center"
AutomationProperties.Name="View model page"
Click="GoToModelPageClicked"
Tag="{x:Bind}"
Content="{ui:FontIcon Glyph=&#xE736;,
FontSize=14}"
Style="{StaticResource SubtleButtonStyle}"
Expand Down
2 changes: 1 addition & 1 deletion AIDevGallery/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void Navigate(string Tag, object? obj = null)
_ = Launcher.LaunchUriAsync(new Uri("https://aka.ms/ai-dev-gallery"));
break;
case "settings":
Navigate(typeof(SettingsPage));
Navigate(typeof(SettingsPage), obj);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion AIDevGallery/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Identity
Name="e7af07c0-77d2-43e5-ab82-9cdb9daa11b3"
Publisher="CN=nikol"
Version="1.0.0.0" />
Version="0.0.1.0" />

<mp:PhoneIdentity PhoneProductId="e7af07c0-77d2-43e5-ab82-9cdb9daa11b3" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

Expand Down
10 changes: 10 additions & 0 deletions AIDevGallery/Pages/ModelSelectionPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,15 @@
<NavigationView.Content>
<Frame x:Name="NavFrame" />
</NavigationView.Content>
<NavigationView.PaneFooter>
<StackPanel Orientation="Vertical">
<HyperlinkButton
Grid.Row="1"
Margin="0,4,0,0"
HorizontalAlignment="Right"
Click="ManageModelsClicked"
Content="Manage models" />
</StackPanel>
</NavigationView.PaneFooter>
</NavigationView>
</Page>
5 changes: 5 additions & 0 deletions AIDevGallery/Pages/ModelSelectionPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,10 @@ private void AddModelClicked(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
NavView.SelectedItem = null;
NavFrame.Navigate(typeof(AddModelPage));
}

private void ManageModelsClicked(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
App.MainWindow.Navigate("settings", "ModelManagement");
}
}
}
2 changes: 1 addition & 1 deletion AIDevGallery/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private async void ClearCache_Click(object sender, RoutedEventArgs e)

if (result == ContentDialogResult.Primary)
{
await App.ModelCache.DeleteCache();
await App.ModelCache.ClearCache();
GetStorageInfo();
}
}
Expand Down
2 changes: 1 addition & 1 deletion AIDevGallery/Utils/AppData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task AddMru(MostRecentlyUsedItem item, string? modelOrApiId = null)
private static AppData GetDefault()
{
var homeDirPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var cacheDir = Path.Combine(homeDirPath, ".cache", "winai");
var cacheDir = Path.Combine(homeDirPath, ".cache", "aigallery");

return new AppData
{
Expand Down
4 changes: 2 additions & 2 deletions AIDevGallery/Utils/ModelCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,13 @@ public async Task DeleteModelFromCache(CachedModel model)
}
}

public async Task DeleteCache()
public async Task ClearCache()
{
ModelCacheDeletedEvent.Log();
await CacheStore.DeleteCache();

var cacheDir = GetCacheFolder();
Directory.Delete(cacheDir, true);
await CacheStore.ClearAsync();
}

public async Task MoveCache(string path, CancellationToken ct)
Expand Down
44 changes: 34 additions & 10 deletions AIDevGallery/Utils/ModelCacheStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ private ModelCacheStore(string cacheDir, List<CachedModel>? models)

public static async Task<ModelCacheStore> CreateForApp(string cacheDir, List<CachedModel>? models = null)
{
ModelCacheStore? modelCacheStore = null;

try
{
if (models == null)
Expand All @@ -39,28 +41,35 @@ public static async Task<ModelCacheStore> CreateForApp(string cacheDir, List<Cac
{
var json = await File.ReadAllTextAsync(cacheFile);

return new ModelCacheStore(cacheDir, JsonSerializer.Deserialize(json, AppDataSourceGenerationContext.Default.ListCachedModel));
modelCacheStore = new ModelCacheStore(cacheDir, JsonSerializer.Deserialize(json, AppDataSourceGenerationContext.Default.ListCachedModel));
}
}
else
{
ModelCacheStore modelCacheStore = new(cacheDir, models);
await modelCacheStore.Save();
return modelCacheStore;
modelCacheStore = new(cacheDir, models);
}
}
catch
{
}

return new ModelCacheStore(cacheDir, null);
modelCacheStore ??= new ModelCacheStore(cacheDir, null);
await modelCacheStore.ValidateAndSaveAsync();

return modelCacheStore;
}

private async Task Save()
private async Task SaveAsync()
{
var cacheFile = Path.Combine(CacheDir, "cache.json");

var str = JsonSerializer.Serialize(_models, AppDataSourceGenerationContext.Default.ListCachedModel);

if (!Path.Exists(CacheDir))
{
Directory.CreateDirectory(CacheDir);
}

await File.WriteAllTextAsync(cacheFile, str);
}

Expand All @@ -76,21 +85,36 @@ public async Task AddModel(CachedModel model)

ModelsChanged?.Invoke(this);

await Save();
await SaveAsync();
}

public async Task RemoveModel(CachedModel model)
{
_models.Remove(model);
ModelsChanged?.Invoke(this);
await Save();
await SaveAsync();
}

public async Task DeleteCache()
public async Task ClearAsync()
{
_models.Clear();
ModelsChanged?.Invoke(this);
await Save();
await SaveAsync();
}

private async Task ValidateAndSaveAsync()
{
List<CachedModel> models = [.. _models];

foreach (var cachedModel in models)
{
if (!Path.Exists(cachedModel.Path))
{
_models.Remove(cachedModel);
}
}

await SaveAsync();
}
}
}

0 comments on commit 917dd7f

Please sign in to comment.