ArbolDecision / app.py
Maicol2001's picture
Upload 3 files
38df1da verified
import numpy as np
np.random.seed(0)
import pickle
from sklearn.compose import ColumnTransformer
from sklearn.datasets import fetch_openml
from sklearn.pipeline import Pipeline
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import StandardScaler, OneHotEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn import tree
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
from typing import List
class InputData(BaseModel):
data: List[float]
# Inicializar la aplicaci贸n FastAPI
app = FastAPI()
def build_model():
with open('miarbol.pkl', 'rb') as fid:
miarbol = pickle.load(fid)
return miarbol
miarbol = build_model()
# Ruta de predicci贸n
@app.post("/predict/")
async def predict(data: InputData):
print(f"Data: {data}")
global miarbol
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 = miarbol.predict(input_data).round()
return {"prediction": prediction.tolist()}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))