Spaces:
Sleeping
Sleeping
Update app.py
Browse filesA inclusão do dicionário HEADER_INFO.
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import model_utils # Importa seu script de utilitários do modelo
|
| 4 |
import warnings
|
| 5 |
-
from typing import Tuple, Union # <---
|
| 6 |
|
| 7 |
# Suprimir avisos de scikit-learn
|
| 8 |
warnings.filterwarnings("ignore", category=UserWarning, module='sklearn')
|
|
@@ -10,6 +10,20 @@ warnings.filterwarnings("ignore", category=UserWarning, module='sklearn')
|
|
| 10 |
# --- Configurações ---
|
| 11 |
DATA_FILE = 'data.csv'
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# --- Instanciar e Treinar o Modelo na Inicialização do Space ---
|
| 14 |
model_pipeline = model_utils.ChurnModelPipeline()
|
| 15 |
df_raw = None
|
|
@@ -25,7 +39,6 @@ except Exception as e:
|
|
| 25 |
|
| 26 |
|
| 27 |
# --- Função de Previsão para a Interface Gradio ---
|
| 28 |
-
# Esta função agora retorna a string de previsão E o DataFrame de input
|
| 29 |
def predict_churn_interface(
|
| 30 |
creditscore: int,
|
| 31 |
geography: str,
|
|
@@ -83,16 +96,18 @@ def predict_churn_interface(
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"Ocorreu um erro inesperado: {e}", pd.DataFrame()
|
| 85 |
|
| 86 |
-
# Nova função para gerar o resumo e o
|
| 87 |
-
def
|
| 88 |
# Gerar o Markdown e os paths dos plots
|
| 89 |
-
summary_md,
|
| 90 |
-
model_pipeline.
|
| 91 |
|
| 92 |
-
# Gerar o
|
| 93 |
-
|
| 94 |
|
| 95 |
-
|
|
|
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
# --- Construir a Interface Gradio com Abas ---
|
|
@@ -152,11 +167,11 @@ with gr.Blocks(title="Detecção de Churn de Clientes Bancários") as demo:
|
|
| 152 |
confusion_matrix_plot_output = gr.Image(label="Matriz de Confusão", show_label=True, interactive=False, height=400)
|
| 153 |
roc_curve_plot_output = gr.Image(label="Curva ROC", show_label=True, interactive=False, height=400)
|
| 154 |
|
| 155 |
-
|
| 156 |
|
| 157 |
-
# Conectar o botão à função que gera o resumo em Markdown
|
| 158 |
summary_button.click(
|
| 159 |
-
fn=
|
| 160 |
inputs=[last_input_for_summary], # Passa o conteúdo do gr.State
|
| 161 |
outputs=[
|
| 162 |
model_summary_output,
|
|
@@ -164,7 +179,7 @@ with gr.Blocks(title="Detecção de Churn de Clientes Bancários") as demo:
|
|
| 164 |
odds_ratio_plot_output,
|
| 165 |
confusion_matrix_plot_output,
|
| 166 |
roc_curve_plot_output,
|
| 167 |
-
|
| 168 |
]
|
| 169 |
)
|
| 170 |
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import model_utils # Importa seu script de utilitários do modelo
|
| 4 |
import warnings
|
| 5 |
+
from typing import Tuple, Union, List # <--- List adicionado aqui
|
| 6 |
|
| 7 |
# Suprimir avisos de scikit-learn
|
| 8 |
warnings.filterwarnings("ignore", category=UserWarning, module='sklearn')
|
|
|
|
| 10 |
# --- Configurações ---
|
| 11 |
DATA_FILE = 'data.csv'
|
| 12 |
|
| 13 |
+
# --- Informações do Cabeçalho para o Relatório LaTeX (fornecidas pelo usuário) ---
|
| 14 |
+
HEADER_INFO = {
|
| 15 |
+
"universidade": "UNIVERSIDADE DE BRASÍLIA",
|
| 16 |
+
"departamento": "DEPARTAMENTO DE CIÊNCIA DA COMPUTAÇÃO",
|
| 17 |
+
"programa": "PROGRAMA DE PÓS-GRADUAÇÃO EM COMPUTAÇÃO APLICADA - PPCA",
|
| 18 |
+
"mestrado": "MESTRADO PROFISSIONAL",
|
| 19 |
+
"titulo_trabalho": "MODELAGEM PREDITIVA DE CHURN DE CLIENTES BANCÁRIOS UTILIZANDO REGRESSÃO LOGÍSTICA",
|
| 20 |
+
"identificacao_aedi": "AEDI",
|
| 21 |
+
"nome_aluno": "ÉDER MARCELO PONTES CUNHA",
|
| 22 |
+
"matricula_aluno": "Mat: 252106862",
|
| 23 |
+
"nome_professor": "Professor: Prof. João Gabriel de Moraes Souza.",
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
# --- Instanciar e Treinar o Modelo na Inicialização do Space ---
|
| 28 |
model_pipeline = model_utils.ChurnModelPipeline()
|
| 29 |
df_raw = None
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
# --- Função de Previsão para a Interface Gradio ---
|
|
|
|
| 42 |
def predict_churn_interface(
|
| 43 |
creditscore: int,
|
| 44 |
geography: str,
|
|
|
|
| 96 |
except Exception as e:
|
| 97 |
return f"Ocorreu um erro inesperado: {e}", pd.DataFrame()
|
| 98 |
|
| 99 |
+
# Nova função para gerar o resumo em Markdown e o arquivo LaTeX
|
| 100 |
+
def _generate_summary_and_latex_file(last_input_for_summary_state: pd.DataFrame) -> Tuple[str, Union[str, None], Union[str, None], Union[str, None], Union[str, None], Union[str, None]]:
|
| 101 |
# Gerar o Markdown e os paths dos plots
|
| 102 |
+
summary_md, latex_content_parts, plot_paths = \
|
| 103 |
+
model_pipeline._get_summary_latex_and_markdown_parts(last_input_for_summary_state)
|
| 104 |
|
| 105 |
+
# Gerar o arquivo LaTeX
|
| 106 |
+
latex_file_path = model_pipeline.generate_latex_report(latex_content_parts, HEADER_INFO, plot_paths)
|
| 107 |
|
| 108 |
+
# Retorna o markdown para exibição no Gradio, os paths dos plots e o path do arquivo LaTeX para download
|
| 109 |
+
return summary_md, plot_paths.get('heatmap'), plot_paths.get('odds_ratios'), \
|
| 110 |
+
plot_paths.get('confusion_matrix'), plot_paths.get('roc_curve'), latex_file_path
|
| 111 |
|
| 112 |
|
| 113 |
# --- Construir a Interface Gradio com Abas ---
|
|
|
|
| 167 |
confusion_matrix_plot_output = gr.Image(label="Matriz de Confusão", show_label=True, interactive=False, height=400)
|
| 168 |
roc_curve_plot_output = gr.Image(label="Curva ROC", show_label=True, interactive=False, height=400)
|
| 169 |
|
| 170 |
+
latex_download_output = gr.File(label="Download Relatório LaTeX (.tex)", file_count="single")
|
| 171 |
|
| 172 |
+
# Conectar o botão à função que gera o resumo em Markdown, paths das imagens e o path do arquivo LaTeX
|
| 173 |
summary_button.click(
|
| 174 |
+
fn=_generate_summary_and_latex_file,
|
| 175 |
inputs=[last_input_for_summary], # Passa o conteúdo do gr.State
|
| 176 |
outputs=[
|
| 177 |
model_summary_output,
|
|
|
|
| 179 |
odds_ratio_plot_output,
|
| 180 |
confusion_matrix_plot_output,
|
| 181 |
roc_curve_plot_output,
|
| 182 |
+
latex_download_output
|
| 183 |
]
|
| 184 |
)
|
| 185 |
|