Spaces:
Running
Running
from fastapi import FastAPI, HTTPException | |
from keras.models import model_from_json | |
from pydantic import BaseModel | |
import numpy as np | |
# Definici贸n del modelo de datos de entrada | |
class InputData(BaseModel): | |
data: list # Asumiendo que la entrada es una lista de caracter铆sticas num茅ricas | |
app = FastAPI() | |
# Carga del modelo | |
def load_model(): | |
json_file = open("model.json", 'r') | |
loaded_model_json = json_file.read() | |
json_file.close() | |
loaded_model = model_from_json(loaded_model_json) | |
loaded_model.load_weights("model.h5") | |
print("Cargado el modelo en el disco") | |
loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy']) | |
return loaded_model | |
model = load_model() | |
async def predict(data: InputData): | |
try: | |
# Convertir la lista de entrada a un array de NumPy para la predicci贸n | |
input_data = np.array(data.data).reshape(1, -1) # Asumiendo que la entrada debe ser de forma (1, num_features) | |
prediction = model.predict(input_data).round() | |
return {"prediction": prediction.tolist()} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) |