File size: 3,109 Bytes
65d5924
 
1852538
65d5924
1852538
65d5924
 
 
1852538
a9d7e8f
1852538
65d5924
 
 
 
 
 
 
 
 
fffdb25
65d5924
 
 
1852538
65d5924
 
1852538
fffdb25
65d5924
 
 
1852538
 
 
65d5924
 
1852538
65d5924
 
 
 
 
 
1852538
a9d7e8f
1852538
 
 
 
 
a9d7e8f
1852538
 
 
 
65d5924
 
 
 
1852538
a9d7e8f
 
 
 
 
1852538
a9d7e8f
65d5924
 
 
1852538
 
 
 
 
 
 
 
65d5924
 
a9d7e8f
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
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)