File size: 10,210 Bytes
4001464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89a5462
 
 
 
 
 
 
 
 
 
 
 
4001464
 
 
 
 
 
 
 
 
9d42348
8485eba
4001464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""
Iris Flower Classifier - Hugging Face Space
=============================================
Descarga el modelo XGBoost desde Kaggle y sirve una interfaz Gradio.
Modelo: gustavodelacruztovar/iris-xgboost-feature-engineered
Dataset: gustavodelacruztovar/iris-flower-feature-engineered
"""

import gradio as gr
import joblib
import numpy as np
import pandas as pd
import json
import matplotlib.pyplot as plt
import seaborn as sns
import os
import zipfile
import urllib.request
import tempfile

# ============================================================
# DESCARGA DE ARTEFACTOS DESDE KAGGLE
# ============================================================
MODEL_DIR = "model_artifacts"
DATA_DIR = "data_artifacts"

KAGGLE_MODEL = "gustavodelacruztovar/iris-xgboost-feature-engineered/Other/default/1"
KAGGLE_DATASET = "gustavodelacruztovar/iris-flower-feature-engineered"


def download_from_kaggle():
    """Descarga modelo y dataset desde Kaggle usando la API."""
    # Configurar credenciales desde variables de entorno (HF Secrets)
    kaggle_dir = os.path.expanduser("~/.kaggle")
    kaggle_json = os.path.join(kaggle_dir, "kaggle.json")
    if not os.path.exists(kaggle_json):
        username = os.environ.get("KAGGLE_USERNAME", "")
        key = os.environ.get("KAGGLE_KEY", "")
        if username and key:
            os.makedirs(kaggle_dir, exist_ok=True)
            with open(kaggle_json, "w") as f:
                json.dump({"username": username, "key": key}, f)
            os.chmod(kaggle_json, 0o600)

    from kaggle.api.kaggle_api_extended import KaggleApi

    api = KaggleApi()
    api.authenticate()

    # Descargar modelo
    if not os.path.exists(os.path.join(MODEL_DIR, "model.joblib")):
        print("Descargando modelo desde Kaggle...")
        os.makedirs(MODEL_DIR, exist_ok=True)
        api.model_instance_version_download(
            "gustavodelacruztovar/iris-xgboost-feature-engineered/Other/default/1",
            path=MODEL_DIR,
            untar=True,
        )
        # Descomprimir si viene en zip
        for f in os.listdir(MODEL_DIR):
            if f.endswith(".zip"):
                with zipfile.ZipFile(os.path.join(MODEL_DIR, f), "r") as z:
                    z.extractall(MODEL_DIR)
                os.remove(os.path.join(MODEL_DIR, f))
        print("✓ Modelo descargado")

    # Descargar dataset
    if not os.path.exists(os.path.join(DATA_DIR, "iris_engineered.csv")):
        print("Descargando dataset desde Kaggle...")
        os.makedirs(DATA_DIR, exist_ok=True)
        api.dataset_download_files(KAGGLE_DATASET, path=DATA_DIR, unzip=True)
        print("✓ Dataset descargado")


download_from_kaggle()

# ============================================================
# CARGAR ARTEFACTOS
# ============================================================
model = joblib.load(os.path.join(MODEL_DIR, "model.joblib"))
le = joblib.load(os.path.join(MODEL_DIR, "label_encoder.joblib"))

with open(os.path.join(MODEL_DIR, "model_info.json")) as f:
    model_info = json.load(f)

df = pd.read_csv(os.path.join(DATA_DIR, "iris_engineered.csv"))

# ============================================================
# FUNCIONES
# ============================================================
FEATURE_RANGES = {
    "sepal_length": (4.0, 8.0, 5.8),
    "sepal_width": (2.0, 4.5, 3.0),
    "petal_length": (1.0, 7.0, 3.8),
    "petal_width": (0.1, 2.5, 1.2),
}

SPECIES_EMOJI = {
    "Iris-setosa": "🌸",
    "Iris-versicolor": "🌺",
    "Iris-virginica": "🌻",
}

ENGINEERED_FEATURES = model_info.get("features", [])
ORIGINAL_FEATURES = ["sepal_length", "sepal_width", "petal_length", "petal_width"]


def engineer_features(sepal_length, sepal_width, petal_length, petal_width):
    """Aplica el mismo feature engineering del entrenamiento."""
    row = {
        "sepal_length": sepal_length,
        "sepal_width": sepal_width,
        "petal_length": petal_length,
        "petal_width": petal_width,
        "sepal_ratio": sepal_length / sepal_width,
        "petal_ratio": petal_length / petal_width,
        "sepal_petal_length_ratio": sepal_length / petal_length,
        "sepal_petal_width_ratio": sepal_width / petal_width,
        "sepal_area": sepal_length * sepal_width,
        "petal_area": petal_length * petal_width,
        "area_ratio": (sepal_length * sepal_width) / (petal_length * petal_width),
        "length_diff": sepal_length - petal_length,
        "width_diff": sepal_width - petal_width,
        "log_petal_area": np.log1p(petal_length * petal_width),
        "log_sepal_area": np.log1p(sepal_length * sepal_width),
        "sepal_perimeter": 2 * (sepal_length + sepal_width),
        "petal_perimeter": 2 * (petal_length + petal_width),
    }
    return np.array([[row[f] for f in ENGINEERED_FEATURES]])


def predict(sepal_length, sepal_width, petal_length, petal_width):
    """Predecir especie de Iris con features engineered."""
    features = engineer_features(sepal_length, sepal_width, petal_length, petal_width)
    proba = model.predict_proba(features)[0]
    return {
        f"{SPECIES_EMOJI.get(cls, '')} {cls}": float(p)
        for cls, p in zip(le.classes_, proba)
    }


def create_eda_plot(column, plot_type):
    """Generar gráfico EDA interactivo."""
    fig, ax = plt.subplots(figsize=(10, 6))
    if plot_type == "Histograma":
        for species in df["species"].unique():
            subset = df[df["species"] == species]
            ax.hist(subset[column], alpha=0.6, label=species, bins=15)
        ax.legend()
    elif plot_type == "Boxplot":
        sns.boxplot(x="species", y=column, data=df, ax=ax)
    elif plot_type == "Violin":
        sns.violinplot(x="species", y=column, data=df, ax=ax)
    elif plot_type == "Scatter (vs petal_length)":
        for species in df["species"].unique():
            subset = df[df["species"] == species]
            ax.scatter(subset[column], subset["petal_length"], alpha=0.7, label=species)
        ax.set_ylabel("petal_length")
        ax.legend()
    ax.set_title(f"{plot_type} de {column}")
    ax.set_xlabel(column)
    plt.tight_layout()
    return fig


def show_correlation():
    """Mostrar matriz de correlación de features originales."""
    fig, ax = plt.subplots(figsize=(8, 6))
    corr = df[ORIGINAL_FEATURES].corr()
    mask = np.triu(np.ones_like(corr, dtype=bool))
    sns.heatmap(corr, mask=mask, annot=True, fmt=".2f", cmap="coolwarm", center=0, ax=ax)
    ax.set_title("Matriz de Correlación")
    plt.tight_layout()
    return fig


def show_pairplot():
    """Generar pairplot."""
    fig = sns.pairplot(df[ORIGINAL_FEATURES + ["species"]], hue="species", diag_kind="kde", height=2.2)
    return fig.figure


# ============================================================
# INTERFAZ GRADIO
# ============================================================
with gr.Blocks(title="Iris Flower Classifier") as demo:
    gr.Markdown(
        """
        # 🌺 Iris Flower Classifier
        Clasificador de flores Iris usando **XGBoost** con **17 features engineered**.

        Modelo descargado desde [Kaggle Models](https://www.kaggle.com/models/gustavodelacruztovar/iris-xgboost-feature-engineered)
        | Dataset desde [Kaggle Datasets](https://www.kaggle.com/datasets/gustavodelacruztovar/iris-flower-feature-engineered)
        """
    )

    with gr.Tab("🔮 Predicción"):
        with gr.Row():
            with gr.Column():
                sl = gr.Slider(*FEATURE_RANGES["sepal_length"], label="Sepal Length (cm)")
                sw = gr.Slider(*FEATURE_RANGES["sepal_width"], label="Sepal Width (cm)")
                pl = gr.Slider(*FEATURE_RANGES["petal_length"], label="Petal Length (cm)")
                pw = gr.Slider(*FEATURE_RANGES["petal_width"], label="Petal Width (cm)")
                predict_btn = gr.Button("Clasificar", variant="primary")
            with gr.Column():
                output_label = gr.Label(num_top_classes=3, label="Predicción")

        predict_btn.click(predict, inputs=[sl, sw, pl, pw], outputs=output_label)

        gr.Examples(
            examples=[
                [5.1, 3.5, 1.4, 0.2],   # Setosa
                [6.2, 2.9, 4.3, 1.3],   # Versicolor
                [7.7, 3.0, 6.1, 2.3],   # Virginica
            ],
            inputs=[sl, sw, pl, pw],
            label="Ejemplos por especie",
        )

    with gr.Tab("📊 EDA Interactivo"):
        with gr.Row():
            col_selector = gr.Dropdown(
                choices=ORIGINAL_FEATURES, value="petal_length", label="Feature"
            )
            plot_type = gr.Dropdown(
                choices=["Histograma", "Boxplot", "Violin", "Scatter (vs petal_length)"],
                value="Histograma",
                label="Tipo de gráfico",
            )
        eda_plot = gr.Plot(label="Visualización")
        col_selector.change(create_eda_plot, [col_selector, plot_type], eda_plot)
        plot_type.change(create_eda_plot, [col_selector, plot_type], eda_plot)

        with gr.Row():
            corr_btn = gr.Button("Matriz de Correlación")
            pair_btn = gr.Button("Pairplot")
        extra_plot = gr.Plot(label="Análisis")
        corr_btn.click(show_correlation, outputs=extra_plot)
        pair_btn.click(show_pairplot, outputs=extra_plot)

    with gr.Tab("📋 Datos"):
        gr.Markdown("### Dataset Iris con Feature Engineering (150 muestras × 18 columnas)")
        gr.DataFrame(value=df.head(50), label="Primeras 50 filas")
        gr.DataFrame(value=df.describe().reset_index(), label="Estadísticas")

    with gr.Tab("📈 Métricas del Modelo"):
        gr.Markdown(f"""
        ### Rendimiento del modelo XGBoost (17 features engineered)
        - **Test Accuracy**: {model_info['metrics']['test_accuracy']:.4f}
        - **CV Accuracy**: {model_info['metrics']['cv_accuracy_mean']:.4f} ± {model_info['metrics']['cv_accuracy_std']:.4f}
        - **Clases**: {', '.join(model_info['classes'])}
        - **Features**: {len(ENGINEERED_FEATURES)} ({len(ORIGINAL_FEATURES)} originales + {len(ENGINEERED_FEATURES) - len(ORIGINAL_FEATURES)} engineered)
        """)
        gr.JSON(value=model_info, label="Metadata del modelo")


if __name__ == "__main__":
    demo.launch()