"""Detect whether text contains a popular producer tag.""" import re import string import unidecode from suno_utils.worker.top_producer_tags import TOP_PRODUCER_TAGS def _string_simplify(s): s = unidecode.unidecode(s).lower() s = re.sub(r"\s+", " ", s).strip() s = "".join([e for e in s if e in string.ascii_letters + string.digits + " "]) s = re.sub(r"\s+", " ", s).strip() return s def tag_to_re(s): s = re.escape(s) return r"\b" + s + r"\b" # these strings will be blocked additionally MANUAL_BLOCKED_STRINGS = ["CashMoneyAP"] # these strings are ok even though they're contained in the producer tag text file MANUAL_EXCEPTIONS = [ "absolutely", "after party", "all or nothing", "all star", "almighty", "bangladesh", "beam me up", "believe that", "cast spells", "christian", "clipping", "cut it up", "do better", "does it really matter", "dont stop", "fast life", "fly life", "forever alone", "fuck that bitch", "haha haha", "hahahaha", "he broke my heart", "hehehehe", "here comes the", "here comes the pain", "hey michael", "high quality", "high tower", "hollow beats", "hoodrich", "i hate it all", "im sorry", "incoming", "justice league", "lama lama", "landfill", "moonshine", "narcotic", "neighborhood watch", "nerdesin", "never forever", "nightshift", "one hunnid", "pathetic", "punchline", "renegade", "skeleton", "southside", "syncopate", "the avengers", "the punisher", "the renegades", "trademark", "triple a", "tuned up", "undefined", "what have you done", "whatever", "white rocks", "yo saber", "you lose", ] MIN_TAG_LENGTH = 8 def _define_BLOCKED_TAGS(): """Get list of all TAGS we want to exclude""" producer_tags = [ s for s in TOP_PRODUCER_TAGS if ("Producer Tags Directory (" not in s and len(s) <= 200) ] producer_tags = [_string_simplify(s) for s in producer_tags] manual_exceptions = set([_string_simplify(s) for s in MANUAL_EXCEPTIONS]) producer_tags = [s for s in producer_tags if s not in manual_exceptions and len(s) >= MIN_TAG_LENGTH] producer_tags = sorted(producer_tags, key=len, reverse=True) exact_producer_tags = sorted(MANUAL_BLOCKED_STRINGS, key=len, reverse=True) return producer_tags, exact_producer_tags BLOCKED_TAGS, EXACT_BLOCKED_TAGS = _define_BLOCKED_TAGS() BLOCKED_TAGS_REGEX = re.compile(r"|".join([tag_to_re(s) for s in BLOCKED_TAGS]), flags=re.IGNORECASE) BLOCKED_EXCACT_TAGS_REGEX = re.compile(r"|".join([tag_to_re(s) for s in EXACT_BLOCKED_TAGS])) def extract_producer_tag_name_from_text(text: str) -> str | None: """If text contains a popular producer tag, return it, else None.""" if match_ := BLOCKED_TAGS_REGEX.search(_string_simplify(text)): return match_.group() elif match_ := BLOCKED_EXCACT_TAGS_REGEX.search(text): return match_.group() else: return None