Spaces:
Sleeping
Sleeping
LArzuaga commited on
Commit 路
e27cc0f
1
Parent(s): 3aab8fc
Update
Browse files- Dockerfile +12 -0
- main.py +98 -0
- mejor_modelo_score_febrero_25.pkl +3 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY . .
|
| 9 |
+
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
|
| 12 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile, Response
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pickle
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Cargar modelo y datos est谩ticos al iniciar la aplicaci贸n
|
| 11 |
+
model1 = None
|
| 12 |
+
df1 = None
|
| 13 |
+
|
| 14 |
+
@app.on_event("startup")
|
| 15 |
+
def load_assets():
|
| 16 |
+
global model1, df1
|
| 17 |
+
with open('mejor_modelo_score_febrero_25.pkl', 'rb') as f:
|
| 18 |
+
model1 = pickle.load(f)
|
| 19 |
+
df1 = pd.read_excel('df1.xlsx')
|
| 20 |
+
|
| 21 |
+
@app.get("/", response_class=HTMLResponse)
|
| 22 |
+
def read_root():
|
| 23 |
+
return """
|
| 24 |
+
<html>
|
| 25 |
+
<head>
|
| 26 |
+
<title>API de Predicci贸n</title>
|
| 27 |
+
<style>
|
| 28 |
+
body {
|
| 29 |
+
font-family: Arial, sans-serif;
|
| 30 |
+
margin: 40px;
|
| 31 |
+
background-color: #f0f2f5;
|
| 32 |
+
}
|
| 33 |
+
.container {
|
| 34 |
+
max-width: 800px;
|
| 35 |
+
margin: 0 auto;
|
| 36 |
+
padding: 20px;
|
| 37 |
+
background-color: white;
|
| 38 |
+
border-radius: 8px;
|
| 39 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 40 |
+
}
|
| 41 |
+
h1 {
|
| 42 |
+
color: #1a73e8;
|
| 43 |
+
border-bottom: 2px solid #1a73e8;
|
| 44 |
+
padding-bottom: 10px;
|
| 45 |
+
}
|
| 46 |
+
p {
|
| 47 |
+
line-height: 1.6;
|
| 48 |
+
color: #333;
|
| 49 |
+
}
|
| 50 |
+
</style>
|
| 51 |
+
</head>
|
| 52 |
+
<body>
|
| 53 |
+
<div class="container">
|
| 54 |
+
<h1>API Funcional 馃殌</h1>
|
| 55 |
+
<p>Bienvenido al sistema de predicci贸n de riesgo.</p>
|
| 56 |
+
<p>Usa el endpoint <code>/model_predict</code> via POST con un archivo XLSX para obtener predicciones.</p>
|
| 57 |
+
<p>Estado del servicio: <span style="color: green; font-weight: bold;">Operativo</span></p>
|
| 58 |
+
</div>
|
| 59 |
+
</body>
|
| 60 |
+
</html>
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
@app.post("/model_predict")
|
| 64 |
+
async def model_predict(file: UploadFile = File(...)):
|
| 65 |
+
content = await file.read()
|
| 66 |
+
data = pd.read_excel(BytesIO(content))
|
| 67 |
+
|
| 68 |
+
predacierta = model1.predict_proba(data)
|
| 69 |
+
predacierta = [p[1] for p in predacierta]
|
| 70 |
+
prediccion_acierta = pd.Series(predacierta, name='prob_90')
|
| 71 |
+
|
| 72 |
+
prediccionrf = pd.concat([df1, prediccion_acierta], axis=1)
|
| 73 |
+
|
| 74 |
+
risk_class = []
|
| 75 |
+
for row in prediccionrf['prob_90']:
|
| 76 |
+
if row > 0.1000039:
|
| 77 |
+
risk_class.append('Extremo')
|
| 78 |
+
elif row > 0.0500117:
|
| 79 |
+
risk_class.append('Muy Alto')
|
| 80 |
+
elif row > 0.0200018:
|
| 81 |
+
risk_class.append('Alto')
|
| 82 |
+
elif row > 0.0060008:
|
| 83 |
+
risk_class.append('Moderado')
|
| 84 |
+
elif row > 0.0020001:
|
| 85 |
+
risk_class.append('Bajo')
|
| 86 |
+
else:
|
| 87 |
+
risk_class.append('Muy Bajo')
|
| 88 |
+
|
| 89 |
+
prediccionrf['risk_class'] = risk_class
|
| 90 |
+
prediccionrf['Score'] = 99.9 - prediccionrf['prob_90'] * 100
|
| 91 |
+
|
| 92 |
+
csv_data = prediccionrf.to_csv(index=False)
|
| 93 |
+
|
| 94 |
+
return Response(
|
| 95 |
+
content=csv_data,
|
| 96 |
+
media_type="text/csv",
|
| 97 |
+
headers={"Content-Disposition": "attachment; filename=predicciones.csv"}
|
| 98 |
+
)
|
mejor_modelo_score_febrero_25.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f6c00143b64821c61cd590a9bf44532f5ac449b7079cb21643a6d8740f3e9ed5
|
| 3 |
+
size 123313
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.68.0
|
| 2 |
+
pandas>=1.3.0
|
| 3 |
+
numpy==1.24.3
|
| 4 |
+
catboost
|
| 5 |
+
scikit-learn>=1.0.0
|
| 6 |
+
openpyxl>=3.0.0
|
| 7 |
+
python-multipart>=0.0.5
|
| 8 |
+
uvicorn>=0.15.0
|