File size: 2,728 Bytes
65d5924
 
 
 
 
fffdb25
 
65d5924
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fffdb25
65d5924
 
 
 
 
 
 
 
 
 
 
 
fffdb25
65d5924
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fffdb25
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import requests
import gradio as gr
import pandas as pd

# Google Maps API Key (replace with your actual key)
api_key = "GOOGLE_MAPS_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:
        return response.json()
    else:
        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:
        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:
        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)