Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -307,20 +307,10 @@ chain_neo4j = (
|
|
307 |
def generate_answer(message, choice, retrieval_mode):
|
308 |
logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
|
309 |
|
310 |
-
# Check if the question is about flights
|
311 |
-
if "flight" in message.lower() and ("to" in message.lower() or "from" in message.lower()):
|
312 |
-
response = fetch_flight_info(message)
|
313 |
-
return response, extract_addresses(response)
|
314 |
-
|
315 |
# Check if the question is about restaurants
|
316 |
if "restaurant" in message.lower() and "birmingham" in message.lower():
|
317 |
response = fetch_yelp_restaurants()
|
318 |
return response, extract_addresses(response)
|
319 |
-
|
320 |
-
# Check if the question is about hotels
|
321 |
-
if "hotel" in message.lower() and "birmingham" in message.lower():
|
322 |
-
response = fetch_google_hotels()
|
323 |
-
return response, extract_addresses(response)
|
324 |
|
325 |
prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
|
326 |
|
@@ -973,108 +963,7 @@ def fetch_yelp_restaurants():
|
|
973 |
return response_text
|
974 |
|
975 |
|
976 |
-
from serpapi.google_search import GoogleSearch
|
977 |
-
from datetime import datetime, timedelta
|
978 |
-
|
979 |
-
def fetch_google_hotels():
|
980 |
-
import os
|
981 |
-
|
982 |
-
params = {
|
983 |
-
"engine": "google_hotels",
|
984 |
-
"q": "Birmingham Resorts",
|
985 |
-
"check_in_date": "2024-08-14",
|
986 |
-
"check_out_date": "2024-08-15",
|
987 |
-
"adults": "2",
|
988 |
-
"currency": "USD",
|
989 |
-
"gl": "us",
|
990 |
-
"hl": "en",
|
991 |
-
"api_key": os.getenv("SERP_API") # Ensure you use your SERP API key
|
992 |
-
}
|
993 |
-
|
994 |
-
search = GoogleSearch(params)
|
995 |
-
results = search.get_dict()
|
996 |
-
|
997 |
-
# Debugging: Print the entire response to check the content
|
998 |
-
print("Google Hotels API Response:", results)
|
999 |
-
|
1000 |
-
hotel_results = results.get("hotels_results", [])
|
1001 |
-
|
1002 |
-
if not hotel_results:
|
1003 |
-
print("No hotel results found.")
|
1004 |
-
return "No hotel information available."
|
1005 |
-
|
1006 |
-
def star_rating(rating):
|
1007 |
-
full_stars = int(float(rating))
|
1008 |
-
half_star = 1 if (float(rating) - full_stars) >= 0.5 else 0
|
1009 |
-
empty_stars = 5 - full_stars - half_star
|
1010 |
-
stars = "★" * full_stars + "½" * half_star + "☆" * empty_stars
|
1011 |
-
return stars
|
1012 |
-
|
1013 |
-
response_text = ""
|
1014 |
-
|
1015 |
-
for hotel in hotel_results[:5]: # Limiting to top 5 hotels
|
1016 |
-
name = hotel.get("title", "No name")
|
1017 |
-
rating = hotel.get("rating", "No rating")
|
1018 |
-
reviews = hotel.get("reviews", "No reviews")
|
1019 |
-
address = hotel.get("address", "Not Available")
|
1020 |
-
price = hotel.get("price", "Not Available")
|
1021 |
-
link = hotel.get("link", "#")
|
1022 |
-
|
1023 |
-
# Format the output for each hotel
|
1024 |
-
response_text += f"[{name}]({link})\n" # Name with clickable link, no bold
|
1025 |
-
response_text += f"*{name}, {address}*\n" # Location with hotel name and address
|
1026 |
-
response_text += f"**Rating:** {star_rating(rating)} ({rating} stars, {reviews} reviews)\n"
|
1027 |
-
response_text += f"**Price:** {price}\n"
|
1028 |
-
response_text += "-" * 50 + "\n"
|
1029 |
-
|
1030 |
-
# Debugging: Print the generated response text
|
1031 |
-
print("Generated Hotel Response Text:", response_text)
|
1032 |
-
|
1033 |
-
return response_text
|
1034 |
-
|
1035 |
-
|
1036 |
-
def fetch_flight_info(message):
|
1037 |
-
|
1038 |
-
departure_id = "JFK" # Replace with parsed value from the message
|
1039 |
-
arrival_id = "BHM" # Replace with parsed value from the message
|
1040 |
-
outbound_date = "2024-08-14" # Replace with parsed date from the message
|
1041 |
-
return_date = "2024-08-20" # Replace with parsed date if applicable
|
1042 |
-
|
1043 |
-
params = {
|
1044 |
-
"engine": "google_flights",
|
1045 |
-
"departure_id": departure_id,
|
1046 |
-
"arrival_id": arrival_id,
|
1047 |
-
"outbound_date": outbound_date,
|
1048 |
-
"return_date": return_date,
|
1049 |
-
"currency": "USD",
|
1050 |
-
"hl": "en",
|
1051 |
-
"api_key": os.getenv("SERP_API")
|
1052 |
-
}
|
1053 |
-
|
1054 |
-
search = GoogleSearch(params)
|
1055 |
-
results = search.get_dict()
|
1056 |
-
flights_results = results.get("flights_results", [])
|
1057 |
|
1058 |
-
response_text = ""
|
1059 |
-
|
1060 |
-
for flight in flights_results[:5]: # Limiting to top 5 flight options
|
1061 |
-
price = flight.get("price", "No price")
|
1062 |
-
duration = flight.get("duration", "No duration")
|
1063 |
-
stops = flight.get("stops", "Direct flight")
|
1064 |
-
departure_time = flight.get("departure_time", "No departure time")
|
1065 |
-
arrival_time = flight.get("arrival_time", "No arrival time")
|
1066 |
-
airlines = flight.get("airlines", ["No airline"])
|
1067 |
-
|
1068 |
-
# Format the output for each flight
|
1069 |
-
response_text += f"Flight: {', '.join(airlines)}\n"
|
1070 |
-
response_text += f"Price: {price}\n"
|
1071 |
-
response_text += f"Duration: {duration}\n"
|
1072 |
-
response_text += f"Stops: {stops}\n"
|
1073 |
-
response_text += f"Departure: {departure_time}\n"
|
1074 |
-
response_text += f"Arrival: {arrival_time}\n"
|
1075 |
-
response_text += "-" * 50 + "\n"
|
1076 |
-
|
1077 |
-
return response_text
|
1078 |
|
1079 |
|
1080 |
|
|
|
307 |
def generate_answer(message, choice, retrieval_mode):
|
308 |
logging.debug(f"generate_answer called with choice: {choice} and retrieval_mode: {retrieval_mode}")
|
309 |
|
|
|
|
|
|
|
|
|
|
|
310 |
# Check if the question is about restaurants
|
311 |
if "restaurant" in message.lower() and "birmingham" in message.lower():
|
312 |
response = fetch_yelp_restaurants()
|
313 |
return response, extract_addresses(response)
|
|
|
|
|
|
|
|
|
|
|
314 |
|
315 |
prompt_template = QA_CHAIN_PROMPT_1 if choice == "Details" else QA_CHAIN_PROMPT_2
|
316 |
|
|
|
963 |
return response_text
|
964 |
|
965 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
966 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
967 |
|
968 |
|
969 |
|