|
|
@ -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])
|