Spaces:
Sleeping
Sleeping
File size: 2,225 Bytes
7230c4d ff973ff 7230c4d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
from difflib import Differ
import gradio as gr
from transformers import pipeline
pipe = pipeline("summarization", "dominguesm/positive-reframing-ptbr")
def predict(text, operation):
try:
res = pipe(f"[{operation}]: {text}", max_length=128)
except Exception as e:
return e
d = Differ()
return (
res[0]["summary_text"],
[
(token[2:], token[0] if token[0] != " " else None)
for token in d.compare(text, res[0]["summary_text"])
],
)
# return res[0]["summary_text"]
iface = gr.Interface(
title="Positive Reframing PT-BR",
description="This model is a PTT5 adjusted to the sentiment transfer task, where the objective is to reverse the sentiment polarity of a text without contradicting the original meaning. Positive reframing induces a complementary positive viewpoint (e.g. glass-half-full) escaping negative patterns. More info [here](https://huggingface.co/dominguesm/positive-reframing-ptbr).",
fn=predict,
inputs=[
gr.Textbox(
lines=1,
placeholder=(
f"Pensar no meu futuro me faz querer viver numa ilha sozinha para sempre"
),
),
gr.Radio(
[
"growth",
"impermanence",
"neutralizing",
"optimism",
"self_affirmation",
"thankfulness",
]
),
],
outputs=[
gr.Textbox(label="Generated Text"),
gr.HighlightedText(
label="Diff",
combine_adjacent=True,
).style(color_map={"+": "green", "-": "red"}),
],
examples=[
[
"Tenho tanta coisa para fazer antes de sair da cidade por uma semana no domingo.",
"thankfulness",
],
[
"Aquele momento, você percebe que todo o trabalho duro foi para nada e você não tem controle sobre nada.",
"self_affirmation",
],
[
"Um daqueles dias em que você sente que precisa de um abraço de cinco minutos, um pacote de marshmallows e um amigo para conversar.",
"impermanence",
],
],
)
iface.launch()
|