Skip to content

Commit

Permalink
WIP: implementing a sample input parsing and verification implementat…
Browse files Browse the repository at this point in the history
…ion for classycat
  • Loading branch information
ashkankzme committed Aug 14, 2024
1 parent 350e892 commit 5229b1a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
31 changes: 27 additions & 4 deletions lib/model/classycat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Union, Dict, Any
from lib.logger import logger
from lib.model.model import Model
from lib.schemas import Message, ClassyCatSchemaResponse, ClassyCatBatchClassificationResponse
from lib.schemas import Message, ClassyCatSchemaResponse, ClassyCatBatchClassificationResponse, ClassyCatResponse
from lib.model.classycat_classify import Model as ClassifyModel
from lib.model.classycat_schema_create import Model as ClassyCatSchemaCreateModel
from lib.model.classycat_schema_lookup import Model as ClassyCatSchemaLookupModel
Expand Down Expand Up @@ -29,11 +29,34 @@ def validate_input(cls, data: Dict) -> None:
"""
Validate input data. Must be implemented by all child "Model" classes.
"""
pass
event_type = data['parameters']['event_type']

if event_type == 'classify':
ClassifyModel.validate_input(data)
elif event_type == 'schema_lookup':
ClassyCatSchemaLookupModel.validate_input(data)
elif event_type == 'schema_create':
ClassyCatSchemaCreateModel.validate_input(data)
else:
logger.error(f"Unknown event type {event_type}")
raise PrestoBaseException(f"Unknown event type {event_type}", 422)

@classmethod
def parse_input_message(cls, data: Dict) -> Any:
"""
Validate input data. Must be implemented by all child "Model" classes.
Parse input into appropriate response instances.
"""
return None
event_type = data['parameters']['event_type']

if event_type == 'classify':
result_instance_class = ClassifyModel
elif event_type == 'schema_lookup':
result_instance_class = ClassyCatSchemaLookupModel
elif event_type == 'schema_create':
result_instance_class = ClassyCatSchemaCreateModel

else:
logger.error(f"Unknown event type {event_type}")
raise PrestoBaseException(f"Unknown event type {event_type}", 422)

return result_instance_class.parse_input_message(data)
28 changes: 24 additions & 4 deletions lib/model/classycat_schema_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ def process(self, message: Message) -> ClassyCatSchemaResponse:
raise PrestoBaseException(f"Error creating schema: {e}", 500) from e


def verify_schema_parameters(self, schema_name, topics, examples, languages): #todo
@classmethod
def verify_schema_parameters(schema_name, topics, examples, languages):

if not schema_name or not isinstance(schema_name, str) or len(schema_name) == 0:
raise ValueError("schema_name is invalid. It must be a non-empty string")
Expand Down Expand Up @@ -251,11 +252,30 @@ def validate_input(cls, data: Dict) -> None:
"""
Validate input data. Must be implemented by all child "Model" classes.
"""
pass
schema_specs = data['parameters']

schema_name = schema_specs["schema_name"]
topics = schema_specs["topics"]
examples = schema_specs["examples"]
languages = schema_specs["languages"] # ['English', 'Spanish']

try:
cls.verify_schema_parameters(schema_name, topics, examples, languages)
except Exception as e:
logger.exception(f"Error verifying schema parameters: {e}")
raise PrestoBaseException(f"Error verifying schema parameters: {e}", 422) from e

@classmethod
def parse_input_message(cls, data: Dict) -> Any:
"""
Validate input data. Must be implemented by all child "Model" classes.
Parse input into appropriate response instances.
"""
return None
event_type = data['parameters']['event_type']
result_data = data.get('result', {})

if event_type == 'schema_create':
result_instance = ClassyCatSchemaResponse(**result_data)
else:
raise PrestoBaseException(f"Unknown event type {event_type}", 422)

return result_instance
15 changes: 12 additions & 3 deletions lib/model/classycat_schema_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,20 @@ def validate_input(cls, data: Dict) -> None:
"""
Validate input data. Must be implemented by all child "Model" classes.
"""
pass
if "schema_name" not in data["parameters"] or data["parameters"]["schema_name"] == "":
raise PrestoBaseException("schema_name is required as input to schema look up", 422)

@classmethod
def parse_input_message(cls, data: Dict) -> Any:
"""
Validate input data. Must be implemented by all child "Model" classes.
Parse input into appropriate response instances.
"""
return None
event_type = data['parameters']['event_type']
result_data = data.get('result', {})

if event_type == 'schema_lookup':
result_instance = ClassyCatSchemaResponse(**result_data)
else:
raise PrestoBaseException(f"Unknown event type {event_type}", 422)

return result_instance
2 changes: 1 addition & 1 deletion lib/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def parse_input_message(message_data: Dict) -> Message:
result_data = body_data.get('result', {})

modelClass = get_class('lib.model.', os.environ.get('MODEL_NAME'))
modelClass.validate_input(result_data) # will raise exceptions in case of validation errors
modelClass.validate_input(body_data) # will raise exceptions in case of validation errors
# parse_input_message will enable us to have more complicated result types without having to change the schema file
result_instance = modelClass.parse_input_message(result_data) # assumes input is valid

Expand Down

0 comments on commit 5229b1a

Please sign in to comment.