Spaces:
Runtime error
Runtime error
yusyel
commited on
Commit
Β·
d4e742a
1
Parent(s):
723e7d6
first
Browse files- README.md +5 -4
- app.py +190 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.10.0
|
| 8 |
app_file: app.py
|
|
@@ -10,3 +10,4 @@ pinned: false
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Test
|
| 3 |
+
emoji: π
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: red
|
| 6 |
sdk: streamlit
|
| 7 |
sdk_version: 1.10.0
|
| 8 |
app_file: app.py
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
app.py
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#%%
|
| 2 |
+
from matplotlib.pyplot import title
|
| 3 |
+
import tensorflow as tf
|
| 4 |
+
from tensorflow import keras
|
| 5 |
+
from huggingface_hub import from_pretrained_keras
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
import streamlit as st
|
| 9 |
+
from zipfile import ZipFile
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
import warnings
|
| 13 |
+
warnings.filterwarnings("ignore")
|
| 14 |
+
|
| 15 |
+
uri = "https://storage.googleapis.com/tensorflow/tf-keras-datasets/jena_climate_2009_2016.csv.zip"
|
| 16 |
+
zip_path = keras.utils.get_file(origin=uri, fname="jena_climate_2009_2016.csv.zip")
|
| 17 |
+
zip_file = ZipFile(zip_path)
|
| 18 |
+
zip_file.extractall()
|
| 19 |
+
csv_path = "jena_climate_2009_2016.csv"
|
| 20 |
+
df = pd.read_csv(csv_path)
|
| 21 |
+
|
| 22 |
+
#%%
|
| 23 |
+
|
| 24 |
+
title = "Timeseries forecasting for weather prediction"
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# %% model
|
| 29 |
+
|
| 30 |
+
titles = [
|
| 31 |
+
"Pressure",
|
| 32 |
+
"Temperature",
|
| 33 |
+
"Temperature in Kelvin",
|
| 34 |
+
"Temperature (dew point)",
|
| 35 |
+
"Relative Humidity",
|
| 36 |
+
"Saturation vapor pressure",
|
| 37 |
+
"Vapor pressure",
|
| 38 |
+
"Vapor pressure deficit",
|
| 39 |
+
"Specific humidity",
|
| 40 |
+
"Water vapor concentration",
|
| 41 |
+
"Airtight",
|
| 42 |
+
"Wind speed",
|
| 43 |
+
"Maximum wind speed",
|
| 44 |
+
"Wind direction in degrees",
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
feature_keys = [
|
| 48 |
+
"p (mbar)",
|
| 49 |
+
"T (degC)",
|
| 50 |
+
"Tpot (K)",
|
| 51 |
+
"Tdew (degC)",
|
| 52 |
+
"rh (%)",
|
| 53 |
+
"VPmax (mbar)",
|
| 54 |
+
"VPact (mbar)",
|
| 55 |
+
"VPdef (mbar)",
|
| 56 |
+
"sh (g/kg)",
|
| 57 |
+
"H2OC (mmol/mol)",
|
| 58 |
+
"rho (g/m**3)",
|
| 59 |
+
"wv (m/s)",
|
| 60 |
+
"max. wv (m/s)",
|
| 61 |
+
"wd (deg)",
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
date_time_key = "Date Time"
|
| 65 |
+
split_fraction = 0.715
|
| 66 |
+
train_split = int(split_fraction * int(df.shape[0]))
|
| 67 |
+
step = 6
|
| 68 |
+
|
| 69 |
+
past = 720
|
| 70 |
+
future = 72
|
| 71 |
+
learning_rate = 0.001
|
| 72 |
+
batch_size = 256
|
| 73 |
+
epochs = 10
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def normalize(data, train_split):
|
| 77 |
+
data_mean = data[:train_split].mean(axis=0)
|
| 78 |
+
data_std = data[:train_split].std(axis=0)
|
| 79 |
+
return (data - data_mean) / data_std
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
print(
|
| 83 |
+
"The selected parameters are:",
|
| 84 |
+
", ".join([titles[i] for i in [0, 1, 5, 7, 8, 10, 11]]),
|
| 85 |
+
)
|
| 86 |
+
selected_features = [feature_keys[i] for i in [0, 1, 5, 7, 8, 10, 11]]
|
| 87 |
+
features = df[selected_features]
|
| 88 |
+
features.index = df[date_time_key]
|
| 89 |
+
features.head()
|
| 90 |
+
|
| 91 |
+
features = normalize(features.values, train_split)
|
| 92 |
+
features = pd.DataFrame(features)
|
| 93 |
+
features.head()
|
| 94 |
+
|
| 95 |
+
train_data = features.loc[0 : train_split - 1]
|
| 96 |
+
val_data = features.loc[train_split:]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
split_fraction = 0.715
|
| 100 |
+
train_split = int(split_fraction * int(df.shape[0]))
|
| 101 |
+
step = 6
|
| 102 |
+
|
| 103 |
+
past = 720
|
| 104 |
+
future = 72
|
| 105 |
+
learning_rate = 0.001
|
| 106 |
+
batch_size = 256
|
| 107 |
+
epochs = 10
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def normalize(data, train_split):
|
| 111 |
+
data_mean = data[:train_split].mean(axis=0)
|
| 112 |
+
data_std = data[:train_split].std(axis=0)
|
| 113 |
+
return (data - data_mean) / data_std
|
| 114 |
+
print(
|
| 115 |
+
"The selected parameters are:",
|
| 116 |
+
", ".join([titles[i] for i in [0, 1, 5, 7, 8, 10, 11]]),
|
| 117 |
+
)
|
| 118 |
+
selected_features = [feature_keys[i] for i in [0, 1, 5, 7, 8, 10, 11]]
|
| 119 |
+
features = df[selected_features]
|
| 120 |
+
features.index = df[date_time_key]
|
| 121 |
+
features.head()
|
| 122 |
+
|
| 123 |
+
features = normalize(features.values, train_split)
|
| 124 |
+
features = pd.DataFrame(features)
|
| 125 |
+
features.head()
|
| 126 |
+
|
| 127 |
+
train_data = features.loc[0 : train_split - 1]
|
| 128 |
+
val_data = features.loc[train_split:]
|
| 129 |
+
start = past + future
|
| 130 |
+
end = start + train_split
|
| 131 |
+
|
| 132 |
+
x_train = train_data[[i for i in range(7)]].values
|
| 133 |
+
y_train = features.iloc[start:end][[1]]
|
| 134 |
+
|
| 135 |
+
sequence_length = int(past / step)
|
| 136 |
+
x_end = len(val_data) - past - future
|
| 137 |
+
|
| 138 |
+
label_start = train_split + past + future
|
| 139 |
+
|
| 140 |
+
x_val = val_data.iloc[:x_end][[i for i in range(7)]].values
|
| 141 |
+
y_val = features.iloc[label_start:][[1]]
|
| 142 |
+
|
| 143 |
+
dataset_val = keras.preprocessing.timeseries_dataset_from_array(
|
| 144 |
+
x_val,
|
| 145 |
+
y_val,
|
| 146 |
+
sequence_length=sequence_length,
|
| 147 |
+
sampling_rate=step,
|
| 148 |
+
batch_size=batch_size,
|
| 149 |
+
)
|
| 150 |
+
#%%
|
| 151 |
+
model = from_pretrained_keras("yusyel/test")
|
| 152 |
+
|
| 153 |
+
#%% please don't code shaming me :/
|
| 154 |
+
st.set_option('deprecation.showPyplotGlobalUse', False)
|
| 155 |
+
def plot():
|
| 156 |
+
n = st.sidebar.slider("Step", min_value = 1, max_value=5, value = 1)
|
| 157 |
+
def show_plot(plot_data, delta, title):
|
| 158 |
+
labels = ["History", "True Future", "Model Prediction"]
|
| 159 |
+
marker = [".-", "rx", "go"]
|
| 160 |
+
time_steps = list(range(-(plot_data[0].shape[0]), 0))
|
| 161 |
+
if delta:
|
| 162 |
+
future = delta
|
| 163 |
+
else:
|
| 164 |
+
future = 0
|
| 165 |
+
|
| 166 |
+
plt.title(title)
|
| 167 |
+
for i, val in enumerate(plot_data):
|
| 168 |
+
if i:
|
| 169 |
+
plt.plot(future, plot_data[i], marker[i], markersize=10, label=labels[i])
|
| 170 |
+
else:
|
| 171 |
+
plt.plot(time_steps, plot_data[i].flatten(), marker[i], label=labels[i])
|
| 172 |
+
plt.legend(loc='lower center', bbox_to_anchor=(0.5, 1.05),
|
| 173 |
+
ncol=3, fancybox=True, shadow=True)
|
| 174 |
+
plt.xlim([time_steps[0], (future + 5) * 2])
|
| 175 |
+
plt.xlabel("Time-Step")
|
| 176 |
+
plt.show()
|
| 177 |
+
return
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
for x, y in dataset_val.take(n):
|
| 181 |
+
show_plot(
|
| 182 |
+
[x[0][:, 1].numpy(), y[0].numpy(), model.predict(x)[0]],
|
| 183 |
+
12,
|
| 184 |
+
"Single Step Prediction",
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
fig = plot()
|
| 188 |
+
st.pyplot(fig)
|
| 189 |
+
|
| 190 |
+
# %%
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
huggingface_hub
|
| 3 |
+
pandas
|
| 4 |
+
streamlit
|
| 5 |
+
matplotlib
|