def get_retriever(cls, args): selector = args['retriever_selector'] if str(selector) in cls._selector_to_dr: dr = cls._selector_to_dr[str(selector)] else: dr = Retriever.objects.get(**selector) cls._selector_to_dr[str(selector)] = dr retriever_pk = dr.pk if retriever_pk not in cls._visual_retriever: cls._retriever_object[retriever_pk] = dr if dr.algorithm == Retriever.EXACT and dr.approximator_shasum and dr.approximator_shasum.strip( ): approximator, da = Approximators.get_trained_model({ "trainedmodel_selector": { "shasum": dr.approximator_shasum } }) da.ensure() approximator.load() cls._visual_retriever[ retriever_pk] = retriever.SimpleRetriever( name=dr.name, approximator=approximator) elif dr.algorithm == Retriever.EXACT: cls._visual_retriever[ retriever_pk] = retriever.SimpleRetriever(name=dr.name) elif dr.algorithm == Retriever.FAISS and dr.approximator_shasum is None: _, di = Indexers.get_trained_model( {"trainedmodel_selector": { "shasum": dr.indexer_shasum }}) cls._visual_retriever[ retriever_pk] = retriever.FaissFlatRetriever( name=dr.name, components=di.arguments['components']) elif dr.algorithm == Retriever.FAISS: approximator, da = Approximators.get_trained_model({ "trainedmodel_selector": { "shasum": dr.approximator_shasum } }) da.ensure() approximator.load() cls._visual_retriever[ retriever_pk] = retriever.FaissApproximateRetriever( name=dr.name, approximator=approximator) elif dr.algorithm == Retriever.LOPQ: approximator, da = Approximators.get_trained_model({ "trainedmodel_selector": { "shasum": dr.approximator_shasum } }) da.ensure() approximator.load() cls._visual_retriever[retriever_pk] = retriever.LOPQRetriever( name=dr.name, approximator=approximator) else: raise ValueError("{} not valid retriever algorithm".format( dr.algorithm)) return cls._visual_retriever[retriever_pk], cls._retriever_object[ retriever_pk]
def get_retriever(cls, retriever_pk): if retriever_pk not in cls._visual_retriever: dr = Retriever.objects.get(pk=retriever_pk) cls._retriever_object[retriever_pk] = dr if dr.algorithm == Retriever.EXACT and dr.approximator_shasum and dr.approximator_shasum.strip(): approximator, da = Approximators.get_approximator_by_shasum(dr.approximator_shasum) da.ensure() approximator.load() cls._visual_retriever[retriever_pk] = retriever.BaseRetriever(name=dr.name, approximator=approximator) elif dr.algorithm == Retriever.EXACT: cls._visual_retriever[retriever_pk] = retriever.BaseRetriever(name=dr.name) elif dr.algorithm == Retriever.FAISS and dr.approximator_shasum is None: di = Indexers.get_indexer_by_shasum(dr.indexer_shasum) cls._visual_retriever[retriever_pk] = retriever.FaissFlatRetriever(name=dr.name, components=di.arguments[ 'components']) elif dr.algorithm == Retriever.FAISS: approximator, da = Approximators.get_approximator_by_shasum(dr.approximator_shasum) da.ensure() approximator.load() cls._visual_retriever[retriever_pk] = retriever.FaissApproximateRetriever(name=dr.name, approximator=approximator) elif dr.algorithm == Retriever.LOPQ: approximator, da = Approximators.get_approximator_by_shasum(dr.approximator_shasum) da.ensure() approximator.load() cls._visual_retriever[retriever_pk] = retriever.LOPQRetriever(name=dr.name, approximator=approximator) else: raise ValueError("{} not valid retriever algorithm".format(dr.algorithm)) return cls._visual_retriever[retriever_pk], cls._retriever_object[retriever_pk]