-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
94 lines (69 loc) · 2.56 KB
/
application.py
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
from flask import Flask, render_template, abort
from werkzeug.exceptions import abort
import pandas as pd
import json
import plotly
import plotly.express as px
import numpy as np
from helpers import ddb_connection, graphers
import os
from datetime import datetime
import yaml
import markdown2
from flaskext.markdown import Markdown
import tflite_runtime.interpreter as tflite
application = Flask(__name__)
Markdown(application)
@application.route("/")
def index():
return render_template("index.html")
@application.route("/about")
def about():
return render_template("about.html")
@application.route("/live_data")
def live_data():
# Download data for the last 24 hours
day_ago = str(pd.Timestamp.now() - pd.Timedelta(days=2))
connection = ddb_connection.DynamoResource()
df = ddb_connection.DynamoResource.query(connection, day_ago)
fig = graphers.temperature_only(df)
baseline_forecast = df["temperature"].iloc[-1]
# Load TFLite model and allocate tensors
interpreter = tflite.Interpreter(model_path="static/converted_model_fused.tflite")
interpreter.allocate_tensors()
# Get input and output tensors
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
mean = 22.981637
std = 2.30707
past_hour = df["temperature"].iloc[-12:].values
past_hour -= mean
past_hour /= std
# tflite interpreter - https://www.tensorflow.org/api_docs/python/tf/lite/Interpreter
interpreter.set_tensor(input_details[0]["index"], past_hour.astype('float32').reshape([1,12,1]))
interpreter.invoke()
result = interpreter.get_tensor(output_details[0]["index"])[0][0]
model_forecast = np.round((result*std)+mean,2)
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return render_template(
"plotly.html",
graphJSON=graphJSON,
baseline_forecast=baseline_forecast,
model_forecast=model_forecast,
)
@application.route("/all_time")
def all_time():
connection = ddb_connection.DynamoResource()
df = ddb_connection.DynamoResource.query(connection)
fig = px.scatter(df, x="timestamp", y="temperature")
fig.update_yaxes(title_text="Temperature")
fig.update_xaxes(title_text="Time")
graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
return render_template(
"plotly.html",
graphJSON=graphJSON,
header="Temperature only",
description="Temperature only, since start of measurements.",
)
if __name__ == "__main__":
application.run(host="0.0.0.0", port=8080)