File size: 1,522 Bytes
60de325
 
a1e2d43
60de325
 
 
a1e2d43
9068905
a1e2d43
 
6ac2735
a1e2d43
9627e09
 
9068905
 
9627e09
9068905
60de325
a1e2d43
 
6ac2735
a1e2d43
6ac2735
a1e2d43
 
60de325
9068905
a1e2d43
 
 
 
 
60de325
 
 
 
6ac2735
 
60de325
a1e2d43
60de325
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import gradio as gr
from transformers import TimeSeriesTransformerForPrediction, TimeSeriesTransformerConfig
import torch
import pandas as pd
import numpy as np

# Carregar configuração
config = TimeSeriesTransformerConfig.from_pretrained("google/timesfm-2.0-500m-pytorch")
config.prediction_length = 3
config.context_length = 12

# Carregar modelo
model = TimeSeriesTransformerForPrediction.from_pretrained(
    "google/timesfm-2.0-500m-pytorch",
    config=config,
    torch_dtype="auto"
)

def prever_vendas(historico):
    # Converter entrada em tensor
    historico = [float(x) for x in historico.split(",") if x.strip()]  # Ignorar valores vazios
    if len(historico) != config.context_length:
        raise ValueError(f"O histórico deve ter exatamente {config.context_length} valores numéricos.")
    
    # Formatar dados como tensor (batch, sequence_length)
    inputs = torch.tensor(historico).unsqueeze(0)
    
    # Gerar previsão
    with torch.no_grad():
        outputs = model(inputs)  # Método correto é __call__, não predict
        forecast = outputs.mean.squeeze().tolist()
    
    return np.round(forecast, 2)

# Interface Gradio
iface = gr.Interface(
    fn=prever_vendas,
    inputs=gr.Textbox(label=f"Histórico de Vendas ({config.context_length} meses, separados por vírgulas)"),
    outputs=gr.Textbox(label=f"Previsão para os Próximos {config.prediction_length} Meses"),
    examples=[
        ["140,155,160,145,150,165,170,160,175,160,155,170"],  # 12 meses
    ]
)

iface.launch()