Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,10 @@ import numpy as np
|
|
5 |
|
6 |
# Carregar configuração do modelo
|
7 |
config = TimeSeriesTransformerConfig.from_pretrained("google/timesfm-2.0-500m-pytorch")
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
# Carregar modelo com a configuração ajustada
|
11 |
model = TimeSeriesTransformerForPrediction.from_pretrained(
|
@@ -18,20 +21,24 @@ def prever_vendas(historico):
|
|
18 |
# Converter entrada em lista de números
|
19 |
historico = [float(x) for x in historico.split(",")]
|
20 |
|
21 |
-
#
|
|
|
|
|
|
|
|
|
22 |
data = pd.Series(historico)
|
23 |
|
24 |
# Gerar previsão
|
25 |
-
forecast = model.predict(data, prediction_length=
|
26 |
return np.round(forecast.mean, 2).tolist()
|
27 |
|
28 |
# Interface Gradio
|
29 |
iface = gr.Interface(
|
30 |
fn=prever_vendas,
|
31 |
-
inputs=gr.Textbox(label="Histórico de Vendas (
|
32 |
-
outputs=gr.Textbox(label="Previsão para os Próximos
|
33 |
examples=[
|
34 |
-
["140,155,160,145,150,165,170,160,175,160,155,170"], #
|
35 |
]
|
36 |
)
|
37 |
|
|
|
5 |
|
6 |
# Carregar configuração do modelo
|
7 |
config = TimeSeriesTransformerConfig.from_pretrained("google/timesfm-2.0-500m-pytorch")
|
8 |
+
|
9 |
+
# Definir parâmetros obrigatórios
|
10 |
+
config.prediction_length = 3 # Períodos futuros a prever
|
11 |
+
config.context_length = 12 # Períodos históricos usados (ex: 12 meses)
|
12 |
|
13 |
# Carregar modelo com a configuração ajustada
|
14 |
model = TimeSeriesTransformerForPrediction.from_pretrained(
|
|
|
21 |
# Converter entrada em lista de números
|
22 |
historico = [float(x) for x in historico.split(",")]
|
23 |
|
24 |
+
# Garantir que o histórico tem o tamanho do context_length
|
25 |
+
if len(historico) != config.context_length:
|
26 |
+
raise ValueError(f"Histórico deve ter {config.context_length} valores (context_length).")
|
27 |
+
|
28 |
+
# Preparar dados
|
29 |
data = pd.Series(historico)
|
30 |
|
31 |
# Gerar previsão
|
32 |
+
forecast = model.predict(data, prediction_length=config.prediction_length)
|
33 |
return np.round(forecast.mean, 2).tolist()
|
34 |
|
35 |
# Interface Gradio
|
36 |
iface = gr.Interface(
|
37 |
fn=prever_vendas,
|
38 |
+
inputs=gr.Textbox(label=f"Histórico de Vendas ({config.context_length} meses, separados por vírgulas)"),
|
39 |
+
outputs=gr.Textbox(label=f"Previsão para os Próximos {config.prediction_length} Meses"),
|
40 |
examples=[
|
41 |
+
["140,155,160,145,150,165,170,160,175,160,155,170"], # 12 meses (context_length=12)
|
42 |
]
|
43 |
)
|
44 |
|