ML database modelsΒΆ
Now that we have ways to handle users and a created machine learning model, we need a couple of more tables in our database to log the inputs to our model and the outputs that our model produces.
For this, we will create two new database tables:
requests
- logs the requests to the ML API.
responses
- logs the responses from the ML API.
The full Python code to generate them:
!cat ML_API/MLDB.py
# ORM functions for the database
from sqlalchemy.orm import declarative_base
# Model creation
from sqlalchemy import Column, Integer, DateTime, JSON, ForeignKey
# Dates and times
import datetime
# Users
from Users import User
# Initiating the Base class
Base = declarative_base()
class MLRequests(Base):
# Table name in database
__tablename__ = 'requests'
# If any changes are made to the columns, allow the database to know about it
__table_args__ = {'extend_existing': True}
# Database columns
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey(User.id))
input = Column(JSON)
created_datetime = Column(DateTime)
updated_datetime = Column(DateTime)
def __init__(
self,
user_id: int,
input: JSON
):
# Infering the time of creation
_cur_time = datetime.datetime.now()
# Variables for the object
self.user_id = user_id
self.input = input
self.created_datetime = _cur_time
self.updated_datetime = _cur_time
class MLResponses(Base):
# Table name in database
__tablename__ = 'responses'
# If any changes are made to the columns, allow the database to know about it
__table_args__ = {'extend_existing': True}
# Database columns
id = Column(Integer, primary_key=True)
request_id = Column(Integer, ForeignKey('requests.id'))
output = Column(JSON)
created_datetime = Column(DateTime)
updated_datetime = Column(DateTime)
def __init__(
self,
request_id: int,
output: JSON
):
# Infering the time of creation
_cur_time = datetime.datetime.now()
# Variables for the object
self.request_id = request_id
self.output = output
self.created_datetime = _cur_time
self.updated_datetime = _cur_time
PSQL
can hold a JSON data type in one of the collumns. We will save the what is necesary and used by the API in the requests
table input column
. Because we are saving a JSON file, we can be assured that if in the feature the feature inputs for the model changes, we will not have to create or delete any columns in the table.
The same logic applies to the reponses
table - the JSON which will be sent to the user is saved in the response
column.