From 18d667d9bc9a2e3ee819cebf99722008408cc954 Mon Sep 17 00:00:00 2001 From: Zachary Teutsch Date: Sat, 25 Jan 2025 14:22:51 -0800 Subject: [PATCH 1/5] Removed RAG progress indicator, added select new PDF button, refactored --- .../RetrievalAugmentedGeneration.xaml | 56 +-- .../RetrievalAugmentedGeneration.xaml.cs | 452 +++++++++--------- 2 files changed, 245 insertions(+), 263 deletions(-) diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml index 0773a4f0..d1ef1bb0 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml @@ -10,33 +10,23 @@ mc:Ignorable="d"> - - - - - - - - - - + + + + + - diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs index 999b140b..c9d6ee54 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs @@ -14,7 +14,6 @@ using Microsoft.UI.Xaml.Navigation; using System; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; @@ -68,13 +67,10 @@ public class PdfPageData { [VectorStoreRecordKey] public required int Key { get; init; } - [VectorStoreRecordData] public required uint Page { get; init; } - [VectorStoreRecordData] public required string Text { get; init; } - [VectorStoreRecordVector(384, DistanceFunction.CosineSimilarity)] public required ReadOnlyMemory Vector { get; init; } } @@ -82,10 +78,7 @@ public class PdfPageData public RetrievalAugmentedGeneration() { this.InitializeComponent(); - this.Unloaded += (s, e) => - { - CleanUp(); - }; + this.Unloaded += (s, e) => CleanUp(); this.Loaded += (s, e) => Page_Loaded(); // } @@ -128,146 +121,36 @@ private void CleanUp() _cts = null; } - private void Grid_Loaded(object sender, RoutedEventArgs e) - { - searchTextBoxInitialText = SearchTextBox.Text; - } - - private async Task UpdatePdfImageAsync() - { - if (_pdfFile == null || selectedPages == null || selectedPages.Count == 0) - { - return; - } - - var pdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(_pdfFile).AsTask().ConfigureAwait(false); - var pageId = selectedPages[selectedPageIndex]; - if (pageId < 0 || pdfDocument.PageCount < pageId) - { - return; - } - - var page = pdfDocument.GetPage(pageId - 1); - _inMemoryRandomAccessStream?.Dispose(); - _inMemoryRandomAccessStream = new(); - var rect = page.Dimensions.TrimBox; - await page.RenderToStreamAsync(_inMemoryRandomAccessStream).AsTask().ConfigureAwait(false); - - DispatcherQueue.TryEnqueue(() => - { - BitmapImage bitmapImage = new(); - bitmapImage.SetSource(_inMemoryRandomAccessStream); - - PdfImage.Source = bitmapImage; - PdfImageGrid.Visibility = Visibility.Visible; - UpdatePreviousAndNextPageButtonEnabled(); - }); - } - - private void UpdatePreviousAndNextPageButtonEnabled() + private async void IndexPDFButton_Click(object sender, RoutedEventArgs e) { - if (selectedPages == null || selectedPages.Count == 0) + if (_embeddings == null) { - PreviousPageButton.IsEnabled = false; - NextPageButton.IsEnabled = false; return; } - PreviousPageButton.IsEnabled = selectedPageIndex > 0; - NextPageButton.IsEnabled = selectedPageIndex < selectedPages.Count - 1; - } + ToSelectingState(); - private void SearchTextBox_GotFocus(object sender, RoutedEventArgs e) - { - if (SearchTextBox.Text == searchTextBoxInitialText) - { - SearchTextBox.Text = string.Empty; - } - } - - private void SearchTextBox_LostFocus(object sender, RoutedEventArgs e) - { - if (string.IsNullOrWhiteSpace(SearchTextBox.Text)) - { - SearchTextBox.Text = searchTextBoxInitialText; - } - } - - private async void ShowPDFPage_Click(object sender, RoutedEventArgs e) - { - await UpdatePdfImageAsync().ConfigureAwait(false); - } - - private void PdfImage_Tapped(object sender, TappedRoutedEventArgs e) - { - PdfImageGrid.Visibility = Visibility.Collapsed; - } - - private async void PreviousPageButton_Click(object sender, RoutedEventArgs e) - { - if (selectedPageIndex <= 0) + _pdfFile = await SelectPDFFromFileSystem(); + if (_pdfFile == null) { + ToSelectState(); return; } - selectedPageIndex--; - await UpdatePdfImageAsync().ConfigureAwait(false); + await IndexPDF(); } - private async void NextPageButton_Click(object sender, RoutedEventArgs e) + private async Task IndexPDF() { - if (selectedPages == null || selectedPageIndex >= selectedPages.Count - 1) + if (_pdfFile == null || _embeddings == null) { return; } - selectedPageIndex++; - await UpdatePdfImageAsync().ConfigureAwait(false); - } - - private void ClosePdfButton_Click(object sender, RoutedEventArgs e) - { - PdfImageGrid.Visibility = Visibility.Collapsed; - } - - private async void IndexPDFButton_Click(object sender, RoutedEventArgs e) - { - if (_embeddings == null) - { - return; - } - - IndexPDFButton.IsEnabled = false; - LoadPDFProgressRing.IsActive = true; - LoadPDFProgressRing.Visibility = Visibility.Visible; - PdfProblemTextBlock.Text = string.Empty; - IndexPDFText.Text = "Selecting PDF..."; - - var window = new Window(); - var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window); - - var picker = new FileOpenPicker(); - WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd); - - // Set the file type filter - picker.FileTypeFilter.Add(".pdf"); - - // Pick a file - _pdfFile = await picker.PickSingleFileAsync(); - if (_pdfFile == null) - { - IndexPDFButton.IsEnabled = true; - LoadPDFProgressRing.IsActive = false; - LoadPDFProgressRing.Visibility = Visibility.Collapsed; - IndexPDFProgressStackPanel.Visibility = Visibility.Collapsed; - return; - } - IndexPDFText.Text = "Indexing PDF..."; var contents = new List<(string Text, uint Page)>(); - // 1) Read the PDF file using (PdfDocument document = PdfDocument.Open(_pdfFile.Path)) { foreach (var page in document.GetPages()) @@ -286,10 +169,7 @@ private async void IndexPDFButton_Click(object sender, RoutedEventArgs e) if (contents.Count == 0) { - IndexPDFButton.IsEnabled = true; - LoadPDFProgressRing.IsActive = false; - LoadPDFProgressRing.Visibility = Visibility.Collapsed; - IndexPDFText.Text = "Select PDF"; + ToSelectState(); PdfProblemTextBlock.Text = "We weren't able to read this PDF. Please try another."; return; } @@ -305,26 +185,6 @@ private async void IndexPDFButton_Click(object sender, RoutedEventArgs e) contents = chunkedContents; - IndexPDFProgressBar.Minimum = 0; - IndexPDFProgressBar.Maximum = contents.Count; - IndexPDFProgressBar.Value = 0; - - Stopwatch sw = Stopwatch.StartNew(); - - void UpdateProgress(float progress) - { - var elapsed = sw.Elapsed; - if (progress == 0) - { - progress = 0.0001f; - } - - var remaining = TimeSpan.FromSeconds((long)(elapsed.TotalSeconds / progress * (1 - progress) / 5) * 5); - - LoadPDFProgressRing.Value = progress * contents.Count; - IndexPDFText.Text = $"Indexing PDF... {progress:P0} ({remaining})"; - } - if (_cts != null) { _cts.Cancel(); @@ -358,24 +218,12 @@ await _pdfPages.UpsertAsync( }, null, _cts.Token).ConfigureAwait(false); - DispatcherQueue.TryEnqueue(() => - { - UpdateProgress((float)i / total); - }); i++; } } catch (OperationCanceledException) { - DispatcherQueue.TryEnqueue(() => - { - IndexPDFButton.IsEnabled = true; - LoadPDFProgressRing.IsActive = false; - LoadPDFProgressRing.Visibility = Visibility.Collapsed; - IndexPDFProgressStackPanel.Visibility = Visibility.Collapsed; - IndexPDFText.Text = "Select PDF"; - }); - + DispatcherQueue.TryEnqueue(ToSelectState); return; } finally @@ -386,77 +234,12 @@ await _pdfPages.UpsertAsync( DispatcherQueue.TryEnqueue(() => { ShowPDFPage.IsEnabled = true; - IndexPDFText.Text = "Indexing PDF... Done!"; - IndexPDFProgressStackPanel.Visibility = Visibility.Collapsed; IndexPDFGrid.Visibility = Visibility.Collapsed; ChatGrid.Visibility = Visibility.Visible; + SelectNewPDFButton.Visibility = Visibility.Visible; }); } - private IEnumerable<(string Text, uint Page)> SplitInChunks((string Text, uint Page) input, int maxLength) - { - var sentences = input.Text.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - StringBuilder currentChunk = new(); - - foreach (var sentence in sentences) - { - if (sentence.Length > maxLength) - { - if (currentChunk.Length > 0) - { - yield return (currentChunk.ToString(), input.Page); - currentChunk.Clear(); - } - - var words = sentence.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - foreach (var word in words) - { - if (currentChunk.Length + word.Length + 1 > maxLength) - { - yield return (currentChunk.ToString(), input.Page); - currentChunk.Clear(); - } - - currentChunk.Append(word); - currentChunk.Append(' '); - } - - continue; - } - - if (currentChunk.Length + sentence.Length + 2 > maxLength) - { - yield return (currentChunk.ToString(), input.Page); - - currentChunk.Clear(); - } - - currentChunk.Append(sentence); - currentChunk.Append(". "); - } - - if (currentChunk.Length > 0) - { - yield return (currentChunk.ToString(), input.Page); - } - } - - private async void AskSLMButton_Click(object sender, RoutedEventArgs e) - { - if (SearchTextBox.Text.Length > 0) - { - await DoRAG(); - } - } - - private async void TextBox_KeyUp(object sender, KeyRoutedEventArgs e) - { - if (e.Key == Windows.System.VirtualKey.Enter && sender is TextBox && SearchTextBox.Text.Length > 0) - { - await DoRAG(); - } - } - private async Task DoRAG() { if (_embeddings == null || _chatClient == null || _pdfPages == null) @@ -535,4 +318,213 @@ await Task.Run( AskSLMButton.Content = "Answer"; SearchTextBox.IsEnabled = true; } + + private void Grid_Loaded(object sender, RoutedEventArgs e) + { + searchTextBoxInitialText = SearchTextBox.Text; + } + + private async Task UpdatePdfImageAsync() + { + if (_pdfFile == null || selectedPages == null || selectedPages.Count == 0) + { + return; + } + + var pdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(_pdfFile).AsTask().ConfigureAwait(false); + var pageId = selectedPages[selectedPageIndex]; + if (pageId < 0 || pdfDocument.PageCount < pageId) + { + return; + } + + var page = pdfDocument.GetPage(pageId - 1); + _inMemoryRandomAccessStream?.Dispose(); + _inMemoryRandomAccessStream = new(); + var rect = page.Dimensions.TrimBox; + await page.RenderToStreamAsync(_inMemoryRandomAccessStream).AsTask().ConfigureAwait(false); + + DispatcherQueue.TryEnqueue(() => + { + BitmapImage bitmapImage = new(); + bitmapImage.SetSource(_inMemoryRandomAccessStream); + + PdfImage.Source = bitmapImage; + PdfImageGrid.Visibility = Visibility.Visible; + UpdatePreviousAndNextPageButtonEnabled(); + }); + } + + private void UpdatePreviousAndNextPageButtonEnabled() + { + if (selectedPages == null || selectedPages.Count == 0) + { + PreviousPageButton.IsEnabled = false; + NextPageButton.IsEnabled = false; + return; + } + + PreviousPageButton.IsEnabled = selectedPageIndex > 0; + NextPageButton.IsEnabled = selectedPageIndex < selectedPages.Count - 1; + } + + private void SearchTextBox_GotFocus(object sender, RoutedEventArgs e) + { + if (SearchTextBox.Text == searchTextBoxInitialText) + { + SearchTextBox.Text = string.Empty; + } + } + + private void SearchTextBox_LostFocus(object sender, RoutedEventArgs e) + { + if (string.IsNullOrWhiteSpace(SearchTextBox.Text)) + { + SearchTextBox.Text = searchTextBoxInitialText; + } + } + + private async void ShowPDFPage_Click(object sender, RoutedEventArgs e) + { + await UpdatePdfImageAsync().ConfigureAwait(false); + } + + private void PdfImage_Tapped(object sender, TappedRoutedEventArgs e) + { + PdfImageGrid.Visibility = Visibility.Collapsed; + } + + private async void PreviousPageButton_Click(object sender, RoutedEventArgs e) + { + if (selectedPageIndex <= 0) + { + return; + } + + selectedPageIndex--; + await UpdatePdfImageAsync().ConfigureAwait(false); + } + + private async void NextPageButton_Click(object sender, RoutedEventArgs e) + { + if (selectedPages == null || selectedPageIndex >= selectedPages.Count - 1) + { + return; + } + + selectedPageIndex++; + await UpdatePdfImageAsync().ConfigureAwait(false); + } + + private void ClosePdfButton_Click(object sender, RoutedEventArgs e) + { + PdfImageGrid.Visibility = Visibility.Collapsed; + } + + private IEnumerable<(string Text, uint Page)> SplitInChunks((string Text, uint Page) input, int maxLength) + { + var sentences = input.Text.Split('.', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + StringBuilder currentChunk = new(); + + foreach (var sentence in sentences) + { + if (sentence.Length > maxLength) + { + if (currentChunk.Length > 0) + { + yield return (currentChunk.ToString(), input.Page); + currentChunk.Clear(); + } + + var words = sentence.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + foreach (var word in words) + { + if (currentChunk.Length + word.Length + 1 > maxLength) + { + yield return (currentChunk.ToString(), input.Page); + currentChunk.Clear(); + } + + currentChunk.Append(word); + currentChunk.Append(' '); + } + + continue; + } + + if (currentChunk.Length + sentence.Length + 2 > maxLength) + { + yield return (currentChunk.ToString(), input.Page); + + currentChunk.Clear(); + } + + currentChunk.Append(sentence); + currentChunk.Append(". "); + } + + if (currentChunk.Length > 0) + { + yield return (currentChunk.ToString(), input.Page); + } + } + + private void ToSelectState() + { + ShowPDFPage.IsEnabled = false; + SelectNewPDFButton.Visibility = Visibility.Collapsed; + IndexPDFGrid.Visibility = Visibility.Visible; + ChatGrid.Visibility = Visibility.Collapsed; + IndexPDFButton.IsEnabled = true; + LoadPDFProgressRing.IsActive = false; + LoadPDFProgressRing.Visibility = Visibility.Collapsed; + IndexPDFText.Text = "Select PDF"; + } + + private void ToSelectingState() + { + IndexPDFButton.IsEnabled = false; + LoadPDFProgressRing.IsActive = true; + LoadPDFProgressRing.Visibility = Visibility.Visible; + PdfProblemTextBlock.Text = string.Empty; + IndexPDFText.Text = "Selecting PDF..."; + } + + private async void AskSLMButton_Click(object sender, RoutedEventArgs e) + { + if (SearchTextBox.Text.Length > 0) + { + await DoRAG(); + } + } + + private async void TextBox_KeyUp(object sender, KeyRoutedEventArgs e) + { + if (e.Key == Windows.System.VirtualKey.Enter && sender is TextBox && SearchTextBox.Text.Length > 0) + { + await DoRAG(); + } + } + + private async Task SelectPDFFromFileSystem() + { + var window = new Window(); + var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window); + var picker = new FileOpenPicker(); + WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd); + picker.FileTypeFilter.Add(".pdf"); + return await picker.PickSingleFileAsync(); + } + + private async void SelectNewPDF_Click(object sender, RoutedEventArgs e) + { + StorageFile pdfFile = await SelectPDFFromFileSystem(); + if(pdfFile != null) + { + _pdfFile = pdfFile; + ToSelectState(); + ToSelectingState(); + await IndexPDF(); + } + } } \ No newline at end of file From 363c08bfc5c6f1bcbcf597fba3aa89e8bfca1a62 Mon Sep 17 00:00:00 2001 From: Zachary Teutsch Date: Tue, 28 Jan 2025 14:25:48 -0800 Subject: [PATCH 2/5] Fixed button overlap, added progress indicator, and added cancellation --- .../RetrievalAugmentedGeneration.xaml | 5 + .../RetrievalAugmentedGeneration.xaml.cs | 163 +++++++++++------- 2 files changed, 101 insertions(+), 67 deletions(-) diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml index d1ef1bb0..49a256a5 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml @@ -143,6 +143,7 @@ + + + + + diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs index edd71afd..192baee6 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs @@ -22,6 +22,7 @@ using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; +using WinUICommunity; namespace AIDevGallery.Samples.OpenSourceModels.SentenceEmbeddings.Embeddings; @@ -59,6 +60,7 @@ internal sealed partial class RetrievalAugmentedGeneration : BaseSamplePage private StorageFile? _pdfFile; private InMemoryRandomAccessStream? _inMemoryRandomAccessStream; private CancellationTokenSource? _cts; + private bool _isCancellable = false; private List? selectedPages; private int selectedPageIndex = -1; @@ -124,6 +126,14 @@ private void CleanUp() private async void IndexPDFButton_Click(object sender, RoutedEventArgs e) { + if (_isCancellable) + { + _cts?.Cancel(); + _cts = null; + ToSelectState(); + return; + } + if (_embeddings == null) { return; @@ -148,88 +158,77 @@ private async Task IndexPDF() return; } - IndexPDFText.Text = "Indexing PDF..."; - - var contents = new List<(string Text, uint Page)>(); - - using (PdfDocument document = PdfDocument.Open(_pdfFile.Path)) - { - foreach (var page in document.GetPages()) - { - var words = page.GetWords(); - var builder = string.Join(" ", words); - - var range = builder - .Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries) - .Where(x => !string.IsNullOrWhiteSpace(x)) - .Select(x => ((string Text, uint Page))(x, page.Number)); - - contents.AddRange(range); - } - } - - if (contents.Count == 0) - { - ToSelectState(); - PdfProblemTextBlock.Text = "We weren't able to read this PDF. Please try another."; - return; - } - - // 2) Split the text into chunks to make sure they are - // smaller than what the Embeddings model supports - var maxLength = 1024 / 2; - List<(string Text, uint Page)> chunkedContents = []; - foreach (var content in contents) - { - chunkedContents.AddRange(SplitInChunks(content, maxLength)); - } - - contents = chunkedContents; - - if (_cts != null) - { - _cts.Cancel(); - _cts = null; - AskSLMButton.Content = "Answer"; - return; - } - + ToIndexingState(); _cts = new CancellationTokenSource(); + CancellationToken ct = _cts.Token; - // 3) Index the chunks #pragma warning disable SKEXP0020 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. _vectorStore = new InMemoryVectorStore(); #pragma warning restore SKEXP0020 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed. _pdfPages = _vectorStore.GetCollection("pages"); - await _pdfPages.CreateCollectionIfNotExistsAsync(_cts.Token).ConfigureAwait(false); + await _pdfPages.CreateCollectionIfNotExistsAsync(ct).ConfigureAwait(false); + int chunksProcessedCount = 0; - var total = contents.Count; try { - int i = 0; - await foreach (var embedding in _embeddings.GenerateStreamingAsync(contents.Select(c => c.Text), null, _cts.Token).ConfigureAwait(false)) + await Task.Run( + async () => { - await _pdfPages.UpsertAsync( - new PdfPageData + using PdfDocument document = PdfDocument.Open(_pdfFile.Path); + foreach (var page in document.GetPages()) + { + string pageText = string.Join(" ", page.GetWords()); + + if(pageText == string.Empty) { - Key = i, - Page = contents[i].Page, - Text = contents[i].Text, - Vector = embedding.Vector - }, - null, - _cts.Token).ConfigureAwait(false); - i++; - } + continue; + } + + List<(string Text, uint Page)> pageChunks = SplitInChunks((pageText, (uint)page.Number), 512).ToList(); + int i = 0; + await foreach(var embedding in _embeddings.GenerateStreamingAsync(pageChunks.Select(c => c.Text), null, ct).ConfigureAwait(false)) + { + await _pdfPages.UpsertAsync( + new PdfPageData + { + Key = chunksProcessedCount, + Page = pageChunks[i].Page, + Text = pageChunks[i].Text, + Vector = embedding.Vector + }, + null, + ct).ConfigureAwait(false); + i++; + chunksProcessedCount++; + } + + DispatcherQueue.TryEnqueue(() => + { + UpdateProgress(page.Number, document.NumberOfPages); + }); + } + }, + ct); + + ct.ThrowIfCancellationRequested(); } - catch (OperationCanceledException) + catch (Exception ex) { - DispatcherQueue.TryEnqueue(ToSelectState); + ToSelectState(); + + if (ex is not OperationCanceledException) + { + PdfProblemTextBlock.Text = "We weren't able to read this PDF. Please try another."; + } + return; } - finally + + if(chunksProcessedCount == 0) { - _cts = null; + ToSelectState(); + PdfProblemTextBlock.Text = "We weren't able to read this PDF. Please try another."; + return; } DispatcherQueue.TryEnqueue(() => @@ -352,6 +351,7 @@ private async Task UpdatePdfImageAsync() PdfImage.Source = bitmapImage; PdfImageGrid.Visibility = Visibility.Visible; + SelectNewPDFButton.Visibility = Visibility.Collapsed; UpdatePreviousAndNextPageButtonEnabled(); }); } @@ -393,6 +393,7 @@ private async void ShowPDFPage_Click(object sender, RoutedEventArgs e) private void PdfImage_Tapped(object sender, TappedRoutedEventArgs e) { PdfImageGrid.Visibility = Visibility.Collapsed; + SelectNewPDFButton.Visibility = Visibility.Visible; } private async void PreviousPageButton_Click(object sender, RoutedEventArgs e) @@ -420,6 +421,7 @@ private async void NextPageButton_Click(object sender, RoutedEventArgs e) private void ClosePdfButton_Click(object sender, RoutedEventArgs e) { PdfImageGrid.Visibility = Visibility.Collapsed; + SelectNewPDFButton.Visibility = Visibility.Visible; } private IEnumerable<(string Text, uint Page)> SplitInChunks((string Text, uint Page) input, int maxLength) @@ -472,6 +474,9 @@ private void ClosePdfButton_Click(object sender, RoutedEventArgs e) private void ToSelectState() { + _pdfPages?.DeleteCollectionAsync(); + HideProgress(); + _isCancellable = false; ShowPDFPage.IsEnabled = false; SelectNewPDFButton.Visibility = Visibility.Collapsed; IndexPDFGrid.Visibility = Visibility.Visible; @@ -491,6 +496,15 @@ private void ToSelectingState() IndexPDFText.Text = "Selecting PDF..."; } + private void ToIndexingState() + { + IndexPDFButton.IsEnabled = true; + IndexPDFText.Text = "Cancel"; + _isCancellable = true; + ProgressPanel.Visibility = Visibility.Visible; + PdfProblemTextBlock.Text = string.Empty; + } + private async void AskSLMButton_Click(object sender, RoutedEventArgs e) { if (SearchTextBox.Text.Length > 0) @@ -524,8 +538,23 @@ private async void SelectNewPDF_Click(object sender, RoutedEventArgs e) { _pdfFile = pdfFile; ToSelectState(); - ToSelectingState(); await IndexPDF(); } } + + private void UpdateProgress(int currentPage, int totalPages) + { + int progressValue = (int)Math.Floor((float)currentPage / (float)totalPages * 100); + string progressString = $"Indexed {currentPage} of {totalPages} pages ({progressValue}%)"; + + IndexingProgressBar.Value = progressValue; + ProgressStatusTextBlock.Text = progressString; + } + + private void HideProgress() + { + ProgressPanel.Visibility = Visibility.Collapsed; + IndexingProgressBar.Value = 0; + ProgressStatusTextBlock.Text = string.Empty; + } } \ No newline at end of file From c217f5af7deb8fa4bb961372c0e7dd88dbf850f1 Mon Sep 17 00:00:00 2001 From: Zach Teutsch <88554871+zateutsch@users.noreply.github.com> Date: Tue, 28 Jan 2025 14:32:50 -0800 Subject: [PATCH 3/5] Update AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs --- .../Embeddings/RetrievalAugmentedGeneration.xaml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs index 192baee6..6a9cdc74 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs @@ -22,7 +22,6 @@ using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Streams; -using WinUICommunity; namespace AIDevGallery.Samples.OpenSourceModels.SentenceEmbeddings.Embeddings; From 8f86950dfa89e25d05bf6c9131d7eebce349f52f Mon Sep 17 00:00:00 2001 From: Zach Teutsch <88554871+zateutsch@users.noreply.github.com> Date: Tue, 28 Jan 2025 14:33:19 -0800 Subject: [PATCH 4/5] Update AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs --- .../Embeddings/RetrievalAugmentedGeneration.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs index 6a9cdc74..d7b62572 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml.cs @@ -59,7 +59,7 @@ internal sealed partial class RetrievalAugmentedGeneration : BaseSamplePage private StorageFile? _pdfFile; private InMemoryRandomAccessStream? _inMemoryRandomAccessStream; private CancellationTokenSource? _cts; - private bool _isCancellable = false; + private bool _isCancellable; private List? selectedPages; private int selectedPageIndex = -1; From 4c99b6ec8388b409756eb802559ba56c5045f2f9 Mon Sep 17 00:00:00 2001 From: Zach Teutsch <88554871+zateutsch@users.noreply.github.com> Date: Wed, 29 Jan 2025 10:08:03 -0800 Subject: [PATCH 5/5] Update AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml Co-authored-by: Niels Laute --- .../Embeddings/RetrievalAugmentedGeneration.xaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml index 49a256a5..803acbfb 100644 --- a/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml +++ b/AIDevGallery/Samples/Open Source Models/Embeddings/RetrievalAugmentedGeneration.xaml @@ -24,7 +24,7 @@ HorizontalAlignment="Right" VerticalAlignment="Top" Click="SelectNewPDF_Click" - Margin="12" + Margin="12" Style="{StaticResource AccentButtonStyle}" Visibility="Collapsed"/>