From 307da24503883be11a942a30802068040ae724c1 Mon Sep 17 00:00:00 2001 From: Pablo Marin Date: Fri, 3 Jan 2025 00:20:22 +0000 Subject: [PATCH] fix issue when UI repeats last text input --- apps/frontend/README.md | 2 +- apps/frontend/app/pages/3_FastAPI_Chat.py | 26 +++++++++++++---------- credentials.env | 2 ++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/apps/frontend/README.md b/apps/frontend/README.md index dce6db45..90d576ed 100644 --- a/apps/frontend/README.md +++ b/apps/frontend/README.md @@ -32,7 +32,7 @@ az webapp deployment source config-zip --resource-group "" - Run the followin comand on the console to export the env variables (at the /frontend folder level) ```bash -export $FAST_API_SERVER = "" +export FAST_API_SERVER = "" export $(grep -v '^#' ../../credentials.env | sed -E '/^\s*$/d;s/#.*//' | xargs) ``` - Run the stramlit server on port 8500 diff --git a/apps/frontend/app/pages/3_FastAPI_Chat.py b/apps/frontend/app/pages/3_FastAPI_Chat.py index a98e5b79..50ded62a 100644 --- a/apps/frontend/app/pages/3_FastAPI_Chat.py +++ b/apps/frontend/app/pages/3_FastAPI_Chat.py @@ -73,12 +73,16 @@ display_chat_history() logger.debug("Displayed existing chat history.") + # ----------------------------------------------------------------------------- -# Input for text messages (bottom of the page) +# Handle User Input (Text & Audio) # ----------------------------------------------------------------------------- user_query = st.chat_input("Type your message here...") -# If we have audio input, transcribe and add to chat +# Track whether a new user message was added +new_user_message = False + +# Handle audio input if audio_bytes: transcript = speech_to_text(audio_bytes) logger.debug(f"Transcript from STT: {transcript}") @@ -87,26 +91,25 @@ with st.chat_message("Human"): st.write(transcript) logger.info("Transcript added to chat history.") + new_user_message = True -# If there's a typed user query, add it to chat history -if user_query is not None and user_query.strip(): +# Handle text input (st.chat_input) +if user_query is not None and user_query.strip() and not new_user_message: st.session_state.chat_history.append(HumanMessage(content=user_query)) with st.chat_message("Human"): st.markdown(user_query) logger.info("User query added to chat history: %s", user_query) + new_user_message = True # ----------------------------------------------------------------------------- # Generate AI response if the last message is from a Human # ----------------------------------------------------------------------------- -if not isinstance(st.session_state.chat_history[-1], AIMessage): +if new_user_message and not isinstance(st.session_state.chat_history[-1], AIMessage): with st.chat_message("AI"): try: - # SSE streaming: We read partial chunks from consume_api() logger.info("Sending request to SSE /stream endpoint with user query.") - # The last message in chat_history is the user's question user_text = st.session_state.chat_history[-1].content - # st.write_stream is a Streamlit function that streams from a generator ai_response = st.write_stream( consume_api(api_url, user_text, session_id, user_id) ) @@ -116,19 +119,20 @@ st.error("Failed to get a response from the AI.") ai_response = None - # If we got a response, store it in chat_history as an AIMessage + # Append AI response to chat history if ai_response: st.session_state.chat_history.append(AIMessage(content=ai_response)) - # If voice is enabled, convert AI response to speech + # Voice Output (if enabled) if voice_enabled: try: audio_file_path = text_to_speech(ai_response) if audio_file_path: autoplay_audio(audio_file_path) logger.info("Audio response generated and played.") - # Remove the temporary file to avoid clutter os.remove(audio_file_path) logger.info("Temporary audio file removed.") except Exception as ex: logger.error(f"Error generating or playing audio: {ex}", exc_info=True) + + diff --git a/credentials.env b/credentials.env index 0ca9cec2..99a9fa29 100644 --- a/credentials.env +++ b/credentials.env @@ -43,3 +43,5 @@ AZURE_SPEECH_VOICE_NAME="en-US-AriaNeural" BOT_ID="ENTER YOUR VALUE HERE" # This is the name of your bot service created in Notebook 12 BOT_SERVICE_DIRECT_LINE_SECRET="ENTER YOUR VALUE HERE" # Find this in Azure Bot Service -> Channels -> Direct Line + +