yashjainme commited on
Commit
dc72841
·
verified ·
1 Parent(s): 1d8de16

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +126 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ import requests
4
+ import os
5
+ from datetime import datetime, timedelta
6
+
7
+ # --- Configuration ---
8
+ BACKEND_URL = os.getenv("BACKEND_URL", "http://127.0.0.1:8000/chat")
9
+
10
+ # --- Streamlit Page Setup ---
11
+ st.set_page_config(
12
+ page_title="Calendar AI Assistant",
13
+ page_icon="📅",
14
+ layout="centered"
15
+ )
16
+
17
+ st.title("📅 Calendar AI Assistant")
18
+ st.caption("I can help you book meetings. Try asking: 'Book an appointment on July 8th at 5:00 PM for 1 hour'")
19
+
20
+ # --- Session State Initialization ---
21
+ # 'messages' will store the chat history
22
+ if "messages" not in st.session_state:
23
+ st.session_state.messages = [
24
+ {"role": "assistant", "content": "Hello! How can I help you schedule an event today?"}
25
+ ]
26
+
27
+ # Initialize prompt state if it doesn't exist
28
+ if "current_prompt" not in st.session_state:
29
+ st.session_state.current_prompt = ""
30
+
31
+ # --- Helper Functions ---
32
+ def get_default_datetime():
33
+ """Get a default datetime for tomorrow at 5:00 PM"""
34
+ tomorrow = datetime.now() + timedelta(days=1)
35
+ return tomorrow.replace(hour=17, minute=0, second=0, microsecond=0)
36
+
37
+ def format_datetime_for_prompt(dt):
38
+ """Format datetime for the prompt"""
39
+ return dt.strftime("%B %d at %I:%M %p")
40
+
41
+ def send_message_to_backend(prompt):
42
+ """Send message to backend and handle response"""
43
+ # Add user message to chat history and display it
44
+ st.session_state.messages.append({"role": "user", "content": prompt})
45
+ with st.chat_message("user"):
46
+ st.markdown(prompt)
47
+
48
+ # Prepare request for the backend
49
+ history_for_api = [
50
+ {"role": msg["role"], "content": msg["content"]}
51
+ for msg in st.session_state.messages[:-1] # Exclude the latest user message
52
+ ]
53
+
54
+ payload = {
55
+ "message": prompt,
56
+ "history": history_for_api
57
+ }
58
+
59
+ # Get response from the backend
60
+ with st.chat_message("assistant"):
61
+ with st.spinner("Thinking..."):
62
+ try:
63
+ response = requests.post(BACKEND_URL, json=payload, timeout=120)
64
+ response.raise_for_status()
65
+
66
+ assistant_response = response.json().get("response")
67
+ st.markdown(assistant_response)
68
+
69
+ # Add assistant response to chat history
70
+ st.session_state.messages.append({"role": "assistant", "content": assistant_response})
71
+
72
+ except requests.exceptions.RequestException as e:
73
+ error_message = f"Sorry, I couldn't connect to the backend. Please make sure it's running. Error: {e}"
74
+ st.error(error_message)
75
+ st.session_state.messages.append({"role": "assistant", "content": error_message})
76
+ except Exception as e:
77
+ error_message = f"An unexpected error occurred: {e}"
78
+ st.error(error_message)
79
+ st.session_state.messages.append({"role": "assistant", "content": error_message})
80
+
81
+ # --- Quick Action Buttons ---
82
+ st.markdown("### Quick Actions")
83
+ col1, col2 = st.columns(2)
84
+
85
+ with col1:
86
+ if st.button("📅 Schedule 1 Hour Meeting", use_container_width=True):
87
+ default_dt = get_default_datetime()
88
+ st.session_state.current_prompt = f"Book an appointment on {format_datetime_for_prompt(default_dt)} for 1 hour"
89
+ st.rerun()
90
+
91
+ with col2:
92
+ if st.button("⏰ Schedule 30 Min Meeting", use_container_width=True):
93
+ default_dt = get_default_datetime()
94
+ st.session_state.current_prompt = f"Book an appointment on {format_datetime_for_prompt(default_dt)} for 30 minutes"
95
+ st.rerun()
96
+
97
+ st.markdown("---")
98
+
99
+ # --- UI Rendering ---
100
+
101
+ # Display chat messages from history
102
+ for message in st.session_state.messages:
103
+ with st.chat_message(message["role"]):
104
+ st.markdown(message["content"])
105
+
106
+ # --- Chat Input Handling ---
107
+ # Use a form with text input that supports pre-population
108
+ with st.form(key="chat_form", clear_on_submit=True):
109
+ col1, col2 = st.columns([5, 1])
110
+
111
+ with col1:
112
+ prompt = st.text_input(
113
+ "What would you like to do?",
114
+ value=st.session_state.current_prompt,
115
+ placeholder="Type your message here...",
116
+ label_visibility="collapsed"
117
+ )
118
+
119
+ with col2:
120
+ submit_button = st.form_submit_button("Send", use_container_width=True)
121
+
122
+ if submit_button and prompt:
123
+ # Clear the current prompt after using it
124
+ st.session_state.current_prompt = ""
125
+ send_message_to_backend(prompt)
126
+ st.rerun()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ streamlit
2
+ requests