OwusuDynamo
commited on
Commit
·
a5cde1f
1
Parent(s):
6cf5b3d
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""
|
3 |
+
Created on Thu Jun 1 13:52:42 2023
|
4 |
+
|
5 |
+
@author: ME
|
6 |
+
"""
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
import matplotlib.pyplot as plt
|
11 |
+
import streamlit as st
|
12 |
+
import seaborn as sns
|
13 |
+
import pickle
|
14 |
+
import base64
|
15 |
+
import joblib
|
16 |
+
from src.preprocess import preprocess_data
|
17 |
+
import mplcursors
|
18 |
+
|
19 |
+
|
20 |
+
#load json file
|
21 |
+
json_path = r"C:/Users/ME/Desktop/Blessing_AI/Weather_Prediction/Artifacts/feature_dict.joblib"
|
22 |
+
loaded_data = joblib.load(json_path)
|
23 |
+
|
24 |
+
#load model
|
25 |
+
model_path = r"C:/Users/ME/Desktop/Blessing_AI/Weather_Prediction/Artifacts/trained_prophet_model.pkl"
|
26 |
+
with open(model_path, 'rb') as file_:
|
27 |
+
model = pickle.load(file_)
|
28 |
+
|
29 |
+
|
30 |
+
st.title("Weather Forecast: Average Daily Temperature Projections for Nigerian Airports")
|
31 |
+
|
32 |
+
col1,col2 = st.columns(2)
|
33 |
+
|
34 |
+
start_d = col1.date_input("Select start date")
|
35 |
+
end_d = col2.date_input("Select end date")
|
36 |
+
#get number of days
|
37 |
+
delta = end_d - start_d
|
38 |
+
days = delta.days
|
39 |
+
|
40 |
+
#split start date into year ,month and date
|
41 |
+
y1 = start_d.year
|
42 |
+
m1 = start_d.month
|
43 |
+
d1 = start_d.day
|
44 |
+
|
45 |
+
#split end date into year ,month and date
|
46 |
+
y2 = end_d.year
|
47 |
+
m2 = end_d.month
|
48 |
+
d2 = end_d.day
|
49 |
+
|
50 |
+
start_date = (y1,m1,d1)
|
51 |
+
end_date = (y2,m2,d2)
|
52 |
+
|
53 |
+
if days > 10 :
|
54 |
+
st.write("Select a lower number of days range (10 and below)")
|
55 |
+
|
56 |
+
else:
|
57 |
+
|
58 |
+
name = st.selectbox("Select airport of interest",options=tuple(loaded_data["Airport_name"].keys()))
|
59 |
+
|
60 |
+
data = preprocess_data(
|
61 |
+
start_d=start_date,
|
62 |
+
end_d= end_date,
|
63 |
+
airport_name = name
|
64 |
+
)
|
65 |
+
|
66 |
+
forecast_bn = st.button("Forecast")
|
67 |
+
|
68 |
+
if forecast_bn:
|
69 |
+
pred_df = model.predict(data)
|
70 |
+
pred_df = pred_df[["ds","yhat"]]
|
71 |
+
pred_df.columns = ["Date","Prediction"]
|
72 |
+
|
73 |
+
#Display only year,month and date
|
74 |
+
pred_df["Date"]= pred_df['Date'].dt.strftime('%Y-%m-%d')
|
75 |
+
|
76 |
+
#Convert column to 2 dp
|
77 |
+
pred_df["Prediction"] = pred_df["Prediction"].round(2)
|
78 |
+
|
79 |
+
tab1,tab2 = st.tabs(["Predictions","Check plot"])
|
80 |
+
tab1.write(pred_df)
|
81 |
+
|
82 |
+
# Add a download button
|
83 |
+
csv = pred_df.to_csv(index=False)
|
84 |
+
b64 = base64.b64encode(csv.encode()).decode() # Encode the DataFrame as a base64 string
|
85 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="data.csv">Download CSV File</a>'
|
86 |
+
st.markdown(href, unsafe_allow_html=True)
|
87 |
+
|
88 |
+
|
89 |
+
# Create the figure and axis objects
|
90 |
+
fig, ax = plt.subplots()
|
91 |
+
|
92 |
+
# Plot the time series
|
93 |
+
x = pred_df["Date"]
|
94 |
+
y = pred_df["Prediction"]
|
95 |
+
ax.plot(x, y, marker='o', linestyle='--')
|
96 |
+
|
97 |
+
# Set x-axis label
|
98 |
+
ax.set_xlabel('Date')
|
99 |
+
|
100 |
+
# Set y-axis label
|
101 |
+
ax.set_ylabel('Average temperature')
|
102 |
+
|
103 |
+
# Style the plot
|
104 |
+
sns.set_style('whitegrid')
|
105 |
+
|
106 |
+
# Set the limits of the x-axis and y-axis
|
107 |
+
ax.set_xlim(min(x), max(x))
|
108 |
+
ax.set_ylim(min(y), max(y))
|
109 |
+
|
110 |
+
# Format the x-axis date ticks
|
111 |
+
fig.autofmt_xdate()
|
112 |
+
|
113 |
+
# Add cursor interaction
|
114 |
+
cursor = mplcursors.cursor(hover=True)
|
115 |
+
# Define the annotation format
|
116 |
+
@cursor.connect("add")
|
117 |
+
def on_add(sel):
|
118 |
+
x = sel.target[0]
|
119 |
+
y = sel.target[1]
|
120 |
+
date_str = x.strftime("%Y-%m-%d")
|
121 |
+
sel.annotation.set_text(f'Date: {date_str}\nValue: {y:.2f}')
|
122 |
+
|
123 |
+
# Show the plot
|
124 |
+
tab2.pyplot(fig)
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
+
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
|
137 |
+
|
138 |
+
|