File size: 1,003 Bytes
2ff7669
3585217
2ff7669
 
 
 
3585217
2ff7669
 
 
 
3585217
 
 
 
 
 
 
 
 
 
2ff7669
 
 
 
 
 
3585217
2ff7669
 
 
3585217
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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()

@app.post("/predict/")
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))