import gradio as gr import plotly.graph_objects as go import json import requests import os from PIL import Image import hopsworks import joblib import pandas as pd API_KEY = os.getenv("API-KEY-TOMTOM") # Log into hopsworks project = hopsworks.login() fs = project.get_feature_store() mr = project.get_model_registry() model = mr.get_model("stockholm_incidents_model", version=1) model_dir = model.download() model = joblib.load(model_dir + "/stockholm_incidents_model.pkl") print("Model downloaded") api_params_incidents = { 'base_url': 'api.tomtom.com', 'API_KEY': "XyqEVsk8ELwIx6GGPMOqyAIE8dJ22vZM", 'min_lon': 18.00, 'max_lon': 18.16, 'min_lat': 59.25, 'max_lat': 59.40, 'version_number': 5, 'category_filter': '0%2C1%2C2%2C3%2C4%2C5%2C6%2C7%2C8%2C9%2C10%2C11%2C14', 'time_validity_filter': 'present', 'language': 'en-GB', 'fields': '%7Bincidents%7Btype%2Cgeometry%7Bcoordinates%7D%2Cproperties%7Bid%2CmagnitudeOfDelay%2Cevents%7Bdescription%2Ccode%2CiconCategory%7D%2CstartTime%2CendTime%7D%7D%7D' } def get_incident_details(params): url = f"https://{params['base_url']}/traffic/services/{params['version_number']}/incidentDetails?bbox={params['min_lon']}%2C{params['min_lat']}%2C{params['max_lon']}%2C{params['max_lat']}&fields={params['fields']}&language={params['language']}&categoryFilter={params['category_filter']}&timeValidityFilter={params['time_validity_filter']}&key={params['API_KEY']}" return json.loads(requests.get(url).text) def filter_map(): latitude_list = [] longitude_list = [] index_list = [] # Get the data from the json incidents = get_incident_details(api_params_incidents)['incidents'] for (_, i) in enumerate(incidents): print(i) # Add the coordinates to the list if any(isinstance(j, list) for j in i['geometry']['coordinates']): latitude_list.append(i['geometry']['coordinates'][0][1]) longitude_list.append(i['geometry']['coordinates'][0][0]) else: latitude_list.append(i['geometry']['coordinates'][1]) longitude_list.append(i['geometry']['coordinates'][0]) index_list.append(i['properties']['id']) fig = go.Figure(go.Scattermapbox( customdata=index_list, lat=latitude_list, lon=longitude_list, mode='markers', marker=go.scattermapbox.Marker( size=6 ), hoverinfo="text", hovertemplate='Index: %{customdata}' )) fig.update_layout( mapbox_style="open-street-map", hovermode='closest', mapbox=dict( bearing=0, center=go.layout.mapbox.Center( lat=59.32, lon=18.08 ), pitch=0, zoom=9 ), ) return fig with gr.Blocks() as demo: with gr.Column(): btn = gr.Button(value="Update Filter") map = gr.Plot() demo.load(filter_map, outputs=map) btn.click(filter_map, outputs=map) demo.launch()