import gradio as gr import pandas as pd from geopy.extra.rate_limiter import RateLimiter from geopy.geocoders import Nominatim def geocode(address): try: address2 = address.replace(' ', '+').replace(',', '%2C') df = pd.read_json( f'https://geocoding.geo.census.gov/geocoder/locations/onelineaddress?address={address2}&benchmark=2020&format=json') results = df.iloc[:1, 0][0][0]['coordinates'] lat, lon = results['y'], results['x'] except: geolocator = Nominatim(user_agent="GTA Lookup") geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1) location = geolocator.geocode(address) lat, lon = location.latitude, location.longitude return f'{round(lat,7)}, {round(lon,7)}' with gr.Blocks() as demo: address = gr.Textbox(label="Address") output = gr.Textbox(label="Lat, Lon") greet_btn = gr.Button("Get Lat, Lon") greet_btn.click(fn=geocode, inputs=address, outputs=output, api_name="api") demo.launch()