Spaces:
Sleeping
Sleeping
File size: 1,986 Bytes
60de325 a1e2d43 60de325 a1e2d43 9068905 a1e2d43 d1d52f3 dc40231 6ac2735 6c8b39b 9627e09 9068905 9627e09 9068905 60de325 a1e2d43 1106ef4 6ac2735 1106ef4 6ac2735 1106ef4 a1e2d43 60de325 6c8b39b 1106ef4 9068905 a1e2d43 1106ef4 a1e2d43 60de325 6ac2735 60de325 d1d52f3 1106ef4 6c8b39b 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 46 47 48 49 50 51 52 53 54 |
import gradio as gr
from transformers import TimeSeriesTransformerForPrediction, TimeSeriesTransformerConfig
import torch
import numpy as np
# Carregar configuração
config = TimeSeriesTransformerConfig.from_pretrained("google/timesfm-2.0-500m-pytorch")
config.prediction_length = 3
config.context_length = 20 # Aumentado para acomodar os lags
config.lags_sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] # Lags menores que context_length
# Carregar modelo com a configuração ajustada
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()]
if len(historico) != config.context_length:
raise ValueError(f"Histórico deve ter {config.context_length} valores.")
# Formatar dados
inputs = torch.tensor(historico).unsqueeze(0)
# Adicionar parâmetros necessários
past_time_features = torch.zeros(1, config.context_length, 1) # Características temporais dummy
past_observed_mask = torch.ones(1, config.context_length) # Dados observados
# Gerar previsão
with torch.no_grad():
outputs = model(
inputs,
past_time_features=past_time_features,
past_observed_mask=past_observed_mask
)
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,180,190,200,210,220,230,240,250"], # 20 meses
],
cache_examples=False # Desativar cache para evitar erros
)
iface.launch() |