Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
3 |
|
4 |
-
""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
"""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import pipeline
|
4 |
|
5 |
+
app_title = "Portuguese Counter Hate Speech Detection (NFAA)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
app_description = """
|
8 |
+
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.
|
9 |
+
This app leverages state-of-the-art Natural Language Processing models developed in the scope of this project to classify harmful text.
|
10 |
+
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.
|
11 |
+
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).
|
12 |
"""
|
13 |
+
|
14 |
+
app_examples = [
|
15 |
+
["As pessoas tem que perceber que ser 'panasca' não é deixar de ser homem, é deixar de ser humano 😂😂", "knowhate/HateBERTimbau-youtube"],
|
16 |
+
["Vamo-nos unir para criar um mundo mais inclusivo e tolerante.", "knowhate/HateBERTimbau-twitter"],
|
17 |
+
["Isso pulhiticos merdosos, continuem a importar lixo, até Portugal deixar de ser Portugal.", "knowhate/HateBERTimbau-yt-tt"],
|
18 |
+
["Eu admiro muito a coragem e a determinação da minha colega de trabalho.", "knowhate/HateBERTimbau-twitter"],
|
19 |
+
["Vai pá puta que te pariu seu paneleiro do caralho, virgem ofendida", "knowhate/HateBERTimbau-youtube"],
|
20 |
+
["O tempo está ensolarado hoje, perfeito para um passeio no parque.", "knowhate/HateBERTimbau-yt-tt"]
|
21 |
+
]
|
22 |
+
|
23 |
+
model_list = [
|
24 |
+
"knowhate/HateBERTimbau-youtube",
|
25 |
+
"knowhate/HateBERTimbau-twitter",
|
26 |
+
"knowhate/HateBERTimbau-yt-tt",
|
27 |
+
]
|
28 |
+
|
29 |
+
def predict(text, target, chosen_model):
|
30 |
+
|
31 |
+
# Initialize the pipeline with the chosen model
|
32 |
+
model_pipeline = pipeline("text-classification", model=chosen_model)
|
33 |
+
result = model_pipeline(text)
|
34 |
+
|
35 |
+
|
36 |
+
predicted_label = result[0]['label']
|
37 |
+
predicted_score = result[0]['score']
|
38 |
+
|
39 |
+
non_predicted_label = "Hate Speech" if predicted_label == "Non Hate Speech" else "Non Hate Speech"
|
40 |
+
non_predicted_score = 1 - predicted_score
|
41 |
+
|
42 |
+
scores_dict = {
|
43 |
+
predicted_label: predicted_score,
|
44 |
+
non_predicted_label: non_predicted_score
|
45 |
+
}
|
46 |
+
|
47 |
+
return scores_dict
|
48 |
+
|
49 |
+
inputs = [
|
50 |
+
gr.Textbox(label="Text", value= app_examples[0][0]),
|
51 |
+
gr.Textbox(label="Text", value= app_examples[0][0]),
|
52 |
+
gr.Dropdown(label="Model", choices=model_list, value=model_list[2])
|
53 |
+
]
|
54 |
+
|
55 |
+
outputs = [
|
56 |
+
gr.Label(label="Result"),
|
57 |
+
]
|
58 |
+
|
59 |
+
gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=app_title,
|
60 |
+
description=app_description, examples=app_examples, theme=gr.themes.Base(primary_hue="red")).launch()
|