Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,124 +1,109 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import pandas as pd
|
| 4 |
-
import numpy as np
|
| 5 |
-
import tensorflow as tf
|
| 6 |
import joblib
|
| 7 |
-
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 8 |
-
from sklearn.preprocessing import MinMaxScaler
|
| 9 |
|
| 10 |
# Load the dataset
|
| 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 |
-
sarima_predictions = sarima_model.predict(n_periods=future_periods)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
lstm_predictions = scaler_y.inverse_transform(lstm_predictions_scaled)
|
| 49 |
-
|
| 50 |
-
# Combine predictions into a DataFrame for visualization
|
| 51 |
-
future_predictions = pd.DataFrame({
|
| 52 |
-
"Datetime": test_data['Datetime'],
|
| 53 |
-
"SARIMA_Predicted": sarima_predictions,
|
| 54 |
-
"LSTM_Predicted": lstm_predictions.flatten()
|
| 55 |
-
})
|
| 56 |
-
|
| 57 |
-
# Calculate metrics
|
| 58 |
-
mae_sarima_future = mean_absolute_error(test_data['Sessions'], sarima_predictions)
|
| 59 |
-
rmse_sarima_future = mean_squared_error(test_data['Sessions'], sarima_predictions, squared=False)
|
| 60 |
-
|
| 61 |
-
mae_lstm_future = mean_absolute_error(test_data['Sessions'], lstm_predictions)
|
| 62 |
-
rmse_lstm_future = mean_squared_error(test_data['Sessions'], lstm_predictions, squared=False)
|
| 63 |
-
|
| 64 |
-
# Function to plot actual vs. predicted traffic
|
| 65 |
-
def plot_predictions():
|
| 66 |
plt.figure(figsize=(15, 6))
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
plt.title("
|
| 84 |
plt.xlabel("Datetime", fontsize=12)
|
| 85 |
plt.ylabel("Sessions", fontsize=12)
|
| 86 |
plt.legend(loc="upper left")
|
| 87 |
plt.grid(True)
|
| 88 |
plt.tight_layout()
|
| 89 |
|
| 90 |
-
|
| 91 |
-
plot_path = "/content/predictions_plot.png"
|
| 92 |
plt.savefig(plot_path)
|
| 93 |
plt.close()
|
| 94 |
return plot_path
|
| 95 |
|
| 96 |
-
|
|
|
|
| 97 |
def display_metrics():
|
| 98 |
metrics = {
|
| 99 |
-
"Model": ["SARIMA"
|
| 100 |
-
"Mean Absolute Error (MAE)": [mae_sarima_future
|
| 101 |
-
"Root Mean Squared Error (RMSE)": [rmse_sarima_future,
|
| 102 |
}
|
| 103 |
return pd.DataFrame(metrics)
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
metrics_df = display_metrics()
|
| 109 |
return plot_path, metrics_df.to_string()
|
| 110 |
|
| 111 |
-
|
|
|
|
| 112 |
with gr.Blocks() as dashboard:
|
| 113 |
-
gr.Markdown("## Web Traffic Prediction Dashboard")
|
| 114 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
# Show the plot
|
| 117 |
plot_output = gr.Image(label="Prediction Plot")
|
| 118 |
-
metrics_output = gr.Textbox(label="
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
# Launch the dashboard
|
| 124 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import matplotlib.pyplot as plt
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
| 4 |
import joblib
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load the dataset
|
| 7 |
+
data_file = "webtraffic.csv"
|
| 8 |
+
webtraffic_data = pd.read_csv(data_file)
|
| 9 |
+
|
| 10 |
+
# Verify if 'Datetime' exists, or create it
|
| 11 |
+
if "Datetime" not in webtraffic_data.columns:
|
| 12 |
+
print("Datetime column missing. Attempting to create from 'Hour Index'.")
|
| 13 |
+
start_date = pd.Timestamp("2024-01-01 00:00:00")
|
| 14 |
+
webtraffic_data["Datetime"] = start_date + pd.to_timedelta(
|
| 15 |
+
webtraffic_data["Hour Index"], unit="h"
|
| 16 |
+
)
|
| 17 |
+
else:
|
| 18 |
+
webtraffic_data["Datetime"] = pd.to_datetime(webtraffic_data["Datetime"])
|
| 19 |
+
|
| 20 |
+
# Ensure 'Datetime' column is sorted
|
| 21 |
+
webtraffic_data.sort_values("Datetime", inplace=True)
|
| 22 |
+
|
| 23 |
+
# Load the SARIMA model
|
| 24 |
+
sarima_model = joblib.load("sarima_model.pkl")
|
| 25 |
+
|
| 26 |
+
# Define future periods for evaluation
|
| 27 |
+
future_periods = 48
|
| 28 |
+
|
| 29 |
+
# Dummy values for metrics (if needed)
|
| 30 |
+
mae_sarima_future = 100
|
| 31 |
+
rmse_sarima_future = 150
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Function to generate plot based on SARIMA model
|
| 35 |
+
def generate_plot():
|
| 36 |
+
future_dates = pd.date_range(
|
| 37 |
+
start=webtraffic_data["Datetime"].iloc[-1], periods=future_periods + 1, freq="H"
|
| 38 |
+
)[1:]
|
| 39 |
+
|
| 40 |
+
sarima_predictions = sarima_model.predict(n_periods=future_periods)
|
| 41 |
+
future_predictions = pd.DataFrame(
|
| 42 |
+
{"Datetime": future_dates, "SARIMA_Predicted": sarima_predictions}
|
| 43 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
plt.figure(figsize=(15, 6))
|
| 45 |
+
plt.plot(
|
| 46 |
+
webtraffic_data["Datetime"],
|
| 47 |
+
webtraffic_data["Sessions"],
|
| 48 |
+
label="Actual Traffic",
|
| 49 |
+
color="black",
|
| 50 |
+
linestyle="dotted",
|
| 51 |
+
linewidth=2,
|
| 52 |
+
)
|
| 53 |
+
plt.plot(
|
| 54 |
+
future_predictions["Datetime"],
|
| 55 |
+
future_predictions["SARIMA_Predicted"],
|
| 56 |
+
label="SARIMA Predicted",
|
| 57 |
+
color="blue",
|
| 58 |
+
linewidth=2,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
plt.title("SARIMA Predictions vs Actual Traffic", fontsize=16)
|
| 62 |
plt.xlabel("Datetime", fontsize=12)
|
| 63 |
plt.ylabel("Sessions", fontsize=12)
|
| 64 |
plt.legend(loc="upper left")
|
| 65 |
plt.grid(True)
|
| 66 |
plt.tight_layout()
|
| 67 |
|
| 68 |
+
plot_path = "sarima_prediction_plot.png"
|
|
|
|
| 69 |
plt.savefig(plot_path)
|
| 70 |
plt.close()
|
| 71 |
return plot_path
|
| 72 |
|
| 73 |
+
|
| 74 |
+
# Function to display SARIMA metrics
|
| 75 |
def display_metrics():
|
| 76 |
metrics = {
|
| 77 |
+
"Model": ["SARIMA"],
|
| 78 |
+
"Mean Absolute Error (MAE)": [mae_sarima_future],
|
| 79 |
+
"Root Mean Squared Error (RMSE)": [rmse_sarima_future],
|
| 80 |
}
|
| 81 |
return pd.DataFrame(metrics)
|
| 82 |
|
| 83 |
+
|
| 84 |
+
# Gradio interface function
|
| 85 |
+
def dashboard_interface():
|
| 86 |
+
plot_path = generate_plot()
|
| 87 |
metrics_df = display_metrics()
|
| 88 |
return plot_path, metrics_df.to_string()
|
| 89 |
|
| 90 |
+
|
| 91 |
+
# Build the Gradio interface
|
| 92 |
with gr.Blocks() as dashboard:
|
| 93 |
+
gr.Markdown("## Interactive SARIMA Web Traffic Prediction Dashboard")
|
| 94 |
+
gr.Markdown(
|
| 95 |
+
"This dashboard shows SARIMA model predictions vs actual traffic along with performance metrics."
|
| 96 |
+
)
|
| 97 |
|
|
|
|
| 98 |
plot_output = gr.Image(label="Prediction Plot")
|
| 99 |
+
metrics_output = gr.Textbox(label="Metrics", lines=15)
|
| 100 |
|
| 101 |
+
gr.Button("Generate Predictions").click(
|
| 102 |
+
fn=dashboard_interface,
|
| 103 |
+
inputs=[],
|
| 104 |
+
outputs=[plot_output, metrics_output],
|
| 105 |
+
)
|
| 106 |
|
| 107 |
+
# Launch the Gradio dashboard
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
dashboard.launch()
|