Spaces:
Sleeping
Sleeping
from gradio to streamlit
Browse files
app.py
CHANGED
@@ -1,25 +1,18 @@
|
|
1 |
-
import
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
import matplotlib.pyplot as plt
|
|
|
5 |
import io
|
6 |
-
import base64
|
7 |
import pickle
|
8 |
import torch.nn as nn
|
9 |
from sklearn.preprocessing import MinMaxScaler
|
10 |
-
import pandas as pd
|
11 |
-
|
12 |
-
df_weekly_km = pd.read_csv('Weekly_Km.csv')
|
13 |
-
|
14 |
|
15 |
INPUT_SIZE = 1
|
16 |
HIDDEN_LAYER_SIZE = 100
|
17 |
OUTPUT_SIZE = 1
|
18 |
|
19 |
-
data = df_weekly_km['Total Kilometers'].values.astype(float)
|
20 |
scaler = MinMaxScaler(feature_range=(-1, 1))
|
21 |
-
data_normalized = scaler.fit_transform(data.reshape(-1, 1))
|
22 |
-
|
23 |
seq_length = 4
|
24 |
|
25 |
class LSTM(nn.Module):
|
@@ -36,68 +29,54 @@ class LSTM(nn.Module):
|
|
36 |
predictions = self.linear(lstm_out.view(len(input_seq), -1))
|
37 |
return predictions[-1]
|
38 |
|
|
|
|
|
|
|
39 |
def prepare_custom_input(last_values, seq_length, scaler):
|
40 |
last_values_normalized = scaler.transform(np.array(last_values).reshape(-1, 1))
|
41 |
input_seq = torch.from_numpy(last_values_normalized).float()
|
42 |
return input_seq.view(-1)
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
model = LSTM(INPUT_SIZE, HIDDEN_LAYER_SIZE, OUTPUT_SIZE)
|
47 |
-
|
48 |
-
model.load_state_dict(torch.load(model_path))
|
49 |
-
|
50 |
|
51 |
-
def predict_and_plot(
|
52 |
-
last_four_weeks = [
|
53 |
|
54 |
custom_input = prepare_custom_input(last_four_weeks, seq_length, scaler)
|
55 |
|
56 |
-
model.eval()
|
57 |
with torch.no_grad():
|
58 |
model.hidden_cell = (torch.zeros(1, 1, model.hidden_layer_size),
|
59 |
torch.zeros(1, 1, model.hidden_layer_size))
|
60 |
prediction = model(custom_input)
|
61 |
|
62 |
predicted_kilometers = scaler.inverse_transform(prediction.numpy().reshape(-1, 1))
|
63 |
-
|
64 |
-
predicted_value = f"{predicted_kilometers[0][0]:.2f}"
|
65 |
|
66 |
weeks = ['Week -4', 'Week -3', 'Week -2', 'Week -1', 'Predicted Week']
|
67 |
-
actual_values =
|
68 |
-
|
69 |
-
plt.figure(figsize=(
|
70 |
-
|
71 |
plt.axvline(x='Predicted Week', color='red', linestyle='--', label='Predicted Week')
|
72 |
plt.title('Total Kilometers for Last 4 Weeks and Prediction')
|
73 |
plt.xlabel('Weeks')
|
74 |
plt.ylabel('Total Kilometers')
|
75 |
-
plt.
|
76 |
-
plt.grid()
|
77 |
-
plt.tight_layout()
|
78 |
plt.legend()
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
outputs = [gr.Textbox(label='Predicted Week'),
|
97 |
-
gr.HTML(label='Plot')]
|
98 |
-
|
99 |
-
gr.Interface(fn=predict_and_plot,
|
100 |
-
inputs=inputs,
|
101 |
-
outputs=outputs,
|
102 |
-
title="LSTM Model Prediction",
|
103 |
-
description="Enter the total kilometers for the last 4 weeks to get the prediction for the next week along with a plot.").launch(share=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
import io
|
|
|
7 |
import pickle
|
8 |
import torch.nn as nn
|
9 |
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
|
|
|
|
|
10 |
|
11 |
INPUT_SIZE = 1
|
12 |
HIDDEN_LAYER_SIZE = 100
|
13 |
OUTPUT_SIZE = 1
|
14 |
|
|
|
15 |
scaler = MinMaxScaler(feature_range=(-1, 1))
|
|
|
|
|
16 |
seq_length = 4
|
17 |
|
18 |
class LSTM(nn.Module):
|
|
|
29 |
predictions = self.linear(lstm_out.view(len(input_seq), -1))
|
30 |
return predictions[-1]
|
31 |
|
32 |
+
with open('LSTM_MODEL.pkl', 'rb') as f:
|
33 |
+
model = pickle.load(f)
|
34 |
+
|
35 |
def prepare_custom_input(last_values, seq_length, scaler):
|
36 |
last_values_normalized = scaler.transform(np.array(last_values).reshape(-1, 1))
|
37 |
input_seq = torch.from_numpy(last_values_normalized).float()
|
38 |
return input_seq.view(-1)
|
39 |
|
40 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
def predict_and_plot(week1, week2, week3, week4):
|
43 |
+
last_four_weeks = [week1, week2, week3, week4]
|
44 |
|
45 |
custom_input = prepare_custom_input(last_four_weeks, seq_length, scaler)
|
46 |
|
|
|
47 |
with torch.no_grad():
|
48 |
model.hidden_cell = (torch.zeros(1, 1, model.hidden_layer_size),
|
49 |
torch.zeros(1, 1, model.hidden_layer_size))
|
50 |
prediction = model(custom_input)
|
51 |
|
52 |
predicted_kilometers = scaler.inverse_transform(prediction.numpy().reshape(-1, 1))
|
53 |
+
predicted_value = predicted_kilometers[0][0]
|
|
|
54 |
|
55 |
weeks = ['Week -4', 'Week -3', 'Week -2', 'Week -1', 'Predicted Week']
|
56 |
+
actual_values = [week1, week2, week3, week4, predicted_value]
|
57 |
+
|
58 |
+
plt.figure(figsize=(8, 5))
|
59 |
+
sns.lineplot(x=weeks, y=actual_values, marker='o', label='Total Kilometers')
|
60 |
plt.axvline(x='Predicted Week', color='red', linestyle='--', label='Predicted Week')
|
61 |
plt.title('Total Kilometers for Last 4 Weeks and Prediction')
|
62 |
plt.xlabel('Weeks')
|
63 |
plt.ylabel('Total Kilometers')
|
64 |
+
plt.grid(True)
|
|
|
|
|
65 |
plt.legend()
|
66 |
|
67 |
+
st.pyplot(plt)
|
68 |
+
|
69 |
+
return predicted_value
|
70 |
+
|
71 |
+
# Streamlit interface
|
72 |
+
st.title("LSTM Model Prediction")
|
73 |
+
st.write("Enter the total kilometers for the last 4 weeks to get the prediction for the next week along with a plot.")
|
74 |
+
|
75 |
+
week1 = st.number_input('Week -4', min_value=0)
|
76 |
+
week2 = st.number_input('Week -3', min_value=0)
|
77 |
+
week3 = st.number_input('Week -2', min_value=0)
|
78 |
+
week4 = st.number_input('Week -1', min_value=0)
|
79 |
+
|
80 |
+
if st.button('Predict'):
|
81 |
+
predicted_value = predict_and_plot(week1, week2, week3, week4)
|
82 |
+
st.write(f"Predicted Total Kilometers for the next week: {predicted_value:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|