Merge pull request #1211 from pangeachat/1207-orange-box-on-audio-select

don't rely on token offsets from speech_to_text endpoint for creating…
pull/1544/head
ggurdin 11 months ago committed by GitHub
commit 4d7686188a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -72,12 +72,11 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
TextSpan _buildTranscriptText(BuildContext context) {
final Transcript transcript = speechToTextResponse!.transcript;
final List<InlineSpan> spans = [];
final String fullText = transcript.text;
int lastEnd = 0;
String remainingFullText = transcript.text;
if (transcript.sttTokens.isEmpty) {
return TextSpan(
text: fullText,
text: remainingFullText,
style: BotStyle.text(
context,
existingStyle: TextStyle(
@ -89,19 +88,26 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
}
for (final token in transcript.sttTokens) {
if (token.offset > lastEnd) {
final offset = remainingFullText.indexOf(token.token.text.content);
final length = token.length;
if (remainingFullText.substring(0, offset).trim().isNotEmpty) {
continue;
}
if (offset > 0) {
// Add any plain text before the token
spans.add(
TextSpan(
text: fullText.substring(lastEnd, token.offset),
),
TextSpan(text: remainingFullText.substring(0, offset)),
);
// debugPrint('Pre: ${fullText.substring(lastEnd, token.offset)}');
}
spans.add(
TextSpan(
text: fullText.substring(token.offset, token.offset + token.length),
text: remainingFullText.substring(
offset,
offset + token.length,
),
style: BotStyle.text(
context,
existingStyle: TextStyle(color: token.color(context)),
@ -125,21 +131,12 @@ class MessageSpeechToTextCardState extends State<MessageSpeechToTextCard> {
),
);
// debugPrint(
// 'Main: ${fullText.substring(token.offset, token.offset + token.length)}',
// );
lastEnd = token.offset + token.length;
remainingFullText = remainingFullText.substring(offset + length);
}
if (lastEnd < fullText.length) {
if (remainingFullText.isNotEmpty) {
// Add any remaining text after the last token
spans.add(
TextSpan(
text: fullText.substring(lastEnd),
),
);
// debugPrint('Post: ${fullText.substring(lastEnd)}');
spans.add(TextSpan(text: remainingFullText));
}
return TextSpan(children: spans);

Loading…
Cancel
Save