Spaces:
Sleeping
Sleeping
| import openpyxl | |
| import gradio as gr | |
| import joblib | |
| import pandas as pd | |
| import numpy as np | |
| import seaborn as sns | |
| from sklearn.preprocessing import MinMaxScaler | |
| import statsmodels.api as sm | |
| import matplotlib.pyplot as plt | |
| df_model = pd.read_excel('dados/MOD_V_SALA_Z1_002E/MOD_V_SALA_Z1_002E.xlsx') | |
| df_model[['RH', 'APRIV', 'VUNIT']] = np.log(df_model[['RH', 'APRIV', 'VUNIT']]) | |
| # Separar as variáveis independentes (X) e dependente (y) | |
| X = df_model.drop(['VUNIT'], axis=1) | |
| y = df_model['VUNIT'] | |
| def execute(ANO_2020, ANO_2021, ANO_2022, ANO_2023, ANO_2024, ANO_2025, RH, PAVTO, IDmenor10, ID11a20, ID21a30, ID31a40, FIN, LUX, APRIV): | |
| df = pd.DataFrame.from_dict({'ANO_2020': int(ANO_2020), | |
| 'ANO_2021': int(ANO_2021), | |
| 'ANO_2022': int(ANO_2022), | |
| 'ANO_2023': int(ANO_2023), | |
| 'ANO_2024': int(ANO_2024), | |
| 'ANO_2025': int(ANO_2025), | |
| 'RH': np.log([RH]), | |
| 'PAVTO': [PAVTO], | |
| 'IDmenor10': int(IDmenor10), | |
| 'ID11a20': int(ID11a20), | |
| 'ID21a30': int(ID21a30), | |
| 'ID31a40': int(ID31a40), | |
| 'FIN': int(FIN), | |
| 'LUX': int(LUX), | |
| 'APRIV': np.log([APRIV])} | |
| ) | |
| X_with_constant = sm.add_constant(X) | |
| model = sm.OLS(y, X_with_constant) | |
| results = model.fit() | |
| X_new = np.array(df).reshape(1, -1) | |
| X_new_with_constant = np.insert(X_new, 0, 1) | |
| y_pred = results.predict(X_new_with_constant) | |
| ci = results.get_prediction(X_new_with_constant).conf_int(alpha=0.2) | |
| lower_bound = np.exp(ci[0][0]) | |
| per_low = (np.exp(y_pred)-np.exp(ci[0][0]))/np.exp(y_pred) | |
| upper_bound = np.exp(ci[0][1]) | |
| per_up = (np.exp(ci[0][1])-np.exp(y_pred))/np.exp(y_pred) | |
| pred = np.exp(y_pred) | |
| X_plot = np.linspace(min(df_model['VUNIT']), max(df_model['VUNIT']), 100) | |
| Y_plot = X_plot | |
| X_pred = np.array(X) | |
| X_pred_cons = np.insert(X_pred, 0, 1, axis=1) | |
| x_, y_ = pd.Series(np.array(df_model['VUNIT']), name="Observado"), pd.Series(results.predict(X_pred_cons), name="Previsto") | |
| plt.figure(figsize=(18, 6)) | |
| sns.scatterplot(x=x_, y=y_) | |
| plt.xlabel("Observados") | |
| plt.ylabel("Previstos") | |
| plt.tight_layout() | |
| plt.plot(X_plot, Y_plot, color='r') | |
| plt.savefig("scatterplot.png") | |
| plt.close() | |
| texto_1 = f"R$ {round(pred[0], 2)}" | |
| texto_2 = f"R$ {round(lower_bound, 2)}" | |
| texto_3 = f"{round(per_low[0]*100, 2)}%" | |
| texto_4 = f"R$ {round(upper_bound, 2)}" | |
| texto_5 = f"{round(per_up[0]*100, 2)}%" | |
| return texto_1, texto_2, texto_3, texto_4, texto_5, "scatterplot.png" | |
| def load_inputs(): | |
| APRIV = gr.Number(value = 35., label='Área Privativa') | |
| RH = gr.Number(value = 250, label='Região Homogênea') | |
| PAVTO = gr.Number(value = 5, label='Pavimento') | |
| IDmenor10 = gr.Checkbox(value=True, label='Idade menor ou igual a 10 anos') | |
| ID11a20 = gr.Checkbox(value=False, label='Idade entre 11 anos e 20 anos') | |
| ID21a30 = gr.Checkbox(value=False, label='Idade entre 21 anos e 30 anos') | |
| ID31a40 = gr.Checkbox(value=False, label='Idade entre 31 anos e 40 anos') | |
| FIN = gr.Checkbox(value=False, label='Padrão Fino') | |
| LUX = gr.Checkbox(value=True, label='Padrão Luxo') | |
| ANO_2020 = gr.Checkbox(value=False, label='Ano 2020 - Carga Geral 2021') | |
| ANO_2021 = gr.Checkbox(value=False, label='Ano 2021 - Carga Geral 2022') | |
| ANO_2022 = gr.Checkbox(value=False, label='Ano 2022 - Carga Geral 2023') | |
| ANO_2023 = gr.Checkbox(value=False, label='Ano 2023 - Carga Geral 2024') | |
| ANO_2024 = gr.Checkbox(value=True, label='Ano 2024 - Carga Geral 2025') | |
| ANO_2025 = gr.Checkbox(value=False, label='Ano 2025') | |
| return [ANO_2020, ANO_2021, ANO_2022, ANO_2023, ANO_2024, ANO_2025, RH, PAVTO, IDmenor10, ID11a20, ID21a30, ID31a40, FIN, LUX, APRIV] | |
| title = 'MOD_V_SALA_Z1_002E (Salas Comerciais)' | |
| description = f""" | |
| # <p style="text-align: center;">MOD_V_SALA_Z1_002E</p> | |
| <p style="text-align: center;">949 dados de 2019 a 2025</p> | |
| <hr style="color: #333; background-color: #333; height: 1px; border: none;"> | |
| <p style="text-align: center;"> <b>RH</b> | Menor: 120 - Maior: 380 </p> | |
| <p style="text-align: center;"> <b>Área Privativa</b> | Menor: 15.86 - Maior: 381.84 </p> | |
| <p style="text-align: center;"> <b>Pavimento</b> | Menor: 1 - Maior: 24 </p> | |
| <p style="text-align: center;"> <b>Idade</b> | Menor: 0 - Maior: 73 </p> | |
| <hr style="color: #333; background-color: #333; height: 1px; border: none;"> | |
| <p style="text-align: center;"> <b>BAIRROS:</b> Auxiliadora, Bomfim, Centro Histórico, Cidade Baixa, Floresta, Higienópolis, Independência, Menino Deus, Moinhos de Vento, Mont Serrat, Petrópolis, Praia de Belas, Rio Branco e São João.</p> | |
| <hr style="color: #333; background-color: #333; height: 1px; border: none;"> | |
| <p style="text-align: center;"> <b>Observações:</b> </p> | |
| <p style="text-align: center;"> <b>1)</b> Para o Padrão Médio, deixar Padrão Fino e Luxo desmarcados. </p> | |
| """ | |