Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -749,6 +749,7 @@ class NutritionBot:
|
|
749 |
|
750 |
#=====================User Interface using streamlit ===========================#
|
751 |
import streamlit as st
|
|
|
752 |
|
753 |
def nutrition_disorder_streamlit():
|
754 |
"""
|
@@ -766,7 +767,6 @@ def nutrition_disorder_streamlit():
|
|
766 |
|
767 |
# Login form: Only if user is not logged in
|
768 |
if st.session_state.user_id is None:
|
769 |
-
# Use a unique key for the form
|
770 |
with st.form(key="login_form", clear_on_submit=True):
|
771 |
user_id = st.text_input("Please enter your name to begin:")
|
772 |
submit_button = st.form_submit_button("Login")
|
@@ -776,10 +776,10 @@ def nutrition_disorder_streamlit():
|
|
776 |
"role": "assistant",
|
777 |
"content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?"
|
778 |
})
|
779 |
-
st.session_state.login_submitted = True
|
780 |
if st.session_state.get("login_submitted", False):
|
781 |
st.session_state.pop("login_submitted")
|
782 |
-
st.rerun()
|
783 |
else:
|
784 |
# Display chat history
|
785 |
for message in st.session_state.chat_history:
|
@@ -789,13 +789,11 @@ def nutrition_disorder_streamlit():
|
|
789 |
# Chat input with custom placeholder text
|
790 |
user_query = st.chat_input("Type your question here (or 'exit' to end): ")
|
791 |
|
792 |
-
# Check if user_query is None before processing
|
793 |
if user_query is not None:
|
794 |
user_query = user_query.strip()
|
795 |
else:
|
796 |
-
user_query = ""
|
797 |
|
798 |
-
# Only process non-empty input
|
799 |
if user_query:
|
800 |
if user_query.lower() == "exit":
|
801 |
st.session_state.chat_history.append({"role": "user", "content": "exit"})
|
@@ -806,23 +804,25 @@ def nutrition_disorder_streamlit():
|
|
806 |
with st.chat_message("assistant"):
|
807 |
st.write(goodbye_msg)
|
808 |
st.session_state.user_id = None
|
809 |
-
st.rerun()
|
810 |
return
|
811 |
|
812 |
-
# Add the user query to the chat history
|
813 |
st.session_state.chat_history.append({"role": "user", "content": user_query})
|
814 |
with st.chat_message("user"):
|
815 |
st.write(user_query)
|
816 |
|
817 |
# Input filtering with Llama Guard
|
818 |
try:
|
819 |
-
filtered_result = filter_input_with_llama_guard(user_query) # Replace with
|
820 |
-
filtered_result = filtered_result.replace("\n", " ")
|
821 |
|
822 |
-
|
823 |
-
if filtered_result in ["safe", "unsafe S7", "unsafe S6"]: # Adjust these statuses as needed
|
824 |
if "chatbot" not in st.session_state:
|
825 |
-
|
|
|
|
|
|
|
|
|
826 |
|
827 |
# Handle the query
|
828 |
response = st.session_state.chatbot.handle_customer_query(
|
@@ -833,21 +833,18 @@ def nutrition_disorder_streamlit():
|
|
833 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
834 |
|
835 |
else:
|
836 |
-
# Handle inappropriate input
|
837 |
inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
|
838 |
with st.chat_message("assistant"):
|
839 |
st.write(inappropriate_msg)
|
840 |
st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
|
841 |
|
842 |
except Exception as e:
|
843 |
-
# Handle errors in query processing
|
844 |
error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
|
845 |
with st.chat_message("assistant"):
|
846 |
st.write(error_msg)
|
847 |
st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
|
848 |
|
849 |
else:
|
850 |
-
# Notify the user if the input is empty
|
851 |
with st.chat_message("assistant"):
|
852 |
st.write("Please type something to ask a question!")
|
853 |
|
|
|
749 |
|
750 |
#=====================User Interface using streamlit ===========================#
|
751 |
import streamlit as st
|
752 |
+
import os
|
753 |
|
754 |
def nutrition_disorder_streamlit():
|
755 |
"""
|
|
|
767 |
|
768 |
# Login form: Only if user is not logged in
|
769 |
if st.session_state.user_id is None:
|
|
|
770 |
with st.form(key="login_form", clear_on_submit=True):
|
771 |
user_id = st.text_input("Please enter your name to begin:")
|
772 |
submit_button = st.form_submit_button("Login")
|
|
|
776 |
"role": "assistant",
|
777 |
"content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?"
|
778 |
})
|
779 |
+
st.session_state.login_submitted = True
|
780 |
if st.session_state.get("login_submitted", False):
|
781 |
st.session_state.pop("login_submitted")
|
782 |
+
st.rerun()
|
783 |
else:
|
784 |
# Display chat history
|
785 |
for message in st.session_state.chat_history:
|
|
|
789 |
# Chat input with custom placeholder text
|
790 |
user_query = st.chat_input("Type your question here (or 'exit' to end): ")
|
791 |
|
|
|
792 |
if user_query is not None:
|
793 |
user_query = user_query.strip()
|
794 |
else:
|
795 |
+
user_query = ""
|
796 |
|
|
|
797 |
if user_query:
|
798 |
if user_query.lower() == "exit":
|
799 |
st.session_state.chat_history.append({"role": "user", "content": "exit"})
|
|
|
804 |
with st.chat_message("assistant"):
|
805 |
st.write(goodbye_msg)
|
806 |
st.session_state.user_id = None
|
807 |
+
st.rerun()
|
808 |
return
|
809 |
|
|
|
810 |
st.session_state.chat_history.append({"role": "user", "content": user_query})
|
811 |
with st.chat_message("user"):
|
812 |
st.write(user_query)
|
813 |
|
814 |
# Input filtering with Llama Guard
|
815 |
try:
|
816 |
+
filtered_result = filter_input_with_llama_guard(user_query) # Replace with the actual function
|
817 |
+
filtered_result = filtered_result.replace("\n", " ")
|
818 |
|
819 |
+
if filtered_result in ["safe", "unsafe S7", "unsafe S6"]:
|
|
|
820 |
if "chatbot" not in st.session_state:
|
821 |
+
# Pass the API key and base to the chatbot initialization
|
822 |
+
st.session_state.chatbot = NutritionBot(
|
823 |
+
api_key=os.getenv("OPENAI_API_KEY"),
|
824 |
+
api_base=os.getenv("OPENAI_API_BASE")
|
825 |
+
)
|
826 |
|
827 |
# Handle the query
|
828 |
response = st.session_state.chatbot.handle_customer_query(
|
|
|
833 |
st.session_state.chat_history.append({"role": "assistant", "content": response})
|
834 |
|
835 |
else:
|
|
|
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 |
error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
|
843 |
with st.chat_message("assistant"):
|
844 |
st.write(error_msg)
|
845 |
st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
|
846 |
|
847 |
else:
|
|
|
848 |
with st.chat_message("assistant"):
|
849 |
st.write("Please type something to ask a question!")
|
850 |
|