Skip to content

Commit

Permalink
fix issue when UI repeats last text input
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomarin committed Jan 3, 2025
1 parent 8ce1181 commit 307da24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apps/frontend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ az webapp deployment source config-zip --resource-group "<resource-group-name>"

- Run the followin comand on the console to export the env variables (at the /frontend folder level)
```bash
export $FAST_API_SERVER = "<your-fastAPI-server-url>"
export FAST_API_SERVER = "<your-fastAPI-server-url>"
export $(grep -v '^#' ../../credentials.env | sed -E '/^\s*$/d;s/#.*//' | xargs)
```
- Run the stramlit server on port 8500
Expand Down
26 changes: 15 additions & 11 deletions apps/frontend/app/pages/3_FastAPI_Chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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)
)
Expand All @@ -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)


2 changes: 2 additions & 0 deletions credentials.env
Original file line number Diff line number Diff line change
Expand Up @@ -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



0 comments on commit 307da24

Please sign in to comment.