Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
return resumo
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import requests
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Token vem dos "Repository secrets" no Hugging Face
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
+
MODEL = "google/timesfm-2.5-200m-pytorch"
|
|
|
|
| 11 |
|
| 12 |
+
API_URL = f"https://api-inference.huggingface.co/models/{MODEL}"
|
| 13 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 14 |
+
|
| 15 |
+
def forecast(file, date_col, value_col, steps):
|
| 16 |
+
# Lê CSV ou Excel
|
| 17 |
+
if file.name.endswith(".csv"):
|
| 18 |
+
df = pd.read_csv(file.name)
|
| 19 |
+
else:
|
| 20 |
+
df = pd.read_excel(file.name)
|
| 21 |
+
|
| 22 |
+
# Converte coluna de datas
|
| 23 |
+
df[date_col] = pd.to_datetime(df[date_col])
|
| 24 |
+
df = df.sort_values(by=date_col)
|
| 25 |
+
|
| 26 |
+
series = df[value_col].tolist()
|
| 27 |
+
|
| 28 |
+
# Payload para a API
|
| 29 |
+
payload = {
|
| 30 |
+
"inputs": series,
|
| 31 |
+
"parameters": {"prediction_length": steps}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 35 |
+
|
| 36 |
+
if response.status_code != 200:
|
| 37 |
+
return f"Erro na API: {response.text}", None
|
| 38 |
+
|
| 39 |
+
preds = response.json().get("prediction", series[-steps:])
|
| 40 |
+
|
| 41 |
+
# Gráfico
|
| 42 |
+
fig, ax = plt.subplots()
|
| 43 |
+
ax.plot(df[date_col], df[value_col], label="Histórico")
|
| 44 |
+
future_dates = pd.date_range(start=df[date_col].iloc[-1], periods=steps+1, freq="D")[1:]
|
| 45 |
+
ax.plot(future_dates, preds, label="Previsão", linestyle="--")
|
| 46 |
+
ax.legend()
|
| 47 |
+
plt.title("📊 Previsão de Vendas (TimesFM)")
|
| 48 |
+
|
| 49 |
+
buf = io.BytesIO()
|
| 50 |
+
plt.savefig(buf, format="png")
|
| 51 |
+
buf.seek(0)
|
| 52 |
+
|
| 53 |
+
return "✅ Previsão concluída!", buf
|
| 54 |
+
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("## 📈 Previsão de Vendas com TimesFM (Hugging Face)")
|
| 57 |
+
|
| 58 |
+
file = gr.File(label="Envie seu arquivo (.csv ou .xlsx)", file_types=[".csv", ".xlsx"])
|
| 59 |
+
date_col = gr.Textbox(label="Nome da coluna de datas")
|
| 60 |
+
value_col = gr.Textbox(label="Nome da coluna de valores")
|
| 61 |
+
steps = gr.Slider(1, 90, value=30, label="Quantos dias prever?")
|
| 62 |
+
output_text = gr.Textbox(label="Resultado")
|
| 63 |
+
output_plot = gr.Image(type="pil", label="Gráfico")
|
| 64 |
+
|
| 65 |
+
btn = gr.Button("Gerar Previsão")
|
| 66 |
+
btn.click(forecast, inputs=[file, date_col, value_col, steps], outputs=[output_text, output_plot])
|
| 67 |
|
| 68 |
demo.launch()
|