Download app.py from Capebm/HW: direct link, hf CLI and curl.
- Browser
- Download file 2.76 kB
-
https://huggingface.co/spaces/Capebm/HW/resolve/main/app.py
- Command line
-
hf download hf://spaces/Capebm/HW/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Capebm/HW/resolve/main/app.py
2.76 kB
| import pandas as pd | |
| import numpy as np | |
| import gradio as gr | |
| from statsmodels.tsa.holtwinters import ExponentialSmoothing | |
| import webbrowser | |
| import threading | |
| # Load the dataset | |
| file_path = 'Volumes_processed.xlsx' # Make sure to upload this file to the space as well | |
| data = pd.read_excel(file_path) | |
| # Transpose the data to have months as rows | |
| data_transposed = data.set_index('Processos').T | |
| # Function to forecast future values for each process using Holt-Winters | |
| def forecast_process(data, process_name, n_future): | |
| process_data = data[process_name].dropna() | |
| model = ExponentialSmoothing(process_data, seasonal='add', seasonal_periods=12) | |
| fit_model = model.fit() | |
| forecasted_values = fit_model.forecast(steps=n_future) | |
| return forecasted_values | |
| # Gradio function to get forecast for a given month and process | |
| def get_forecast(month, processes, n_months): | |
| month_map = { | |
| "Janeiro": 1, "Fevereiro": 2, "Março": 3, "Abril": 4, | |
| "Maio": 5, "Junho": 6, "Julho": 7, "Agosto": 8, | |
| "Setembro": 9, "Outubro": 10, "Novembro": 11, "Dezembro": 12 | |
| } | |
| month_num = month_map[month] | |
| results = {} | |
| forecasted_month_names = pd.date_range(start=f'2024-{month_num:02d}-01', periods=n_months, freq='M').strftime("%b") | |
| results["Mês"] = forecasted_month_names | |
| for process in processes: | |
| forecast_values = forecast_process(data_transposed, process, n_months) | |
| results[process] = forecast_values.values | |
| forecast_df = pd.DataFrame(results) | |
| return forecast_df | |
| # Default values | |
| default_month = pd.Timestamp.now().month | |
| month_map_reverse = { | |
| 1: "Janeiro", 2: "Fevereiro", 3: "Março", 4: "Abril", | |
| 5: "Maio", 6: "Junho", 7: "Julho", 8: "Agosto", | |
| 9: "Setembro", 10: "Outubro", 11: "Novembro", 12: "Dezembro" | |
| } | |
| default_month_name = month_map_reverse[default_month] | |
| default_processes = list(data['Processos'].unique()) | |
| # Gradio interface | |
| def forecast_interface(month=default_month_name, process=default_processes, n_months=9): | |
| return get_forecast(month, process, n_months) | |
| # Gradio UI | |
| css = """ | |
| body { | |
| background-color: black; | |
| color: white; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as iface: | |
| gr.Markdown("## BCTT: Prever Processos com Holt Winters") | |
| month = gr.Dropdown(choices=list(month_map_reverse.values()), value=default_month_name, label="Mês de Inicio da Previsão") | |
| process = gr.CheckboxGroup(choices=default_processes, value=default_processes, label="Processos") | |
| n_months = gr.Slider(minimum=1, maximum=12, step=1, value=9, label="Número de Meses a Prever") | |
| output = gr.DataFrame() | |
| btn = gr.Button("Prever") | |
| btn.click(forecast_interface, inputs=[month, process, n_months], outputs=output) | |
| iface.launch() |