-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
196 lines (167 loc) · 8.03 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import streamlit as st
import time
import uuid
from datetime import datetime
from zoneinfo import ZoneInfo
import rag
from db import (
save_conversation,
save_feedback,
get_recent_conversations,
get_feedback_stats,
init_db,
verify_conversation_saved
)
import logging
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
tz = ZoneInfo("Europe/Berlin")
def print_log(message):
print(message, flush=True)
def main():
print_log("Starting the Mental Health Assistant application")
st.title("Mental Health Assistant")
# Initialize the database
init_db()
# Session state initialization
if "conversation_id" not in st.session_state:
st.session_state.conversation_id = str(uuid.uuid4())
print_log(f"New conversation started with ID: {st.session_state.conversation_id}")
if "count" not in st.session_state:
st.session_state.count = 0
print_log("Feedback count initialized to 0")
if "feedback_given" not in st.session_state:
st.session_state.feedback_given = False
if "past_questions" not in st.session_state:
st.session_state.past_questions = []
if "chat_history" not in st.session_state:
st.session_state.chat_history = [] # Stores the questions and answers for display
if "clear_chat" not in st.session_state:
st.session_state.clear_chat = False
# Session state for user input
if "user_input" not in st.session_state:
st.session_state.user_input = "" # This will control the text input field value
# Check if we need to clear the chat
if st.session_state.clear_chat:
st.session_state.chat_history = []
st.session_state.clear_chat = False
# Model selection
model_choice = st.selectbox(
"Select a model:",
["gemma2-9b-it", "llama-3.1-70b-versatile", "llama3-70b-8192", "mixtral-8x7b-32768"],
)
print_log(f"User selected model: {model_choice}")
# User input text box
user_input = st.text_input("Ask a question about mental health:", value=st.session_state.user_input)
if "last_conversation_id" not in st.session_state:
st.session_state.last_conversation_id = None
if st.button("Ask"):
logger.debug(f"Ask button pressed. User input: {user_input}")
# Check if input is valid
if user_input.strip() == "":
st.warning("Please enter a question before asking.")
elif user_input in st.session_state.past_questions:
st.warning("You've already asked this question.")
else:
# Proceed with getting an answer from the assistant
print_log(f"User asked: '{user_input}'")
with st.spinner("Processing..."):
print_log(f"Getting answer from assistant using {model_choice} model")
start_time = time.time()
answer_data = rag.rag(user_input, model=model_choice)
end_time = time.time()
print_log(f"Answer received in {end_time - start_time:.2f} seconds")
st.success("Completed!")
st.write(answer_data["answer"])
# Store the conversation in chat history
st.session_state.chat_history.append({
"question": user_input,
"answer": answer_data["answer"],
"relevance": answer_data["relevance"],
"model": answer_data["model_used"]})
# Display monitoring information
st.write(f"Response time: {answer_data['response_time']:.2f} seconds")
st.write(f"Relevance: {answer_data['relevance']}")
st.write(f"Model used: {answer_data['model_used']}")
st.write(f"Total tokens: {answer_data['total_tokens']}")
# Save conversation to database
logger.debug(f"Attempting to save conversation: {st.session_state.conversation_id}")
save_conversation(st.session_state.conversation_id, user_input, answer_data)
logger.debug(f"Conversation saved. Verifying...")
verify_conversation_saved(st.session_state.conversation_id)
# Update the last_conversation_id and reset feedback
st.session_state.last_conversation_id = st.session_state.conversation_id
st.session_state.conversation_id = str(uuid.uuid4()) # New conversation ID for the next question
st.session_state.feedback_given = False # Reset feedback state
st.session_state.past_questions.append(user_input) # Add the question to past questions
# Clear the input field by resetting session state variable
st.session_state.user_input = "" # Reset input for next question
# Feedback buttons
col1, col2 = st.columns(2)
with col1:
if st.button("+1"):
if st.session_state.last_conversation_id and not st.session_state.feedback_given:
save_feedback(st.session_state.last_conversation_id, 1)
st.success("Positive feedback saved!")
st.session_state.feedback_given = True # Mark feedback as given
st.session_state.last_conversation_id = None # Clear last conversation
st.session_state.clear_chat = True # Set flag to clear chat on next rerun
st.rerun() # Rerun the app to refresh the UI
elif st.session_state.feedback_given:
st.warning("Feedback has already been provided for this conversation.")
else:
st.warning("No conversation to provide feedback for.")
with col2:
if st.button("-1"):
if st.session_state.last_conversation_id and not st.session_state.feedback_given:
save_feedback(st.session_state.last_conversation_id, -1)
st.success("Negative feedback saved!")
st.session_state.feedback_given = True # Mark feedback as given
st.session_state.last_conversation_id = None # Clear last conversation
st.session_state.clear_chat = True # Set flag to clear chat on next rerun
st.rerun() # Rerun the app to refresh the UI
elif st.session_state.feedback_given:
st.warning("Feedback has already been provided for this conversation.")
else:
st.warning("No conversation to provide feedback for.")
# Display feedback status
if st.session_state.feedback_given:
st.info("Feedback has already been provided for this conversation.")
else:
st.info("You can provide feedback for the current conversation.")
# Display chat history
if st.session_state.chat_history:
st.subheader("Chat History")
for chat in st.session_state.chat_history:
st.write(f"**Q:** {chat['question']}")
st.write(f"**A:** {chat['answer']}")
st.write(f"*Relevance: {chat['relevance']}, Model: {chat['model']}*")
st.write("---")
# Display recent conversations
st.subheader("Recent Conversations")
relevance_filter = st.selectbox(
"Filter by relevance:",
["All", "RELEVANT", "PARTLY_RELEVANT", "NON_RELEVANT"]
)
recent_conversations = get_recent_conversations(
limit=3,
relevance=relevance_filter if relevance_filter != "All" else None
)
for conv in recent_conversations:
st.write(f"Q: {conv['question']}")
st.write(f"A: {conv['answer']}")
st.write(f"Relevance: {conv['relevance']}")
st.write(f"Model: {conv['model_used']}")
st.write("---")
# Display feedback stats
feedback_stats = get_feedback_stats()
st.subheader("Feedback Statistics")
st.write(f"Thumbs up: {feedback_stats['thumbs_up']}")
st.write(f"Thumbs down: {feedback_stats['thumbs_down']}")
# Generate a new conversation ID for the next question
st.session_state.conversation_id = str(uuid.uuid4())
print_log("Streamlit app loop completed")
if __name__ == "__main__":
print_log("Mental Health Assistant application started")
main()