-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
427 lines (351 loc) · 15.9 KB
/
main.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import openai
import os
from dotenv import load_dotenv
import telebot
import requests
from pyswip.prolog import Prolog
from sqlalchemy import create_engine, Column, String, Float, Integer, MetaData, UniqueConstraint, JSON, Text, Boolean
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
load_dotenv()
# keys
BOT_TOKEN = os.getenv('T_TOKEN')
G_API_KEY = os.getenv('G_API_KEY')
API_KEY = os.getenv('OPENAI_API_KEY')
bot = telebot.TeleBot(BOT_TOKEN)
openai.api_key = os.getenv('OPENAI_API_KEY')
# Questions and options for the menus
questions_and_options = {
"Select your city:": ["Seoul", "London", "Berlin", "Buenos Aires", "Hyderabad", "San Francisco", "Taipei"],
"What type of place you are looking for?": ["Cafe", "Restaurant", "Co-working", "Bar", "Night Club"],
"Do you prefer a quiet or lively atmosphere?": ["Quiet", "Lively"],
"Do you require internet access?": ["Yes", "No"],
"Do you need access to power outlets?": ["Yes", "No"],
"Would you like to have the option to purchase food at the place? ": ["Yes", "No"],
"What is your budget for spending?": ["Free", "Inexpensive", "Moderate", "Expensive", "Very Expensive"],
"How many star ratings should the place have? ": ["1", "2", "3", "4", "5"]
}
coordinates = {
"Seoul": {
"Lat": 37.5665,
"Lng": 126.9780,
},
"London": {
"Lat": 51.5074,
"Lng": -0.1278,
},
"Berlin": {
"Lat": 52.5200,
"Lng": 13.4050,
},
"Buenos Aires": {
"Lat": -34.6037,
"Lng": -58.3816,
},
"Hyderabad": {
"Lat": 17.3850,
"Lng": 78.4867,
},
"San Francisco": {
"Lat": 37.7749,
"Lng": -122.4194,
},
"Taipei": {
"Lat": 25.0330,
"Lng": 121.5654,
}
}
# Dictionary to hold user session data
user_sessions = {}
# Define the base class
Base = declarative_base()
# Define the Place model
class Place(Base):
__tablename__ = 'places'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
latitude = Column(Float, nullable=False)
longitude = Column(Float, nullable=False)
rating = Column(Float)
photos = Column(JSON) # Storing JSON data
description = Column(Text)
price_level = Column(Integer) # Google's price level ranging from 0 (free) to 4 (very expensive)
atmosphere = Column(String) # Quiet or Lively
internet_access = Column(Boolean) # Yes or No
power_outlet_access = Column(Boolean) # Yes or No
food_availability = Column(Boolean) # Yes or No
def __repr__(self):
return f"<Place(name={self.name}, latitude={self.latitude}, longitude={self.longitude}, rating={self.rating}, price_level={self.price_level})>"
# Create an engine
engine = create_engine('sqlite:///places.db')
# Create all tables by issuing CREATE TABLE commands to the DB.
Base.metadata.create_all(engine)
# Create a sessionmaker bound to the engine
Session = sessionmaker(bind=engine)
def fetch_place_photos(photo_id):
photo_url = f"https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference={photo_id}&key={G_API_KEY}"
return photo_url
def fetch_place_details(place_id):
details_url = f"https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&key={G_API_KEY}"
response = requests.get(details_url)
if response.status_code == 200:
return response.json().get('result', {})
return {}
def fetch_places_from_google_maps(type_of_place, city):
city_coords = coordinates[city]
location = f"{city_coords['Lat']},{city_coords['Lng']}"
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={location}&radius=500&type={type_of_place}&key={G_API_KEY}"
response = requests.get(url)
places = []
if response.status_code == 200:
results = response.json().get('results', [])
for place in results[:5]:
details = fetch_place_details(place['place_id'])
places.append(details)
return places
def query_openai_for_attribute(prompt):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content": prompt}
]
)
return response.choices[0].message['content'].strip()
except Exception as e:
print(e)
return str(e)
def format_place_for_query(place):
# Extract necessary details to formulate a question context
name = place.get('name', 'This place')
address = place.get('vicinity', 'unknown address')
reviews = place.get('reviews', [])
review_excerpt = reviews[0]['text'] if reviews else 'No reviews available'
# Construct a concise description of the place
place_description = f"{name} located at {address}. Notable review: {review_excerpt}"
return place_description
def get_atmosphere(place):
place_description = format_place_for_query(place)
prompt = f"Is the atmosphere generally quiet or lively in the following place ['Quiet', 'Lively']? {place_description}"
response = query_openai_for_attribute(prompt)
normalized_response = response.lower()
print("Atmosphere: ", normalized_response)
if "quiet" in normalized_response:
return "Quiet"
else:
return "Lively"
def get_internet_access(place):
place_description = format_place_for_query(place)
prompt = f"Does this place offer internet access ['Yes', 'No']? {place_description}"
response = query_openai_for_attribute(prompt)
normalized_response = response.lower()
print("Internet: ", normalized_response)
if "yes" in normalized_response or "available" in normalized_response:
return True
else:
return False
def get_power_outlets(place):
place_description = format_place_for_query(place)
prompt = f"Are there power outlets available for public use at the following place ['Yes', 'No']? {place_description}"
response = query_openai_for_attribute(prompt)
normalized_response = response.lower()
print("Outlets: ", normalized_response)
if "yes" in normalized_response or "available" in normalized_response:
return True
else:
return False
def get_food_availability(place):
place_description = format_place_for_query(place)
prompt = f"Can visitors purchase food at the following place ['Yes', 'No']? {place_description}"
response = query_openai_for_attribute(prompt)
normalized_response = response.lower()
print("Food: ", normalized_response)
if "yes" in normalized_response or "available" in normalized_response:
return True
else:
return False
def update_prolog_kb(prolog, places, city, session):
city_coords = coordinates[city]
for place in places:
name = place.get('name', '').replace("'", '"')
lat = city_coords['Lat']
lng = city_coords['Lng']
rating = place.get('rating', 0)
photos = place.get('photos', [{}])[0].get('photo_reference', '')
description = place.get('description', '')
price_level = place.get('price_level', 0) # Extract price level
atmosphere = get_atmosphere(place)
internet_access = get_internet_access(place)
power_outlet_access = get_power_outlets(place)
food_availability = get_food_availability(place)
# Ensure existing places are updated or new entries are created
existing_place = session.query(Place).filter_by(name=name, latitude=lat, longitude=lng).first()
if existing_place:
if existing_place.rating != rating or existing_place.description != description:
existing_place.rating = rating
existing_place.photos = photos
existing_place.description = description
existing_place.price_level = price_level
existing_place.atmosphere = atmosphere
existing_place.internet_access = internet_access
existing_place.power_outlet_access = power_outlet_access
existing_place.food_availability = food_availability
session.add(existing_place)
else:
place_entry = Place(name=name, latitude=lat, longitude=lng, rating=rating, photos=photos,
description=description, price_level=price_level, atmosphere=atmosphere,
internet_access=internet_access, power_outlet_access=power_outlet_access, food_availability=food_availability)
session.add(place_entry)
# Update Prolog if necessary
command = f"add_place('{name}', {lat}, {lng}, {rating}, '{atmosphere}', '{internet_access}', '{power_outlet_access}', '{food_availability}', {price_level}, '{photos}')"
print(command)
next(prolog.query(command))
session.commit() # Commit the session at the end
def start_conversation(message):
user_id = message.chat.id
# Initialize user session
user_sessions[user_id] = {
'answers': {},
'questions': list(questions_and_options.keys()),
'current_question_index': 0
}
bot.send_message(user_id, "Hello Minervan! Let us start by learning about your address so that I can give you closer places:")
def ask_next_question(message, user_id):
session = user_sessions[user_id]
questions = session['questions']
current_index = session['current_question_index']
if current_index < len(questions):
question = questions[current_index]
options = questions_and_options[question]
markup = telebot.types.InlineKeyboardMarkup(row_width=2)
# Create a button for each option
buttons = [telebot.types.InlineKeyboardButton(option, callback_data=option) for option in options]
markup.add(*buttons)
bot.send_message(user_id, question, reply_markup=markup)
else:
display_summary(message, user_id)
def handle_callback(call):
user_id = call.message.chat.id
session = user_sessions[user_id]
question = session['questions'][session['current_question_index']]
current_index = session['current_question_index']
# Record the user's answer
session['answers'][question] = call.data
if current_index == 0:
city = call.data
session['city'] = city
if current_index == 1:
places = fetch_places_from_google_maps(call.data, session['city'])
print(places)
update_prolog_kb(prolog, places, session['city'], db_session)
result = list(prolog.query("list_places(PlacesList)"))
if result:
places_list = result[0].get('PlacesList', [])
if places_list:
for place in places_list:
print(f"Place: {place}")
else:
print("Places list is empty.")
else:
print("Query returned no results.")
session['current_question_index'] += 1
# Acknowledge the callback
bot.answer_callback_query(call.id, "You selected: " + call.data)
# Ask the next question or end
ask_next_question(call.message, user_id)
def display_summary(message, user_id):
session = user_sessions[user_id]
answers = session['answers']
city = answers.get("Select your city:") # Get the selected city name from answers
city_coords = coordinates.get(city, {"Lat": 0, "Lng": 0})
min_rating = answers.get("How many star ratings should the place have? ", "1") # Default to "1" if not specified
atmosphere = answers.get("Do you prefer a quiet or lively atmosphere?")
internet_access = 'True' if answers.get("Do you require internet access?") == 'Yes' else 'False'
power_outlet_access = 'True' if answers.get("Do you need access to power outlets?") == 'Yes' else 'False'
food_availability = 'True' if answers.get("Would you like to have the option to purchase food at the place? ") == 'Yes' else 'False'
budget = answers.get("What is your budget for spending?", "Inexpensive") # Default to "Inexpensive" if not specified
# Map budget answers to price levels if necessary (this part needs specific implementation details)
price_level_map = {"Free": 0, "Inexpensive": 1, "Moderate": 2, "Expensive": 3, "Very Expensive": 4}
price_level = price_level_map.get(budget, 0) # Default to 0 (Free) if not mapped
query = f"find_best_place({min_rating}, {float(city_coords['Lat'])}, {float(city_coords['Lng'])}, '{atmosphere}', {internet_access}, {power_outlet_access}, {food_availability}, {price_level}, Name, Rating)"
print(query)
best_places = list(prolog.query(query))
print("AAAA BEST", best_places)
if best_places:
response_text = f"Recommended places:\n"
for place in best_places:
print("AAA", place)
try:
chat_query = f"{place['Name']} with rating {place['Rating']}, "
if place['PlaceAtmosphere']:
chat_query += f"PlaceAtmosphere {place['PlaceAtmosphere']}, "
if place['PlaceInternetAccess']:
chat_query += f"PlaceInternetAccess {place['PlaceInternetAccess']}, "
if place['PlacePowerOutletAccess']:
chat_query += f"PlacePowerOutletAccess {place['PlacePowerOutletAccess']}, "
if place['PlaceFoodAvailability']:
chat_query += f"PlaceFoodAvailabilityn{place['PlaceFoodAvailability']}"
if place['PlacePriceLevel']:
chat_query += f"PlacePriceLevel {place['PlacePriceLevel']}"
chat_query += "\n"
except Exception as e:
pass
try:
print(chat_query)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"Write a nice telegram text saying you recomend this place: {chat_query} in {city}"}
]
)
response_text = response.choices[0].message['content'].strip()
except Exception as e:
print(e)
response_text = 'I apologize, but I am unable to provide a response at this time.'
else:
response_text = "No places found with your preferences."
bot.send_message(user_id, response_text)
# Optionally clear the session data if no longer needed
del user_sessions[user_id]
@bot.message_handler(commands=['start'])
def send_welcome(message):
start_conversation(message)
@bot.message_handler(
func=lambda message: message.chat.id in user_sessions and 'Address' not in user_sessions[message.chat.id][
'answers'])
def handle_address(message):
user_id = message.chat.id
user_sessions[user_id]['answers']['Address'] = message.text
bot.send_message(user_id, "Thank you for your address. Now let's move on to some questions.")
ask_next_question(message, user_id)
@bot.callback_query_handler(func=lambda call: True)
def callback_query(call):
handle_callback(call)
@bot.message_handler(commands=['start', 'restart'])
def handle_start_or_restart(message):
# Whether it's a start or a restart, treat it the same
start_conversation(message)
def get_city_info(query):
try:
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are an assistant knowledgeable about various cities and the favorite study spots in these cities. These 7 cities are Buenos Aires, Berlin, London, San Francisco, Hyderabad, Seoul and Taipei!"},
{"role": "user", "content": query}
]
)
return response.choices[0].message['content'].strip()
except Exception as e:
return str(e)
if __name__ == "__main__":
prolog = Prolog()
# Consult KB
kb_path = "kb.pl"
# Setup the database session
db_session = Session()
try:
prolog.consult(kb_path)
print("KB loaded successfully.")
except Exception as e:
print("Failed to load KB:", e)
bot.polling()