import googlemaps import os from huggingface_hub import Repository import googlemaps import gradio as gr import csv from datasets import load_dataset from bokeh.io import show from bokeh.plotting import gmap from bokeh.models import GMapOptions, ColumnDataSource, HoverTool from bokeh.embed import json_item from datasets import load_dataset MAPS_API = os.environ['MAPS_API'] OS_API_KEY = os.environ['OS_API_KEY'] HF_TOKEN = os.environ['HF_TOKEN'] google_maps_client = googlemaps.Client(key=MAPS_API) DATASET_REPO_URL = "https://huggingface.co/datasets/osanseviero/hugging_eats" DATA_FILENAME = "data.csv" DATA_FILE = os.path.join("data", DATA_FILENAME) repo = Repository( local_dir="data", clone_from=DATASET_REPO_URL, use_auth_token=HF_TOKEN ) def predict(place, hugging_secret): if hugging_secret != OS_API_KEY: return "INVALID SECRET - you cannot save places" geocode_result = google_maps_client.geocode(place) if geocode_result == None: return "PLACE NOT FOUND" print("Saving place") lat = geocode_result[0]["geometry"]["location"]["lat"] lng = geocode_result[0]["geometry"]["location"]["lng"] repo.git_pull(rebase=True) with open(DATA_FILE, "a") as csvfile: writer = csv.DictWriter(csvfile, fieldnames=["name", "lat", "lng"]) writer.writerow( {"name": place, "lat": lat, "lng": lng} ) print("Pushing place") repo.push_to_hub() return "PLACE SAVED!" iface_submit = gr.Interface( predict, inputs=[ gr.inputs.Textbox(label="Address or place name"), gr.inputs.Textbox(label="Hugging Secret"), ], outputs="text" ) def plot_map(): dataset = load_dataset('osanseviero/hugging_eats') data = dataset["train"].to_pandas() data = data.drop_duplicates() gmap_options = GMapOptions(lat=data["lat"][0], lng=data["lng"][0], map_type="satellite", zoom=12) # the tools are defined below: p = gmap(MAPS_API, gmap_options, title='Pays de Gex', tools=['reset', 'wheel_zoom', 'pan', 'zoom_in']) data_source = ColumnDataSource(data) center = p.circle('lng', 'lat', size=10, alpha=0.5, color='yellow', source=data_source) TOOLTIPS = [ ("name", "@name"), ] p.add_tools(HoverTool(tooltips = TOOLTIPS)) return json_item(p) iface_display = gr.Interface( plot_map, inputs=None, outputs=gr.Plot(type="bokeh") ) demo = gr.TabbedInterface([iface_display, iface_submit], ["Browse Places", "Submit Places (HF only)"]).launch()