Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
76 changes: 16 additions & 60 deletions Smart_Summarize/AIAssitViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,15 @@ public AIAssitViewModel(PdfViewerControl viewer)
/// </summary>
public async Task GenerateMessages()
{
Chats.Add(new TextMessage
{
Author = currentUser,
DateTime = DateTime.Now,
Text = "Summarizing the PDF document..."
});

// Execute the following asynchronously
await ExtractDetailsFromPDF();

string summaryText = await SummarizePDF();

// Update chats on the UI thread
Chats.Add(new TextMessage
{
Author = new Author { Name = "AIAssistant" },
Author = currentUser,
DateTime = DateTime.Now,
Text = summaryText
Text = "Summarizing the PDF document..."
});
await AddSuggestions(summaryText);
}
/// <summary>
/// Generate suggestion from the answers
Expand All @@ -133,48 +122,20 @@ private async Task AddSuggestions(String text)
/// </summary>
private async Task<string> ExtractDetailsFromPDF()
{
StringBuilder extractedText = new StringBuilder();
List<string> extractedText = new List<string>();
Syncfusion.Pdf.TextLines textLines = new Syncfusion.Pdf.TextLines();
//Extract the text from the PDF document
for (int pageIndex = 0; pageIndex < pdfViewer.PageCount; pageIndex++)
{
string text = $"... Page {pageIndex + 1} ...\n";
text += pdfViewer.ExtractText(pageIndex, out textLines);
extractedText.AppendLine(text);
extractedText.Add(text);
}
return ProcessExtractedText(extractedText.ToString());
}
/// <summary>
/// Processes the extracted full text from a document by splitting it into pages.
/// </summary>
/// <param name="fullText">The complete extracted text from the document.</param>
/// <returns>A formatted string containing the processed text.</returns>
private string ProcessExtractedText(string fullText)
{
string[] pages = fullText.Split(new string[] { "\f", "\n\nPage " }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < pages.Length; i++)
{
processedText.AppendLine($"... Page {i + 1} ...");
string[] lines = pages[i].Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
int maxLines = Math.Min(1000, lines.Length);
microsoftAIExtension.CreateEmbeddedPage(extractedText.ToArray());

for (int j = 0; j < maxLines; j++)
{
processedText.AppendLine(lines[j]);
}
processedText.AppendLine();
}
return processedText.ToString();
}
/// <summary>
/// Summarizes the extracted text from the PDF using Extension AI.
/// </summary>
private async Task<string> SummarizePDF()
{
//Summarize the text using the Semantic Kernel AI
string summary = await microsoftAIExtension.GetAnswerFromGPT(processedText.ToString());
return summary;
return extractedText.ToString();
}

/// <summary>
/// Handles the event when the chat collection changes.
/// </summary>
Expand All @@ -187,23 +148,18 @@ private async void Chats_CollectionChanged(object sender, System.Collections.Spe
var item = e.NewItems[0] as ITextMessage;
if (item != null)
{
if (item.Text != "Summarizing the PDF document...")
if (item.Author.Name == currentUser.Name)
{
if (item.Author.Name == currentUser.Name)
string answer = await microsoftAIExtension.AnswerQuestion(item.Text);
Chats.Add(new TextMessage
{
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);
Chats.Add(new TextMessage
{
Author = new Author { Name = "AIAssistant" },
DateTime = DateTime.Now,
Text = answer
});
Suggestion.Clear();
await AddSuggestions(answer);
}

Author = new Author { Name = "AIAssistant" },
DateTime = DateTime.Now,
Text = answer
});
Suggestion.Clear();
await AddSuggestions(answer);
}

}
}
}
Expand Down
38 changes: 34 additions & 4 deletions Smart_Summarize/MicrosoftAIExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.AI;
using Azure.AI.OpenAI;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using SmartComponents.LocalEmbeddings;
using System.Text;
namespace Smart_Summarize
{
Expand All @@ -8,10 +9,11 @@ internal class MicrosoftAIExtension

const string endpoint = "YOUR-AI-ENDPOINT";
const string deploymentName = "YOUR-DEPLOYMENT-NAME";
internal string key = string.Empty;

private IChatClient clientAI;

public Dictionary<string, EmbeddingF32>? PageEmbeddings { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="MicrosoftAIExtension"/> class.
/// </summary>
Expand Down Expand Up @@ -64,5 +66,33 @@ public async Task<string> GetAnswerFromGPT(string systemPrompt, string userText)
}
return string.Empty;
}

/// <summary>
/// Create the embedded page from the extracted chunks in the PDF
/// </summary>
/// <param name="chunks">Extracted text from pdfViewer</param>
/// <returns></returns>
public async Task CreateEmbeddedPage(string[] chunks)
{
var embedder = new LocalEmbedder();
PageEmbeddings = chunks.Select(x => KeyValuePair.Create(x, embedder.Embed(x))).ToDictionary(k => k.Key, v => v.Value);
}


public async Task<string> AnswerQuestion(string question)
{
var embedder = new LocalEmbedder();
var questionEmbedding = embedder.Embed(question);
var results = LocalEmbedder.FindClosestWithScore(questionEmbedding, PageEmbeddings.Select(x => (x.Key, x.Value)), 5, 0.5f);
StringBuilder builder = new StringBuilder();
foreach (var result in results)
{
builder.AppendLine(result.Item);
}
string message = builder.ToString();
var answer = await GetAnswerFromGPT("You are a helpful assistant. Use the provided PDF document pages and pick a precise page to answer the user question. Provide the answer in plain text without any special formatting or Markdown syntax. Pages: " + message, question);

return answer;
}
}
}
}
8 changes: 2 additions & 6 deletions Smart_Summarize/Smart_Summarize.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<None Remove="Data\send_icon.png" />
</ItemGroup>

<ItemGroup>

Expand All @@ -21,10 +18,9 @@
<PackageReference Include="Azure.AI.OpenAI" Version="*" />
<PackageReference Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25114.11" />
<PackageReference Include="SmartComponents.LocalEmbeddings" Version="0.1.0-preview10148" />
</ItemGroup>

<ItemGroup>
<Resource Include="Data\send_icon.png" />
</ItemGroup>


</Project>