Spaces:
Sleeping
Sleeping
| import pickle | |
| from minisom import MiniSom | |
| import numpy as np | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import List | |
| class InputData(BaseModel): | |
| data: List[float] # Lista de características numéricas (flotantes) | |
| app = FastAPI() | |
| # Función para construir el modelo manualmente | |
| def build_model(): | |
| with open('somlucuma.pkl', 'rb') as fid: | |
| somecoli = pickle.load(fid) | |
| MM = np.loadtxt('matrizMM.txt', delimiter=" ") | |
| return somecoli,MM | |
| som,MM = build_model() # Construir el modelo al iniciar la aplicación | |
| # Ruta de predicción | |
| async def predict(data: InputData): | |
| print(f"Data: {data}") | |
| global som | |
| global MM | |
| 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) | |
| #input_data = [float(f) for f in input_data] | |
| w = som.winner(input_data) | |
| prediction = MM[w] | |
| return {"prediction": prediction.tolist()} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |