Skip to content

Commit 0c1a74d

Browse files
authored
a11y: Fix Narrator accessibility for Custom Parameters sliders (#590)
* a11y: Fix Narrator accessibility for Custom Parameters sliders * fix ToString * update order
1 parent b78de41 commit 0c1a74d

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,32 @@
6868
StepFrequency="1"
6969
ToolTipService.ToolTip="The min length parameter sets the minimum number of tokens for the model to respond with."
7070
Value="0"
71-
AutomationProperties.Name="{x:Bind GetAutomationName(MinLengthSlider.Name, MinLengthSlider.Value), Mode=OneWay}" />
71+
GotFocus="Slider_GotFocus"
72+
ValueChanged="Slider_ValueChanged" />
7273
<Slider x:Name="MaxLengthSlider" Header="Max Length" Maximum="2048"
7374
Minimum="1" StepFrequency="1"
7475
ToolTipService.ToolTip="The max length parameter sets the maximum number of tokens for the model to respond with."
75-
Value="{x:Bind defaultMaxLength, Mode=OneTime}"
76-
AutomationProperties.Name="{x:Bind GetAutomationName(MaxLengthSlider.Name, MaxLengthSlider.Value), Mode=OneWay}" />
76+
Value="{x:Bind defaultMaxLength, Mode=OneTime}"
77+
GotFocus="Slider_GotFocus"
78+
ValueChanged="Slider_ValueChanged" />
7779
<Slider x:Name="TopPSlider" Header="Top P" Maximum="1" Minimum="0"
7880
StepFrequency="0.01"
7981
ToolTipService.ToolTip="The top P parameter tells the model to only consider tokens up until a cumulative probability of P. Lower this value for more predictable, focused generation and increase it for more random response."
8082
Value="{x:Bind defaultTopP, Mode=OneTime}"
81-
AutomationProperties.Name="{x:Bind GetAutomationName(TopPSlider.Name, TopPSlider.Value), Mode=OneWay}" />
83+
GotFocus="Slider_GotFocus"
84+
ValueChanged="Slider_ValueChanged" />
8285
<Slider x:Name="TopKSlider" Header="Top K" Maximum="200" Minimum="0"
8386
StepFrequency="1"
8487
ToolTipService.ToolTip="The top K parameter tells the model to only consider the K most probable tokens at each generation step. Lower this value for more predictable, focused generation and increase it for more random response."
8588
Value="{x:Bind defaultTopK, Mode=OneTime}"
86-
AutomationProperties.Name="{x:Bind GetAutomationName(TopKSlider.Name, TopKSlider.Value), Mode=OneWay}" />
89+
GotFocus="Slider_GotFocus"
90+
ValueChanged="Slider_ValueChanged" />
8791
<Slider x:Name="TemperatureSlider" Header="Temperature" Maximum="5"
8892
Minimum="0.01" StepFrequency="0.01"
8993
ToolTipService.ToolTip="The temperature parameter is a scaling factor for the probability distribution of tokens during generation. Values lower than 1 will produce more deterministic response while values higher than 1 will increase ramdomness."
9094
Value="{x:Bind defaultTemperature, Mode=OneTime}"
91-
AutomationProperties.Name="{x:Bind GetAutomationName(TemperatureSlider.Name, TemperatureSlider.Value), Mode=OneWay}" />
95+
GotFocus="Slider_GotFocus"
96+
ValueChanged="Slider_ValueChanged" />
9297
<ToggleSwitch x:Name="DoSampleToggle" Header="Sampling"
9398
IsOn="{x:Bind defaultDoSample, Mode=OneTime}"
9499
ToolTipService.ToolTip="If Sampling is disabled, a greedy approach will be used and the model will select the most likely token every time. If enabled,tokens will be selected based on the token probability distribution. The Top K, Top P, and Temperature parameters only apply if sampling is enabled." />

AIDevGallery/Samples/Open Source Models/Language Models/CustomSystemPrompt.xaml.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
using Microsoft.Extensions.AI;
99
using Microsoft.UI.Xaml;
1010
using Microsoft.UI.Xaml.Controls;
11+
using Microsoft.UI.Xaml.Controls.Primitives;
1112
using Microsoft.Windows.AI.ContentSafety;
1213
using System;
1314
using System.Collections.Generic;
1415
using System.ComponentModel;
1516
using System.Diagnostics;
17+
using System.Globalization;
1618
using System.Threading;
1719
using System.Threading.Tasks;
1820

@@ -30,8 +32,8 @@ namespace AIDevGallery.Samples.OpenSourceModels.LanguageModels;
3032
internal sealed partial class CustomSystemPrompt : BaseSamplePage, INotifyPropertyChanged
3133
{
3234
private readonly int defaultTopK = 50;
33-
private readonly float defaultTopP = 0.9f;
34-
private readonly float defaultTemperature = 1;
35+
private readonly double defaultTopP = 0.9;
36+
private readonly double defaultTemperature = 1.0;
3537
private readonly int defaultMaxLength = 1024;
3638
private readonly bool defaultDoSample = true;
3739
private readonly SeverityLevel defaultSeverityLevel = SeverityLevel.Minimum;
@@ -136,7 +138,28 @@ private void CleanUp()
136138
chatClient?.Dispose();
137139
}
138140

139-
public string GetAutomationName(string name, double value) => $"{name} {value:F0}";
141+
private void Slider_GotFocus(object sender, RoutedEventArgs e)
142+
{
143+
if (sender is Slider slider)
144+
{
145+
var tooltip = ToolTipService.GetToolTip(slider)?.ToString();
146+
if (!string.IsNullOrEmpty(tooltip))
147+
{
148+
NarratorHelper.Announce(slider, tooltip, $"{slider.Name}FocusAnnouncementId");
149+
}
150+
}
151+
}
152+
153+
private void Slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
154+
{
155+
if (sender is Slider slider)
156+
{
157+
string formattedValue = slider.StepFrequency < 1
158+
? e.NewValue.ToString("F2", CultureInfo.InvariantCulture)
159+
: ((int)e.NewValue).ToString(CultureInfo.InvariantCulture);
160+
NarratorHelper.Announce(slider, $"{slider.Header} {formattedValue}", $"{slider.Name}ValueChangedAnnouncementId");
161+
}
162+
}
140163

141164
public ChatOptions GetDefaultChatOptions(IChatClient? chatClient)
142165
{
@@ -149,8 +172,8 @@ public ChatOptions GetDefaultChatOptions(IChatClient? chatClient)
149172
{ "do_sample", defaultDoSample },
150173
},
151174
MaxOutputTokens = defaultMaxLength,
152-
Temperature = defaultTemperature,
153-
TopP = defaultTopP,
175+
Temperature = (float)defaultTemperature,
176+
TopP = (float)defaultTopP,
154177
TopK = defaultTopK,
155178
};
156179
}

0 commit comments

Comments
 (0)