|
import gradio as gr |
|
from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline |
|
|
|
|
|
|
|
|
|
|
|
def classify_english_text(text): |
|
|
|
|
|
pipe = pipeline("text-classification", model="I2C-UHU/NT-Grief_EN") |
|
label = pipe(text)[0]['label'] |
|
|
|
if label == "LABEL_1": |
|
label = label + " --> Message with non-traumatic grief content" |
|
|
|
elif label == "LABEL_0": |
|
label = label + " --> Message without non-traumatic grief content" |
|
|
|
return label, pipe(text)[0]['score'] |
|
|
|
def classify_spanish_text(text): |
|
|
|
|
|
pipe = pipeline("text-classification", model="I2C-UHU/NT-Grief_SP") |
|
label = pipe(text)[0]['label'] |
|
|
|
if label == "LABEL_1": |
|
label = label + " --> Message with non-traumatic grief content" |
|
|
|
elif label == "LABEL_0": |
|
label = label + " --> Message without non-traumatic grief content" |
|
|
|
return label, pipe(text)[0]['score'] |
|
|
|
|
|
def classify_text(language, text): |
|
if language == "English": |
|
return classify_english_text(text) |
|
elif language == "Español": |
|
return classify_spanish_text(text) |
|
else: |
|
return "Por favor, seleccione un idioma válido / Please select a valid language.",0 |
|
|
|
examples = [ |
|
["el jefe del gobierno local dijo que la muerte de thomas schäfer puede estar relacionada con preocupaciones debido a la crisis por la pandemia del coronavirus."], |
|
["I don't know who but the way the coronavirus pandemic coronavirus pandemic is causing some people to be scared to death some people are dying of fear, it is worth remembering that death is worth remembering that death comes at the right precise moment, not before, not after after..."], |
|
] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=classify_text, |
|
inputs=[ |
|
gr.Radio(["Español", "English"], label="Elija el idioma / Choose the language"), |
|
gr.Textbox(label="Texto / Text") |
|
], |
|
outputs=[gr.Textbox(label = "Prediction"), gr.Number(label = "Probability")], |
|
title = "Non Traumatic Grief Detector", |
|
) |
|
|
|
|
|
iface.launch() |