Files changed (1) hide show
  1. app.py +14 -17
app.py CHANGED
@@ -1,35 +1,32 @@
1
  from fastapi import FastAPI, HTTPException
2
- from keras.models import model_from_json,Model
3
  from pydantic import BaseModel
4
- from keras.models import Sequential
5
- from keras.layers import Dense
6
  import numpy as np
7
 
8
- # Definición del modelo de datos de entrada
9
  class InputData(BaseModel):
10
- data: list # Asumiendo que la entrada es una lista de características numéricas
11
 
12
  app = FastAPI()
13
 
14
- # Carga del modelo
15
  def load_model():
16
- json_file = open("model.json", 'r')
17
- loaded_model_json = json_file.read()
18
- json_file.close()
19
- loaded_model = model_from_json(loaded_model_json)
20
- loaded_model.load_weights("model.h5")
21
- print("Cargado el modelo en el disco")
22
- loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])
23
- return loaded_model
 
 
24
 
25
  model = load_model()
26
 
27
  @app.post("/predict/")
28
  async def predict(data: InputData):
29
  try:
30
- # Convertir la lista de entrada a un array de NumPy para la predicción
31
- input_data = np.array(data.data).reshape(1, -1) # Asumiendo que la entrada debe ser de forma (1, num_features)
32
  prediction = model.predict(input_data).round()
33
  return {"prediction": prediction.tolist()}
34
  except Exception as e:
35
- raise HTTPException(status_code=500, detail=str(e))
 
1
  from fastapi import FastAPI, HTTPException
2
+ from tensorflow.keras.models import model_from_json
3
  from pydantic import BaseModel
 
 
4
  import numpy as np
5
 
 
6
  class InputData(BaseModel):
7
+ data: list
8
 
9
  app = FastAPI()
10
 
 
11
  def load_model():
12
+ try:
13
+ with open("model.json", 'r') as json_file:
14
+ loaded_model_json = json_file.read()
15
+ loaded_model = model_from_json(loaded_model_json)
16
+ loaded_model.load_weights("model.h5")
17
+ loaded_model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])
18
+ return loaded_model
19
+ except Exception as e:
20
+ print(f"Error cargando el modelo: {str(e)}")
21
+ raise
22
 
23
  model = load_model()
24
 
25
  @app.post("/predict/")
26
  async def predict(data: InputData):
27
  try:
28
+ input_data = np.array(data.data).reshape(1, -1)
 
29
  prediction = model.predict(input_data).round()
30
  return {"prediction": prediction.tolist()}
31
  except Exception as e:
32
+ raise HTTPException(status_code=500, detail=str(e))