File size: 2,566 Bytes
156ecd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b97e1da
156ecd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92463ff
cc3999a
156ecd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3743c6b
156ecd4
 
 
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
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()