gl-kp commited on
Commit
646aeb2
·
verified ·
1 Parent(s): 57cf192

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -0
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import sys
3
+ import os
4
+
5
+ # Add project directories to Python path
6
+ sys.path.append(os.path.join(os.path.dirname(__file__), 'services'))
7
+ sys.path.append(os.path.join(os.path.dirname(__file__), 'core'))
8
+ sys.path.append(os.path.join(os.path.dirname(__file__), 'config'))
9
+
10
+ from services.nutrition_bot import NutritionBot
11
+ from services.guardrails import filter_input_with_llama_guard
12
+
13
+ def nutrition_disorder_streamlit():
14
+ """A Streamlit-based UI for the Nutrition Disorder Specialist Agent."""
15
+ st.title("Nutrition Disorder Specialist")
16
+ st.write("Ask me anything about nutrition disorders, symptoms, causes, treatments, and more.")
17
+ st.write("Type 'exit' to end the conversation.")
18
+
19
+ # Initialize session state
20
+ if 'chat_history' not in st.session_state:
21
+ st.session_state.chat_history = []
22
+ if 'user_id' not in st.session_state:
23
+ st.session_state.user_id = None
24
+
25
+ # Login form
26
+ if st.session_state.user_id is None:
27
+ with st.form("login_form", clear_on_submit=True):
28
+ user_id = st.text_input("Please enter your name to begin:")
29
+ submit_button = st.form_submit_button("Login")
30
+ if submit_button and user_id:
31
+ st.session_state.user_id = user_id
32
+ st.session_state.chat_history.append({
33
+ "role": "assistant",
34
+ "content": f"Welcome, {user_id}! How can I help you with nutrition disorders today?"
35
+ })
36
+ st.session_state.login_submitted = True
37
+
38
+ if st.session_state.get("login_submitted", False):
39
+ st.session_state.pop("login_submitted")
40
+ st.rerun()
41
+ else:
42
+ # Display chat history
43
+ for message in st.session_state.chat_history:
44
+ with st.chat_message(message["role"]):
45
+ st.write(message["content"])
46
+
47
+ # Chat input
48
+ user_query = st.chat_input("Type your question here (or 'exit' to end)...")
49
+
50
+ if user_query:
51
+ # Check if user wants to exit
52
+ if user_query.lower() == "exit":
53
+ st.session_state.chat_history.append({"role": "user", "content": "exit"})
54
+ with st.chat_message("user"):
55
+ st.write("exit")
56
+ goodbye_msg = "Goodbye! Feel free to return if you have more questions about nutrition disorders."
57
+ st.session_state.chat_history.append({"role": "assistant", "content": goodbye_msg})
58
+ with st.chat_message("assistant"):
59
+ st.write(goodbye_msg)
60
+ st.session_state.user_id = None
61
+ st.rerun()
62
+ return
63
+
64
+ # Add user message to chat history
65
+ st.session_state.chat_history.append({"role": "user", "content": user_query})
66
+ with st.chat_message("user"):
67
+ st.write(user_query)
68
+
69
+ # Filter input
70
+ filtered_result = filter_input_with_llama_guard(user_query)
71
+
72
+ # Process through the agent
73
+ with st.chat_message("assistant"):
74
+ if filtered_result in ["safe", "unsafe S7", "unsafe S6"]:
75
+ try:
76
+ # Initialize chatbot if not already done
77
+ if 'chatbot' not in st.session_state:
78
+ st.session_state.chatbot = NutritionBot()
79
+
80
+ # Get response from the chatbot
81
+ response = st.session_state.chatbot.handle_customer_query(
82
+ st.session_state.user_id,
83
+ user_query
84
+ )
85
+
86
+ st.write(response)
87
+ st.session_state.chat_history.append({"role": "assistant", "content": response})
88
+ except Exception as e:
89
+ error_msg = f"Sorry, I encountered an error while processing your query. Please try again. Error: {str(e)}"
90
+ st.write(error_msg)
91
+ st.session_state.chat_history.append({"role": "assistant", "content": error_msg})
92
+ else:
93
+ inappropriate_msg = "I apologize, but I cannot process that input as it may be inappropriate. Please try again."
94
+ st.write(inappropriate_msg)
95
+ st.session_state.chat_history.append({"role": "assistant", "content": inappropriate_msg})
96
+
97
+ if __name__ == "__main__":
98
+ nutrition_disorder_streamlit()