File size: 1,850 Bytes
60de325
 
a1e2d43
60de325
 
 
a1e2d43
9068905
a1e2d43
 
6ac2735
a1e2d43
9627e09
 
9068905
 
9627e09
9068905
60de325
a1e2d43
1106ef4
6ac2735
1106ef4
6ac2735
1106ef4
a1e2d43
60de325
1106ef4
 
 
 
9068905
a1e2d43
1106ef4
 
 
 
 
a1e2d43
 
 
60de325
 
 
 
6ac2735
 
60de325
a1e2d43
1106ef4
 
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 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()]
    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 ausentes (valores dummy para exemplo)
    past_time_features = torch.zeros(1, config.context_length, 1)  # Ex: timestamps normalizados
    past_observed_mask = torch.ones(1, config.context_length)      # Todos os 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"],  # 12 meses
    ],
    cache_examples=False  # Desativar cache para evitar erro de arquivo
)

iface.launch()