synthetic-np / NextPlaceBaseModel.py
fivedollarwitch's picture
Update NextPlaceBaseModel.py
ecb5f3e verified
from typing import Union, Tuple
import datetime
class NextPlaceBaseModel:
def __init__(self):
self._load_model()
def _load_model(self):
"""
Perform any actions needed to load the model.
EX: Establish API connections, download an ML model for inference, etc...
"""
print("Loading model...")
# Optional model loading
print("Model loaded.")
def _sale_date_predictor(self, daysOnMarket: int):
"""
Calculate the expected sale date based on the national average
:param daysOnMarket: number of days this house has been on the market
:return: the predicted sale date, based on the national average of 34 days
"""
national_average = 34
if daysOnMarket < national_average:
days_until_sale = national_average - daysOnMarket
sale_date = datetime.date.today() + datetime.timedelta(days=days_until_sale)
return sale_date
else:
return datetime.date.today() + datetime.timedelta(days=1)
def run_inference(self, input_data: dict[str, Union[str, int, float]]) -> Tuple[float, str]:
"""
Predict the sale price and sale date for the house represented by `input_data`
:param input_data: a formatted Synapse from the validator, representing a currently listed house
:return: the predicted sale price and predicted sale date for this home
"""
predicted_sale_price = float(input_data['price']) if ('price' in input_data) else 1.0
predicted_sale_date = self._sale_date_predictor(input_data['dom']) if ('dom' in input_data) else datetime.date.today() + datetime.timedelta(days=1)
predicted_sale_date = predicted_sale_date.strftime("%Y-%m-%d")
print(f"Predicted sale price: {predicted_sale_price}")
print(f"Predicted sale date: {predicted_sale_date}")
return predicted_sale_price, predicted_sale_date