Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -968,28 +968,37 @@ def fetch_yelp_restaurants():
|
|
| 968 |
from serpapi.google_search import GoogleSearch
|
| 969 |
from datetime import datetime, timedelta
|
| 970 |
|
| 971 |
-
|
| 972 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 973 |
|
|
|
|
| 974 |
params = {
|
| 975 |
"engine": "google_hotels",
|
| 976 |
"q": "Birmingham,AL",
|
| 977 |
-
"check_in_date":
|
| 978 |
-
"check_out_date":
|
| 979 |
-
"vacation_rentals": "
|
| 980 |
-
"amenities":
|
| 981 |
"rating": (7,8,9),
|
|
|
|
| 982 |
"currency": "USD",
|
| 983 |
"gl": "us",
|
| 984 |
"hl": "en",
|
| 985 |
"api_key": "74972e6cc9f9e092bd86764a78f7d18c1d71b156eca8b1fe7fd65c2406cfb588"
|
| 986 |
}
|
| 987 |
|
|
|
|
| 988 |
search = GoogleSearch(params)
|
| 989 |
results = search.get_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
hotels = results.get("hotels_results", [])
|
| 991 |
|
| 992 |
def star_rating(rating):
|
|
|
|
| 993 |
full_stars = int(float(rating))
|
| 994 |
half_star = 1 if (float(rating) - full_stars) >= 0.5 else 0
|
| 995 |
empty_stars = 5 - full_stars - half_star
|
|
@@ -998,26 +1007,30 @@ def fetch_hotels_birmingham():
|
|
| 998 |
|
| 999 |
response_text = ""
|
| 1000 |
|
| 1001 |
-
|
|
|
|
| 1002 |
name = hotel.get("title", "No name")
|
| 1003 |
rating = hotel.get("rating", "No rating")
|
| 1004 |
reviews = hotel.get("reviews", "No reviews")
|
| 1005 |
location = f"{name}, Birmingham, AL"
|
| 1006 |
price = hotel.get("price", "Price not available")
|
| 1007 |
link = hotel.get("link", "#")
|
| 1008 |
-
|
|
|
|
| 1009 |
|
| 1010 |
if isinstance(amenities, list):
|
| 1011 |
amenities = ", ".join(amenities)
|
| 1012 |
|
| 1013 |
# Format the output for each hotel
|
| 1014 |
-
response_text += f"[{name}]({link})\n" # Name with clickable link
|
| 1015 |
response_text += f"*{location}*\n" # Location with hotel name and "Birmingham, AL"
|
| 1016 |
response_text += f"**Price:** {price}\n"
|
| 1017 |
-
response_text += f"**
|
|
|
|
| 1018 |
response_text += f"**Rating:** {star_rating(rating)} ({rating} stars, {reviews} reviews)\n"
|
| 1019 |
response_text += "-" * 50 + "\n"
|
| 1020 |
|
|
|
|
| 1021 |
return response_text
|
| 1022 |
|
| 1023 |
|
|
@@ -1026,6 +1039,8 @@ def fetch_hotels_birmingham():
|
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
|
|
|
|
|
|
| 1029 |
with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
| 1030 |
with gr.Row():
|
| 1031 |
with gr.Column():
|
|
|
|
| 968 |
from serpapi.google_search import GoogleSearch
|
| 969 |
from datetime import datetime, timedelta
|
| 970 |
|
| 971 |
+
ef fetch_hotels_birmingham():
|
| 972 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 973 |
|
| 974 |
+
# Define the parameters for the Google Hotels API request
|
| 975 |
params = {
|
| 976 |
"engine": "google_hotels",
|
| 977 |
"q": "Birmingham,AL",
|
| 978 |
+
"check_in_date": "2024-08-14",
|
| 979 |
+
"check_out_date": "2024-08-15",
|
| 980 |
+
"vacation_rentals": "false",
|
| 981 |
+
# "amenities": (2,4,10,15,16,20,21,24,29,32),
|
| 982 |
"rating": (7,8,9),
|
| 983 |
+
"hotel_class": (2,3,4,5),
|
| 984 |
"currency": "USD",
|
| 985 |
"gl": "us",
|
| 986 |
"hl": "en",
|
| 987 |
"api_key": "74972e6cc9f9e092bd86764a78f7d18c1d71b156eca8b1fe7fd65c2406cfb588"
|
| 988 |
}
|
| 989 |
|
| 990 |
+
# Send the request to the Google Hotels API
|
| 991 |
search = GoogleSearch(params)
|
| 992 |
results = search.get_dict()
|
| 993 |
+
|
| 994 |
+
# Debugging: Print the results to ensure the API call is working
|
| 995 |
+
print("API Response:", results)
|
| 996 |
+
|
| 997 |
+
# Extract the hotel results from the response
|
| 998 |
hotels = results.get("hotels_results", [])
|
| 999 |
|
| 1000 |
def star_rating(rating):
|
| 1001 |
+
# Convert the rating to a star format with half-stars if necessary
|
| 1002 |
full_stars = int(float(rating))
|
| 1003 |
half_star = 1 if (float(rating) - full_stars) >= 0.5 else 0
|
| 1004 |
empty_stars = 5 - full_stars - half_star
|
|
|
|
| 1007 |
|
| 1008 |
response_text = ""
|
| 1009 |
|
| 1010 |
+
# Iterate through the top 5 hotels and format the response
|
| 1011 |
+
for hotel in hotels[:5]:
|
| 1012 |
name = hotel.get("title", "No name")
|
| 1013 |
rating = hotel.get("rating", "No rating")
|
| 1014 |
reviews = hotel.get("reviews", "No reviews")
|
| 1015 |
location = f"{name}, Birmingham, AL"
|
| 1016 |
price = hotel.get("price", "Price not available")
|
| 1017 |
link = hotel.get("link", "#")
|
| 1018 |
+
hotel_class = hotel.get("hotel_class", "Not available")
|
| 1019 |
+
# amenities = hotel.get("amenities", "Amenities not available")
|
| 1020 |
|
| 1021 |
if isinstance(amenities, list):
|
| 1022 |
amenities = ", ".join(amenities)
|
| 1023 |
|
| 1024 |
# Format the output for each hotel
|
| 1025 |
+
response_text += f"[{name}]({link})\n" # Name with clickable link
|
| 1026 |
response_text += f"*{location}*\n" # Location with hotel name and "Birmingham, AL"
|
| 1027 |
response_text += f"**Price:** {price}\n"
|
| 1028 |
+
response_text += f"**Hotel Class:** {hotel_class} stars\n"
|
| 1029 |
+
# response_text += f"**Amenities:** {amenities}\n"
|
| 1030 |
response_text += f"**Rating:** {star_rating(rating)} ({rating} stars, {reviews} reviews)\n"
|
| 1031 |
response_text += "-" * 50 + "\n"
|
| 1032 |
|
| 1033 |
+
# Return the formatted response
|
| 1034 |
return response_text
|
| 1035 |
|
| 1036 |
|
|
|
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
+
|
| 1043 |
+
|
| 1044 |
with gr.Blocks(theme='Pijush2023/scikit-learn-pijush') as demo:
|
| 1045 |
with gr.Row():
|
| 1046 |
with gr.Column():
|