Updated app at mån 8 jan 2024 18:02:37 CET
Browse files- app.py +71 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import hopsworks
|
| 2 |
+
import joblib
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def predict_bridge_opening(input_date, input_time):
|
| 8 |
+
"""
|
| 9 |
+
Predicts when the Södertälje Mälarbro will open.
|
| 10 |
+
:param input_date: The date to predict for.
|
| 11 |
+
:param input_time: The time to predict for.
|
| 12 |
+
:return: A dictionary of predictions for different time spans.
|
| 13 |
+
"""
|
| 14 |
+
try:
|
| 15 |
+
# Combine the date and time inputs
|
| 16 |
+
input_date_time = f"{input_date} {input_time}"
|
| 17 |
+
date_time = datetime.strptime(input_date_time, '%Y-%m-%d %H:%M')
|
| 18 |
+
|
| 19 |
+
# Define time spans
|
| 20 |
+
half_hour_span = (date_time - timedelta(minutes=30), date_time + timedelta(minutes=30))
|
| 21 |
+
one_hour_span = (date_time - timedelta(hours=1), date_time + timedelta(hours=1))
|
| 22 |
+
two_hour_span = (date_time - timedelta(hours=2), date_time + timedelta(hours=2))
|
| 23 |
+
today_span = (date_time.replace(hour=0, minute=0, second=0, microsecond=0),
|
| 24 |
+
date_time.replace(hour=23, minute=59, second=59, microsecond=999999))
|
| 25 |
+
|
| 26 |
+
# Dummy predictions (just user interface testing)
|
| 27 |
+
predictions = [False, False, True, True]
|
| 28 |
+
|
| 29 |
+
# predictions = model.predict(date_time)
|
| 30 |
+
|
| 31 |
+
# Return formatted predictions
|
| 32 |
+
return {
|
| 33 |
+
"30 Minute Span": f"{half_hour_span[0]} to {half_hour_span[1]}: {predictions[0]}",
|
| 34 |
+
"1 Hour Span": f"{one_hour_span[0]} to {one_hour_span[1]}: {predictions[1]}",
|
| 35 |
+
"2 Hour Span": f"{two_hour_span[0]} to {two_hour_span[1]}: {predictions[2]}",
|
| 36 |
+
"Today": f"{today_span[0]} to {today_span[1]}: {predictions[3]}"
|
| 37 |
+
}
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return str(e)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# TODO: Enable when model is ready
|
| 43 |
+
# print("Logging in to Hopsworks...")
|
| 44 |
+
# project = hopsworks.login()
|
| 45 |
+
#
|
| 46 |
+
# print("Getting feature store...")
|
| 47 |
+
# fs = project.get_feature_store()
|
| 48 |
+
#
|
| 49 |
+
# print("Getting model registry...")
|
| 50 |
+
# mr = project.get_model_registry()
|
| 51 |
+
#
|
| 52 |
+
# print("Getting model: ...")
|
| 53 |
+
# model = mr.get_model("iris_model", version=1)
|
| 54 |
+
#
|
| 55 |
+
# print("Downloading model...")
|
| 56 |
+
# model_dir = model.download()
|
| 57 |
+
#
|
| 58 |
+
# print("Initializing model locally...")
|
| 59 |
+
# model = joblib.load(model_dir + "/bridge_model.pkl")
|
| 60 |
+
|
| 61 |
+
print("Configuring gradio interface...")
|
| 62 |
+
iface = gr.Interface(
|
| 63 |
+
fn=predict_bridge_opening,
|
| 64 |
+
inputs=[gr.inputs.Date(label="Select Date"), gr.inputs.Time(label="Select Time")],
|
| 65 |
+
outputs=gr.outputs.JSON(label="Bridge Opening Predictions"),
|
| 66 |
+
title="Södertälje Mälarbro Bridge Opening Predictor",
|
| 67 |
+
description="Predict when the Södertälje Mälarbro will open. Select a date and time to see dummy predictions."
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
print("Launching gradio...")
|
| 71 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
hopsworks
|
| 2 |
+
joblib
|
| 3 |
+
scikit-learn==1.1.1
|