Skip to content

Commit

Permalink
Wise hate speech (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBayan authored Jan 20, 2025
1 parent c1751d7 commit e3eab82
Show file tree
Hide file tree
Showing 36 changed files with 2,532 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from llmebench.datasets import OSACT4SubtaskBDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import HateSpeechTask


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": OSACT4SubtaskBDataset,
"task": HateSpeechTask,
"model": OpenAIModel,
"model_args": {
"max_tries": 3,
},
}


def prompt(input_sample, examples):
base_prompt = (
'هل تحتوي التغريدة التالية على خطاب كراهية؟ أجب بـ "كراهية" إن احتوت على خطاب كراهية، و أجب بـ "ليس كراهية" إن لم تكن كذلك.'
" هذه بعض الأمثلة:\n"
)

return [
{
"role": "system",
"content": "أنت خبير في تحليل و تصنيف التغريدات.",
},
{
"role": "user",
"content": few_shot_prompt(input_sample, base_prompt, examples),
},
]


def few_shot_prompt(input_sample, base_prompt, examples):
out_prompt = base_prompt + "\n"
for example in examples:
# Found chatgpt confused when using 0 and 1 in the prompt
# label = "no" if example["label"] == "0" else "yes"
label = "ليس كراهية" if example["label"] == "NOT_HS" else "كراهية"
out_prompt = (
out_prompt + "التغريدة: " + 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"

# print("=========== FS Prompt =============\n")
# print(out_prompt)

return out_prompt


def post_process(response):
out = response["choices"][0]["message"]["content"]
label = out.lower().strip()

if (
"ليس" in label
or "ليس كراهية" in label
or "لا" in label
or "no" in label
or "not" in label
or "don't" in label
or "not_hs" in label
or "not_hatespeech" in label
or "not_hate_speech" in label
):
return "NOT_HS"
elif (
"كراهية" in label
or "نعم" in label
or "أجل" in label
or "yes" in label
or "contins" in label
or "hs" in label
or "hatespeech" in label
or "hate speech" in label
):
return "HS"
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from llmebench.datasets import OSACT4SubtaskBDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import HateSpeechTask


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": OSACT4SubtaskBDataset,
"task": HateSpeechTask,
"model": OpenAIModel,
"model_args": {
"max_tries": 3,
},
}


def prompt(input_sample, examples):
base_prompt = (
"Does the tweet contain hate speech? Answer by "
" 'hate speech' if it does, and 'not hate speech' otherwise. Here are some examples:\n"
)

return [
{
"role": "system",
"content": "You are an expert in tweets analysis and classification.",
},
{
"role": "user",
"content": few_shot_prompt(input_sample, base_prompt, examples),
},
]


def few_shot_prompt(input_sample, base_prompt, examples):
out_prompt = base_prompt + "\n"
for example in examples:
# Found chatgpt confused when using 0 and 1 in the prompt
# label = "no" if example["label"] == "0" else "yes"
label = "not hate speech" if example["label"] == "NOT_HS" else "hate speech"
out_prompt = (
out_prompt + "tweet: " + example["input"] + "\nlabel: " + 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"

# print("=========== FS Prompt =============\n")
# print(out_prompt)

return out_prompt


def post_process(response):
out = response["choices"][0]["message"]["content"]
label = out.lower().strip()

if (
"ليس" in label
or "ليس كراهية" in label
or "لا" in label
or "no" in label
or "not" in label
or "don't" in label
or "not_hs" in label
or "not_hatespeech" in label
or "not_hate_speech" in label
):
return "NOT_HS"
elif (
"كراهية" in label
or "نعم" in label
or "أجل" in label
or "yes" in label
or "contins" in label
or "hs" in label
or "hatespeech" in label
or "hate speech" in label
):
return "HS"
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from llmebench.datasets import OSACT4SubtaskBDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import HateSpeechTask


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": OSACT4SubtaskBDataset,
"task": HateSpeechTask,
"model": OpenAIModel,
"model_args": {
"max_tries": 3,
},
}


def prompt(input_sample, examples):
base_prompt = 'هل تحتوي التغريدة التالية على خطاب كراهية؟ أجب بـ "hate speech" إن احتوت على خطاب كراهية، و أجب بـ "not hate speech" إن لم تكن كذلك.\n'

return [
{
"role": "system",
"content": "أنت خبير في تحليل و تصنيف التغريدات.",
},
{
"role": "user",
"content": few_shot_prompt(input_sample, base_prompt, examples),
},
]


def few_shot_prompt(input_sample, base_prompt, examples):
out_prompt = base_prompt + "\n"
for example in examples:
# Found chatgpt confused when using 0 and 1 in the prompt
# label = "no" if example["label"] == "0" else "yes"
label = "not hate speech" if example["label"] == "NOT_HS" else "hate speech"
out_prompt = (
out_prompt + "التغريدة: " + 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 post_process(response):
out = response["choices"][0]["message"]["content"]
label = out.lower().strip()

if (
"ليس" in label
or "ليس كراهية" in label
or "لا" in label
or "no" in label
or "not" in label
or "don't" in label
or "not_hs" in label
or "not_hatespeech" in label
or "not_hate_speech" in label
):
return "NOT_HS"
elif (
"كراهية" in label
or "نعم" in label
or "أجل" in label
or "yes" in label
or "contins" in label
or "hs" in label
or "hatespeech" in label
or "hate speech" in label
):
return "HS"
else:
return None
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from llmebench.datasets import OSACT4SubtaskBDataset
from llmebench.models import OpenAIModel
from llmebench.tasks import HateSpeechTask


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": OSACT4SubtaskBDataset,
"task": HateSpeechTask,
"model": OpenAIModel,
"model_args": {
"class_labels": ["HS", "NOT_HS"],
"max_tries": 3,
},
}


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


def post_process(response):
out = response["choices"][0]["message"]["content"]
label = out.lower().strip()

if (
"ليس" in label
or "ليس كراهية" in label
or "لا" in label
or "no" in label
or "not" in label
or "don't" in label
or "not_hs" in label
or "not_hatespeech" in label
or "not_hate_speech" in label
):
return "NOT_HS"
elif (
"كراهية" in label
or "نعم" in label
or "أجل" in label
or "yes" in label
or "contins" in label
or "hs" in label
or "hatespeech" in label
or "hate speech" in label
):
return "HS"
else:
return None
Loading

0 comments on commit e3eab82

Please sign in to comment.