Spaces:
Sleeping
Sleeping
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() | |
model = None # Inicializa el modelo como None | |
# Carga del modelo | |
def load_model(): | |
try: | |
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") | |
loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy']) | |
return loaded_model | |
except Exception as e: | |
print(f"Error al cargar el modelo: {e}") | |
return None | |
# Ruta de predicci贸n | |
async def predict(data: InputData): | |
global model | |
if model is None: | |
model = load_model() | |
if model is None: | |
raise HTTPException(status_code=500, detail="Model could not be loaded") | |
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)) | |