import os import requests import gradio as gr import pandas as pd import time #Import missing time library # Google Maps API Key (replace with your actual key) api_key = "GOOGLE_API_KEY" # Google Places API endpoints url = "https://maps.googleapis.com/maps/api/place/textsearch/json" places_details_url = "https://maps.googleapis.com/maps/api/place/details/json" # Function to send a request to Google Places API and fetch places data 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: print("Places data response:", response.json()) # Logging the response return response.json() else: print(f"Error fetching places data: Status code {response.status_code}, {response.text}") #Improved error message return None # Function to fetch detailed information for a specific place using its place_id 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: print("Place details response:", response.json()) # Logging the response details_data = response.json().get("result", {}) return { "name": details_data.get("name", ""), "address": details_data.get("formatted_address", ""), "phone_number": details_data.get("formatted_phone_number", ""), "website": details_data.get("website", "") } else: print(f"Error fetching place details: Status code {response.status_code}, {response.text}") #Improved error message return {} # Function to fetch all places data including pagination 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 data: results = data.get('results', []) for place in results: place_id = place.get("place_id") details = get_place_details(place_id) all_results.append(details) next_page_token = data.get('next_page_token') if not next_page_token: break time.sleep(2) else: break return all_results # Function to display the fetched data using Gradio def display_data(query, location, radius): google_places_data = get_all_places(query, location, radius) return pd.DataFrame(google_places_data) # Gradio interface iface = gr.Interface( fn=display_data, inputs=["text", "text", "number"], outputs="dataframe", title="Wellness Professionals in Hawaii", description="Enter the search query, location coordinates, and radius to fetch wellness professionals from Google Places API." ) # Run the Gradio interface with sharing enabled iface.launch(share=True)