Spaces:
Sleeping
Sleeping
Update app.py
#1
by
JairoDanielMT
- opened
app.py
CHANGED
@@ -1,12 +1,34 @@
|
|
1 |
-
from
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
6 |
+
# Definición del modelo de datos de entrada
|
7 |
+
class InputData(BaseModel):
|
8 |
+
data: list # Asumiendo que la entrada es una lista de características numéricas
|
9 |
+
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
# Carga del modelo
|
13 |
+
def load_model():
|
14 |
+
json_file = open("model.json", 'r')
|
15 |
+
loaded_model_json = json_file.read()
|
16 |
+
json_file.close()
|
17 |
+
loaded_model = model_from_json(loaded_model_json)
|
18 |
+
loaded_model.load_weights("model.h5")
|
19 |
+
print("Cargado el modelo en el disco")
|
20 |
+
loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])
|
21 |
+
return loaded_model
|
22 |
+
|
23 |
+
model = load_model()
|
24 |
+
|
25 |
+
@app.post("/predict/")
|
26 |
+
async def predict(data: InputData):
|
27 |
+
try:
|
28 |
+
# Convertir la lista de entrada a un array de NumPy para la predicción
|
29 |
+
input_data = np.array(data.data).reshape(1, -1) # Asumiendo que la entrada debe ser de forma (1, num_features)
|
30 |
+
prediction = model.predict(input_data).round()
|
31 |
+
return {"prediction": prediction.tolist()}
|
32 |
+
except Exception as e:
|
33 |
+
raise HTTPException(status_code=500, detail=str(e))
|
34 |
+
|