File size: 3,721 Bytes
8505b0b
 
 
5a1efb7
 
06fe75a
 
8505b0b
5a1efb7
73225ce
8505b0b
 
5a1efb7
8505b0b
 
5a1efb7
 
 
 
 
 
 
 
 
 
 
 
8505b0b
5a1efb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f405c69
5a1efb7
 
98ae1ee
8505b0b
73225ce
8505b0b
 
 
 
 
 
 
 
c6e47b8
8505b0b
 
98ae1ee
5a1efb7
8505b0b
5d5436b
 
5a1efb7
8505b0b
 
 
 
 
 
 
 
 
5a1efb7
8505b0b
f405c69
 
73225ce
8505b0b
5a1efb7
8505b0b
 
 
 
 
 
 
 
5a1efb7
8505b0b
06fe75a
 
 
98ae1ee
73225ce
06fe75a
 
73225ce
06fe75a
f405c69
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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()