Spaces:
Runtime error
Runtime error
SevenhuijsenM
commited on
Commit
·
3829ec7
1
Parent(s):
3bb2171
Added gui
Browse files- README.md +1 -1
- app.py +71 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 📚
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 4.10.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import hopsworks
|
| 8 |
+
import joblib
|
| 9 |
+
import pandas as pd
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
+
API_KEY = os.getenv("API-KEY-TOMTOM")
|
| 13 |
+
|
| 14 |
+
# Log into hopsworks
|
| 15 |
+
project = hopsworks.login()
|
| 16 |
+
fs = project.get_feature_store()
|
| 17 |
+
mr = project.get_model_registry()
|
| 18 |
+
|
| 19 |
+
model = mr.get_model("stockholm_incidents_model", version=1)
|
| 20 |
+
model_dir = model.download()
|
| 21 |
+
model = joblib.load(model_dir + "/stckhlm_inc_model.pkl")
|
| 22 |
+
print("Model downloaded")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def predict(magnitudeOfDelay, hour, iconCategory, latitude, longitude, month):
|
| 26 |
+
# Create a row from the input
|
| 27 |
+
row = {
|
| 28 |
+
'magnitudeOfDelay': magnitudeOfDelay,
|
| 29 |
+
'hour': hour,
|
| 30 |
+
'iconCategory': iconCategory,
|
| 31 |
+
'latitude': latitude,
|
| 32 |
+
'longitude': longitude,
|
| 33 |
+
'month': month
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Create a df from the row
|
| 37 |
+
df_row = pd.DataFrame(row, index=[0])
|
| 38 |
+
|
| 39 |
+
# change the order to code hour iconCategory latitude longitude magnitudeOfDelay month duration
|
| 40 |
+
df_row = df_row[['magnitudeOfDelay', 'hour', 'iconCategory', 'latitude', 'longitude', 'month']]
|
| 41 |
+
|
| 42 |
+
# make the features lower case
|
| 43 |
+
df_row.columns = df_row.columns.str.lower()
|
| 44 |
+
df_row.columns = df_row.columns.str.replace(' ', '_')
|
| 45 |
+
|
| 46 |
+
# Get the prediction
|
| 47 |
+
prediction = model.predict(df_row)[0]
|
| 48 |
+
|
| 49 |
+
# Remove the log transformation
|
| 50 |
+
prediction = prediction[0]
|
| 51 |
+
|
| 52 |
+
return prediction
|
| 53 |
+
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn =predict,
|
| 56 |
+
title="Stockholm Incident Prediction",
|
| 57 |
+
description="Predicts the duration of a traffic incident in Stockholm",
|
| 58 |
+
allow_flagging="never",
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.inputs.Slider(0, 60, label="Magnitude of Delay"),
|
| 61 |
+
gr.inputs.Slider(0, 23, label="Hour"),
|
| 62 |
+
gr.inputs.Radio(["Accident", "Construction", "Congestion", "Disabled Vehicle", "Mass Transit", "Miscellaneous", "Other News", "Planned Event", "Road Hazard", "Roadwork", "Traffic Flow", "Weather"], label="Icon Category"),
|
| 63 |
+
gr.inputs.Slider(59.25, 59.40, label="Latitude"),
|
| 64 |
+
gr.inputs.Slider(18.00, 18.16, label="Longitude"),
|
| 65 |
+
gr.inputs.Slider(1, 12, label="Month")
|
| 66 |
+
],
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.outputs.Textbox(label="Duration")
|
| 69 |
+
])
|
| 70 |
+
|
| 71 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy
|
| 2 |
+
pandas
|
| 3 |
+
plotly
|
| 4 |
+
hopsworks
|
| 5 |
+
joblib
|
| 6 |
+
scikit-learn
|