|
| 1 | +using Syncfusion.UI.Xaml.Chat; |
| 2 | +using Syncfusion.Windows.PdfViewer; |
| 3 | +using System.Collections.ObjectModel; |
| 4 | +using System.ComponentModel; |
| 5 | +using System.Text; |
| 6 | + |
| 7 | +namespace Smart_Summarize |
| 8 | +{ |
| 9 | + internal class AIAssitViewModel : INotifyPropertyChanged |
| 10 | + { |
| 11 | + #region Fields |
| 12 | + private ObservableCollection<object> chats; |
| 13 | + private ObservableCollection<string> suggestion; |
| 14 | + private Author currentUser; |
| 15 | + private PdfViewerControl pdfViewer; |
| 16 | + MicrosoftAIExtension microsoftAIExtension; |
| 17 | + StringBuilder processedText = new StringBuilder(); |
| 18 | + public event PropertyChangedEventHandler PropertyChanged; |
| 19 | + #endregion |
| 20 | + |
| 21 | + #region Properties |
| 22 | + /// <summary> |
| 23 | + /// Gets or sets the collection of chat messages. |
| 24 | + /// </summary> |
| 25 | + public ObservableCollection<object> Chats |
| 26 | + { |
| 27 | + get |
| 28 | + { |
| 29 | + return chats; |
| 30 | + } |
| 31 | + set |
| 32 | + { |
| 33 | + chats = value; |
| 34 | + RaisePropertyChanged("Messages"); |
| 35 | + } |
| 36 | + } |
| 37 | + /// <summary> |
| 38 | + /// Gets or sets the collection of chat suggestions. |
| 39 | + /// </summary> |
| 40 | + public ObservableCollection<string> Suggestion |
| 41 | + { |
| 42 | + get |
| 43 | + { |
| 44 | + return suggestion; |
| 45 | + } |
| 46 | + set |
| 47 | + { |
| 48 | + suggestion = value; |
| 49 | + RaisePropertyChanged("Suggestion"); |
| 50 | + } |
| 51 | + } |
| 52 | + /// <summary> |
| 53 | + /// Gets or sets the current user of the chat. |
| 54 | + /// </summary> |
| 55 | + public Author CurrentUser |
| 56 | + { |
| 57 | + get |
| 58 | + { |
| 59 | + return currentUser; |
| 60 | + } |
| 61 | + set |
| 62 | + { |
| 63 | + currentUser = value; |
| 64 | + RaisePropertyChanged("CurrentUser"); |
| 65 | + } |
| 66 | + } |
| 67 | + /// <summary> |
| 68 | + /// Raises the PropertyChanged event to notify the UI of property changes. |
| 69 | + /// </summary> |
| 70 | + /// <param name="propName">The name of the property that changed.</param> |
| 71 | + public void RaisePropertyChanged(string propName) |
| 72 | + { |
| 73 | + if (PropertyChanged != null) |
| 74 | + { |
| 75 | + PropertyChanged(this, new PropertyChangedEventArgs(propName)); |
| 76 | + } |
| 77 | + } |
| 78 | + #endregion |
| 79 | + #region constructor |
| 80 | + public AIAssitViewModel(PdfViewerControl viewer) |
| 81 | + { |
| 82 | + pdfViewer = viewer; |
| 83 | + Chats = new ObservableCollection<object>(); |
| 84 | + suggestion = new ObservableCollection<string>(); |
| 85 | + CurrentUser = new Author() { Name = pdfViewer.CurrentUser }; |
| 86 | + microsoftAIExtension = new MicrosoftAIExtension("Your-AI-Key"); |
| 87 | + Chats.CollectionChanged += Chats_CollectionChanged; |
| 88 | + } |
| 89 | + #endregion |
| 90 | + |
| 91 | + #region Methods |
| 92 | + /// <summary> |
| 93 | + /// Generates chat messages by summarizing the PDF document. |
| 94 | + /// </summary> |
| 95 | + public async Task GenerateMessages() |
| 96 | + { |
| 97 | + Chats.Add(new TextMessage |
| 98 | + { |
| 99 | + Author = currentUser, |
| 100 | + DateTime = DateTime.Now, |
| 101 | + Text = "Summarizing the PDF document..." |
| 102 | + }); |
| 103 | + |
| 104 | + // Execute the following asynchronously |
| 105 | + await ExtractDetailsFromPDF(); |
| 106 | + |
| 107 | + string summaryText = await SummarizePDF(); |
| 108 | + |
| 109 | + // Update chats on the UI thread |
| 110 | + Chats.Add(new TextMessage |
| 111 | + { |
| 112 | + Author = new Author { Name = "AIAssistant" }, |
| 113 | + DateTime = DateTime.Now, |
| 114 | + Text = summaryText |
| 115 | + }); |
| 116 | + await AddSuggestions(summaryText); |
| 117 | + } |
| 118 | + /// <summary> |
| 119 | + /// Generate suggestion from the answers |
| 120 | + /// </summary> |
| 121 | + private async Task AddSuggestions(String text) |
| 122 | + { |
| 123 | + string suggestions = await microsoftAIExtension.GetAnswerFromGPT("You are a helpful assistant. Your task is to analyze the answer and ask 3 short one-line suggestion questions that user asks.", text); |
| 124 | + |
| 125 | + var suggestionList = suggestions.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); |
| 126 | + foreach (var suggestion in suggestionList) |
| 127 | + { |
| 128 | + Suggestion.Add(suggestion); |
| 129 | + } |
| 130 | + } |
| 131 | + /// <summary> |
| 132 | + /// Extracts text from each page of the PDF document. |
| 133 | + /// </summary> |
| 134 | + private async Task<string> ExtractDetailsFromPDF() |
| 135 | + { |
| 136 | + StringBuilder extractedText = new StringBuilder(); |
| 137 | + Syncfusion.Pdf.TextLines textLines = new Syncfusion.Pdf.TextLines(); |
| 138 | + //Extract the text from the PDF document |
| 139 | + for (int pageIndex = 0; pageIndex < pdfViewer.PageCount; pageIndex++) |
| 140 | + { |
| 141 | + string text = $"... Page {pageIndex + 1} ...\n"; |
| 142 | + text += pdfViewer.ExtractText(pageIndex, out textLines); |
| 143 | + extractedText.AppendLine(text); |
| 144 | + } |
| 145 | + return ProcessExtractedText(extractedText.ToString()); |
| 146 | + } |
| 147 | + /// <summary> |
| 148 | + /// Processes the extracted full text from a document by splitting it into pages. |
| 149 | + /// </summary> |
| 150 | + /// <param name="fullText">The complete extracted text from the document.</param> |
| 151 | + /// <returns>A formatted string containing the processed text.</returns> |
| 152 | + private string ProcessExtractedText(string fullText) |
| 153 | + { |
| 154 | + string[] pages = fullText.Split(new string[] { "\f", "\n\nPage " }, StringSplitOptions.RemoveEmptyEntries); |
| 155 | + for (int i = 0; i < pages.Length; i++) |
| 156 | + { |
| 157 | + processedText.AppendLine($"... Page {i + 1} ..."); |
| 158 | + string[] lines = pages[i].Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); |
| 159 | + int maxLines = Math.Min(1000, lines.Length); |
| 160 | + |
| 161 | + for (int j = 0; j < maxLines; j++) |
| 162 | + { |
| 163 | + processedText.AppendLine(lines[j]); |
| 164 | + } |
| 165 | + processedText.AppendLine(); |
| 166 | + } |
| 167 | + return processedText.ToString(); |
| 168 | + } |
| 169 | + /// <summary> |
| 170 | + /// Summarizes the extracted text from the PDF using Extension AI. |
| 171 | + /// </summary> |
| 172 | + private async Task<string> SummarizePDF() |
| 173 | + { |
| 174 | + //Summarize the text using the Semantic Kernel AI |
| 175 | + string summary = await microsoftAIExtension.GetAnswerFromGPT(processedText.ToString()); |
| 176 | + return summary; |
| 177 | + } |
| 178 | + /// <summary> |
| 179 | + /// Handles the event when the chat collection changes. |
| 180 | + /// </summary> |
| 181 | + /// <param name="sender">The source of the event.</param> |
| 182 | + /// <param name="e">The event data containing information about the collection change.</param> |
| 183 | + private async void Chats_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) |
| 184 | + { |
| 185 | + if (e.NewItems != null && e.NewItems.Count > 0) |
| 186 | + { |
| 187 | + var item = e.NewItems[0] as ITextMessage; |
| 188 | + if (item != null) |
| 189 | + { |
| 190 | + if (item.Text != "Summarizing the PDF document...") |
| 191 | + { |
| 192 | + if (item.Author.Name == currentUser.Name) |
| 193 | + { |
| 194 | + string answer = await microsoftAIExtension.GetAnswerFromGPT("You are a helpful assistant. Your task is to analyze the provided question and answer the question based on the pdf", item.Text); |
| 195 | + Chats.Add(new TextMessage |
| 196 | + { |
| 197 | + Author = new Author { Name = "AIAssistant" }, |
| 198 | + DateTime = DateTime.Now, |
| 199 | + Text = answer |
| 200 | + }); |
| 201 | + Suggestion.Clear(); |
| 202 | + await AddSuggestions(answer); |
| 203 | + } |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + #endregion |
| 211 | + } |
| 212 | +} |
0 commit comments