File size: 11,479 Bytes
1bdf519
 
 
 
 
 
 
b7a995b
 
5e5946d
 
b7a995b
 
 
 
 
 
fd09464
b7a995b
 
 
 
1bdf519
 
b7a995b
 
1bdf519
 
b7a995b
1bdf519
 
 
 
 
b7a995b
 
1bdf519
 
b7a995b
1bdf519
b7a995b
 
 
 
 
 
1bdf519
b7a995b
 
1bdf519
 
b7a995b
 
 
1bdf519
b7a995b
 
 
1bdf519
 
 
 
 
 
 
 
b7a995b
1bdf519
b7a995b
5e5946d
 
b7a995b
 
5e5946d
 
b7a995b
 
5e5946d
 
 
 
 
b7a995b
 
5e5946d
b7a995b
 
5e5946d
 
 
 
 
 
 
 
69244f3
5e5946d
69244f3
5e5946d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7a995b
5e5946d
 
 
69244f3
 
 
 
 
 
1bdf519
 
b7a995b
5e5946d
69244f3
5e5946d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69244f3
 
5e5946d
 
 
 
 
 
69244f3
 
 
 
 
5e5946d
 
 
 
69244f3
5e5946d
69244f3
 
 
 
 
 
 
 
1bdf519
b7a995b
 
1bdf519
 
 
b7a995b
5e5946d
1bdf519
 
b7a995b
 
1bdf519
 
 
5e5946d
b7a995b
1bdf519
5e5946d
1bdf519
69244f3
5e5946d
 
 
 
69244f3
 
5e5946d
 
 
 
 
 
 
1bdf519
 
5e5946d
b7a995b
 
 
1bdf519
 
 
 
 
 
 
b7a995b
 
 
 
1bdf519
 
 
69244f3
1bdf519
 
 
 
 
fb35915
1bdf519
 
b7a995b
fb35915
1bdf519
 
fb35915
1bdf519
69244f3
5e5946d
 
fd09464
69244f3
5e5946d
 
69244f3
 
 
 
 
 
 
 
 
 
 
76310a8
fd09464
 
5e5946d
fd09464
 
 
76310a8
 
 
 
fd09464
 
 
fb35915
1bdf519
69244f3
 
fb35915
69244f3
1bdf519
fb35915
 
fd09464
1bdf519
5e5946d
b7a995b
 
 
 
1bdf519
 
5e5946d
b7a995b
1bdf519
 
 
b7a995b
 
e0fcab8
1bdf519
5e5946d
 
b7a995b
69244f3
5e5946d
b7a995b
1bdf519
 
b7a995b
1bdf519
b7a995b
1bdf519
 
 
 
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import gradio as gr
import pycountry
import pandas as pd
import folium
from folium.plugins import MarkerCluster
import base64
import time
import overpy
from geopy.geocoders import Nominatim
import requests
from bs4 import BeautifulSoup

# -------------------------------------------------------------------
# Setup
# -------------------------------------------------------------------
geolocator = Nominatim(user_agent="hf-saas-dashboard")
api = overpy.Overpass()
GOOGLE_API_KEY = "AIzaSyD9W7W7nYKRYbtwPm20uVyVr_aW18Y4uiE"  # <-- replace with your Google API key

# -------------------------------------------------------------------
# Helpers
# -------------------------------------------------------------------
def list_countries():
    items = sorted([(c.name, c.alpha_2) for c in pycountry.countries], key=lambda x: x[0])
    names = [name for name, _ in items]
    codes = [code for _, code in items]
    return names, codes


def list_subdivisions(country_code):
    subs = list(pycountry.subdivisions.get(country_code=country_code))
    if not subs:
        return [], []
    items = sorted([(s.name, s.code) for s in subs], key=lambda x: x[0])
    names = [n for n, _ in items]
    codes = [c for _, c in items]
    return names, codes


def geocode_region(name, country_code):
    """Use geopy + Nominatim to geocode a region and return its bounding box."""
    query = f"{name}, {country_code}"
    location = geolocator.geocode(query, exactly_one=True, addressdetails=False)
    if not location:
        return None
    if not location.raw.get("boundingbox"):
        return None
    bbox = [float(x) for x in location.raw["boundingbox"]]
    # geopy returns boundingbox as [south, north, west, east]
    return (bbox[0], bbox[2], bbox[1], bbox[3])


def fetch_places(amenities, bbox):
    """Fetch places with given amenities inside a bounding box using overpy."""
    south, west, north, east = bbox
    amen_regex = "|".join(amenities)

    query = f"""
    (
      node["amenity"~"^{amen_regex}$"]({south},{west},{north},{east});
      way["amenity"~"^{amen_regex}$"]({south},{west},{north},{east});
      relation["amenity"~"^{amen_regex}$"]({south},{west},{north},{east});
    );
    out center tags;
    """

    result = api.query(query)
    rows = []

    def parse_tags(tags, lat, lon, osm_id):
        return {
            "name": tags.get("name", ""),
            "amenity": tags.get("amenity", ""),
            "lat": lat,
            "lon": lon,
            "phone": tags.get("phone", ""),
            "website": tags.get("website", ""),
            "osm_id": osm_id,
        }

    for node in result.nodes:
        rows.append(parse_tags(node.tags, node.lat, node.lon, f"node/{node.id}"))

    for way in result.ways:
        rows.append(parse_tags(way.tags, getattr(way, "center_lat", None), getattr(way, "center_lon", None), f"way/{way.id}"))

    for rel in result.relations:
        rows.append(parse_tags(rel.tags, getattr(rel, "center_lat", None), getattr(rel, "center_lon", None), f"relation/{rel.id}"))

    return pd.DataFrame(rows).dropna(subset=["lat", "lon"])


# -------------------------------
# Google API Enrichment
# -------------------------------
def enrich_with_google_api(df, log_msgs):
    """Enrich OSM results with Google Places API (phone + website)."""
    used = 0
    for i, row in df.iterrows():
        if row["phone"] and row["website"]:
            continue  # already have data

        query = f"{row['name']} near {row['lat']},{row['lon']}"
        url = "https://maps.googleapis.com/maps/api/place/textsearch/json"
        params = {"query": query, "key": GOOGLE_API_KEY}
        r = requests.get(url, params=params)
        data = r.json()

        if data.get("results"):
            place_id = data["results"][0]["place_id"]

            # Fetch details
            details_url = "https://maps.googleapis.com/maps/api/place/details/json"
            d_params = {"place_id": place_id, "fields": "formatted_phone_number,website", "key": GOOGLE_API_KEY}
            d = requests.get(details_url, params=d_params).json()

            details = d.get("result", {})
            df.at[i, "phone"] = details.get("formatted_phone_number", row["phone"])
            df.at[i, "website"] = details.get("website", row["website"])
            used += 1

    if used > 0:
        log_msgs.append(f"βœ… Google API enriched {used} rows.")
    else:
        log_msgs.append("⚠️ Google API did not enrich any rows.")
    return df


# -------------------------------
# Google Scraper Fallback
# -------------------------------
def scrape_google_maps(name, lat, lon):
    """Scrape Google Maps web UI for phone number (last resort, experimental)."""
    search_url = f"https://www.google.com/maps/search/{name}/@{lat},{lon},15z"
    headers = {"User-Agent": "Mozilla/5.0"}
    r = requests.get(search_url, headers=headers)

    if r.status_code != 200:
        return None

    soup = BeautifulSoup(r.text, "html.parser")
    phone = None
    for span in soup.find_all("span"):
        if "+" in span.text and any(c.isdigit() for c in span.text):
            phone = span.text.strip()
            break
    return phone


def enrich_with_scraper(df, log_msgs):
    used = 0
    for i, row in df.iterrows():
        if row["phone"]:
            continue
        phone = scrape_google_maps(row["name"], row["lat"], row["lon"])
        if phone:
            df.at[i, "phone"] = phone
            used += 1
    if used > 0:
        log_msgs.append(f"βœ… Scraper fallback enriched {used} rows.")
    else:
        log_msgs.append("⚠️ Scraper fallback did not find more phones.")
    return df


# -------------------------------
# Utilities
# -------------------------------
def clean_phone_for_whatsapp(phone):
    """Convert phone number into WhatsApp-friendly format (digits only, keep +)."""
    if not phone:
        return None
    cleaned = "".join(c for c in phone if c.isdigit() or c == "+")
    return cleaned if cleaned else None


def df_to_csv_bytes(df):
    return df.to_csv(index=False).encode("utf-8")


def make_map(df, center_bbox=None):
    if df.empty:
        return folium.Map(location=[20, 0], zoom_start=2)._repr_html_()

    if center_bbox:
        south, west, north, east = center_bbox
        center_lat = (south + north) / 2
        center_lon = (west + east) / 2
    else:
        center_lat = df["lat"].mean()
        center_lon = df["lon"].mean()

    m = folium.Map(location=[center_lat, center_lon], zoom_start=8)
    cluster = MarkerCluster().add_to(m)

    for _, row in df.iterrows():
        wa_link = f"https://wa.me/{clean_phone_for_whatsapp(row['phone'])}" if row['phone'] else None
        popup_html = f"""
        <b>{row['name'] or 'Unnamed Place'}</b><br>
        πŸ“ž {row['phone'] if row['phone'] else 'N/A'}<br>
        🌐 <a href="{row['website']}" target="_blank">{row['website'] or 'N/A'}</a><br>
        🍴 {row['amenity']}<br>
        {f'<a href="{wa_link}" target="_blank">πŸ’¬ WhatsApp</a>' if wa_link else ''}
        """
        folium.Marker(
            [row["lat"], row["lon"]],
            popup=folium.Popup(popup_html, max_width=300),
            tooltip=row["name"] if row["name"] else row["amenity"]
        ).add_to(cluster)

    return m._repr_html_()


# -------------------------------------------------------------------
# Gradio Callbacks
# -------------------------------------------------------------------
COUNTRY_NAMES, COUNTRY_CODES = list_countries()

def update_states(selected_country_name):
    try:
        idx = COUNTRY_NAMES.index(selected_country_name)
        code = COUNTRY_CODES[idx]
    except ValueError:
        return gr.update(choices=[])
    names, _ = list_subdivisions(code)
    return gr.update(choices=names, value=(names[0] if names else None))


def run_search(country_name, state_name, categories):
    start = time.time()
    log_msgs = []
    try:
        idx = COUNTRY_NAMES.index(country_name)
        country_code = COUNTRY_CODES[idx]
    except ValueError:
        return "Invalid country", None, None, None

    bbox = geocode_region(state_name if state_name else country_name, country_code)
    if bbox is None:
        return f"Could not geocode region '{state_name}'.", None, None, None

    if not categories:
        return "Please select at least one category.", None, None, None

    df = fetch_places(categories, bbox)
    log_msgs.append(f"ℹ️ OSM returned {len(df)} places.")

    # Try Google API enrichment
    if GOOGLE_API_KEY and GOOGLE_API_KEY != "AIzaSyD9W7W7nYKRYbtwPm20uVyVr_aW18Y4uiE":
        df = enrich_with_google_api(df, log_msgs)

    # Fallback scraper if still missing phones
    df = enrich_with_scraper(df, log_msgs)

    # πŸ”‘ Keep only businesses with phone numbers
    df = df[df["phone"].notna() & (df["phone"].str.strip() != "")]
    df = df.reset_index(drop=True)

    if df.empty:
        return "No businesses with phone numbers found.", None, None, None

    # Add WhatsApp links
    df["WhatsApp"] = df["phone"].apply(lambda x: f"https://wa.me/{clean_phone_for_whatsapp(x)}" if clean_phone_for_whatsapp(x) else "")
    
    # Keep lat/lon for map rendering
    df_for_map = df.copy()

    # Drop lat/lon/osm_id for display table & CSV
    df_display = df.drop(columns=["lat", "lon", "osm_id"], errors="ignore")
    df_display = df_display[["name", "amenity", "phone", "website", "WhatsApp"]]
    # For display table, convert to clickable link
    df_display["WhatsApp"] = df_display["WhatsApp"].apply(
        lambda x: f'<a href="{x}" target="_blank">πŸ’¬ WhatsApp</a>' if x else ""
    )

    map_html = make_map(df_for_map, center_bbox=bbox)
    csv_bytes = df_to_csv_bytes(df_display)

    elapsed = time.time() - start
    log_msgs.append(f"⏱️ Took {elapsed:.1f}s total.")
    msg = "\n".join(log_msgs)

    # Download link
    csv_b64 = base64.b64encode(csv_bytes).decode("utf-8")
    csv_href = f'<a href="data:text/csv;base64,{csv_b64}" download="results.csv">πŸ“₯ Download CSV</a>'

    return msg, df_display, map_html, csv_href


# -------------------------------------------------------------------
# Build Gradio UI
# -------------------------------------------------------------------
place_options = ["cafe", "motel", "hotel", "restaurant", "bar", "pub", "bakery", "fast_food", "guest_house", "hostel"]

with gr.Blocks() as demo:
    gr.Markdown("# 🌍 Hybrid Client Finder (OSM + Google API + Scraper Fallback)")

    with gr.Row():
        with gr.Column(scale=1):
            country = gr.Dropdown(choices=COUNTRY_NAMES, value="United States", label="Country")
            us_names, _ = list_subdivisions("US")
            state = gr.Dropdown(choices=us_names, value=(us_names[0] if us_names else None), label="State / Subdivision")

            update_btn = gr.Button("Refresh states")
            categories = gr.CheckboxGroup(place_options, label="Categories", value=["cafe", "restaurant"])
            search_btn = gr.Button("Search")

            info = gr.Textbox(label="Status", interactive=False, lines=5)
            download = gr.HTML(label="Download CSV")

        with gr.Column(scale=2):
            map_html_out = gr.HTML(label="Map")
            table_out = gr.Dataframe(label="Results Table")

    update_btn.click(fn=update_states, inputs=country, outputs=state)
    search_btn.click(fn=run_search, inputs=[country, state, categories], outputs=[info, table_out, map_html_out, download])

if __name__ == "__main__":
    demo.launch()