From 4ad90b31df77bbb71ecfb4d5692fd244b9582cb1 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Tue, 20 Feb 2024 18:56:24 -0500 Subject: [PATCH] stop racism too --- .../lib/classify_text/src/classify_prompt.rs | 34 ++++- .../dictionary_racist_terms.txt | 129 ++++++++++++++++++ 2 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 includes/binary_includes/dictionary_racist_terms.txt diff --git a/crates/lib/classify_text/src/classify_prompt.rs b/crates/lib/classify_text/src/classify_prompt.rs index 854900581e..864fb5e74f 100644 --- a/crates/lib/classify_text/src/classify_prompt.rs +++ b/crates/lib/classify_text/src/classify_prompt.rs @@ -6,6 +6,7 @@ use crate::minor_regex::lowercase_mentions_underage; const CHILD_TERMS : &str = include_str!("../../../../includes/binary_includes/dictionary_children_terms.txt"); const SEX_TERMS : &str = include_str!("../../../../includes/binary_includes/dictionary_sex_terms.txt"); +const RACIST_TERMS : &str = include_str!("../../../../includes/binary_includes/dictionary_racist_terms.txt"); /// Classification of a text prompt pub struct PromptClassification { @@ -90,9 +91,15 @@ fn references_violence(_prompt_tokens: &[String]) -> bool { false } -fn references_racism(_prompt_tokens: &[String]) -> bool { - // TODO(bt,2024-02-19): Need to implement - false +fn references_racism(prompt_tokens: &[String]) -> bool { + static RACIST_TERM_DICTIONARY : Lazy> = Lazy::new(|| { + RACIST_TERMS.lines() + .map(|line| line.trim().to_lowercase()) + .filter(|line| !line.is_empty() && !line.starts_with("#")) + .collect::>() + }); + + prompt_tokens.iter().any(|term| RACIST_TERM_DICTIONARY.contains(term)) } static SPACES_REGEX : Lazy = Lazy::new(|| { @@ -195,6 +202,27 @@ mod tests { } } + #[test] + fn test_bad_user_racist_prompts() { + let prompts = [ + "adolf hitler having tea with joe biden", + "niggers", + ]; + + for prompt in prompts { + let classification = classify_prompt(prompt); + + asserting(&format!("is abusive - prompt: {}", prompt)) + .that(&classification.is_abusive()) + .is_equal_to(true); + + asserting(&format!("is racism - prompt: {}", prompt)) + .that(&classification.prompt_references_racism) + .is_equal_to(true); + } + } + + #[test] fn borderline_cases() { // These are actual user prompts that use borderline language, but are safe diff --git a/includes/binary_includes/dictionary_racist_terms.txt b/includes/binary_includes/dictionary_racist_terms.txt new file mode 100644 index 0000000000..36d7da068e --- /dev/null +++ b/includes/binary_includes/dictionary_racist_terms.txt @@ -0,0 +1,129 @@ +# These terms should not be allowed for prompts + +# A few fake slurs for testing +fakeslur +junglehorse +slurfake + +# Nazis +adolf +adolph +hitler +nazi + +# Racism +nigger +niggers + +# Additional slurs from BibbyBoi 2022-03-04 +arabush +aravush +argie +argies +bakra +balija +bamboula +beaner +beaney +bluegum +bogtrotter +boonga +boonie +bootlip +bougnoule +bozgor +buddhahead +bule +bunga +burrhead +carcamano +chankoro +chink +chinks +choc-ice +coon +coonass +dago +dego +dink +dune-coon +dyke +fag +faggot +fags +greaser +groid +guidette +# guido # NB(2022.11.12) - Not a slur for Italian models https://discord.com/channels/963507819011768370/963812062939709471/1041106464867094528 +haole +hebe +heeb +hitler +honkey +honkie +hymie +injun +jigaboo +jigarooni +jiggabo +jigger +jijjiboo +kike +kniggers +kyke +maumau +mayomonkey +moolinyan +mulignan +mulignon +neger +negro +negro +negroid +nicc a +nicc er +nicca +niccer +nig +nigar +nigette +niggah +niggar +nigger +niggers +niggertoe +niggress +nigguh +nignig +nignog +nigs +nik-a +nika +nikk-a +nikk-er +ofay +poortagee +rastus +sambo +sheboon +sheeny +sooty +spic +spick +spig +spig +spigotty +spik +tarbaby +whigg-a +whigg-er +whigga +whigger +wigg-er +wigger +wikk-a +wikk-er +wikka +wikker +zigabo +zipperhead