Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
import torch.optim as optim
|
| 6 |
+
import torch.utils.data as data
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import plotly.graph_objects as go
|
| 9 |
+
from tqdm import tqdm # Progress bar
|
| 10 |
+
|
| 11 |
+
# Function to create dataset for time series prediction
|
| 12 |
+
def create_dataset(dataset, lookback):
|
| 13 |
+
X, y = [], []
|
| 14 |
+
for i in range(len(dataset) - lookback):
|
| 15 |
+
feature = dataset[i:i + lookback]
|
| 16 |
+
target = dataset[i + 1:i + lookback + 1]
|
| 17 |
+
X.append(feature)
|
| 18 |
+
y.append(target)
|
| 19 |
+
X = np.array(X).reshape(-1, lookback, 1) # Reshape to 3D (samples, lookback, features)
|
| 20 |
+
y = np.array(y).reshape(-1, lookback, 1) # Reshape to 3D (samples, lookback, features)
|
| 21 |
+
return torch.tensor(X).float(), torch.tensor(y).float()
|
| 22 |
+
|
| 23 |
+
# Define LSTM model
|
| 24 |
+
class AirModel(nn.Module):
|
| 25 |
+
def __init__(self):
|
| 26 |
+
super(AirModel, self).__init__()
|
| 27 |
+
self.lstm = nn.LSTM(input_size=1, hidden_size=50, num_layers=1, batch_first=True)
|
| 28 |
+
self.linear = nn.Linear(50, 1)
|
| 29 |
+
|
| 30 |
+
def forward(self, x):
|
| 31 |
+
x, _ = self.lstm(x)
|
| 32 |
+
x = self.linear(x)
|
| 33 |
+
return x
|
| 34 |
+
|
| 35 |
+
# Training and prediction function
|
| 36 |
+
def train_and_predict(csv_file, lookback, epochs, batch_size):
|
| 37 |
+
# Load CSV
|
| 38 |
+
df = pd.read_csv(csv_file.name)
|
| 39 |
+
|
| 40 |
+
# Extract time series data
|
| 41 |
+
timeseries = df[["AmtNet Sales USD"]].values.astype('float32')
|
| 42 |
+
|
| 43 |
+
# Train-test split
|
| 44 |
+
train_size = int(len(timeseries) * 0.67)
|
| 45 |
+
test_size = len(timeseries) - train_size
|
| 46 |
+
train, test = timeseries[:train_size], timeseries[train_size:]
|
| 47 |
+
|
| 48 |
+
# Create datasets
|
| 49 |
+
X_train, y_train = create_dataset(train, lookback=lookback)
|
| 50 |
+
X_test, y_test = create_dataset(test, lookback=lookback)
|
| 51 |
+
|
| 52 |
+
if len(X_train) == 0 or len(X_test) == 0:
|
| 53 |
+
return "The lookback value is too large for the dataset. Please reduce the lookback value."
|
| 54 |
+
|
| 55 |
+
# DataLoader for batching
|
| 56 |
+
train_loader = data.DataLoader(data.TensorDataset(X_train, y_train), shuffle=True, batch_size=batch_size)
|
| 57 |
+
|
| 58 |
+
# Initialize model, optimizer, and loss function
|
| 59 |
+
model = AirModel()
|
| 60 |
+
optimizer = optim.Adam(model.parameters())
|
| 61 |
+
loss_fn = nn.MSELoss()
|
| 62 |
+
|
| 63 |
+
# Training loop with progress bar
|
| 64 |
+
for epoch in tqdm(range(epochs), desc="Training Progress"):
|
| 65 |
+
model.train()
|
| 66 |
+
epoch_loss = 0
|
| 67 |
+
for X_batch, y_batch in train_loader:
|
| 68 |
+
y_pred = model(X_batch)
|
| 69 |
+
loss = loss_fn(y_pred, y_batch)
|
| 70 |
+
optimizer.zero_grad()
|
| 71 |
+
loss.backward()
|
| 72 |
+
optimizer.step()
|
| 73 |
+
epoch_loss += loss.item()
|
| 74 |
+
|
| 75 |
+
# Prediction
|
| 76 |
+
model.eval()
|
| 77 |
+
with torch.no_grad():
|
| 78 |
+
train_plot = np.ones_like(timeseries) * np.nan
|
| 79 |
+
train_plot[lookback:train_size] = model(X_train)[:, -1, :].numpy()
|
| 80 |
+
|
| 81 |
+
test_plot = np.ones_like(timeseries) * np.nan
|
| 82 |
+
test_plot[train_size + lookback:len(timeseries)] = model(X_test)[:, -1, :].numpy()
|
| 83 |
+
|
| 84 |
+
# Plot results with Plotly
|
| 85 |
+
fig = go.Figure()
|
| 86 |
+
fig.add_trace(go.Scatter(y=timeseries.squeeze(), mode='lines', name='Original Data'))
|
| 87 |
+
fig.add_trace(go.Scatter(y=train_plot.squeeze(), mode='lines', name='Train Prediction', line=dict(color='red')))
|
| 88 |
+
fig.add_trace(go.Scatter(y=test_plot.squeeze(), mode='lines', name='Test Prediction', line=dict(color='green')))
|
| 89 |
+
fig.update_layout(title="Time Series Prediction", xaxis_title="Time", yaxis_title="Sales")
|
| 90 |
+
|
| 91 |
+
# Calculate Mean Absolute Error (MAE)
|
| 92 |
+
mae = np.mean(np.abs(test_plot[train_size + lookback:len(timeseries)] - timeseries[train_size + lookback:len(timeseries)]))
|
| 93 |
+
|
| 94 |
+
# Calculate Root Mean Squared Error (RMSE)
|
| 95 |
+
rmse = np.sqrt(np.mean((test_plot[train_size + lookback:len(timeseries)] - timeseries[train_size + lookback:len(timeseries)])**2))
|
| 96 |
+
|
| 97 |
+
return fig, f"Mean Absolute Error (MAE) on Test Data: {mae:.4f}, Root Mean Squared Error (RMSE): {rmse:.4f}"
|
| 98 |
+
|
| 99 |
+
# Gradio app interface using new API
|
| 100 |
+
interface = gr.Interface(
|
| 101 |
+
fn=train_and_predict,
|
| 102 |
+
inputs=[
|
| 103 |
+
gr.File(label="Upload your CSV file"),
|
| 104 |
+
gr.Slider(10, 365, step=1, value=100, label="Lookback"),
|
| 105 |
+
gr.Slider(100, 5000, step=100, value=1000, label="Epochs"),
|
| 106 |
+
gr.Slider(4, 32, step=1, value=8, label="Batch size")
|
| 107 |
+
],
|
| 108 |
+
outputs=[
|
| 109 |
+
gr.Plot(label="Prediction Plot"),
|
| 110 |
+
gr.Textbox(label="Error Metrics")
|
| 111 |
+
],
|
| 112 |
+
title="Time Series Prediction with LSTM",
|
| 113 |
+
description="Upload a CSV file with a 'Amount Net Sales' column and get time series predictions using an LSTM model.",
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Launch the app with a shareable link
|
| 117 |
+
interface.launch()
|