Andrey 5 месяцев назад
Родитель 23a36c70dd
Сommit 2cf389a7e8

@ -2,14 +2,13 @@ name: lec_app
services: services:
price-predict: price-predict:
image: estate_model:1 image: estate_model:2
ports: ports:
- "8001:8000" - "8001:8000"
volumes: volumes:
- ./models:/models - ./models:/models
requests: requests:
image: request_service:3 image: request_service:3
deploy: deploy:
@ -19,7 +18,6 @@ services:
delay: 5s delay: 5s
prometheus: prometheus:
image: prom/prometheus image: prom/prometheus
ports: ports:
@ -39,3 +37,24 @@ services:
- GF_SECURITY_ADMIN_USER=admin - GF_SECURITY_ADMIN_USER=admin
- GF_SECURITY_ADMIN_PASSWORD=admin - GF_SECURITY_ADMIN_PASSWORD=admin
database:
image: postgres:17.2
ports:
- 5432:5432
environment:
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=admin
- POSTGRES_DB=my_db_name
- PGDATA=/var/lib/postgresql/data/pgdata
volumes:
- "./database/data:/var/lib/postgresql/data"
pgadmin:
image: dpage/pgadmin4
ports:
- 9091:80
environment:
- PGADMIN_DEFAULT_EMAIL=email@example.com
- PGADMIN_DEFAULT_PASSWORD=admin
volumes:
- "./database/pgadmin:/var/lib/pgadmin"

@ -2,6 +2,8 @@
import logging import logging
import pandas as pd import pandas as pd
import pickle as pkl import pickle as pkl
import psycopg2
from datetime import datetime
logger = logging.getLogger("uvicorn.error") logger = logging.getLogger("uvicorn.error")
class FastAPIHandler(): class FastAPIHandler():
@ -14,7 +16,25 @@ class FastAPIHandler():
except Exception as e: except Exception as e:
logger.error('Error loading model') logger.error('Error loading model')
def predict(self, item_features:dict): def predict(self, flat_id, item_features:dict):
item_df = pd.DataFrame(data=item_features, index=[0]) item_df = pd.DataFrame(data=item_features, index=[0])
prediction = self.model.predict(item_df) prediction = self.model.predict(item_df)
db_cred = {"dbname": "my_db_name",
"user": 'admin',
"password": 'admin',
"host": "database"}
db_conn = psycopg2.connect(**db_cred)
cur = db_conn.cursor()
now = datetime.now()
cur.execute(f"INSERT INTO public.features \
(flat_id, ts, geo_lon, geo_lat, area) \
VALUES ({flat_id}, '{now}', {item_features['geo_lon']}, {item_features['geo_lat']}, {item_features['area']});")
cur.execute(f"INSERT INTO public.predictions \
(flat_id, ts, price) VALUES ({flat_id}, '{now}', {prediction[0]});")
db_conn.commit()
return (prediction[0]) return (prediction[0])

@ -24,7 +24,7 @@ def root_dir():
@app.post('/api/prediction') @app.post('/api/prediction')
def make_prediction(flat_id: int, item_features: dict): def make_prediction(flat_id: int, item_features: dict):
prediction = app.handler.predict(item_features) prediction = app.handler.predict(flat_id, item_features)
prediction_metric.observe(prediction) prediction_metric.observe(prediction)

@ -5,3 +5,4 @@ pickle4
scikit-learn scikit-learn
requests requests
prometheus_fastapi_instrumentator prometheus_fastapi_instrumentator
psycopg2-binary

Двоичные данные
lectures/lec12-database.pptx

Двоичный файл не отображается.
Загрузка…
Отмена
Сохранить