Spaces:
Sleeping
Sleeping
CD: update from GitHub main
Browse files- App/app_gradio.py +94 -0
App/app_gradio.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from models import ClientFeatures
|
| 5 |
+
import sys, os
|
| 6 |
+
|
| 7 |
+
# ROOT = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
+
# sys.path.append(ROOT)
|
| 9 |
+
|
| 10 |
+
# Charger le pipeline
|
| 11 |
+
pipe = joblib.load("./BestModel/pipeline_complet.joblib")
|
| 12 |
+
|
| 13 |
+
# Charger le seuil optimal
|
| 14 |
+
threshold = joblib.load("./BestModel/best_threshold.joblib")
|
| 15 |
+
|
| 16 |
+
# Récupérer l'exemple généré automatiquement dans models.py
|
| 17 |
+
example = ClientFeatures.model_config["json_schema_extra"]["example"]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def predict_gradio(*args):
|
| 21 |
+
try:
|
| 22 |
+
# Reconstruction automatique
|
| 23 |
+
data_dict = {name: value for (name, value) in zip(ClientFeatures.model_fields.keys(), args)}
|
| 24 |
+
df = pd.DataFrame([data_dict])
|
| 25 |
+
|
| 26 |
+
# Conversion robuste : transforme tout ce qui peut être un nombre
|
| 27 |
+
df = df.apply(lambda col: pd.to_numeric(col, errors="coerce"))
|
| 28 |
+
|
| 29 |
+
# Validation métier
|
| 30 |
+
if "DAYS_BIRTH" in df.columns:
|
| 31 |
+
raw_age = df["DAYS_BIRTH"].iloc[0]
|
| 32 |
+
|
| 33 |
+
# Forcer la conversion en float de manière robuste
|
| 34 |
+
try:
|
| 35 |
+
age = float(raw_age)
|
| 36 |
+
except (TypeError, ValueError):
|
| 37 |
+
age = None
|
| 38 |
+
|
| 39 |
+
# Si on a bien un nombre, on applique la règle métier
|
| 40 |
+
if age is not None and (age >= 0 or age > -18 * 365):
|
| 41 |
+
return "Âge invalide", None, None
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
if "AMT_INCOME_TOTAL" in df.columns:
|
| 45 |
+
income = df["AMT_INCOME_TOTAL"].iloc[0]
|
| 46 |
+
if pd.notna(income) and income <= 0:
|
| 47 |
+
return "Revenu invalide", None, None
|
| 48 |
+
|
| 49 |
+
# Prédiction
|
| 50 |
+
score = pipe.predict_proba(df)[0][1]
|
| 51 |
+
decision = "ACCORDÉ" if score < threshold else "REFUSÉ"
|
| 52 |
+
|
| 53 |
+
return decision, float(score), float(threshold)
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"ERREUR : {str(e)}", None, None
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Génération automatique des inputs Gradio AVEC VALEURS PAR DÉFAUT
|
| 61 |
+
inputs = []
|
| 62 |
+
for name, field in ClientFeatures.model_fields.items():
|
| 63 |
+
|
| 64 |
+
default_value = example.get(name, None)
|
| 65 |
+
|
| 66 |
+
if field.annotation == float:
|
| 67 |
+
inputs.append(gr.Number(label=name, value=default_value))
|
| 68 |
+
elif field.annotation == int:
|
| 69 |
+
inputs.append(gr.Number(label=name, value=default_value))
|
| 70 |
+
elif field.annotation == bool:
|
| 71 |
+
inputs.append(gr.Checkbox(label=name, value=default_value))
|
| 72 |
+
elif field.annotation == str:
|
| 73 |
+
inputs.append(gr.Textbox(label=name, value=default_value))
|
| 74 |
+
else:
|
| 75 |
+
inputs.append(gr.Textbox(label=name, value=default_value))
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
# Sorties Gradio
|
| 79 |
+
outputs = [
|
| 80 |
+
gr.Textbox(label="Décision"),
|
| 81 |
+
gr.Number(label="Score"),
|
| 82 |
+
gr.Number(label="Seuil Optimal"),
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
# Interface Gradio
|
| 86 |
+
app = gr.Interface(
|
| 87 |
+
fn=predict_gradio,
|
| 88 |
+
inputs=inputs,
|
| 89 |
+
outputs=outputs,
|
| 90 |
+
title="Scoring Crédit - Interface Gradio",
|
| 91 |
+
description="Interface interactive pour tester le modèle de scoring crédit."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
app.launch()
|