Spaces:
Sleeping
Sleeping
Update app.py
#5
by
JairoDanielMT
- opened
app.py
CHANGED
@@ -1,40 +1,51 @@
|
|
|
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
-
from keras.models import model_from_json
|
3 |
from pydantic import BaseModel
|
4 |
import numpy as np
|
5 |
from typing import List
|
6 |
|
|
|
7 |
class InputData(BaseModel):
|
8 |
data: List[float] # Lista de características numéricas (flotantes)
|
9 |
-
|
|
|
10 |
app = FastAPI()
|
11 |
-
model = None # Inicializa el modelo como None
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Ruta de predicción
|
28 |
@app.post("/predict/")
|
29 |
async def predict(data: InputData):
|
|
|
30 |
global model
|
31 |
-
if model is None:
|
32 |
-
model = load_model()
|
33 |
-
if model is None:
|
34 |
-
raise HTTPException(status_code=500, detail="Model could not be loaded")
|
35 |
try:
|
36 |
# Convertir la lista de entrada a un array de NumPy para la predicción
|
37 |
-
input_data = np.array(data.data).reshape(
|
|
|
|
|
38 |
prediction = model.predict(input_data).round()
|
39 |
return {"prediction": prediction.tolist()}
|
40 |
except Exception as e:
|
|
|
1 |
+
from keras.api.models import Sequential
|
2 |
+
from keras.api.layers import InputLayer, Dense
|
3 |
from fastapi import FastAPI, HTTPException
|
|
|
4 |
from pydantic import BaseModel
|
5 |
import numpy as np
|
6 |
from typing import List
|
7 |
|
8 |
+
|
9 |
class InputData(BaseModel):
|
10 |
data: List[float] # Lista de características numéricas (flotantes)
|
11 |
+
|
12 |
+
|
13 |
app = FastAPI()
|
|
|
14 |
|
15 |
+
|
16 |
+
# Función para construir el modelo manualmente
|
17 |
+
def build_model():
|
18 |
+
model = Sequential(
|
19 |
+
[
|
20 |
+
InputLayer(
|
21 |
+
input_shape=(2,), name="dense_2_input"
|
22 |
+
), # Ajusta el tamaño de entrada según tu modelo
|
23 |
+
Dense(16, activation="relu", name="dense_2"),
|
24 |
+
Dense(1, activation="sigmoid", name="dense_3"),
|
25 |
+
]
|
26 |
+
)
|
27 |
+
model.load_weights(
|
28 |
+
"model.h5"
|
29 |
+
) # Asegúrate de que los nombres de las capas coincidan para que los pesos se carguen correctamente
|
30 |
+
model.compile(
|
31 |
+
loss="mean_squared_error", optimizer="adam", metrics=["binary_accuracy"]
|
32 |
+
)
|
33 |
+
return model
|
34 |
+
|
35 |
+
|
36 |
+
model = build_model() # Construir el modelo al iniciar la aplicación
|
37 |
+
|
38 |
|
39 |
# Ruta de predicción
|
40 |
@app.post("/predict/")
|
41 |
async def predict(data: InputData):
|
42 |
+
print(f"Data: {data}")
|
43 |
global model
|
|
|
|
|
|
|
|
|
44 |
try:
|
45 |
# Convertir la lista de entrada a un array de NumPy para la predicción
|
46 |
+
input_data = np.array(data.data).reshape(
|
47 |
+
1, -1
|
48 |
+
) # Asumiendo que la entrada debe ser de forma (1, num_features)
|
49 |
prediction = model.predict(input_data).round()
|
50 |
return {"prediction": prediction.tolist()}
|
51 |
except Exception as e:
|