import gradio as gr import torch from transformers import pipeline app_title = "Portuguese Counter Hate Speech Detection (NFAA)" app_description = """ This app is the culmination of the kNOwHATE consortium project, which aimed to tackle Online Hate Speech in the Portuguese comunity. It serves as an user-friendly interface to classify text and identify instances of Hate Speech. This app leverages state-of-the-art Natural Language Processing models developed in the scope of this project to classify harmful text. Select a model from the dropdown menu and input your text to see the classification results. Explore the examples of Hate Speech and Non-Hate Speech offered, and join us in fostering a safer and more respectful online community. For more information about the kNOwHATE project and its initiatives, visit our website [here](https://knowhate.eu) and to explore and use these models visit our Hugging Face page [here](https://huggingface.co/knowhate). """ app_examples = [ ["As pessoas tem que perceber que ser 'panasca' não é deixar de ser homem, é deixar de ser humano 😂😂", "", "knowhate/twt-bertimbau"], ["Vamo-nos unir para criar um mundo mais inclusivo e tolerante.", "", "knowhate/twt-xlm-base"] ] model_list = [ "knowhate/twt-bertimbau", "knowhate/twt-xlm-base", ] def predict(text, target, chosen_model): # Initialize the pipeline with the chosen model model_pipeline = pipeline("text-classification", model=chosen_model) result = model_pipeline(text) predicted_label = result[0]['label'] predicted_score = result[0]['score'] non_predicted_label = "Hate Speech" if predicted_label == "Non Hate Speech" else "Non Hate Speech" non_predicted_score = 1 - predicted_score scores_dict = { predicted_label: predicted_score, non_predicted_label: non_predicted_score } return scores_dict inputs = [ gr.Textbox(label="Context", value= app_examples[0][0]), gr.Textbox(label="Target", value= app_examples[0][1]), gr.Dropdown(label="Model", choices=model_list, value=model_list[0]) ] outputs = [ gr.Label(label="Result"), ] gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title, description=app_description, examples=app_examples, theme=gr.themes.Base(primary_hue="red")).launch()