Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Preemptively filter out Rekognition tags (#4667)
Madison Swain-Bowden5ccf7ff950cd
diff --git a/ingestion_server/ingestion_server/cleanup.py b/ingestion_server/ingestion_server/cleanup.pyindex bb6731698..b5bdea2f1 100644--- a/ingestion_server/ingestion_server/cleanup.py+++ b/ingestion_server/ingestion_server/cleanup.py@@ -55,6 +55,9 @@ TAG_CONTAINS_DENYLIST = {# Filter out low-confidence tags, which indicate that the machine-generated tag# may be inaccurate.TAG_MIN_CONFIDENCE = 0.90+# Filter out tags that match the following providers (either because they haven't+# been vetted or because they are known to be low-quality).+FILTERED_TAG_PROVIDERS = {"rekognition"}# We know that flickr and wikimedia support TLS, so we can add them hereTLS_CACHE = {@@ -123,7 +126,7 @@ class CleanupFunctions:@staticmethoddef cleanup_tags(tags):"""- Delete denylisted and low-accuracy tags.+ Filter denylisted, low-accuracy, and unverified provider tags.:return: an SQL fragment if an update is needed, ``None`` otherwise"""@@ -133,12 +136,14 @@ class CleanupFunctions:if not tags:return Nonefor tag in tags:- below_threshold = False+ alt_filtered = Falseif "accuracy" in tag and float(tag["accuracy"]) < TAG_MIN_CONFIDENCE:- below_threshold = True+ alt_filtered = True+ if "provider" in tag and tag["provider"] in FILTERED_TAG_PROVIDERS:+ alt_filtered = Trueif "name" in tag and isinstance(tag["name"], str):lower_tag = tag["name"].lower()- should_filter = _tag_denylisted(lower_tag) or below_threshold+ should_filter = _tag_denylisted(lower_tag) or alt_filteredelse:log.warning(f'Filtering malformed tag "{tag}" in "{tags}"')should_filter = Truediff --git a/ingestion_server/test/unit_tests/test_cleanup.py b/ingestion_server/test/unit_tests/test_cleanup.pyindex 02cc6d047..f09f66088 100644--- a/ingestion_server/test/unit_tests/test_cleanup.py+++ b/ingestion_server/test/unit_tests/test_cleanup.py@@ -1,13 +1,13 @@import pookfrom psycopg2._json import Json-from ingestion_server.cleanup import CleanupFunctions+from ingestion_server.cleanup import FILTERED_TAG_PROVIDERS, CleanupFunctionsfrom test.unit_tests.conftest import create_mock_imageclass TestCleanup:@staticmethod- def test_tag_blacklist():+ def test_tag_denylisted():tags = [{"name": "cc0"},{"name": " cc0"},@@ -40,6 +40,19 @@ class TestCleanup:expected = str(Json([{"name": "accurate", "accuracy": 0.999}]))assert result == expected+ @staticmethod+ def test_provider_filter():+ tags = [+ {"name": "valid", "provider": "provider1"},+ *[+ {"name": "invalid", "provider": provider}+ for provider in FILTERED_TAG_PROVIDERS+ ],+ ]+ result = str(CleanupFunctions.cleanup_tags(tags))+ expected = str(Json([{"name": "valid", "provider": "provider1"}]))+ assert result == expected+@staticmethod@pook.ondef test_url_protocol_fix():