Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,8 @@ import tqdm
|
|
| 31 |
# Streamlit app layout
|
| 32 |
st.title("🦜🔗 Reminder AI")
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
tagging_prompt = ChatPromptTemplate.from_template(
|
| 37 |
"""
|
|
@@ -263,7 +264,7 @@ Current time: {current_time}
|
|
| 263 |
Database sqlite:
|
| 264 |
{context}
|
| 265 |
|
| 266 |
-
If no date speicify assume is today, if user say one hour later or other, adding it into the current time to get the time (for example: Current time: 10pm, I said 1 hour later then 10pm + 1 hour is 11pm)
|
| 267 |
Don't say anything extra, if I am asking to remind something then only reply in this format:
|
| 268 |
|
| 269 |
INSERT INTO reminders (date, time, reason)
|
|
@@ -284,7 +285,7 @@ prompt_tt = ChatPromptTemplate.from_messages([("human", message_thr)])
|
|
| 284 |
rag_chain_tt = prompt_tt | llm_chat
|
| 285 |
|
| 286 |
# Load the check_reminder data
|
| 287 |
-
with open("important_classification_data.json", 'r') as f:
|
| 288 |
important_classification_data = json.load(f)
|
| 289 |
|
| 290 |
# Convert the intent classification data into Document format
|
|
@@ -299,12 +300,20 @@ vectorstore = Chroma.from_documents(documents=splits, embedding=HuggingFaceEmbed
|
|
| 299 |
# Set up the retriever to fetch relevant phrases based on the user's query
|
| 300 |
retriever = vectorstore.as_retriever()
|
| 301 |
|
| 302 |
-
if prompt := st.chat_input("Say something"):
|
| 303 |
-
messages.chat_message("user").write(prompt)
|
| 304 |
-
ai_response = handle_user_input(prompt)
|
| 305 |
-
messages.chat_message("assistant").write(ai_response)
|
| 306 |
|
| 307 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
time_now = get_current_time()
|
| 309 |
current_date = get_date()
|
| 310 |
|
|
@@ -320,7 +329,7 @@ def handle_user_input(user_input):
|
|
| 320 |
new_remind += new_reminder.content
|
| 321 |
print(f"New reminder: {new_remind}")
|
| 322 |
if new_remind == "repeated_reminder":
|
| 323 |
-
|
| 324 |
else:
|
| 325 |
try:
|
| 326 |
with sqlite3.connect('reminders.db') as conn:
|
|
@@ -332,12 +341,12 @@ def handle_user_input(user_input):
|
|
| 332 |
|
| 333 |
# Check if the update was successful
|
| 334 |
if cursor.rowcount > 0:
|
| 335 |
-
|
| 336 |
else:
|
| 337 |
-
|
| 338 |
|
| 339 |
except sqlite3.Error as e:
|
| 340 |
-
|
| 341 |
elif sentimenttocheck == "update reminder":
|
| 342 |
reminders_sec = get_reminders()
|
| 343 |
database_sec = format_reminders_for_context(reminders_sec)
|
|
@@ -346,7 +355,7 @@ def handle_user_input(user_input):
|
|
| 346 |
updated_remind += updated_cont.content
|
| 347 |
print(f"Database: {updated_remind}")
|
| 348 |
if updated_remind == "reminder_x":
|
| 349 |
-
|
| 350 |
else:
|
| 351 |
try:
|
| 352 |
with sqlite3.connect('reminders.db') as conn:
|
|
@@ -358,22 +367,31 @@ def handle_user_input(user_input):
|
|
| 358 |
|
| 359 |
# Check if the update was successful
|
| 360 |
if cursor.rowcount > 0:
|
| 361 |
-
|
| 362 |
else:
|
| 363 |
-
|
| 364 |
|
| 365 |
except sqlite3.Error as e:
|
| 366 |
-
|
| 367 |
elif sentimenttocheck == "check reminder":
|
| 368 |
reminders = get_reminders()
|
| 369 |
database = format_reminders_for_context(reminders)
|
| 370 |
response_max = ""
|
| 371 |
for max in rag_chain.stream({"question": user_input, "context": database, "date_tt": get_date()}):
|
| 372 |
response_max += max.content
|
| 373 |
-
|
| 374 |
elif sentimenttocheck == "remove reminder":
|
| 375 |
-
|
| 376 |
else:
|
| 377 |
-
ai_response
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# Streamlit app layout
|
| 32 |
st.title("🦜🔗 Reminder AI")
|
| 33 |
|
| 34 |
+
|
| 35 |
+
|
| 36 |
|
| 37 |
tagging_prompt = ChatPromptTemplate.from_template(
|
| 38 |
"""
|
|
|
|
| 264 |
Database sqlite:
|
| 265 |
{context}
|
| 266 |
|
| 267 |
+
If no date speicify assume is today, if user say one hour later or other, adding it into the current time to get the time (for example: Current time: 10pm, I said 1 hour later then 10pm + 1 hour is 11pm)
|
| 268 |
Don't say anything extra, if I am asking to remind something then only reply in this format:
|
| 269 |
|
| 270 |
INSERT INTO reminders (date, time, reason)
|
|
|
|
| 285 |
rag_chain_tt = prompt_tt | llm_chat
|
| 286 |
|
| 287 |
# Load the check_reminder data
|
| 288 |
+
with open("/content/drive/MyDrive/Colab_storage/important_classification_data.json", 'r') as f:
|
| 289 |
important_classification_data = json.load(f)
|
| 290 |
|
| 291 |
# Convert the intent classification data into Document format
|
|
|
|
| 300 |
# Set up the retriever to fetch relevant phrases based on the user's query
|
| 301 |
retriever = vectorstore.as_retriever()
|
| 302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
+
# Initialize a list to store chat messages
|
| 305 |
+
if "messages" not in st.session_state:
|
| 306 |
+
st.session_state.messages = []
|
| 307 |
+
|
| 308 |
+
# Text input for user message
|
| 309 |
+
user_input = st.text_input("You:", key="input")
|
| 310 |
+
|
| 311 |
+
|
| 312 |
+
# Loop for continuous interaction
|
| 313 |
+
if user_input:
|
| 314 |
+
try:
|
| 315 |
+
# Take user input
|
| 316 |
+
st.session_state.messages.append(f"You: {user_input}")
|
| 317 |
time_now = get_current_time()
|
| 318 |
current_date = get_date()
|
| 319 |
|
|
|
|
| 329 |
new_remind += new_reminder.content
|
| 330 |
print(f"New reminder: {new_remind}")
|
| 331 |
if new_remind == "repeated_reminder":
|
| 332 |
+
st.session_state.messages.append("Reminder already existed!")
|
| 333 |
else:
|
| 334 |
try:
|
| 335 |
with sqlite3.connect('reminders.db') as conn:
|
|
|
|
| 341 |
|
| 342 |
# Check if the update was successful
|
| 343 |
if cursor.rowcount > 0:
|
| 344 |
+
st.session_state.messages.append("New reminder created.")
|
| 345 |
else:
|
| 346 |
+
st.session_state.messages.append("No reminder created, Errors occur")
|
| 347 |
|
| 348 |
except sqlite3.Error as e:
|
| 349 |
+
st.session_state.messages.append(f"An error occurred while updating the reminder: {e}")
|
| 350 |
elif sentimenttocheck == "update reminder":
|
| 351 |
reminders_sec = get_reminders()
|
| 352 |
database_sec = format_reminders_for_context(reminders_sec)
|
|
|
|
| 355 |
updated_remind += updated_cont.content
|
| 356 |
print(f"Database: {updated_remind}")
|
| 357 |
if updated_remind == "reminder_x":
|
| 358 |
+
st.session_state.messages.append("No reminder found to change!")
|
| 359 |
else:
|
| 360 |
try:
|
| 361 |
with sqlite3.connect('reminders.db') as conn:
|
|
|
|
| 367 |
|
| 368 |
# Check if the update was successful
|
| 369 |
if cursor.rowcount > 0:
|
| 370 |
+
st.session_state.messages.append("Reminder updated successfully.")
|
| 371 |
else:
|
| 372 |
+
st.session_state.messages.append("No reminder found to update with the given details.")
|
| 373 |
|
| 374 |
except sqlite3.Error as e:
|
| 375 |
+
st.session_state.messages.append(f"An error occurred while updating the reminder: {e}")
|
| 376 |
elif sentimenttocheck == "check reminder":
|
| 377 |
reminders = get_reminders()
|
| 378 |
database = format_reminders_for_context(reminders)
|
| 379 |
response_max = ""
|
| 380 |
for max in rag_chain.stream({"question": user_input, "context": database, "date_tt": get_date()}):
|
| 381 |
response_max += max.content
|
| 382 |
+
st.session_state.messages.append(f"Database remind: {response_max}")
|
| 383 |
elif sentimenttocheck == "remove reminder":
|
| 384 |
+
st.session_state.messages.append("Remove Reminder")
|
| 385 |
else:
|
| 386 |
+
ai_response = get_ai_response(user_input)
|
| 387 |
+
st.session_state.messages.append(f"{ai_response}")
|
| 388 |
+
|
| 389 |
+
except Exception as e:
|
| 390 |
+
# Print the error and continue
|
| 391 |
+
st.session_state.messages.append(f"\nAn error occurred: {e}. Please try again.")
|
| 392 |
+
|
| 393 |
+
st.session_state.input = ""
|
| 394 |
|
| 395 |
+
# Display the chat history
|
| 396 |
+
for message in st.session_state.messages:
|
| 397 |
+
st.write(message)
|