Skip to content

Commit

Permalink
Wise adult content detection (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBayan authored Jan 20, 2025
1 parent 98f082a commit 2f10ba2
Show file tree
Hide file tree
Showing 18 changed files with 1,451 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from llmebench.datasets import AdultDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import AdultTask


def metadata():
return {
"author": "Mohamed Bayan Kmainasi, Rakif Khan, Ali Ezzat Shahroor, Boushra Bendou, Maram Hasanain, and Firoj Alam",
"affiliation": "Arabic Language Technologies, Qatar Computing Research Institute (QCRI), Hamad Bin Khalifa University (HBKU)",
"model": "GPT-4o-2024-05-22",
"description": "For a comprehensive analysis and results, refer to our peer-reviewed publication available at [Springer](https://doi.org/10.1007/978-981-96-0576-7_30) or explore the preprint version on [arXiv](https://arxiv.org/abs/2409.07054).",
}


def config():
return {
"dataset": AdultDataset,
"task": AdultTask,
"model": OpenAIModel,
"model_args": {
"class_labels": ["ADULT", "NOT_ADULT"],
"max_tries": 30,
},
"general_args": {
"fewshot": {
"deduplicate": True,
},
},
}


def few_shot_prompt(input_sample, base_prompt, examples):
out_prompt = base_prompt + "\n\n"
out_prompt = out_prompt + "اليك بعض الامثلة:\n\n"
for index, example in enumerate(examples):
label = "للبالغين" if example["label"] == "ADULT" else "ليست للبالغين"

out_prompt = (
out_prompt
+ "مثال "
+ str(index)
+ ":"
+ "\n"
+ "التغريدة: "
+ example["input"]
+ "\التصنيف: "
+ label
+ "\n\n"
)

# Append the sentence we want the model to predict for but leave the Label blank
out_prompt = out_prompt + "التغريدة: " + input_sample + "\التصنيف: \n"

return out_prompt


def prompt(input_sample, examples):
base_prompt = "هل تحتوي التغريدة التالية على لغة للبالغين؟ اجب 'للبالغين' أو 'ليست للبالغين' بدون أي تفسير."
return [
{
"role": "system",
"content": "أنت محلل خبير، يمكنك تحديد وتصنيف المحتوى الخاص بالبالغين في التغريدة.",
},
{
"role": "user",
"content": few_shot_prompt(input_sample, base_prompt, examples),
},
]


def post_process(response):
if not response:
return None

label = response["choices"][0]["message"]["content"]
label = label.replace("التصنيف:", "").strip()
label = label.replace("label:", "").strip()
label = label.lower()

if (
"ليس" in label
or "ليست" in label
or "not" in label
or "no" in label
or "غير" in label
or "لا" in label
or "not_adult" in label
or "not adult" in label
):
return "NOT_ADULT"
elif "للبالغين" in label or "نعم" in label or "adult" in label:
return "ADULT"
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from llmebench.datasets import AdultDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import AdultTask


def metadata():
return {
"author": "Mohamed Bayan Kmainasi, Rakif Khan, Ali Ezzat Shahroor, Boushra Bendou, Maram Hasanain, and Firoj Alam",
"affiliation": "Arabic Language Technologies, Qatar Computing Research Institute (QCRI), Hamad Bin Khalifa University (HBKU)",
"model": "GPT-4o-2024-05-22",
"description": "For a comprehensive analysis and results, refer to our peer-reviewed publication available at [Springer](https://doi.org/10.1007/978-981-96-0576-7_30) or explore the preprint version on [arXiv](https://arxiv.org/abs/2409.07054).",
}


def config():
return {
"dataset": AdultDataset,
"task": AdultTask,
"model": OpenAIModel,
"model_args": {
"class_labels": ["ADULT", "NOT_ADULT"],
"max_tries": 30,
},
"general_args": {
"fewshot": {
"deduplicate": True,
},
},
}


def few_shot_prompt(input_sample, base_prompt, examples):
out_prompt = base_prompt + "\n\n"
out_prompt = out_prompt + "Here are some examples:\n\n"
for index, example in enumerate(examples):
out_prompt = (
out_prompt
+ "Example "
+ str(index)
+ ":"
+ "\n"
+ "tweet: "
+ example["input"]
+ "\nlabel: "
+ example["label"]
+ "\n\n"
)

# Append the sentence we want the model to predict for but leave the Label blank
out_prompt = out_prompt + "tweet: " + input_sample + "\nlabel: \n"

return out_prompt


def prompt(input_sample, examples):
base_prompt = f'Does the following tweet contain adult content? label it as "ADULT" if it does, or "NOT_ADULT" otherwise. Return label only.'
return [
{
"role": "system",
"content": "You are an expert annotator, you can identify and label adult content within a tweet.",
},
{
"role": "user",
"content": few_shot_prompt(input_sample, base_prompt, examples),
},
]


def post_process(response):
label = response["choices"][0]["message"]["content"]
label = label.replace("التصنيف:", "").strip()
label = label.replace("label:", "").strip()

label = label.replace("<s>", "").replace("</s>", "")
label = label.lower()

if (
"ليس" in label
or "ليست" in label
or "not" in label
or "no" in label
or "غير" in label
or "لا" in label
or "not_adult" in label
or "not adult" in label
):
return "NOT_ADULT"
elif "للبالغين" in label or "نعم" in label or "adult" in label:
return "ADULT"
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from llmebench.datasets import AdultDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import AdultTask


def metadata():
return {
"author": "Mohamed Bayan Kmainasi, Rakif Khan, Ali Ezzat Shahroor, Boushra Bendou, Maram Hasanain, and Firoj Alam",
"affiliation": "Arabic Language Technologies, Qatar Computing Research Institute (QCRI), Hamad Bin Khalifa University (HBKU)",
"model": "GPT-4o-2024-05-22",
"description": "For a comprehensive analysis and results, refer to our peer-reviewed publication available at [Springer](https://doi.org/10.1007/978-981-96-0576-7_30) or explore the preprint version on [arXiv](https://arxiv.org/abs/2409.07054).",
}


def config():
return {
"dataset": AdultDataset,
"task": AdultTask,
"model": OpenAIModel,
"model_args": {
"class_labels": ["ADULT", "NOT_ADULT"],
"max_tries": 30,
},
"general_args": {
"fewshot": {
"deduplicate": True,
},
},
}


def few_shot_prompt(input_sample, base_prompt, examples):
out_prompt = base_prompt + "\n\n"
out_prompt = out_prompt + "اليك بعض الامثلة:\n\n"
for index, example in enumerate(examples):
out_prompt = (
out_prompt
+ "مثال "
+ str(index)
+ ":"
+ "\n"
+ "التغريدة: "
+ example["input"]
+ "\التصنيف: "
+ example["label"]
+ "\n\n"
)

# Append the sentence we want the model to predict for but leave the Label blank
out_prompt = out_prompt + "التغريدة: " + input_sample + "\التصنيف: \n"

return out_prompt


def prompt(input_sample, examples):
base_prompt = f'بالنظر إلى التغريدة التالية، صنفها على أنها "ADULT" أو "NOT_ADULT" بناء على محتوى التغريدة'
return [
{
"role": "system",
"content": "أنت محلل خبير، يمكنك تحديد وتصنيف المحتوى الخاص بالبالغين في التغريدة.",
},
{
"role": "user",
"content": few_shot_prompt(input_sample, base_prompt, examples),
},
]


def post_process(response):
if not response:
return None

label = response["choices"][0]["message"]["content"]

label = label.replace("التصنيف:", "").strip()
label = label.replace("label:", "").strip()

label = label.replace("<s>", "").replace("</s>", "")
label = label.lower()

if (
"ليس" in label
or "ليست" in label
or "not" in label
or "no" in label
or "غير" in label
or "لا" in label
or "not_adult" in label
or "not adult" in label
):
return "NOT_ADULT"
elif "للبالغين" in label or "نعم" in label or "adult" in label:
return "ADULT"
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from llmebench.datasets import AdultDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import AdultTask


def metadata():
return {
"author": "Mohamed Bayan Kmainasi, Rakif Khan, Ali Ezzat Shahroor, Boushra Bendou, Maram Hasanain, and Firoj Alam",
"affiliation": "Arabic Language Technologies, Qatar Computing Research Institute (QCRI), Hamad Bin Khalifa University (HBKU)",
"model": "GPT-4o-2024-05-22",
"description": "For a comprehensive analysis and results, refer to our peer-reviewed publication available at [Springer](https://doi.org/10.1007/978-981-96-0576-7_30) or explore the preprint version on [arXiv](https://arxiv.org/abs/2409.07054).",
}


def config():
return {
"dataset": AdultDataset,
"task": AdultTask,
"model": OpenAIModel,
"model_args": {
"class_labels": ["ADULT", "NOT_ADULT"],
"max_tries": 3,
},
}


def prompt(input_sample):
prompt_string = (
"هل تحتوي التغريدة التالية على لغة للبالغين؟ اجب 'للبالغين' أو 'ليست للبالغين' بدون أي تفسير.\n\n"
+ "التغريدة: "
+ input_sample
+ "\nالتصنيف: "
)
return [
{
"role": "system",
"content": "أنت محلل خبير، يمكنك تحديد وتصنيف المحتوى الخاص بالبالغين في التغريدة.",
},
{
"role": "user",
"content": prompt_string,
},
]


def post_process(response):
label = response["choices"][0]["message"]["content"]
label = label.replace("التصنيف:", "").strip()
label = label.replace("label:", "").strip()

label = label.replace("<s>", "").replace("</s>", "")
label = label.lower()

if (
"ليس" in label
or "ليست" in label
or "not" in label
or "no" in label
or "غير" in label
or "لا" in label
or "not_adult" in label
or "not adult" in label
):
return "NOT_ADULT"
elif "للبالغين" in label or "نعم" in label or "adult" in label:
return "ADULT"
else:
return None
Loading

0 comments on commit 2f10ba2

Please sign in to comment.