Skip to content

Commit

Permalink
fix /listings in api to use db
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrelefevre committed Jan 5, 2024
1 parent a996ce3 commit 6c267fe
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
15 changes: 14 additions & 1 deletion api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ def setup():
db = client["bostadspriser"]

global c
c["listings-live-clean"] = db["listings-live-clean"]
c["listings"] = db["listings"]
c["inflation"] = db["inflation"]


setup()


def get_live_listings(page: int, page_size: int):
live_listings = (
c["listings-live-clean"]
.find({}, {"_id": 0})
.skip(page * page_size)
.limit(page_size)
)

return list(live_listings)


def get_inflation(year: int, month: int):
if month < 10:
key = f"{year}M0{month}"
Expand All @@ -53,6 +65,7 @@ def get_inflation(year: int, month: int):
res = c["inflation"].find_one({"id": key})
return res


def get_latest_inflation():
now = datetime.datetime.now()

Expand All @@ -62,4 +75,4 @@ def get_latest_inflation():
if res is not None:
return res
else:
now = now - datetime.timedelta(days=30)
now = now - datetime.timedelta(days=30)
37 changes: 37 additions & 0 deletions api/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import pickle
import json
import db

import pandas as pd


# dict of {"name": name, {"model": model, "scaler": scaler, "results": results}}
models = {}


def load_models():
print("Loading models")
for name in os.listdir("models"):
print(f"Loading model {name}")
model = pickle.load(open(f"models/{name}/model.pkl", "rb"))
scaler = pickle.load(open(f"models/{name}/scaler.pkl", "rb"))
results = pd.read_csv(f"models/{name}/results.csv", index_col=0)
metadata = json.load(open(f"models/{name}/metadata.json", "r"))
models[name] = {
"name": name,
"model": model,
"scaler": scaler,
"results": results,
"metadata": metadata,
}

def choose_model(transformed_params):
if "askingPrice" in transformed_params:
return models["bostadspriser-with-askingPrice"]

return models["bostadspriser-without-askingPrice"]


def get_live_listings(page: int, page_size: int):
return db.get_live_listings(page, page_size)
37 changes: 6 additions & 31 deletions api/server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import json
import flask
import pickle
import cerberus
import datetime
import transform
import helpers

import pandas as pd

Expand All @@ -22,23 +21,6 @@
listings = []
locations = []

# dict of {"name": name, {"model": model, "scaler": scaler, "results": results}}
models = {}

print("Loading models")
for name in os.listdir("models"):
print(f"Loading model {name}")
model = pickle.load(open(f"models/{name}/model.pkl", "rb"))
scaler = pickle.load(open(f"models/{name}/scaler.pkl", "rb"))
results = pd.read_csv(f"models/{name}/results.csv", index_col=0)
metadata = json.load(open(f"models/{name}/metadata.json", "r"))
models[name] = {
"name": name,
"model": model,
"scaler": scaler,
"results": results,
"metadata": metadata,
}

with open("listings.json", "r") as f:
listings = json.load(f)
Expand All @@ -47,13 +29,6 @@
locations = json.load(f)


def choose_model(transformed_params):
if "askingPrice" in transformed_params:
return models["bostadspriser-with-askingPrice"]

return models["bostadspriser-without-askingPrice"]


@app.route("/", methods=["GET"])
def home():
return "Bostadspriser API"
Expand All @@ -67,7 +42,7 @@ def healthz():
@app.route("/models", methods=["GET"])
def get_models():
models_dto = []
for model in models.values():
for model in helpers.models.values():
models_dto.append(
{
"name": model["name"],
Expand All @@ -83,10 +58,10 @@ def get_models():
def get_listings():
args = flask.request.args

skip = int(args.get("skip", 0))
limit = int(args.get("limit", 10))
skip = int(args.get("page", 0))
limit = int(args.get("pageSize", 10))

return flask.jsonify(listings[skip : skip + limit])
return helpers.get_live_listings(skip, limit)


@app.route("/locations", methods=["GET"])
Expand Down Expand Up @@ -138,7 +113,7 @@ def predict():
return flask.jsonify({"error": str(e)}), 400

# Chose model depending on the parameters
model = choose_model(transformed_params.keys())
model = helpers.choose_model(transformed_params.keys())

print("model chosen: " + model["name"])

Expand Down

0 comments on commit 6c267fe

Please sign in to comment.