Skip to content

Commit d25371d

Browse files
authored
fix: prevent multipes requests in chatbot recipe (#498)
Fixes #497 Signed-off-by: Jeff MAURY <jmaury@redhat.com>
1 parent 484d7f8 commit d25371d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

recipes/natural_language_processing/chatbot/app/chatbot_ui.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,18 @@ def get_models():
4646
with st.spinner("Checking Model Service Availability..."):
4747
server = checking_model_service()
4848

49+
def enableInput():
50+
st.session_state["input_disabled"] = False
51+
52+
def disableInput():
53+
st.session_state["input_disabled"] = True
54+
4955
st.title("💬 Chatbot")
5056
if "messages" not in st.session_state:
5157
st.session_state["messages"] = [{"role": "assistant",
5258
"content": "How can I help you?"}]
59+
if "input_disabled" not in st.session_state:
60+
enableInput()
5361

5462
for msg in st.session_state.messages:
5563
st.chat_message(msg["role"]).write(msg["content"])
@@ -86,10 +94,11 @@ def memory():
8694
verbose=False,
8795
memory=memory())
8896

89-
if prompt := st.chat_input():
97+
if prompt := st.chat_input(disabled=st.session_state["input_disabled"],on_submit=disableInput):
9098
st.session_state.messages.append({"role": "user", "content": prompt})
9199
st.chat_message("user").markdown(prompt)
92100
response = chain.invoke(prompt)
93101
st.chat_message("assistant").markdown(response["text"])
94102
st.session_state.messages.append({"role": "assistant", "content": response["text"]})
103+
enableInput()
95104
st.rerun()

0 commit comments

Comments
 (0)