hatespeech / app.py
hm-auch
update classifier and demonstrator-code
b642a67
raw
history blame
2.24 kB
import transformers
import gradio as gr
import tensorflow as tf
MODEL_DIRECTORY = './result/model'
PRETRAINED_MODEL_NAME = 'dbmdz/bert-base-german-cased'
TOKENIZER = transformers.BertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)
MAX_SEQUENCE_LENGTH = 300
def encode(sentences, tokenizer, sequence_length):
return tokenizer.batch_encode_plus(
sentences,
max_length=sequence_length, # set the length of the sequences
add_special_tokens=True, # add [CLS] and [SEP] tokens
return_attention_mask=True,
return_token_type_ids=False, # not needed for this type of ML task
pad_to_max_length=True, # add 0 pad tokens to the sequences less than max_length
return_tensors='tf'
)
hs_detection_model = tf.keras.models.load_model(MODEL_DIRECTORY, compile=True)
def inference(sentence):
encoded_sentence = encode([sentence], TOKENIZER, MAX_SEQUENCE_LENGTH)
return hs_detection_model.predict(encoded_sentence.values())
title = "HS-Detector Demonstrator"
description = """
<center>
<p>Dataset: germeval18_hasoc19_rp21_combi_dataset (17,7% HS)</p>
<p>Das bisher beste Modell basierend auf Bert nach 2 Epochen und max. 300 Token pro Eintrag fine-tuning mit folgenden Evaluationsergebnissen:</p>
Accuracy: 0.8794712286158631<br/>
Balanced Accuracy: 0.7561891312100413<br/>
Binary F1-Score: 0.6249999999999999<br/>
Binary Precision: 0.6994584837545126<br/>
Binary Recall: 0.564868804664723<br/>
Weighted F1-Score: 0.8742843536656945<br/>
Weighted Precision: 0.8722794361456155<br/>
Weighted Recall: 0.8794712286158631<br/>
Macro F1-Score: 0.7765982087708463<br/>
Macro Precision: 0.80455672371745<br/>
Macro Recall: 0.7561891312100413<br/>
MCC score: 0.558655967312084<br/>
AUROC score: 0.7561891312100413<br/>
<img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px>
</center>
"""
article = "Die Eingaben werden nicht geloggt. Klassifikator einfach ausprobieren."
input_sentence_text = gr.inputs.Textbox(placeholder="Hier den Satz eingeben, der Hassrede enthalten kann.")
ui = gr.Interface(fn=inference, inputs=input_sentence_text, outputs="text", title = title, description = description, article = article)
ui.launch()