from difflib import Differ import gradio as gr from transformers import pipeline pipe = pipeline("summarization", "dominguesm/positive-reframing-en") def predict(text, operation): try: res = pipe(f"[{operation}]: {text}", max_length=1024) 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 EN", description="This model is a T5 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-en).", 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=[ [ "You know I really don't care about the power struggle between the papacy and secular authority in the medieval ages. stupid", "growth", ], [ "thinking about my future makes me want to go live on a island alone forever. annoyed", "optimism", ], [ "Who would have ever guessed that it would be so freaking hard to get three different grades from two different schools together.", "thankfulness", ], ], ) iface.launch()