Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,14 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
5 |
-
import
|
6 |
-
|
7 |
-
# Google Maps API Key (replace with your actual key)
|
8 |
-
api_key = "GOOGLE_API_KEY"
|
9 |
|
10 |
-
# Google Places API
|
11 |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
12 |
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json"
|
13 |
|
14 |
-
#
|
|
|
|
|
15 |
def get_places_data(query, location, radius, next_page_token=None):
|
16 |
params = {
|
17 |
"query": query,
|
@@ -19,76 +16,64 @@ def get_places_data(query, location, radius, next_page_token=None):
|
|
19 |
"radius": radius,
|
20 |
"key": api_key
|
21 |
}
|
22 |
-
|
23 |
if next_page_token:
|
24 |
params["pagetoken"] = next_page_token
|
25 |
-
|
26 |
response = requests.get(url, params=params)
|
27 |
-
|
28 |
if response.status_code == 200:
|
29 |
-
print("Places data response:", response.json()) # Logging the response
|
30 |
return response.json()
|
31 |
else:
|
32 |
-
|
33 |
-
return None
|
34 |
|
35 |
-
# Function to fetch detailed information for a specific place using its place_id
|
36 |
def get_place_details(place_id):
|
37 |
-
params = {
|
38 |
-
"place_id": place_id,
|
39 |
-
"key": api_key
|
40 |
-
}
|
41 |
response = requests.get(places_details_url, params=params)
|
42 |
-
|
43 |
if response.status_code == 200:
|
44 |
-
print("Place details response:", response.json()) # Logging the response
|
45 |
details_data = response.json().get("result", {})
|
46 |
return {
|
47 |
-
"
|
48 |
-
"
|
49 |
-
"
|
50 |
-
"website": details_data.get("website", "")
|
51 |
}
|
52 |
else:
|
53 |
-
|
54 |
-
return {}
|
55 |
|
56 |
-
# Function to fetch all places data including pagination
|
57 |
def get_all_places(query, location, radius):
|
58 |
all_results = []
|
59 |
next_page_token = None
|
60 |
while True:
|
61 |
data = get_places_data(query, location, radius, next_page_token)
|
62 |
-
if data:
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
else:
|
75 |
break
|
76 |
-
|
77 |
return all_results
|
78 |
|
79 |
-
# Function to display the fetched data using Gradio
|
80 |
def display_data(query, location, radius):
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
83 |
|
84 |
-
# Gradio interface
|
85 |
iface = gr.Interface(
|
86 |
fn=display_data,
|
87 |
-
inputs=[
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
91 |
)
|
92 |
|
93 |
-
|
94 |
-
iface.launch(share=True)
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
+
import requests
|
|
|
|
|
|
|
4 |
|
5 |
+
# Google Places API endpoint
|
6 |
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
|
7 |
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json"
|
8 |
|
9 |
+
# Your actual Google API Key (replace with your key)
|
10 |
+
api_key = "GOOGLE_API_KEY" #Remember to replace this!
|
11 |
+
|
12 |
def get_places_data(query, location, radius, next_page_token=None):
|
13 |
params = {
|
14 |
"query": query,
|
|
|
16 |
"radius": radius,
|
17 |
"key": api_key
|
18 |
}
|
|
|
19 |
if next_page_token:
|
20 |
params["pagetoken"] = next_page_token
|
|
|
21 |
response = requests.get(url, params=params)
|
|
|
22 |
if response.status_code == 200:
|
|
|
23 |
return response.json()
|
24 |
else:
|
25 |
+
return {"error": f"Error: {response.status_code} - {response.text}"}
|
|
|
26 |
|
|
|
27 |
def get_place_details(place_id):
|
28 |
+
params = {"place_id": place_id, "key": api_key}
|
|
|
|
|
|
|
29 |
response = requests.get(places_details_url, params=params)
|
|
|
30 |
if response.status_code == 200:
|
|
|
31 |
details_data = response.json().get("result", {})
|
32 |
return {
|
33 |
+
"opening_hours": details_data.get("opening_hours", {}).get("weekday_text", "Not available"),
|
34 |
+
"phone_number": details_data.get("formatted_phone_number", "Not available"),
|
35 |
+
"website": details_data.get("website", "Not available")
|
|
|
36 |
}
|
37 |
else:
|
38 |
+
return {"error": f"Error: {response.status_code} - {response.text}"}
|
|
|
39 |
|
|
|
40 |
def get_all_places(query, location, radius):
|
41 |
all_results = []
|
42 |
next_page_token = None
|
43 |
while True:
|
44 |
data = get_places_data(query, location, radius, next_page_token)
|
45 |
+
if "error" in data:
|
46 |
+
return [data] #Return error message
|
47 |
+
results = data.get('results', [])
|
48 |
+
for place in results:
|
49 |
+
place_id = place.get("place_id")
|
50 |
+
details = get_place_details(place_id)
|
51 |
+
if "error" in details:
|
52 |
+
return [details] #Return error message
|
53 |
+
all_results.append({**place, **details})
|
54 |
+
|
55 |
+
next_page_token = data.get('next_page_token')
|
56 |
+
if not next_page_token:
|
|
|
57 |
break
|
|
|
58 |
return all_results
|
59 |
|
|
|
60 |
def display_data(query, location, radius):
|
61 |
+
results = get_all_places(query, location, radius)
|
62 |
+
if isinstance(results[0], dict):
|
63 |
+
return pd.DataFrame([results])
|
64 |
+
else:
|
65 |
+
return pd.DataFrame(results)
|
66 |
|
|
|
67 |
iface = gr.Interface(
|
68 |
fn=display_data,
|
69 |
+
inputs=[
|
70 |
+
gr.Textbox(lines=2, label="Search Query (e.g., 'therapist in Hawaii')"),
|
71 |
+
gr.Textbox(label="Location (latitude,longitude)"),
|
72 |
+
gr.Number(label="Radius (meters)")
|
73 |
+
],
|
74 |
+
outputs=gr.Dataframe(headers=["Name", "Address", "Phone", "Website", "Opening Hours", "Rating", "Types", "Latitude", "Longitude"], type="pandas"),
|
75 |
+
title="Hawaii Wellness Professionals Finder",
|
76 |
+
description="Find wellness professionals in Hawaii using the Google Places API."
|
77 |
)
|
78 |
|
79 |
+
iface.launch(share=True)
|
|