Spaces:
Build error
Build error
Download app.py from Capebm/RF: direct link, hf CLI and curl.
- Browser
- Download file 3.72 kB
-
https://huggingface.co/spaces/Capebm/RF/resolve/main/app.py
- Command line
-
hf download hf://spaces/Capebm/RF/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Capebm/RF/resolve/main/app.py
3.72 kB
| import pandas as pd | |
| import numpy as np | |
| import gradio as gr | |
| from sklearn.ensemble import RandomForestRegressor | |
| from sklearn.model_selection import train_test_split | |
| import webbrowser | |
| import threading | |
| # Load the dataset | |
| file_path = 'Volumes_processed.xlsx' # This file should be uploaded to the Hugging Face Space | |
| data = pd.read_excel(file_path) | |
| # Transpose the data to have months as rows | |
| data_transposed = data.set_index('Processos').T | |
| # Prepare data for training | |
| def create_lagged_features(data, lag=12): | |
| df = pd.DataFrame(data) | |
| columns = [df.shift(i) for i in range(1, lag+1)] | |
| columns.append(df) | |
| df = pd.concat(columns, axis=1) | |
| df.columns = [f'lag_{i}' for i in range(lag, 0, -1)] + ['target'] | |
| df.dropna(inplace=True) | |
| return df | |
| # Function to forecast future values for each process | |
| def forecast_process_ml(data, process_name, n_future): | |
| process_data = data[process_name].dropna() | |
| lagged_data = create_lagged_features(process_data) | |
| X = lagged_data.drop('target', axis=1) | |
| y = lagged_data['target'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| model = RandomForestRegressor() | |
| model.fit(X_train, y_train) | |
| future_lags = X.values[-1].tolist() | |
| forecasts = [] | |
| for _ in range(n_future): | |
| future_lag_df = pd.DataFrame([future_lags], columns=X.columns) | |
| forecast = model.predict(future_lag_df)[0] | |
| forecasts.append(int(forecast)) # Convert forecast to integer | |
| future_lags = future_lags[1:] + [forecast] | |
| return forecasts | |
| # 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_ml(data_transposed, process, n_months) | |
| results[process] = forecast_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): | |
| forecast_df = get_forecast(month, process, n_months) | |
| forecast_df.to_excel("forecast_results.xlsx", index=False) # Export to Excel | |
| return forecast_df | |
| # Gradio UI with CSS for black background | |
| css = """ | |
| body { | |
| background-color: black; | |
| color: white; | |
| } | |
| """ | |
| with gr.Blocks(css=css) as iface: | |
| gr.Markdown("## BCTT: Prever Processos com Random Forest") | |
| 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() | |