Download app.py from Junior16/semana13: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/Junior16/semana13/resolve/main/app.py
- Command line
-
hf download hf://spaces/Junior16/semana13/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Junior16/semana13/resolve/main/app.py
1.36 kB
| 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 | |
| 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)) |