From dd0f20e56c55f38b8e16edc12393a0de7ef2563f Mon Sep 17 00:00:00 2001 From: Wilson Date: Thu, 21 Nov 2024 09:55:25 -0500 Subject: [PATCH] enforce using bot transcription if exist (#1078) * enforce bot transcription if exist * follow snake case model key convention * update text color in speech to text toolbar --------- Co-authored-by: ggurdin Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com> --- lib/pangea/constants/model_keys.dart | 1 + .../pangea_message_event.dart | 9 +++++++ lib/pangea/models/speech_to_text_models.dart | 5 +--- .../chat/message_speech_to_text_card.dart | 24 +++++++++++++++---- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/pangea/constants/model_keys.dart b/lib/pangea/constants/model_keys.dart index 253d73e55..20c95a6fd 100644 --- a/lib/pangea/constants/model_keys.dart +++ b/lib/pangea/constants/model_keys.dart @@ -102,6 +102,7 @@ class ModelKey { static const String feedbackLang = "feedback_lang"; static const String transcription = "transcription"; + static const String botTranscription = 'bot_transcription'; // bot options static const String languageLevel = "difficulty"; diff --git a/lib/pangea/matrix_event_wrappers/pangea_message_event.dart b/lib/pangea/matrix_event_wrappers/pangea_message_event.dart index dda9e8e00..5925af571 100644 --- a/lib/pangea/matrix_event_wrappers/pangea_message_event.dart +++ b/lib/pangea/matrix_event_wrappers/pangea_message_event.dart @@ -248,6 +248,15 @@ class PangeaMessageEvent { return null; } + final rawBotTranscription = + event.content.tryGetMap(ModelKey.botTranscription); + if (rawBotTranscription != null) { + final botTranscription = SpeechToTextModel.fromJson( + Map.from(rawBotTranscription), + ); + return botTranscription; + } + final SpeechToTextModel? speechToTextLocal = representations .firstWhereOrNull( (element) => element.content.speechToText != null, diff --git a/lib/pangea/models/speech_to_text_models.dart b/lib/pangea/models/speech_to_text_models.dart index 3bcb4711c..d2bc57aca 100644 --- a/lib/pangea/models/speech_to_text_models.dart +++ b/lib/pangea/models/speech_to_text_models.dart @@ -91,10 +91,7 @@ class STTToken { Color color(BuildContext context) { if (confidence == null) { - return Theme.of(context).textTheme.bodyMedium?.color ?? - (Theme.of(context).brightness == Brightness.dark - ? Colors.white - : Colors.black); + return Theme.of(context).colorScheme.onSurface; } if (confidence! > THRESHOLD_FOR_GREEN) { return AppConfig.success; diff --git a/lib/pangea/widgets/chat/message_speech_to_text_card.dart b/lib/pangea/widgets/chat/message_speech_to_text_card.dart index c55a0942c..ae05cbca9 100644 --- a/lib/pangea/widgets/chat/message_speech_to_text_card.dart +++ b/lib/pangea/widgets/chat/message_speech_to_text_card.dart @@ -77,9 +77,20 @@ class MessageSpeechToTextCardState extends State { final String fullText = transcript.text; int lastEnd = 0; + if (transcript.sttTokens.isEmpty) { + return TextSpan( + text: fullText, + style: BotStyle.text( + context, + existingStyle: TextStyle( + color: Theme.of(context).colorScheme.onSurface, + ), + setColor: false, + ), + ); + } + for (final token in transcript.sttTokens) { - // debugPrint('Token confidence: ${token.confidence}'); - // debugPrint('color: ${token.color(context)}'); if (token.offset > lastEnd) { // Add any plain text before the token spans.add( @@ -187,8 +198,13 @@ class MessageSpeechToTextCardState extends State { ), ], ), - const InlineTooltip( - instructionsEnum: InstructionsEnum.speechToText, + const Row( + mainAxisSize: MainAxisSize.min, + children: [ + InlineTooltip( + instructionsEnum: InstructionsEnum.speechToText, + ), + ], ), ], ),