Michaeldavidstein commited on
Commit
01c8010
·
verified ·
1 Parent(s): 32f33a9

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +51 -29
app.py CHANGED
@@ -748,6 +748,8 @@ class NutritionBot:
748
  traceback.print_exc()
749
 
750
  #=====================User Interface using streamlit ===========================#
 
 
751
  def nutrition_disorder_streamlit():
752
  """
753
  A Streamlit-based UI for the Nutrition Disorder Specialist Agent.
@@ -757,15 +759,15 @@ def nutrition_disorder_streamlit():
757
  st.write("Type 'exit' to end the conversation.")
758
 
759
  # Initialize session state for chat history and user_id if they don't exist
760
- if 'chat_history' not in st.session_state:
761
  st.session_state.chat_history = []
762
- if 'user_id' not in st.session_state:
763
  st.session_state.user_id = None
764
 
765
  # Login form: Only if user is not logged in
766
  if st.session_state.user_id is None:
767
  with st.form("login_form", clear_on_submit=True):
768
- user_id = st.text_input("Please enter your name to begin:")
769
  submit_button = st.form_submit_button("Login")
770
  if submit_button and user_id:
771
  st.session_state.user_id = user_id
@@ -776,7 +778,7 @@ def nutrition_disorder_streamlit():
776
  st.session_state.login_submitted = True # Set flag to trigger rerun
777
  if st.session_state.get("login_submitted", False):
778
  st.session_state.pop("login_submitted")
779
- st.rerun()
780
  else:
781
  # Display chat history
782
  for message in st.session_state.chat_history:
@@ -784,14 +786,15 @@ def nutrition_disorder_streamlit():
784
  st.write(message["content"])
785
 
786
  # Chat input with custom placeholder text
787
- user_query = st.chat_input("Type your question here (or 'exit' to end: ").strip() # Blank #1: Fill in the chat input prompt (e.g., "Type your question here (or 'exit' to end)...")
788
 
789
- # Check if user_query is None before calling .strip()
790
- if user_query:
791
- user_query = user_query.strip()
792
  else:
793
- user_query = "" # Default to an empty string if no input is provided
794
 
 
795
  if user_query:
796
  if user_query.lower() == "exit":
797
  st.session_state.chat_history.append({"role": "user", "content": "exit"})
@@ -802,34 +805,53 @@ def nutrition_disorder_streamlit():
802
  with st.chat_message("assistant"):
803
  st.write(goodbye_msg)
804
  st.session_state.user_id = None
805
- st.rerun()
806
  return
807
 
 
808
  st.session_state.chat_history.append({"role": "user", "content": user_query})
809
  with st.chat_message("user"):
810
  st.write(user_query)
811
 
812
- # Filter input using Llama Guard
813
- filtered_result = filter_input_with_llama_guard(user_query) # Blank #2: Fill in with the function name for filtering input (e.g., filter_input_with_llama_guard)
814
- filtered_result = filtered_result.replace("\n", " ") # Normalize the result
815
-
816
- # Check if input is safe based on allowed statuses
817
- if filtered_result in ["safe", "unsafe S7", "unsafe S6"]: # Blanks #3, #4, #5: Fill in with allowed safe statuses (e.g., "safe", "unsafe S7", "unsafe S6")
818
- try:
819
- if 'chatbot' not in st.session_state:
820
- st.session_state.chatbot = NutritionBot() # Blank #6: Fill in with the chatbot class initialization (e.g., NutritionBot)
821
- response = st.session_state.chatbot.andle_customer_query(st.session_state.user_id, user_query)
822
- # Blank #7: Fill in with the method to handle queries (e.g., handle_customer_query)
823
- st.write(response)
 
 
 
 
824
  st.session_state.chat_history.append({"role": "assistant", "content": response})
825
- except Exception as e:
826
- error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
827
  st.write(error_msg)
828
- st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
829
- else:
830
- inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
831
- st.write(inappropriate_msg)
832
- st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
 
 
 
 
833
 
834
  if __name__ == "__main__":
835
  nutrition_disorder_streamlit()
 
748
  traceback.print_exc()
749
 
750
  #=====================User Interface using streamlit ===========================#
751
+ import streamlit as st
752
+
753
  def nutrition_disorder_streamlit():
754
  """
755
  A Streamlit-based UI for the Nutrition Disorder Specialist Agent.
 
759
  st.write("Type 'exit' to end the conversation.")
760
 
761
  # Initialize session state for chat history and user_id if they don't exist
762
+ if "chat_history" not in st.session_state:
763
  st.session_state.chat_history = []
764
+ if "user_id" not in st.session_state:
765
  st.session_state.user_id = None
766
 
767
  # Login form: Only if user is not logged in
768
  if st.session_state.user_id is None:
769
  with st.form("login_form", clear_on_submit=True):
770
+ user_id = st.text_input("Please enter your name or id and press Login to begin:")
771
  submit_button = st.form_submit_button("Login")
772
  if submit_button and user_id:
773
  st.session_state.user_id = user_id
 
778
  st.session_state.login_submitted = True # Set flag to trigger rerun
779
  if st.session_state.get("login_submitted", False):
780
  st.session_state.pop("login_submitted")
781
+ st.experimental_rerun()
782
  else:
783
  # Display chat history
784
  for message in st.session_state.chat_history:
 
786
  st.write(message["content"])
787
 
788
  # Chat input with custom placeholder text
789
+ user_query = st.chat_input("Type your question here (or 'exit' to end): ")
790
 
791
+ # Check if user_query is None before processing
792
+ if user_query is not None:
793
+ user_query = user_query.strip()
794
  else:
795
+ user_query = "" # Default to an empty string if no input is provided
796
 
797
+ # Only process non-empty input
798
  if user_query:
799
  if user_query.lower() == "exit":
800
  st.session_state.chat_history.append({"role": "user", "content": "exit"})
 
805
  with st.chat_message("assistant"):
806
  st.write(goodbye_msg)
807
  st.session_state.user_id = None
808
+ st.experimental_rerun() # Reset the app for a new user
809
  return
810
 
811
+ # Add the user query to the chat history
812
  st.session_state.chat_history.append({"role": "user", "content": user_query})
813
  with st.chat_message("user"):
814
  st.write(user_query)
815
 
816
+ # Input filtering with Llama Guard
817
+ try:
818
+ filtered_result = filter_input_with_llama_guard(user_query) # Replace with your actual function
819
+ filtered_result = filtered_result.replace("\n", " ") # Normalize the result
820
+
821
+ # Check if input is safe based on allowed statuses
822
+ if filtered_result in ["safe", "unsafe S7", "unsafe S6"]: # Adjust these statuses as needed
823
+ if "chatbot" not in st.session_state:
824
+ st.session_state.chatbot = NutritionBot() # Replace with your chatbot initialization
825
+
826
+ # Handle the query
827
+ response = st.session_state.chatbot.handle_customer_query(
828
+ st.session_state.user_id, user_query
829
+ )
830
+ with st.chat_message("assistant"):
831
+ st.write(response)
832
  st.session_state.chat_history.append({"role": "assistant", "content": response})
833
+
834
+ else:
835
+ # Handle inappropriate input
836
+ inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
837
+ with st.chat_message("assistant"):
838
+ st.write(inappropriate_msg)
839
+ st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
840
+
841
+ except Exception as e:
842
+ # Handle errors in query processing
843
+ error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
844
+ with st.chat_message("assistant"):
845
  st.write(error_msg)
846
+ st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
847
+
848
+ else:
849
+ # Notify the user if the input is empty
850
+ with st.chat_message("assistant"):
851
+ st.write("Please type something to ask a question!")
852
+
853
+ if __name__ == "__main__":
854
+ nutrition_disorder_streamlit()
855
 
856
  if __name__ == "__main__":
857
  nutrition_disorder_streamlit()