-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
263 lines (216 loc) · 10.1 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import pyrebase
import streamlit as st
import asyncio
import requests
from datetime import datetime
import re
import openai
import streamlit.components.v1 as components
from streamlit_pills import pills
import pycountry
import pandas as pd
import folium
from streamlit_folium import st_folium, folium_static
firebaseConfig = {
"apiKey": "AIzaSyCgRqAR-Hr5yNeG31Qgd9ROgBpAOZqRtPc",
"authDomain": "lifehack2023-f362e.firebaseapp.com",
"projectId": "lifehack2023-f362e",
"storageBucket": "lifehack2023-f362e.appspot.com",
"messagingSenderId": "141951242897",
"appId": "1:141951242897:web:afa573c83b30d45a19e530",
"measurementId": "G-EDRDB79V2N",
"databaseURL": "https://lifehack2023-f362e-default-rtdb.asia-southeast1.firebasedatabase.app/"
}
def isFloat(x):
try:
float(x)
return True
except:
return False
# String will look something like "x: (2.342,3.124),y: (5.2443,1.4536)"
def find_long_and_lat(s):
lst = []
curr_left, curr_comma, curr_right = -1, -1, -1
for i in range(len(s)):
if (s[i] == "("):
curr_left = i
elif (s[i] == ","):
if (curr_left != -1):
curr_comma = i
elif (s[i] == ")"):
if (curr_comma > curr_left):
curr_right = i
long = s[curr_left + 1: curr_comma]
lat = s[curr_comma + 2: curr_right]
if ((len(long) > 3) and (long[-1].isalpha())):
long = long[-3]
if ((len(lat) > 3) and (lat[-1].isalpha())):
lat = lat[-3]
if ((isFloat(long)) and (isFloat(lat))):
st.text(str(long) + " " + str(lat))
lst.append((long, lat))
curr_left, curr_comma, curr_right = -1, -1, -1
else:
curr_left, curr_comma = -1, -1
if (curr_comma > curr_left): #Check final
long = s[curr_left + 1: curr_comma]
lat = s[curr_comma + 2: len(s)]
if ((len(long) > 3) and (long[-1].isalpha())):
long = long[-3]
if ((len(lat) > 3) and (lat[-1].isalpha())):
lat = lat[-3]
if ((isFloat(long)) and (isFloat(lat))):
st.text(str(long) + " " + str(lat))
lst.append((long, lat))
return lst
base_url = "https://test.api.amadeus.com/v1/"
openai.api_key = st.secrets['openai_key']
firebase = pyrebase.initialize_app(firebaseConfig)
auth = firebase.auth()
st.session_state.authenticated = False
url = "https://test.api.amadeus.com/v1/security/oauth2/token"
data = {
"grant_type": "client_credentials",
"client_id": st.secrets["amadeus_key"],
"client_secret": st.secrets["amadeus_secret"]
}
response = requests.post(url, data=data)
access_token = response.json()["access_token"]
headers = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json"
}
db = firebase.database()
st.session_state.db = db
storage = firebase.storage()
st.sidebar.title("Your Travelling App")
# Authentication
authenticate = st.sidebar.selectbox('Login/Signup', ['Login', 'Signup'])
if authenticate == 'Signup':
email = st.sidebar.text_input('Enter your email address')
password = st.sidebar.text_input('Enter your password', type = 'password')
name = st.sidebar.text_input('Enter your Name')
place = st.sidebar.text_input('Enter your Location')
handle = st.sidebar.text_input('Please input your name', value = " ")
submit = st.sidebar.button('Sign Up')
if submit :
user = auth.create_user_with_email_and_password(email, password)
st.success('Your account is successfully made')
#Signing the user and create a user variable
user = auth.sign_in_with_email_and_password(email, password)
db.child(user['localId']).child("Handle").set(handle)
db.child(user['localId']).child("ID").set(user['localId'])
db.child(user['localId']).child("Location").set(place)
st.title("Hi, " + handle)
st.info('Login successfully')
if authenticate == 'Login' :
email = st.sidebar.text_input('Enter your email address')
password = st.sidebar.text_input('Enter your password', type = 'password')
submit = st.sidebar.button('Sign Up')
openai.api_key = st.secrets['openai_key']
st.subheader("AI Assistant : Streamlit + OpenAI: `stream` *argument*")
people_array = ""
date = st.date_input("Departure Date")
adults = st.number_input("Number of adults ", step=1, key="adult")
childrens = st.number_input("Number of childrens ", step=1, value=0, key="child")
childrens_string = ""
if childrens > 0 :
childrens_string = "&children=" + str(childrens)
for i in range(0, int(adults) + int(childrens)) :
user_input = st.text_area("Person " + str(i) ,placeholder = "Your suggestion", key="input" + str(i))
people_array += "People " + str(i + 1) + " likes to " + user_input + "."
chat_gpt = people_array + "Suggest 1 city according to these people in the format 'Bangkok'. City only "
pressed = st.button('Submit')
res_box = st.empty()
if pressed :
completions = openai.ChatCompletion.create(model="gpt-4", messages=[
{"role": "assistant",
"content": chat_gpt,
}],
temperature=0.5,
max_tokens=1000,
frequency_penalty=0.0,)
result = completions.choices[0].message.content
array = result.split(", ")
res_box.write(result)
for answer in array :
data = requests.get("https://test.api.amadeus.com/v1/reference-data/locations/cities?keyword=" + answer, headers=headers)
res = (data.json()['data'][0])
dataTwo = requests.get("https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=SYD&destinationLocationCode=" + res['iataCode'] +"&departureDate=" + str(date) + "&adults=" + str(int(adults)) + childrens_string + "¤cyCode=SGD&max=2", headers=headers)
dataThree = requests.get("https://test.api.amadeus.com/v2/duty-of-care/diseases/covid19-area-report?countryCode=" + res['address']['countryCode'], headers=headers)
st.write(dataTwo.json())
resTwo = dataTwo.json()['data']
st.session_state.flight = resTwo
for ansTwo in resTwo :
st.text("SGD " + ansTwo['price']['total'])
secondPrompt = "Plan me an itinerary of " + answer + "to do if" + people_array + ". Limit to 50 words each day for 5 days"
completions = openai.ChatCompletion.create(model="gpt-4", messages=[
{"role": "assistant",
"content": secondPrompt,
}],
temperature=0.2,
max_tokens=1000,
frequency_penalty=0.0,)
secondResult = completions.choices[0].message.content
st.text(secondResult)
st.session_state.itin = secondResult
thirdResult = ''
response2 = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "assistant",
"content": "Extract the places' longitude and latitude from this intinerary:" + secondResult + " in the strict number format without degree '(longitude, latitude)'",
}],
temperature=0,
max_tokens=64,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
thirdResult = response2.choices[0].message.content
thirdArray = find_long_and_lat(thirdResult)
mapData = []
for long, lat in thirdArray:
if ((isFloat(long)) and (isFloat(lat))):
st.text("Longtitude: " + long + ", Latitude: " + lat)
mama = requests.get("https://test.api.amadeus.com/v1/shopping/activities?longitude=" + long + "&latitude=" + lat + "&radius=100", headers=headers)
i = 0
for ans in mama.json()['data'] :
i += 1
if i == 5 :
break
st.text(ans['name'])
if len(ans['pictures']) != 0:
st.image(ans['pictures'][0])
if 'shortDescription' in ans:
components.html(ans['shortDescription'])
object = {
'name': ans['name'],
'latitude': lat,
'longitude': long,
'safe': str(dataThree.json()['data']['summary']['text']),
'bookingLink': ans['bookingLink'],
'price': ans['price']['amount']
}
if object not in mapData :
if object['name'] in mapData.values() :
mapData.append(object)
st.header("Analysis")
# Convert latitude and longitude columns to numeric format
dataMap = pd.DataFrame(mapData)
dataMap['latitude'] = pd.to_numeric(dataMap['latitude'])
dataMap['longitude'] = pd.to_numeric(dataMap['longitude'])
st.write(dataMap)
st.session_state.mapData = mapData
if submit :
user = auth.sign_in_with_email_and_password(email, password)
bio = st.radio('Jump to', ['Home', 'Meet Up'])
username = db.child(user['localId']).child("Handle").get()
st.session_state.name = username
st.session_state.user = user
if user is not None :
st.session_state.authenticated = True
if bio == 'Home' :
st.title("Home")
else :
st.title("Welcome")