Приведенный ниже код работает должным образом. Обратите внимание, что переменная ответа не использовалась в ответе_to_mongo().
Модель представляет собой модель sklearn ElasticNet.
Код: Выделить всё
app = FastAPI()
def response_to_mongo(r: dict):
client = pymongo.MongoClient("mongodb://mongo:27017")
db = client["models"]
model_collection = db["example-model"]
model_collection.insert_one(r)
@app.post("/predict")
async def predict_model(features: List[float]):
prediction = model.predict(
pd.DataFrame(
[features],
columns=model.feature_names_in_,
)
)
response = {"predictions": prediction.tolist()}
response_to_mongo(
{"predictions": prediction.tolist()},
)
return response
Код: Выделить всё
@app.post("/predict")
async def predict_model(features: List[float]):
prediction = model.predict(
pd.DataFrame(
[features],
columns=model.feature_names_in_,
)
)
response = {"predictions": prediction.tolist()}
response_to_mongo(
response,
)
return response
Код: Выделить всё
TypeError: 'ObjectId' object is not iterable
Подробнее здесь: https://stackoverflow.com/questions/714 ... t-iterable