|
import gradio as gr |
|
import pandas as pd |
|
import numpy as np |
|
import plotly.graph_objects as go |
|
import joblib |
|
import os |
|
|
|
|
|
def load_arima_model(): |
|
model = joblib.load('arima_sales_model.pkl') |
|
return model |
|
|
|
|
|
arima_model = load_arima_model() |
|
|
|
|
|
def drop(dataframe): |
|
pass |
|
|
|
def date_format(dataframe): |
|
pass |
|
|
|
def group_to_three(dataframe): |
|
pass |
|
|
|
def get_forecast_period(period): |
|
return period |
|
|
|
def sales_growth(df, forecasted_series): |
|
return forecasted_series.diff() |
|
|
|
def merge_forecast_data(actual, predicted, future): |
|
return pd.DataFrame({ |
|
"Actual Sales": actual, |
|
"Predicted Sales": predicted, |
|
"Forecasted Future Sales": future |
|
}) |
|
|
|
|
|
def check_file(uploaded_file): |
|
if uploaded_file is None: |
|
return gr.Error("⚠️ No file uploaded. Please upload a CSV file.") |
|
|
|
|
|
if not uploaded_file.endswith('.csv'): |
|
return gr.Error("⚠️ Invalid file format. Please upload a CSV file.") |
|
|
|
|
|
file_size = uploaded_file.size |
|
if file_size > 200 * 1024 * 1024: |
|
return gr.Error("⚠️ File size exceeds the 200MB limit. Please upload a smaller file.") |
|
|
|
return None |
|
|
|
|
|
def upload_and_forecast(uploaded_file, period): |
|
|
|
error_message = check_file(uploaded_file) |
|
if error_message: |
|
return error_message |
|
|
|
|
|
df = pd.read_csv(uploaded_file) |
|
df = drop(df) |
|
df = date_format(df) |
|
series = group_to_three(df) |
|
|
|
|
|
forecast_period = get_forecast_period(period) |
|
forecasted_values, confint = arima_model.predict(n_periods=forecast_period, return_conf_int=True) |
|
|
|
|
|
forecasted_series = pd.Series(forecasted_values) |
|
forecasted_series.index = pd.date_range(df['Date'].iloc[-1], periods=forecast_period, freq='3D') |
|
|
|
|
|
future_sales_growth = sales_growth(df, forecasted_series) |
|
|
|
|
|
merged_data = merge_forecast_data(df['Sales'], series, forecasted_series) |
|
|
|
|
|
fig_compare = go.Figure() |
|
fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales')) |
|
fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Predicted Sales'], mode='lines', name='Predicted Sales', line=dict(color='#006400'))) |
|
fig_compare.update_layout(title='📊 Historical Sales Data', xaxis_title='Date', yaxis_title='Sales') |
|
|
|
fig_forecast = go.Figure() |
|
fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales')) |
|
fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=forecasted_series, mode='lines', name='Forecasted Sales')) |
|
fig_forecast.update_layout(title='🔮 Forecasted Sales Data', xaxis_title='Date', yaxis_title='Sales') |
|
|
|
return fig_compare, fig_forecast, future_sales_growth |
|
|
|
|
|
def create_sidebar(): |
|
with gr.Column(): |
|
|
|
gr.Markdown("### 📂 Upload your sales data (CSV)") |
|
uploaded_file = gr.File( |
|
label="Choose your file", |
|
elem_id="file-uploader", |
|
type="filepath", |
|
file_count="single", |
|
file_types=[".csv"], |
|
interactive=True, |
|
) |
|
gr.Markdown("### ⏳ Forecast Period (Days)") |
|
period = gr.Slider(minimum=30, maximum=90, step=1, label="Forecast period (in days)") |
|
|
|
|
|
sample_file_path = "sample_data.csv" |
|
if not os.path.exists(sample_file_path): |
|
sample_data = pd.DataFrame({ |
|
"Date": ["2023-01-01", "2023-01-02", "2023-01-03"], |
|
"Sales": [100, 200, 300] |
|
}) |
|
sample_data.to_csv(sample_file_path, index=False) |
|
gr.File(label="Download our sample CSV", file_path=sample_file_path) |
|
|
|
return uploaded_file, period |
|
|
|
|
|
uploaded_file, period = create_sidebar() |
|
|
|
output_plots = [ |
|
gr.Plot(label="📈 Historical vs Predicted Sales"), |
|
gr.Plot(label="🔮 Forecasted Sales Data"), |
|
gr.DataFrame(label="📊 Sales Growth") |
|
] |
|
|
|
iface = gr.Interface( |
|
fn=upload_and_forecast, |
|
inputs=[uploaded_file, period], |
|
outputs=output_plots, |
|
live=True, |
|
title="Sales Forecasting System ✨", |
|
description="Upload your sales data to start forecasting 🚀", |
|
css=open("styles.css", "r").read() |
|
) |
|
|
|
iface.launch() |
|
|