Skip to content

Commit 051196f

Browse files
committed
Add mongo for api calls
1 parent cc2fb76 commit 051196f

File tree

4 files changed

+125
-7
lines changed

4 files changed

+125
-7
lines changed

api/main.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import logging
1+
import datetime
22
from typing import List
33
import os
44

55
import pandas as pd
66
from fastapi import FastAPI
77
import mlflow
88
import numpy as np
9+
import pymongo
910

1011
from ml.data import get_data
1112
from ml.evaluate import eval_metrics
@@ -16,6 +17,13 @@
1617
app = FastAPI()
1718

1819

20+
def response_to_mongo(r: dict):
21+
client = pymongo.MongoClient("mongodb://mongo:27017")
22+
db = client["models"]
23+
model_collection = db["example-model"]
24+
model_collection.insert_one(r)
25+
26+
1927
@app.on_event("startup")
2028
def load_api_model():
2129
model_id = os.environ.get("MODEL_ID")
@@ -41,6 +49,9 @@ async def predict_model(features: List[float]):
4149
)
4250

4351
response = {"predictions": prediction.tolist()}
52+
response_to_mongo(
53+
{"predictions": prediction.tolist(), "date": datetime.datetime.utcnow()},
54+
)
4455
return response
4556

4657

docker-compose.yml

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
version: "3.9"
22

33
services:
4-
postgres:
5-
image: "postgres"
6-
restart: always
7-
environment:
8-
POSTGRES_PASSWORD: password
4+
mongo:
5+
image: mongo:latest
6+
ports:
7+
- 27017:27017
8+
volumes:
9+
- mongodb_data_container:/data/db
910

1011
api:
1112
build: .
@@ -34,3 +35,6 @@ services:
3435
- "5001:5001"
3536
volumes:
3637
- ./mlruns:/code/mlruns
38+
39+
volumes:
40+
mongodb_data_container:

poetry.lock

+103-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ typer = "^0.4.0"
1212
fastapi = "^0.75.0"
1313
uvicorn = "^0.17.5"
1414
streamlit = "^1.7.0"
15+
pymongo = "^4.0.2"
1516

1617
[tool.poetry.dev-dependencies]
1718
black = "^22.1.0"

0 commit comments

Comments
 (0)