Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- Prueba_1_con_RF.ipynb +221 -0
- app.py +58 -0
Prueba_1_con_RF.ipynb
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": null,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"colab": {
|
| 22 |
+
"base_uri": "https://localhost:8080/"
|
| 23 |
+
},
|
| 24 |
+
"id": "2zN17g8q2JSN",
|
| 25 |
+
"outputId": "1eed363e-9824-4f06-9f85-a9d5d65a2f3d"
|
| 26 |
+
},
|
| 27 |
+
"outputs": [
|
| 28 |
+
{
|
| 29 |
+
"output_type": "stream",
|
| 30 |
+
"name": "stdout",
|
| 31 |
+
"text": [
|
| 32 |
+
"Requirement already satisfied: gradio in /usr/local/lib/python3.12/dist-packages (5.44.1)\n",
|
| 33 |
+
"Requirement already satisfied: scikit-learn in /usr/local/lib/python3.12/dist-packages (1.6.1)\n",
|
| 34 |
+
"Requirement already satisfied: openpyxl in /usr/local/lib/python3.12/dist-packages (3.1.5)\n",
|
| 35 |
+
"Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (2.2.2)\n",
|
| 36 |
+
"Requirement already satisfied: aiofiles<25.0,>=22.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (24.1.0)\n",
|
| 37 |
+
"Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (4.10.0)\n",
|
| 38 |
+
"Requirement already satisfied: brotli>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (1.1.0)\n",
|
| 39 |
+
"Requirement already satisfied: fastapi<1.0,>=0.115.2 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.116.1)\n",
|
| 40 |
+
"Requirement already satisfied: ffmpy in /usr/local/lib/python3.12/dist-packages (from gradio) (0.6.1)\n",
|
| 41 |
+
"Requirement already satisfied: gradio-client==1.12.1 in /usr/local/lib/python3.12/dist-packages (from gradio) (1.12.1)\n",
|
| 42 |
+
"Requirement already satisfied: groovy~=0.1 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.1.2)\n",
|
| 43 |
+
"Requirement already satisfied: httpx<1.0,>=0.24.1 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.28.1)\n",
|
| 44 |
+
"Requirement already satisfied: huggingface-hub<1.0,>=0.33.5 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.34.4)\n",
|
| 45 |
+
"Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (3.1.6)\n",
|
| 46 |
+
"Requirement already satisfied: markupsafe<4.0,>=2.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (3.0.2)\n",
|
| 47 |
+
"Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (2.0.2)\n",
|
| 48 |
+
"Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (3.11.3)\n",
|
| 49 |
+
"Requirement already satisfied: packaging in /usr/local/lib/python3.12/dist-packages (from gradio) (25.0)\n",
|
| 50 |
+
"Requirement already satisfied: pillow<12.0,>=8.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (11.3.0)\n",
|
| 51 |
+
"Requirement already satisfied: pydantic<2.12,>=2.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (2.11.7)\n",
|
| 52 |
+
"Requirement already satisfied: pydub in /usr/local/lib/python3.12/dist-packages (from gradio) (0.25.1)\n",
|
| 53 |
+
"Requirement already satisfied: python-multipart>=0.0.18 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.0.20)\n",
|
| 54 |
+
"Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (6.0.2)\n",
|
| 55 |
+
"Requirement already satisfied: ruff>=0.9.3 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.12.12)\n",
|
| 56 |
+
"Requirement already satisfied: safehttpx<0.2.0,>=0.1.6 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.1.6)\n",
|
| 57 |
+
"Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (2.10.0)\n",
|
| 58 |
+
"Requirement already satisfied: starlette<1.0,>=0.40.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.47.3)\n",
|
| 59 |
+
"Requirement already satisfied: tomlkit<0.14.0,>=0.12.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.13.3)\n",
|
| 60 |
+
"Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.17.4)\n",
|
| 61 |
+
"Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (4.15.0)\n",
|
| 62 |
+
"Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.12/dist-packages (from gradio) (0.35.0)\n",
|
| 63 |
+
"Requirement already satisfied: fsspec in /usr/local/lib/python3.12/dist-packages (from gradio-client==1.12.1->gradio) (2025.3.0)\n",
|
| 64 |
+
"Requirement already satisfied: websockets<16.0,>=10.0 in /usr/local/lib/python3.12/dist-packages (from gradio-client==1.12.1->gradio) (15.0.1)\n",
|
| 65 |
+
"Requirement already satisfied: scipy>=1.6.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn) (1.16.1)\n",
|
| 66 |
+
"Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn) (1.5.2)\n",
|
| 67 |
+
"Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn) (3.6.0)\n",
|
| 68 |
+
"Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.12/dist-packages (from openpyxl) (2.0.0)\n",
|
| 69 |
+
"Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.12/dist-packages (from pandas) (2.9.0.post0)\n",
|
| 70 |
+
"Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.2)\n",
|
| 71 |
+
"Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas) (2025.2)\n",
|
| 72 |
+
"Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.12/dist-packages (from anyio<5.0,>=3.0->gradio) (3.10)\n",
|
| 73 |
+
"Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.12/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
|
| 74 |
+
"Requirement already satisfied: certifi in /usr/local/lib/python3.12/dist-packages (from httpx<1.0,>=0.24.1->gradio) (2025.8.3)\n",
|
| 75 |
+
"Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx<1.0,>=0.24.1->gradio) (1.0.9)\n",
|
| 76 |
+
"Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx<1.0,>=0.24.1->gradio) (0.16.0)\n",
|
| 77 |
+
"Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (3.19.1)\n",
|
| 78 |
+
"Requirement already satisfied: requests in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (2.32.4)\n",
|
| 79 |
+
"Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (4.67.1)\n",
|
| 80 |
+
"Requirement already satisfied: hf-xet<2.0.0,>=1.1.3 in /usr/local/lib/python3.12/dist-packages (from huggingface-hub<1.0,>=0.33.5->gradio) (1.1.9)\n",
|
| 81 |
+
"Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<2.12,>=2.0->gradio) (0.7.0)\n",
|
| 82 |
+
"Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.12/dist-packages (from pydantic<2.12,>=2.0->gradio) (2.33.2)\n",
|
| 83 |
+
"Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.12/dist-packages (from pydantic<2.12,>=2.0->gradio) (0.4.1)\n",
|
| 84 |
+
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil>=2.8.2->pandas) (1.17.0)\n",
|
| 85 |
+
"Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.12/dist-packages (from typer<1.0,>=0.12->gradio) (8.2.1)\n",
|
| 86 |
+
"Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.12/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
|
| 87 |
+
"Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.12/dist-packages (from typer<1.0,>=0.12->gradio) (13.9.4)\n",
|
| 88 |
+
"Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (4.0.0)\n",
|
| 89 |
+
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.12/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.19.2)\n",
|
| 90 |
+
"Requirement already satisfied: charset_normalizer<4,>=2 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub<1.0,>=0.33.5->gradio) (3.4.3)\n",
|
| 91 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.12/dist-packages (from requests->huggingface-hub<1.0,>=0.33.5->gradio) (2.5.0)\n",
|
| 92 |
+
"Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.12/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n"
|
| 93 |
+
]
|
| 94 |
+
}
|
| 95 |
+
],
|
| 96 |
+
"source": [
|
| 97 |
+
"# Instalar librer铆as necesarias\n",
|
| 98 |
+
"!pip install gradio scikit-learn openpyxl pandas\n"
|
| 99 |
+
]
|
| 100 |
+
},
|
| 101 |
+
{
|
| 102 |
+
"cell_type": "code",
|
| 103 |
+
"source": [
|
| 104 |
+
"import gradio as gr\n",
|
| 105 |
+
"import pandas as pd\n",
|
| 106 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 107 |
+
"from sklearn.ensemble import RandomForestRegressor\n",
|
| 108 |
+
"from sklearn.metrics import mean_squared_error"
|
| 109 |
+
],
|
| 110 |
+
"metadata": {
|
| 111 |
+
"id": "EI5LyrH62Oi9"
|
| 112 |
+
},
|
| 113 |
+
"execution_count": null,
|
| 114 |
+
"outputs": []
|
| 115 |
+
},
|
| 116 |
+
{
|
| 117 |
+
"cell_type": "code",
|
| 118 |
+
"source": [],
|
| 119 |
+
"metadata": {
|
| 120 |
+
"id": "fDxWOYB7f8Ei"
|
| 121 |
+
},
|
| 122 |
+
"execution_count": null,
|
| 123 |
+
"outputs": []
|
| 124 |
+
},
|
| 125 |
+
{
|
| 126 |
+
"cell_type": "code",
|
| 127 |
+
"source": [
|
| 128 |
+
"# 馃敼 Funci贸n que encapsula TODO tu flujo\n",
|
| 129 |
+
"def ejecutar_modelo():\n",
|
| 130 |
+
" # 1. Cargar datos (simulamos con dataset de sklearn o podr铆as leer desde tu Drive)\n",
|
| 131 |
+
" from sklearn.datasets import load_diabetes\n",
|
| 132 |
+
" data = load_diabetes(as_frame=True)\n",
|
| 133 |
+
" X, y = data.data, data.target\n",
|
| 134 |
+
"\n",
|
| 135 |
+
" # 2. Split train/test\n",
|
| 136 |
+
" X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
|
| 137 |
+
"\n",
|
| 138 |
+
" # 3. Entrenar modelo\n",
|
| 139 |
+
" model = RandomForestRegressor(n_estimators=100, random_state=42)\n",
|
| 140 |
+
" model.fit(X_train, y_train)\n",
|
| 141 |
+
"\n",
|
| 142 |
+
" # 4. Predicciones\n",
|
| 143 |
+
" y_pred = model.predict(X_test)\n",
|
| 144 |
+
"\n",
|
| 145 |
+
" # 5. Guardar resultados en Excel\n",
|
| 146 |
+
" df_resultados = pd.DataFrame({\n",
|
| 147 |
+
" \"Real\": y_test.values,\n",
|
| 148 |
+
" \"Predicho\": y_pred\n",
|
| 149 |
+
" })\n",
|
| 150 |
+
" output_path = \"resultados.xlsx\"\n",
|
| 151 |
+
" df_resultados.to_excel(output_path, index=False)\n",
|
| 152 |
+
"\n",
|
| 153 |
+
" return output_path\n"
|
| 154 |
+
],
|
| 155 |
+
"metadata": {
|
| 156 |
+
"id": "m9nt4GOL2Kln"
|
| 157 |
+
},
|
| 158 |
+
"execution_count": null,
|
| 159 |
+
"outputs": []
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"cell_type": "code",
|
| 163 |
+
"source": [
|
| 164 |
+
"# Interfaz: un bot贸n para correr el modelo y un bot贸n para descargar Excel\n",
|
| 165 |
+
"with gr.Blocks() as demo:\n",
|
| 166 |
+
" gr.Markdown(\"## Random Forest Predictor 馃搳\")\n",
|
| 167 |
+
" gr.Markdown(\"Haz clic en el bot贸n para entrenar el modelo y obtener resultados\")\n",
|
| 168 |
+
"\n",
|
| 169 |
+
" boton = gr.Button(\"Ejecutar modelo\")\n",
|
| 170 |
+
" salida = gr.File(label=\"Descargar Excel de resultados\")\n",
|
| 171 |
+
"\n",
|
| 172 |
+
" boton.click(fn=ejecutar_modelo, inputs=None, outputs=salida)\n",
|
| 173 |
+
"\n",
|
| 174 |
+
"demo.launch()\n"
|
| 175 |
+
],
|
| 176 |
+
"metadata": {
|
| 177 |
+
"colab": {
|
| 178 |
+
"base_uri": "https://localhost:8080/",
|
| 179 |
+
"height": 646
|
| 180 |
+
},
|
| 181 |
+
"id": "R9PNIOdu2zTd",
|
| 182 |
+
"outputId": "fb32c8a1-74dd-4607-b5eb-546b13a86d18"
|
| 183 |
+
},
|
| 184 |
+
"execution_count": null,
|
| 185 |
+
"outputs": [
|
| 186 |
+
{
|
| 187 |
+
"output_type": "stream",
|
| 188 |
+
"name": "stdout",
|
| 189 |
+
"text": [
|
| 190 |
+
"It looks like you are running Gradio on a hosted Jupyter notebook, which requires `share=True`. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
|
| 191 |
+
"\n",
|
| 192 |
+
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
|
| 193 |
+
"* Running on public URL: https://8ee144fa096d18fed6.gradio.live\n",
|
| 194 |
+
"\n",
|
| 195 |
+
"This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
| 196 |
+
]
|
| 197 |
+
},
|
| 198 |
+
{
|
| 199 |
+
"output_type": "display_data",
|
| 200 |
+
"data": {
|
| 201 |
+
"text/plain": [
|
| 202 |
+
"<IPython.core.display.HTML object>"
|
| 203 |
+
],
|
| 204 |
+
"text/html": [
|
| 205 |
+
"<div><iframe src=\"https://8ee144fa096d18fed6.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 206 |
+
]
|
| 207 |
+
},
|
| 208 |
+
"metadata": {}
|
| 209 |
+
},
|
| 210 |
+
{
|
| 211 |
+
"output_type": "execute_result",
|
| 212 |
+
"data": {
|
| 213 |
+
"text/plain": []
|
| 214 |
+
},
|
| 215 |
+
"metadata": {},
|
| 216 |
+
"execution_count": 4
|
| 217 |
+
}
|
| 218 |
+
]
|
| 219 |
+
}
|
| 220 |
+
]
|
| 221 |
+
}
|
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""Prueba 1 con RF.ipynb
|
| 3 |
+
|
| 4 |
+
Automatically generated by Colab.
|
| 5 |
+
|
| 6 |
+
Original file is located at
|
| 7 |
+
https://colab.research.google.com/drive/1uLC6Z9l_iiLNQSpLQ8srEj6ziSJ6sPxs
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
# Instalar librer铆as necesarias
|
| 11 |
+
!pip install gradio scikit-learn openpyxl pandas
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
import pandas as pd
|
| 15 |
+
from sklearn.model_selection import train_test_split
|
| 16 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 17 |
+
from sklearn.metrics import mean_squared_error
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# 馃敼 Funci贸n que encapsula TODO tu flujo
|
| 22 |
+
def ejecutar_modelo():
|
| 23 |
+
# 1. Cargar datos (simulamos con dataset de sklearn o podr铆as leer desde tu Drive)
|
| 24 |
+
from sklearn.datasets import load_diabetes
|
| 25 |
+
data = load_diabetes(as_frame=True)
|
| 26 |
+
X, y = data.data, data.target
|
| 27 |
+
|
| 28 |
+
# 2. Split train/test
|
| 29 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 30 |
+
|
| 31 |
+
# 3. Entrenar modelo
|
| 32 |
+
model = RandomForestRegressor(n_estimators=100, random_state=42)
|
| 33 |
+
model.fit(X_train, y_train)
|
| 34 |
+
|
| 35 |
+
# 4. Predicciones
|
| 36 |
+
y_pred = model.predict(X_test)
|
| 37 |
+
|
| 38 |
+
# 5. Guardar resultados en Excel
|
| 39 |
+
df_resultados = pd.DataFrame({
|
| 40 |
+
"Real": y_test.values,
|
| 41 |
+
"Predicho": y_pred
|
| 42 |
+
})
|
| 43 |
+
output_path = "resultados.xlsx"
|
| 44 |
+
df_resultados.to_excel(output_path, index=False)
|
| 45 |
+
|
| 46 |
+
return output_path
|
| 47 |
+
|
| 48 |
+
# Interfaz: un bot贸n para correr el modelo y un bot贸n para descargar Excel
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("## Random Forest Predictor 馃搳")
|
| 51 |
+
gr.Markdown("Haz clic en el bot贸n para entrenar el modelo y obtener resultados")
|
| 52 |
+
|
| 53 |
+
boton = gr.Button("Ejecutar modelo")
|
| 54 |
+
salida = gr.File(label="Descargar Excel de resultados")
|
| 55 |
+
|
| 56 |
+
boton.click(fn=ejecutar_modelo, inputs=None, outputs=salida)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|