Spaces:
Runtime error
Runtime error
initial commit
Browse files- app.py +54 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from statsmodels.tsa.holtwinters import ExponentialSmoothing
|
| 5 |
+
from statsmodels.tsa.arima.model import ARIMA
|
| 6 |
+
from statsmodels.tsa.statespace.sarimax import SARIMAX
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def plot_graph(data, algorithm):
|
| 10 |
+
df = pd.read_csv(data)
|
| 11 |
+
|
| 12 |
+
columns = df.columns.values
|
| 13 |
+
|
| 14 |
+
if len(columns) < 2:
|
| 15 |
+
raise gr.Error('Неверная структура данных. Ожидается второй столбец value.')
|
| 16 |
+
|
| 17 |
+
df['Date'] = pd.to_datetime(df[columns[0]])
|
| 18 |
+
df = df.groupby(pd.Grouper(key='Date', freq='ME'))[columns[1]].sum().reset_index()
|
| 19 |
+
df.set_index('Date', inplace=True)
|
| 20 |
+
|
| 21 |
+
if algorithm == 'Exponential Smoothing':
|
| 22 |
+
if len(df) < 24:
|
| 23 |
+
raise gr.Error("Для Exponential Smoothing нужны данные за как минимум 24 месяца.")
|
| 24 |
+
model = ExponentialSmoothing(df[columns[1]], seasonal_periods=12, trend="add", seasonal="add")
|
| 25 |
+
model_fit = model.fit()
|
| 26 |
+
elif algorithm == 'ARIMA':
|
| 27 |
+
model = ARIMA(df[columns[1]], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
|
| 28 |
+
model_fit = model.fit()
|
| 29 |
+
elif algorithm == 'SARIMA':
|
| 30 |
+
model = SARIMAX(df[columns[1]], order=(1, 1, 1), seasonal_order=(1, 1, 1, 12))
|
| 31 |
+
model_fit = model.fit(disp=False)
|
| 32 |
+
|
| 33 |
+
last_date = df.index[-1]
|
| 34 |
+
forecast_dates = pd.date_range(start=last_date, periods=101, freq='MS')[1:]
|
| 35 |
+
prediction = model_fit.forecast(steps=100)
|
| 36 |
+
|
| 37 |
+
plt.figure(figsize=(10, 5))
|
| 38 |
+
plt.plot(df[columns[1]], label=columns[1])
|
| 39 |
+
plt.plot(forecast_dates, prediction, label="Прогноз")
|
| 40 |
+
plt.title(f'Прогноз {columns[1]} на следующие 100 месяцев')
|
| 41 |
+
plt.legend()
|
| 42 |
+
|
| 43 |
+
return plt
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
iface = gr.Interface(fn=plot_graph,
|
| 48 |
+
inputs=[gr.File(label="\'Date - Value\'. Example: 2010-01-01,100"),
|
| 49 |
+
gr.Radio(["Exponential Smoothing", "ARIMA", "SARIMA"],
|
| 50 |
+
label='Выберите алгоритм')],
|
| 51 |
+
outputs="plot"
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.33.0
|
| 2 |
+
matplotlib==3.9.0
|
| 3 |
+
pandas==2.2.2
|
| 4 |
+
statsmodels==0.14.2
|