Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import TimeSeriesTransformerForPrediction, TimeSeriesTransformerConfig
|
|
|
3 |
import pandas as pd
|
4 |
import numpy as np
|
5 |
|
6 |
-
# Carregar configuração
|
7 |
config = TimeSeriesTransformerConfig.from_pretrained("google/timesfm-2.0-500m-pytorch")
|
|
|
|
|
8 |
|
9 |
-
#
|
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(
|
15 |
"google/timesfm-2.0-500m-pytorch",
|
16 |
config=config,
|
@@ -18,19 +17,20 @@ model = TimeSeriesTransformerForPrediction.from_pretrained(
|
|
18 |
)
|
19 |
|
20 |
def prever_vendas(historico):
|
21 |
-
# Converter entrada em
|
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"
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
|
31 |
# Gerar previsão
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
34 |
|
35 |
# Interface Gradio
|
36 |
iface = gr.Interface(
|
@@ -38,7 +38,7 @@ iface = gr.Interface(
|
|
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
|
42 |
]
|
43 |
)
|
44 |
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import TimeSeriesTransformerForPrediction, TimeSeriesTransformerConfig
|
3 |
+
import torch
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
|
7 |
+
# Carregar configuração
|
8 |
config = TimeSeriesTransformerConfig.from_pretrained("google/timesfm-2.0-500m-pytorch")
|
9 |
+
config.prediction_length = 3
|
10 |
+
config.context_length = 12
|
11 |
|
12 |
+
# Carregar modelo
|
|
|
|
|
|
|
|
|
13 |
model = TimeSeriesTransformerForPrediction.from_pretrained(
|
14 |
"google/timesfm-2.0-500m-pytorch",
|
15 |
config=config,
|
|
|
17 |
)
|
18 |
|
19 |
def prever_vendas(historico):
|
20 |
+
# Converter entrada em tensor
|
21 |
+
historico = [float(x) for x in historico.split(",") if x.strip()] # Ignorar valores vazios
|
|
|
|
|
22 |
if len(historico) != config.context_length:
|
23 |
+
raise ValueError(f"O histórico deve ter exatamente {config.context_length} valores numéricos.")
|
24 |
|
25 |
+
# Formatar dados como tensor (batch, sequence_length)
|
26 |
+
inputs = torch.tensor(historico).unsqueeze(0)
|
27 |
|
28 |
# Gerar previsão
|
29 |
+
with torch.no_grad():
|
30 |
+
outputs = model(inputs) # Método correto é __call__, não predict
|
31 |
+
forecast = outputs.mean.squeeze().tolist()
|
32 |
+
|
33 |
+
return np.round(forecast, 2)
|
34 |
|
35 |
# Interface Gradio
|
36 |
iface = gr.Interface(
|
|
|
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
|
42 |
]
|
43 |
)
|
44 |
|