Skip to content

Commit

Permalink
fix: prevent multipes requests in chatbot recipe (containers#498)
Browse files Browse the repository at this point in the history
Fixes containers#497

Signed-off-by: Jeff MAURY <[email protected]>
  • Loading branch information
jeffmaury authored May 17, 2024
1 parent 484d7f8 commit d25371d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion recipes/natural_language_processing/chatbot/app/chatbot_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ def get_models():
with st.spinner("Checking Model Service Availability..."):
server = checking_model_service()

def enableInput():
st.session_state["input_disabled"] = False

def disableInput():
st.session_state["input_disabled"] = True

st.title("💬 Chatbot")
if "messages" not in st.session_state:
st.session_state["messages"] = [{"role": "assistant",
"content": "How can I help you?"}]
if "input_disabled" not in st.session_state:
enableInput()

for msg in st.session_state.messages:
st.chat_message(msg["role"]).write(msg["content"])
Expand Down Expand Up @@ -86,10 +94,11 @@ def memory():
verbose=False,
memory=memory())

if prompt := st.chat_input():
if prompt := st.chat_input(disabled=st.session_state["input_disabled"],on_submit=disableInput):
st.session_state.messages.append({"role": "user", "content": prompt})
st.chat_message("user").markdown(prompt)
response = chain.invoke(prompt)
st.chat_message("assistant").markdown(response["text"])
st.session_state.messages.append({"role": "assistant", "content": response["text"]})
enableInput()
st.rerun()

0 comments on commit d25371d

Please sign in to comment.