def load(cls, model_dir: str) -> 'CategoryClassifier': model_dir_path = pathlib.Path(model_dir) transformer = joblib.load(str(model_dir_path / cls.TRANSFORMER_PATH)) classifier = joblib.load(str(model_dir_path / cls.CLASSIFIER_PATH)) with open(str(model_dir_path / cls.CATEGORY_TAXONOMY_PATH), 'r') as f: category_taxonomy_data = json.load(f) category_taxonomy = Taxonomy.from_dict(category_taxonomy_data) instance = cls(category_taxonomy) instance.transformer = transformer instance.classifier = classifier return instance
def test_predict(mocker, deepest_only, mock_response, expected_values): category_taxonomy = Taxonomy.from_dict({ "en:meat": { "names": "meat", }, "en:fish": { "names": "fish", }, "en:salmon": { "names": "salmon", "parents": ["en:fish"], }, "en:smoked-salmon": { "names": "salmon", "parents": ["en:salmon"], }, }) classifier = CategoryClassifier(category_taxonomy) mocker.patch( "robotoff.prediction.category.neural.category_classifier.http_session.post", return_value=mock_response, ) predictions = classifier.predict( { "ingredients_tags": ["ingredient1"], "product_name": "Test Product" }, deepest_only, ) assert len(predictions) == len(expected_values) for prediction, (value_tag, confidence) in zip(predictions, expected_values): assert prediction.value_tag == value_tag assert prediction.data.get("confidence") == confidence
def _category_taxonomy() -> Taxonomy: return Taxonomy.from_dict({"en:mushrooms": {"lang": "fr"}})