-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathapp.py
141 lines (112 loc) · 4.15 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
# pylint: disable = invalid-name
import os
import uuid
import streamlit as st
from langchain_core.messages import HumanMessage
from agents.agent import Agent
def populate_envs(sender_email, receiver_email, subject):
os.environ['FROM_EMAIL'] = sender_email
os.environ['TO_EMAIL'] = receiver_email
os.environ['EMAIL_SUBJECT'] = subject
def send_email(sender_email, receiver_email, subject, thread_id):
try:
populate_envs(sender_email, receiver_email, subject)
config = {'configurable': {'thread_id': thread_id}}
st.session_state.agent.graph.invoke(None, config=config)
st.success('Email sent successfully!')
# Clear session state
for key in ['travel_info', 'thread_id']:
st.session_state.pop(key, None)
except Exception as e:
st.error(f'Error sending email: {e}')
def initialize_agent():
if 'agent' not in st.session_state:
st.session_state.agent = Agent()
def render_custom_css():
st.markdown(
'''
<style>
.main-title {
font-size: 2.5em;
color: #333;
text-align: center;
margin-bottom: 0.5em;
font-weight: bold;
}
.sub-title {
font-size: 1.2em;
color: #333;
text-align: left;
margin-bottom: 0.5em;
}
.center-container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.query-box {
width: 80%;
max-width: 600px;
margin-top: 0.5em;
margin-bottom: 1em;
}
.query-container {
width: 80%;
max-width: 600px;
margin: 0 auto;
}
</style>
''', unsafe_allow_html=True)
def render_ui():
st.markdown('<div class="center-container">', unsafe_allow_html=True)
st.markdown('<div class="main-title">✈️🌍 AI Travel Agent 🏨🗺️</div>', unsafe_allow_html=True)
st.markdown('<div class="query-container">', unsafe_allow_html=True)
st.markdown('<div class="sub-title">Enter your travel query and get flight and hotel information:</div>', unsafe_allow_html=True)
user_input = st.text_area(
'Travel Query',
height=200,
key='query',
placeholder='Type your travel query here...',
)
st.markdown('</div>', unsafe_allow_html=True)
st.sidebar.image('images/ai-travel.png', caption='AI Travel Assistant')
return user_input
def process_query(user_input):
if user_input:
try:
thread_id = str(uuid.uuid4())
st.session_state.thread_id = thread_id
messages = [HumanMessage(content=user_input)]
config = {'configurable': {'thread_id': thread_id}}
result = st.session_state.agent.graph.invoke({'messages': messages}, config=config)
st.subheader('Travel Information')
st.write(result['messages'][-1].content)
st.session_state.travel_info = result['messages'][-1].content
except Exception as e:
st.error(f'Error: {e}')
else:
st.error('Please enter a travel query.')
def render_email_form():
send_email_option = st.radio('Do you want to send this information via email?', ('No', 'Yes'))
if send_email_option == 'Yes':
with st.form(key='email_form'):
sender_email = st.text_input('Sender Email')
receiver_email = st.text_input('Receiver Email')
subject = st.text_input('Email Subject', 'Travel Information')
submit_button = st.form_submit_button(label='Send Email')
if submit_button:
if sender_email and receiver_email and subject:
send_email(sender_email, receiver_email, subject, st.session_state.thread_id)
else:
st.error('Please fill out all email fields.')
def main():
initialize_agent()
render_custom_css()
user_input = render_ui()
if st.button('Get Travel Information'):
process_query(user_input)
if 'travel_info' in st.session_state:
render_email_form()
if __name__ == '__main__':
main()