DreamStream-1's picture
Update app.py
a9d7e8f verified
raw
history blame
3.11 kB
import gradio as gr
import pandas as pd
import requests
# Google Places API endpoint
url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
places_details_url = "https://maps.googleapis.com/maps/api/place/details/json"
# Your actual Google API Key (replace with your key)
api_key = "GOOGLE_API_KEY"
def get_places_data(query, location, radius, next_page_token=None):
params = {
"query": query,
"location": location,
"radius": radius,
"key": api_key
}
if next_page_token:
params["pagetoken"] = next_page_token
response = requests.get(url, params=params)
if response.status_code == 200:
return response.json()
else:
return {"error": f"Error: {response.status_code} - {response.text}"}
def get_place_details(place_id):
params = {"place_id": place_id, "key": api_key}
response = requests.get(places_details_url, params=params)
if response.status_code == 200:
details_data = response.json().get("result", {})
return {
"opening_hours": details_data.get("opening_hours", {}).get("weekday_text", "Not available"),
"phone_number": details_data.get("formatted_phone_number", "Not available"),
"website": details_data.get("website", "Not available")
}
else:
return {"error": f"Error: {response.status_code} - {response.text}"}
def get_all_places(query, location, radius):
all_results = []
next_page_token = None
while True:
data = get_places_data(query, location, radius, next_page_token)
if "error" in data:
return [data] # Return error message
results = data.get('results', [])
for place in results:
place_id = place.get("place_id")
details = get_place_details(place_id)
if "error" in details:
return [details] # Return error message
all_results.append({**place, **details})
next_page_token = data.get('next_page_token')
if not next_page_token:
break
return all_results
def display_data(query, location, radius):
results = get_all_places(query, location, radius)
if isinstance(results, list):
if results and isinstance(results[0], dict):
return pd.DataFrame(results)
else:
return pd.DataFrame() # Return an empty DataFrame if results is empty or not a list of dicts
else:
return pd.DataFrame() # Return an empty DataFrame if results is not a list
iface = gr.Interface(
fn=display_data,
inputs=[
gr.Textbox(lines=2, label="Search Query (e.g., 'therapist in Hawaii')"),
gr.Textbox(label="Location (latitude,longitude)"),
gr.Number(label="Radius (meters)")
],
outputs=gr.Dataframe(headers=["Name", "Address", "Phone", "Website", "Opening Hours", "Rating", "Types", "Latitude", "Longitude"], type="pandas"),
title="Hawaii Wellness Professionals Finder",
description="Find wellness professionals in Hawaii using the Google Places API."
)
iface.launch(share=True)