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('somecoli.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)) | |