Spaces:
Running
Running
from fastapi import FastAPI, HTTPException | |
from tensorflow.keras.models import model_from_json | |
from pydantic import BaseModel | |
import numpy as np | |
class InputData(BaseModel): | |
data: list | |
app = FastAPI() | |
def load_model(): | |
try: | |
with open("model.json", 'r') as json_file: | |
loaded_model_json = json_file.read() | |
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 cargando el modelo: {str(e)}") | |
raise | |
model = load_model() | |
async def predict(data: InputData): | |
try: | |
input_data = np.array(data.data).reshape(1, -1) | |
prediction = model.predict(input_data).round() | |
return {"prediction": prediction.tolist()} | |
except Exception as e: | |
raise HTTPException(status_code=500, detail=str(e)) | |