Spaces:
Runtime error
Runtime error
Antonio Jesu虂s Acosta Lo虂pez
commited on
Commit
路
dd62e30
1
Parent(s):
e17265b
Commit inicial.
Browse files- app.py +83 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from kats.models.prophet import ProphetModel, ProphetParams
|
2 |
+
from kats.consts import TimeSeriesData
|
3 |
+
import pandas as pd
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
def build_model(time_series_csv, changepoint_range, changepoint_prior_scale, seasonality_prior_scale, seasonality_mode, test_size):
|
8 |
+
|
9 |
+
# time_series_csv = pd.DataFrame(time_series_csv)
|
10 |
+
time_series_csv = pd.read_csv(time_series_csv.name, delimiter=",")
|
11 |
+
|
12 |
+
# Divis贸n entre entrenamiento y test (sintaxis de Kats)
|
13 |
+
train_size = 1 - test_size
|
14 |
+
split_idx = int(len(time_series_csv) * train_size)
|
15 |
+
|
16 |
+
ts_train = TimeSeriesData(
|
17 |
+
pd.DataFrame(
|
18 |
+
{
|
19 |
+
"time": time_series_csv['time'][:split_idx],
|
20 |
+
"target": time_series_csv['value'][:split_idx]
|
21 |
+
}
|
22 |
+
)
|
23 |
+
)
|
24 |
+
|
25 |
+
ts_test = TimeSeriesData(
|
26 |
+
pd.DataFrame(
|
27 |
+
{
|
28 |
+
"time": time_series_csv['time'][split_idx:],
|
29 |
+
"target": time_series_csv['value'][split_idx:]
|
30 |
+
}
|
31 |
+
)
|
32 |
+
)
|
33 |
+
|
34 |
+
|
35 |
+
# Creaci贸n del modelo
|
36 |
+
prophet_params = ProphetParams(
|
37 |
+
changepoint_range = changepoint_range,
|
38 |
+
changepoint_prior_scale = changepoint_prior_scale,
|
39 |
+
seasonality_prior_scale = seasonality_prior_scale,
|
40 |
+
seasonality_mode = seasonality_mode
|
41 |
+
)
|
42 |
+
|
43 |
+
model = ProphetModel(ts_train, prophet_params)
|
44 |
+
|
45 |
+
# Entrenamiento del modelo
|
46 |
+
model.fit()
|
47 |
+
|
48 |
+
# Predicci贸n
|
49 |
+
forecasting = model.predict(
|
50 |
+
steps=len(ts_test)
|
51 |
+
)
|
52 |
+
|
53 |
+
result = pd.concat([
|
54 |
+
ts_test.to_dataframe().set_index('time'),
|
55 |
+
forecasting.set_index('time')
|
56 |
+
], axis=1)
|
57 |
+
|
58 |
+
fig = plt.figure()
|
59 |
+
plt.plot(result)
|
60 |
+
plt.title("Prediction")
|
61 |
+
|
62 |
+
return fig
|
63 |
+
|
64 |
+
|
65 |
+
# Interfaz
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=build_model,
|
68 |
+
inputs=[
|
69 |
+
gr.File(),
|
70 |
+
gr.Slider(0.8, 0.99),
|
71 |
+
gr.Number(precision=2),
|
72 |
+
gr.Number(precision=2),
|
73 |
+
gr.Radio(choices=['additive','multiplicative']),
|
74 |
+
gr.Slider(0.1,0.35,step=0.1,value=0.2)
|
75 |
+
],
|
76 |
+
outputs=gr.Plot(),
|
77 |
+
title="Prophet model generator",
|
78 |
+
description="Upload your .csv with a time series and set up your own Prophet model to make predictions. 馃敭",
|
79 |
+
allow_flagging='never',
|
80 |
+
theme="peach"
|
81 |
+
)
|
82 |
+
|
83 |
+
iface.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
kats
|
2 |
+
pandas
|
3 |
+
matplotlib
|