Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from autots import AutoTS
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
|
6 |
+
# Function to handle forecasting
|
7 |
+
def forecast_queues(data):
|
8 |
+
# Convert the input string to a pandas DataFrame
|
9 |
+
data = pd.read_csv(data)
|
10 |
+
|
11 |
+
# Ensure the 'date' column is in the correct datetime format
|
12 |
+
data['date'] = pd.to_datetime(data['date'], errors='coerce')
|
13 |
+
|
14 |
+
# Check for missing values and drop them if necessary
|
15 |
+
data = data.dropna(subset=['date', 'queue_length'])
|
16 |
+
|
17 |
+
# Setup AutoTS for time-series forecasting
|
18 |
+
model = AutoTS(forecast_length=12, frequency='D', ensemble=True, model_list="all")
|
19 |
+
model = model.fit(data, date_col='date', value_col='queue_length') # Adjust column names as per your CSV
|
20 |
+
|
21 |
+
# Generate forecast for the next 12 time periods
|
22 |
+
forecast = model.predict()
|
23 |
+
|
24 |
+
# Get the forecasted values
|
25 |
+
forecast_df = forecast.forecast
|
26 |
+
forecast_values = forecast_df['queue_length'].values.tolist()
|
27 |
+
|
28 |
+
# Create a Plotly figure to visualize the forecast
|
29 |
+
fig = go.Figure()
|
30 |
+
fig.add_trace(go.Scatter(x=forecast_df.index, y=forecast_values, mode='lines+markers', name='Forecast'))
|
31 |
+
fig.update_layout(title="Queue Length Forecast", xaxis_title="Date", yaxis_title="Queue Length")
|
32 |
+
|
33 |
+
return fig
|
34 |
+
|
35 |
+
# Gradio Interface function
|
36 |
+
def gradio_interface(file):
|
37 |
+
return forecast_queues(file.name)
|
38 |
+
|
39 |
+
# Define Gradio interface with file input and plot output
|
40 |
+
iface = gr.Interface(fn=gradio_interface,
|
41 |
+
inputs=gr.File(label="Upload your historical queue data (CSV with date and queue_length columns)"),
|
42 |
+
outputs=gr.Plot(),
|
43 |
+
live=True,
|
44 |
+
title="Queue Length Forecasting with AutoTS",
|
45 |
+
description="Upload a CSV file containing historical queue data with a date and queue_length columns, and get a forecast for the next 12 periods.")
|
46 |
+
|
47 |
+
# Launch the app
|
48 |
+
if __name__ == "__main__":
|
49 |
+
iface.launch()
|