rajsinghparihar commited on
Commit
31f9688
·
1 Parent(s): 137c97d

add search hotels functionality

Browse files
Files changed (1) hide show
  1. app.py +114 -7
app.py CHANGED
@@ -2,17 +2,37 @@ import gradio as gr
2
  import os
3
  from dotenv import load_dotenv
4
  import serpapi
 
 
5
  load_dotenv()
6
 
7
- def search_flights(departure_id: str, arrival_id: str, outbound_date: str, return_date: str, currency: str):
 
 
 
 
 
 
 
8
  """
9
- Uses Google Flights API to search for flights, giving price insights, best flights and airport information
10
- params:
11
- departure_id (str): The departure airport IATA code.
12
- arrival_id (str): The arrival airport IATA code.
 
 
 
 
13
  outbound_date (str): The outbound date in YYYY-MM-DD format.
14
  return_date (str): The return date in YYYY-MM-DD format.
15
- currency (str): The currency code.
 
 
 
 
 
 
 
16
  """
17
  params = {
18
  "engine": "google_flights",
@@ -27,5 +47,92 @@ def search_flights(departure_id: str, arrival_id: str, outbound_date: str, retur
27
  result = serpapi.search(params)
28
  return result.as_dict()
29
 
30
- demo = gr.Interface(fn=search_flights, inputs=["text", "text", "text", "text", "text"], outputs=gr.JSON())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  demo.launch(mcp_server=True)
 
2
  import os
3
  from dotenv import load_dotenv
4
  import serpapi
5
+ from typing import Optional, Dict
6
+
7
  load_dotenv()
8
 
9
+
10
+ def search_flights(
11
+ departure_id: str,
12
+ arrival_id: str,
13
+ outbound_date: str,
14
+ return_date: str,
15
+ currency: str,
16
+ ) -> Dict:
17
  """
18
+ Search for flights using Google Flights API via SerpAPI.
19
+
20
+ This function retrieves flight information including prices, schedules, and airport details
21
+ for a given route and date range.
22
+
23
+ Args:
24
+ departure_id (str): The departure airport IATA code (e.g., "HYD" for Hyderabad).
25
+ arrival_id (str): The arrival airport IATA code (e.g., "HKT" for Phuket).
26
  outbound_date (str): The outbound date in YYYY-MM-DD format.
27
  return_date (str): The return date in YYYY-MM-DD format.
28
+ currency (str): The currency code for prices (e.g., "USD", "EUR").
29
+
30
+ Returns:
31
+ Dict: A dictionary containing flight search results including:
32
+ - Price insights
33
+ - Best flight options
34
+ - Airport information
35
+ - Flight schedules
36
  """
37
  params = {
38
  "engine": "google_flights",
 
47
  result = serpapi.search(params)
48
  return result.as_dict()
49
 
50
+
51
+ def search_hotels(
52
+ query: str,
53
+ check_in_date: str,
54
+ check_out_date: str,
55
+ num_adults: str,
56
+ currency: str,
57
+ sort_by_id: Optional[str] = None,
58
+ min_price: Optional[str] = None,
59
+ max_price: Optional[str] = None,
60
+ rating_id: Optional[str] = None,
61
+ ):
62
+ """
63
+ Uses Google Hotels API via SerpAPI to search for hotels and returns property information.
64
+
65
+ Args:
66
+ query (str): The location or hotel name to search for.
67
+ check_in_date (str): The check-in date in YYYY-MM-DD format.
68
+ check_out_date (str): The check-out date in YYYY-MM-DD format.
69
+ num_adults (str): Number of adult guests.
70
+ currency (str): The currency code for prices.
71
+ sort_by_id (Optional[str]): Parameter is used to sort results (Available options: "3" - Lowest price, "8" - Highest rating, "13" - Most reviewed)
72
+ min_price (Optional[str]): Filter for price lower limit.
73
+ max_price (Optional[str]): Filter for price upper limit.
74
+ rating_id (Optional[str]): Parameter is used for filtering the results to certain rating. (Available options: "7" - 3.5+, "8" - 4.0+, "9" - 4.5+)
75
+
76
+ Returns:
77
+ dict: A dictionary containing hotel properties with their details including
78
+ prices, ratings, amenities, and location information.
79
+ """
80
+ params = {
81
+ "engine": "google_hotels",
82
+ "q": query,
83
+ "check_in_date": check_in_date,
84
+ "check_out_date": check_out_date,
85
+ "adults": num_adults,
86
+ "currency": currency,
87
+ "hl": "en",
88
+ "api_key": os.getenv("SERP_API_KEY"),
89
+ }
90
+
91
+ if sort_by_id:
92
+ params["sort_by"] = sort_by_id
93
+
94
+ if min_price:
95
+ params["min_price"] = min_price
96
+
97
+ if max_price:
98
+ params["max_price"] = max_price
99
+
100
+ if rating_id:
101
+ params["rating"] = rating_id
102
+
103
+ result = serpapi.search(params)
104
+ result_dict = result.as_dict()
105
+ hotel_results = {"properties": []}
106
+ if "properties" in result_dict:
107
+ hotel_results["properties"] = result_dict["properties"]
108
+
109
+ return hotel_results
110
+
111
+
112
+ with gr.Blocks(theme="gradio/monochrome") as demo:
113
+ with gr.Tab("Search Flights"):
114
+ gr.Interface(
115
+ fn=search_flights,
116
+ inputs=[
117
+ "text",
118
+ "text",
119
+ gr.DateTime(include_time=False),
120
+ gr.DateTime(include_time=False),
121
+ "text",
122
+ ],
123
+ outputs=gr.JSON(),
124
+ )
125
+ with gr.Tab("Search Hotels"):
126
+ gr.Interface(
127
+ fn=search_hotels,
128
+ inputs=[
129
+ "text",
130
+ gr.DateTime(include_time=False),
131
+ gr.DateTime(include_time=False),
132
+ ]
133
+ + ["text"] * 6,
134
+ outputs=gr.JSON(),
135
+ )
136
+
137
+ demo.queue(max_size=1)
138
  demo.launch(mcp_server=True)