Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
+
from keras.models import load_model
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
|
| 10 |
+
# Load the trained model
|
| 11 |
+
model = joblib.load('./lstm_model.pkl')
|
| 12 |
+
|
| 13 |
+
# Function to prepare the data
|
| 14 |
+
def prepare_data(df, time_steps=60):
|
| 15 |
+
data = df['quantity'].values.reshape(-1, 1)
|
| 16 |
+
|
| 17 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
| 18 |
+
scaled_data = scaler.fit_transform(data)
|
| 19 |
+
|
| 20 |
+
x_test = []
|
| 21 |
+
for i in range(time_steps, len(scaled_data)):
|
| 22 |
+
x_test.append(scaled_data[i - time_steps:i, 0])
|
| 23 |
+
x_test = np.array(x_test)
|
| 24 |
+
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))
|
| 25 |
+
|
| 26 |
+
return x_test, scaler
|
| 27 |
+
|
| 28 |
+
# Function to forecast the next 60 days
|
| 29 |
+
def forecast(model, x_test, scaler, time_steps=60, future=60):
|
| 30 |
+
forecast_data = x_test[-1] # Use the last sequence of the test set for forecasting
|
| 31 |
+
forecast_predictions = []
|
| 32 |
+
for _ in range(future):
|
| 33 |
+
prediction = model.predict(forecast_data.reshape(1, time_steps, 1))
|
| 34 |
+
forecast_predictions.append(prediction[0, 0])
|
| 35 |
+
forecast_data = np.append(forecast_data[1:], prediction[0, 0]).reshape(-1, 1)
|
| 36 |
+
forecast_predictions = np.array(forecast_predictions).reshape(-1, 1)
|
| 37 |
+
forecast_predictions = scaler.inverse_transform(forecast_predictions)
|
| 38 |
+
|
| 39 |
+
return forecast_predictions
|
| 40 |
+
|
| 41 |
+
# Streamlit UI
|
| 42 |
+
st.title('Product Sales Forecasting')
|
| 43 |
+
|
| 44 |
+
uploaded_file = st.file_uploader("Choose a file")
|
| 45 |
+
if uploaded_file is not None:
|
| 46 |
+
df = pd.read_csv(uploaded_file, parse_dates=['date'])
|
| 47 |
+
st.write(df.tail()) # Display the tail of the dataframe
|
| 48 |
+
|
| 49 |
+
family = st.selectbox("Select a family", df['family'].unique())
|
| 50 |
+
|
| 51 |
+
if st.button('Predict'):
|
| 52 |
+
df_family = df[df['family'] == family]
|
| 53 |
+
|
| 54 |
+
# Ensure df_family is not empty
|
| 55 |
+
if df_family.empty:
|
| 56 |
+
st.write("No data available for the selected family.")
|
| 57 |
+
else:
|
| 58 |
+
# Prepare data
|
| 59 |
+
x_test, scaler = prepare_data(df_family)
|
| 60 |
+
|
| 61 |
+
# Forecast
|
| 62 |
+
forecast_predictions = forecast(model, x_test, scaler)
|
| 63 |
+
|
| 64 |
+
# Prepare forecast dataframe
|
| 65 |
+
last_date = df_family['date'].max()
|
| 66 |
+
forecast_dates = [last_date + timedelta(days=i) for i in range(1, 61)]
|
| 67 |
+
forecast_df = pd.DataFrame({'date': forecast_dates, 'forecasted_quantity': forecast_predictions.flatten()})
|
| 68 |
+
|
| 69 |
+
# Plot using Plotly with green line
|
| 70 |
+
fig = go.Figure()
|
| 71 |
+
fig.add_trace(go.Scatter(x=forecast_df['date'], y=forecast_df['forecasted_quantity'], mode='lines', name='Forecasted Quantity', line=dict(color='green')))
|
| 72 |
+
fig.update_layout(title=f'Sales Forecast for {family}', xaxis_title='Date', yaxis_title='Quantity Sold')
|
| 73 |
+
st.plotly_chart(fig)
|
| 74 |
+
|
| 75 |
+
st.write(forecast_df)
|