File size: 605 Bytes
dccd1a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from fastapi import FastAPI
from pydantic import BaseModel
import numpy as np
from tensorflow.keras.models import load_model

app = FastAPI()
model = load_model("titanic_model.h5")

class InputData(BaseModel):
    pclass: int
    sex: str
    age: float
    fare: float

@app.post("/predict")
async def predict(data: InputData):
    sex_num = 1 if data.sex.lower() == "male" else 0
    input_array = np.array([[data.pclass, sex_num, data.age, data.fare]])
    prediction = model.predict(input_array)[0][0]
    result = "Sobrevivió" if prediction > 0.5 else "No sobrevivió"
    return {"data": [result]}