Spaces:
Sleeping
Sleeping
Si2469
commited on
Commit
•
ef0a000
1
Parent(s):
6bdc094
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,80 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
-
def
|
7 |
-
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import tensorflow as tf
|
6 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
7 |
+
from sklearn.compose import ColumnTransformer
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
+
# Cargar el modelo preentrenado desde el archivo .h5
|
12 |
+
model = tf.keras.models.load_model('Model.h5')
|
13 |
+
|
14 |
+
# Cargar el conjunto de datos para ajuste del preprocesador
|
15 |
+
file_path = 'heart.csv'
|
16 |
+
df = pd.read_csv(file_path)
|
17 |
+
|
18 |
+
# Identificar columnas numéricas y categóricas
|
19 |
+
num_features = ['Age', 'RestingBP', 'Cholesterol', 'MaxHR', 'Oldpeak']
|
20 |
+
cat_features = ['Sex', 'ChestPainType', 'FastingBS', 'RestingECG', 'ExerciseAngina', 'ST_Slope']
|
21 |
+
|
22 |
+
# Preprocesamiento: Escalar características numéricas y codificar características categóricas
|
23 |
+
preprocessor = ColumnTransformer(
|
24 |
+
transformers=[
|
25 |
+
('num', StandardScaler(), num_features),
|
26 |
+
('cat', OneHotEncoder(handle_unknown='ignore'), cat_features)
|
27 |
+
])
|
28 |
+
|
29 |
+
# Ajustar el preprocesador al conjunto de datos
|
30 |
+
X = df.drop('HeartDisease', axis=1)
|
31 |
+
preprocessor.fit(X)
|
32 |
+
|
33 |
+
class HeartDiseaseInput(BaseModel):
|
34 |
+
age: float
|
35 |
+
sex: str
|
36 |
+
chest_pain_type: str
|
37 |
+
resting_bp: float
|
38 |
+
cholesterol: float
|
39 |
+
fasting_bs: int
|
40 |
+
resting_ecg: str
|
41 |
+
max_hr: float
|
42 |
+
exercise_angina: str
|
43 |
+
oldpeak: float
|
44 |
+
st_slope: str
|
45 |
+
|
46 |
@app.get("/")
|
47 |
+
def read_root():
|
48 |
+
return {"Hello": "World!"}
|
49 |
+
|
50 |
+
@app.post("/predict")
|
51 |
+
def predict(input: HeartDiseaseInput):
|
52 |
+
try:
|
53 |
+
# Crear un DataFrame con los datos de entrada
|
54 |
+
input_data = pd.DataFrame({
|
55 |
+
'Age': [input.age],
|
56 |
+
'Sex': [input.sex],
|
57 |
+
'ChestPainType': [input.chest_pain_type],
|
58 |
+
'RestingBP': [input.resting_bp],
|
59 |
+
'Cholesterol': [input.cholesterol],
|
60 |
+
'FastingBS': [input.fasting_bs],
|
61 |
+
'RestingECG': [input.resting_ecg],
|
62 |
+
'MaxHR': [input.max_hr],
|
63 |
+
'ExerciseAngina': [input.exercise_angina],
|
64 |
+
'Oldpeak': [input.oldpeak],
|
65 |
+
'ST_Slope': [input.st_slope]
|
66 |
+
})
|
67 |
+
|
68 |
+
# Aplicar preprocesamiento
|
69 |
+
input_data_preprocessed = preprocessor.transform(input_data)
|
70 |
+
|
71 |
+
# Realizar la predicción
|
72 |
+
prediction_prob = model.predict(input_data_preprocessed)
|
73 |
+
predicted_class = (prediction_prob > 0.3).astype(int)
|
74 |
+
|
75 |
+
# Interpretar la predicción
|
76 |
+
result = "Tiene enfermedad cardíaca." if predicted_class == 1 else "No tiene enfermedad cardíaca."
|
77 |
+
|
78 |
+
return {"prediction": result, "probability": prediction_prob[0][0]}
|
79 |
+
except Exception as e:
|
80 |
+
raise HTTPException(status_code=400, detail=str(e))
|