Skip to content

Commit

Permalink
Merge branch 'alzollin/wcrapis' into nmetulev/wcrapis2
Browse files Browse the repository at this point in the history
  • Loading branch information
nmetulev authored Feb 13, 2025
2 parents f60d141 + dcfdb6b commit f6d54f6
Show file tree
Hide file tree
Showing 30 changed files with 83 additions and 6 deletions.
4 changes: 3 additions & 1 deletion AIDevGallery.SourceGenerator/SamplesSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ private void ExecuteSharedCodeEnumGeneration(SourceProductionContext context, Im
context.AddSource("SharedCodeEnum.g.cs", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
}

private static readonly Regex UsingAIDevGalleryTelemetryNamespace = new(@"using AIDevGallery.Telemetry\S*;\r?\n", RegexOptions.Multiline | RegexOptions.Compiled);
private static readonly Regex GallerySampleAttributeRemovalRegex = new(@"\n(\s)*\[GallerySample\((?>[^()]+|\((?<DEPTH>)|\)(?<-DEPTH>))*(?(DEPTH)(?!))\)\]", RegexOptions.Compiled);
private static readonly Regex ExcludedElementXamlRemovalRegex = new(@"<EXCLUDE:(([^<]*\/>)|(.*<\/EXCLUDE:[a-zA-Z]*>))", RegexOptions.Singleline | RegexOptions.Compiled);
private static readonly Regex ExcludedAttrbitueXamlRemovalRegex = new(@"EXCLUDE:[^""]*""[^""]*""", RegexOptions.Singleline | RegexOptions.Compiled);
Expand All @@ -203,6 +204,7 @@ private static string SampleSourceCleanUp(string input, string filePath)
.TrimStart();
}

input = UsingAIDevGalleryTelemetryNamespace.Replace(input, string.Empty);
input = GallerySampleAttributeRemovalRegex.Replace(input, string.Empty);
input = RemoveExcludedLinesCs(input, filePath);

Expand Down Expand Up @@ -248,7 +250,7 @@ private static string RemoveExcludedLinesCs(string input, string filePath)

lines.RemoveAt(i);
}
else if (lines[i].Contains("// <exclude-line>") || lines[i].Contains("//<exclude-line>"))
else if (lines[i].Contains("// <exclude-line>") || lines[i].Contains("//<exclude-line>") || lines[i].Contains("SendSampleInteractedEvent"))
{
lines.RemoveAt(i);
}
Expand Down
10 changes: 5 additions & 5 deletions AIDevGallery/ProjectGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,24 +698,24 @@ private string CleanXamlSource(string xamlCode, string newNamespace, out string
}

[GeneratedRegex(@"using AIDevGallery\S*;\r?\n", RegexOptions.Multiline)]
private static partial Regex UsingAIDevGalleryGNamespace();
private static partial Regex UsingAIDevGalleryNamespace();

[GeneratedRegex(@"namespace AIDevGallery(?:[^;\r\n])*(;?)\r\n", RegexOptions.Multiline)]
private static partial Regex AIDevGalleryGNamespace();
private static partial Regex AIDevGalleryNamespace();

private static string CleanCsSource(string source, string newNamespace, bool addSharedSourceNamespace)
{
// Remove the using statements for the AIDevGallery.* namespaces
source = UsingAIDevGalleryGNamespace().Replace(source, string.Empty);
source = UsingAIDevGalleryNamespace().Replace(source, string.Empty);

source = source.Replace("\r\r", "\r");

// Replace the AIDevGallery namespace with the namespace of the new project
// consider the 1st capture group to add the ; or not
var match = AIDevGalleryGNamespace().Match(source);
var match = AIDevGalleryNamespace().Match(source);
if (match.Success)
{
source = AIDevGalleryGNamespace().Replace(source, $"namespace {newNamespace}{match.Groups[1].Value}{Environment.NewLine}");
source = AIDevGalleryNamespace().Replace(source, $"namespace {newNamespace}{match.Groups[1].Value}{Environment.NewLine}");
}

if (addSharedSourceNamespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ public void Search(string sourceText, string searchText)
return;
}

SendSampleInteractedEvent("Search"); // <exclude-line>

Task.Run(
async () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private async void UploadButton_Click(object sender, RoutedEventArgs e)
{
// Call function to run inference and classify image
UploadButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await EnhanceImage(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private async void UploadButton_Click(object sender, RoutedEventArgs e)
if (file != null)
{
UploadButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await Segment(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private async void UploadButton_Click(object sender, RoutedEventArgs e)
if (file != null)
{
UploadButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await DetectObjects(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ private async void UploadButton_Click(object sender, RoutedEventArgs e)
if (file != null)
{
UploadButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await DetectPose(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ private async void UploadImageButton_Click(object sender, RoutedEventArgs e)
if (file != null)
{
UploadImageButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await ClassifyImage(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private async void UploadButton_Click(object sender, RoutedEventArgs e)
{
// Call function to run inference and classify image
UploadButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await RunPipeline(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private async void UploadButton_Click(object sender, RoutedEventArgs e)
{
// Call function to run inference and classify image
UploadButton.Focus(FocusState.Programmatic);
SendSampleInteractedEvent("FileSelected"); // <exclude-line>
await DetectObjects(file.Path);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void GenerateText(string prompt)
InputTextBox.IsEnabled = false;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Moderating content, please wait.", "ContentModerationWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("GenerateText"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public void GenerateText(string query, string systemPrompt)
InputTextBox.IsEnabled = false;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Generating content, please wait.", "CustomPromptWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("GenerateText"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void Explain(string code)
ExplainButton.Visibility = Visibility.Collapsed;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Analyzing code, please wait.", "ExplainCodeWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("Explain"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void GenerateText(string topic)
InputTextBox.IsEnabled = false;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Generating content, please wait.", "GenerateTextWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("GenerateText"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public void GenerateSolution(string problem, string currentLanguage)
return;
}

SendSampleInteractedEvent("GenerateSolution"); // <exclude-line>

string generatedCode = string.Empty;
this.GenerateRichTextBlock.Blocks.Clear();
GenerateButton.Visibility = Visibility.Collapsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void GrammarCheckText(string text)
CheckGrammarButton.Visibility = Visibility.Collapsed;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Checking grammar, please wait.", "GrammarCheckWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("GrammarCheckText"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public void ParaphraseText(string text)
ParaphraseButton.Visibility = Visibility.Collapsed;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Paraphrasing text, please wait.", "ParaphraseWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("ParaphraseText"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void AnalyzeSentiment(string text)
SentimentTextBlock.Text = string.Empty;
SentimentButton.Visibility = Visibility.Collapsed;
NarratorHelper.Announce(InputTextBox, "Checking sentiment, please wait.", "SentimentCheckWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("AnalyzeSentiment"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void SummarizeText(string text)
SummarizeButton.Visibility = Visibility.Collapsed;
var contentStartedBeingGenerated = false; // <exclude-line>
NarratorHelper.Announce(InputTextBox, "Summarizing content, please wait.", "SummarizeTextWaitAnnouncementActivityId"); // <exclude-line>
SendSampleInteractedEvent("SummarizeText"); // <exclude-line>

Task.Run(
async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void TranslateText(string text)
// <exclude>
var contentStartedBeingGenerated = false;
NarratorHelper.Announce(InputTextBox, "Translating content, please wait.", "TranslateTextWaitAnnouncementActivityId");
SendSampleInteractedEvent("TranslateText"); // <exclude-line>

// </exclude>
if (LanguageBox.SelectedItem is string language)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ private async Task DescribeTheImage()
Loader.Visibility = Visibility.Visible;

_cts.Token.ThrowIfCancellationRequested();
SendSampleInteractedEvent("DescribeTheImage"); // <exclude-line>

await Task.Run(async () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ private async Task DoStableDiffusion()
InputBox.IsEnabled = false;

CancellationToken token = CancelGenerationAndGetNewToken();
SendSampleInteractedEvent("GenerateImage"); // <exclude-line>

inferenceTask = Task.Run(
() =>
Expand Down Expand Up @@ -217,6 +218,7 @@ private async void SaveButton_Click(object sender, RoutedEventArgs e)

if(file != null && DefaultImage.Source != null)
{
SendSampleInteractedEvent("SaveFile"); // <exclude-line>
RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap();
await renderTargetBitmap.RenderAsync(DefaultImage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private async Task<string> TranscribeAudio()
return "Invalid language selected";
}

SendSampleInteractedEvent("TranscribeAudio"); // <exclude-line>
cts = new CancellationTokenSource();

var transcribedChunks = await whisper.TranscribeAsync(audioData, sourceLanguage, WhisperWrapper.TaskType.Transcribe, (bool)TimeStampsToggle.IsChecked!, cts.Token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ private async Task TranslateAudioAsync()
return;
}

SendSampleInteractedEvent("TranslateAudio"); // <exclude-line>
try
{
var audioData = audioStream.ToArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ private void StartStopButton_Click(object sender, RoutedEventArgs e)
{
audioRecorder.StopRecording();
StartStopButton.Content = "Start Recording";
SendSampleInteractedEvent("StartRecording"); // <exclude-line>
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions AIDevGallery/Samples/SharedCode/SemanticComboBox.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using AIDevGallery.Telemetry.Events;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.InMemory;
Expand Down Expand Up @@ -74,6 +75,7 @@ public async Task<IEnumerable<StringData>> Search(string searchTerm)
return [];
}

SampleInteractionEvent.SendSampleInteractedEvent(EmbeddingGenerator.Metadata, Models.ScenarioType.SmartControlsSemanticComboBox, "Search"); // <exclude-line>
GeneratedEmbeddings<Embedding<float>> results = [];

var searchVectors = await EmbeddingGenerator.GenerateAsync([searchTerm]);
Expand Down
2 changes: 2 additions & 0 deletions AIDevGallery/Samples/SharedCode/SmartPasteForm.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using AIDevGallery.Telemetry.Events;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.AI;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -99,6 +100,7 @@ private async Task<Dictionary<string, string>> InferPasteValues(string clipboard
return [];
}

SampleInteractionEvent.SendSampleInteractedEvent(model, Models.ScenarioType.SmartControlsSmartPaste, "InferPasteValues"); // <exclude-line>
string outputMessage = string.Empty;
PromptInput input = new()
{
Expand Down
8 changes: 8 additions & 0 deletions AIDevGallery/Samples/SharedCode/SmartTextBox.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using AIDevGallery.Models;
using AIDevGallery.Telemetry.Events;
using Microsoft.Extensions.AI;
using Microsoft.UI.Input;
using Microsoft.UI.Text;
Expand Down Expand Up @@ -98,6 +100,7 @@ private async Task<string> Infer(string systemPrompt, string query, ChatOptions?

private async Task<string> ChangeToneProfessional(string textToChange)
{
SampleInteractionEvent.SendSampleInteractedEvent(_chatClient, ScenarioType.SmartControlsSmartTextBox, "ChangeToneProfessional"); // <exclude-line>
string systemPrompt = "You rewrite user-provided writing to adjust the tone of the text. In this case, you will rewrite whatever is provided to sound more \"professional\". When provided with text, respond with only the tone-adjusted version of the text and nothing else. Keep the underlying meaning of the text the same and of around the same length as the source material. Do NOT be overly formal but be polite, succinct, and you MUST USE command MODERN American English. Respond with ONLY the tone-adjusted text and DO NOT provide an explanation, note, or any sort of justification of your changes.";
ChatOptions chatOptions = GenAIModel.GetDefaultChatOptions();
chatOptions.MaxOutputTokens = systemPrompt.Length + textToChange.Length;
Expand All @@ -106,6 +109,7 @@ private async Task<string> ChangeToneProfessional(string textToChange)

private async Task<string> ChangeToneCasual(string textToChange)
{
SampleInteractionEvent.SendSampleInteractedEvent(_chatClient, ScenarioType.SmartControlsSmartTextBox, "ChangeToneCasual"); // <exclude-line>
string systemPrompt = "You rewrite user-provided writing to adjust the tone of the text. In this case, you will rewrite whatever is provided to sound more \"casual\". When provided with text, respond with only the tone-adjusted version of the text and nothing else. Keep the underlying meaning of the text the same and of around the same length as the source material. Do not be inappropriate but be friendly, succinct, and use every day American English. Respond with ONLY the tone-adjusted text and DO NOT provide an explanation, note, or any sort of justification of your changes.";
ChatOptions chatOptions = GenAIModel.GetDefaultChatOptions();
chatOptions.MaxOutputTokens = systemPrompt.Length + textToChange.Length;
Expand All @@ -114,6 +118,7 @@ private async Task<string> ChangeToneCasual(string textToChange)

private async Task<string> Shorten(string textToChange)
{
SampleInteractionEvent.SendSampleInteractedEvent(_chatClient, ScenarioType.SmartControlsSmartTextBox, "Shorten"); // <exclude-line>
string systemPrompt = "You change the length of user-provided text to be shorter. When provided with text, respond with only shortened version of the text and nothing else. Maintain the original meaning as much as possible. Respond with ONLY the shortened text and DO NOT provide an explanation, note, or any sort of justification of your changes.";
ChatOptions chatOptions = GenAIModel.GetDefaultChatOptions();
chatOptions.MaxOutputTokens = systemPrompt.Length + textToChange.Length;
Expand All @@ -122,6 +127,7 @@ private async Task<string> Shorten(string textToChange)

private async Task<string> Lengthen(string textToChange)
{
SampleInteractionEvent.SendSampleInteractedEvent(_chatClient, ScenarioType.SmartControlsSmartTextBox, "Lengthen"); // <exclude-line>
string systemPrompt = "You change the length of user-provided text to be longer. When provided with text, respond with only lengthened version of the text and nothing else. Maintain the original meaning as much as possible. Respond with ONLY the lengthened text and DO NOT provide an explanation, note, or any sort of justification of your changes.";
ChatOptions chatOptions = GenAIModel.GetDefaultChatOptions();
chatOptions.MaxOutputTokens = systemPrompt.Length + 2 * textToChange.Length;
Expand All @@ -130,6 +136,7 @@ private async Task<string> Lengthen(string textToChange)

private async Task<string> Proofread(string textToChange)
{
SampleInteractionEvent.SendSampleInteractedEvent(_chatClient, ScenarioType.SmartControlsSmartTextBox, "Proofread"); // <exclude-line>
string systemPrompt = "You proofread user-provided text to remove spelling mistakes and grammar mistakes. When provided with text, respond with only spelling and grammar corrected text and nothing else. Maintain the original meaning as much as possible. Respond with ONLY the corrected text and DO NOT provide an explanation, note, or any sort of justification of your changes.";
ChatOptions chatOptions = GenAIModel.GetDefaultChatOptions();
chatOptions.MaxOutputTokens = systemPrompt.Length + textToChange.Length;
Expand All @@ -138,6 +145,7 @@ private async Task<string> Proofread(string textToChange)

private async Task<string> DescribeChanges(string textToChange, string changes)
{
SampleInteractionEvent.SendSampleInteractedEvent(_chatClient, ScenarioType.SmartControlsSmartTextBox, "DescribeChanges"); // <exclude-line>
string systemPrompt = "You apply user-defined changes to text. When provided with text, apply the described changes to the text. Respond with only the changed text. Respond with ONLY the changed text and DO NOT provide an explanation, note, any sort of justification of your changes. The changes are: " + changes + ". The provided text is: " + textToChange;
ChatOptions chatOptions = GenAIModel.GetDefaultChatOptions();
chatOptions.MaxOutputTokens = systemPrompt.Length + textToChange.Length;
Expand Down
Loading

0 comments on commit f6d54f6

Please sign in to comment.