Spaces:
Sleeping
Sleeping
File size: 4,570 Bytes
c4cf758 a32c351 873bd97 c4cf758 1867a74 3154606 1867a74 a32c351 c4cf758 1867a74 a32c351 c4cf758 1867a74 a32c351 f69d4dd a32c351 f69d4dd a32c351 873bd97 a32c351 1867a74 f69d4dd 1867a74 f69d4dd a32c351 f69d4dd a32c351 c4cf758 1867a74 f69d4dd c4cf758 f69d4dd c4cf758 f69d4dd a32c351 f69d4dd a32c351 f69d4dd c4cf758 f69d4dd c4cf758 f69d4dd a32c351 f69d4dd a32c351 f69d4dd 1867a74 a32c351 c4cf758 f69d4dd c4cf758 1867a74 f69d4dd c4cf758 f69d4dd c4cf758 f69d4dd c4cf758 1867a74 c4cf758 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import gradio as gr
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import tensorflow as tf
import joblib
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.preprocessing import MinMaxScaler
# Load the dataset
webtraffic_data = pd.read_csv("webtraffic.csv")
# Convert 'Hour Index' to datetime
start_date = pd.Timestamp("2024-01-01 00:00:00")
webtraffic_data['Datetime'] = start_date + pd.to_timedelta(webtraffic_data['Hour Index'], unit='h')
webtraffic_data.drop(columns=['Hour Index'], inplace=True)
# Split the data into train/test
train_size = int(len(webtraffic_data) * 0.8)
train_data = webtraffic_data.iloc[:train_size]
test_data = webtraffic_data.iloc[train_size:]
# Load pre-trained models
sarima_model = joblib.load("sarima_model.pkl") # SARIMA model
lstm_model = tf.keras.models.load_model("lstm_model.keras") # LSTM model
# Initialize scalers and scale the data for LSTM
scaler_X = MinMaxScaler(feature_range=(0, 1))
scaler_y = MinMaxScaler(feature_range=(0, 1))
# Scale training data
X_train_scaled = scaler_X.fit_transform(train_data['Sessions'].values.reshape(-1, 1))
y_train_scaled = scaler_y.fit_transform(train_data['Sessions'].values.reshape(-1, 1))
# Scale test data
X_test_scaled = scaler_X.transform(test_data['Sessions'].values.reshape(-1, 1))
y_test_scaled = scaler_y.transform(test_data['Sessions'].values.reshape(-1, 1))
# Reshape test data for LSTM (samples, time_steps, features)
X_test_lstm = X_test_scaled.reshape((X_test_scaled.shape[0], 1, 1))
# Generate predictions for SARIMA
future_periods = len(test_data)
sarima_predictions = sarima_model.predict(n_periods=future_periods)
# Generate predictions for LSTM
lstm_predictions_scaled = lstm_model.predict(X_test_lstm[:future_periods])
lstm_predictions = scaler_y.inverse_transform(lstm_predictions_scaled)
# Combine predictions into a DataFrame for visualization
future_predictions = pd.DataFrame({
"Datetime": test_data['Datetime'],
"SARIMA_Predicted": sarima_predictions,
"LSTM_Predicted": lstm_predictions.flatten()
})
# Calculate metrics
mae_sarima_future = mean_absolute_error(test_data['Sessions'], sarima_predictions)
rmse_sarima_future = mean_squared_error(test_data['Sessions'], sarima_predictions, squared=False)
mae_lstm_future = mean_absolute_error(test_data['Sessions'], lstm_predictions)
rmse_lstm_future = mean_squared_error(test_data['Sessions'], lstm_predictions, squared=False)
# Function to plot actual vs. predicted traffic
def plot_predictions():
plt.figure(figsize=(15, 6))
# Plot actual traffic
plt.plot(webtraffic_data['Datetime'].iloc[-future_periods:],
test_data['Sessions'].values[-future_periods:],
label='Actual Traffic', color='black', linestyle='dotted', linewidth=2)
# Plot SARIMA predictions
plt.plot(future_predictions['Datetime'],
future_predictions['SARIMA_Predicted'],
label='SARIMA Predicted', color='blue', linewidth=2)
# Plot LSTM predictions
plt.plot(future_predictions['Datetime'],
future_predictions['LSTM_Predicted'],
label='LSTM Predicted', color='green', linewidth=2)
plt.title("Future Traffic Predictions: SARIMA vs LSTM", fontsize=16)
plt.xlabel("Datetime", fontsize=12)
plt.ylabel("Sessions", fontsize=12)
plt.legend(loc="upper left")
plt.grid(True)
plt.tight_layout()
# Save the plot to a file
plot_path = "/content/predictions_plot.png"
plt.savefig(plot_path)
plt.close()
return plot_path
# Function to display prediction metrics
def display_metrics():
metrics = {
"Model": ["SARIMA", "LSTM"],
"Mean Absolute Error (MAE)": [mae_sarima_future, mae_lstm_future],
"Root Mean Squared Error (RMSE)": [rmse_sarima_future, rmse_lstm_future]
}
return pd.DataFrame(metrics)
# Gradio function to display the dashboard
def gradio_dashboard():
plot_path = plot_predictions()
metrics_df = display_metrics()
return plot_path, metrics_df.to_string()
# Gradio interface
with gr.Blocks() as dashboard:
gr.Markdown("## Web Traffic Prediction Dashboard")
gr.Markdown("This dashboard compares predictions from SARIMA and LSTM models.")
# Show the plot
plot_output = gr.Image(label="Prediction Plot")
metrics_output = gr.Textbox(label="Prediction Metrics", lines=15)
# Define the Gradio button and actions
gr.Button("Update Dashboard").click(gradio_dashboard, outputs=[plot_output, metrics_output])
# Launch the dashboard
dashboard.launch()
|