File size: 1,585 Bytes
eafc169
7b6f763
 
 
 
 
 
 
 
 
 
 
 
eafc169
 
 
 
 
 
 
 
 
 
 
 
7b6f763
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import pipeline
from transformers import AutoModelForSeq2SeqLM
from transformers import AutoTokenizer

# Load trained model
model = AutoModelForSeq2SeqLM.from_pretrained("/output/reframer")
tokenizer = AutoTokenizer.from_pretrained("/output/reframer")
reframer = pipeline('summarization', model=model, tokenizer=tokenizer)


def reframe(text, strategy):
    text_with_strategy = text +  "Strategy: ['" + strategy + "']"
    #Input Control
    #The input text cannot be too short to ensure it has substantial content to be reframed. It also cannot be too long to ensure the text has a focused idea.
    if len(text) < 15 or len(text) > 150:
      return "Please try again by inputing text with moderate length."
    if TextBlob(text).sentiment.polarity > 0.2:
      return "Please try again by inputing text that is negative."
    from hatesonar import Sonar
    sonar = Sonar()
    if sonar.ping(text)['classes'][1]['confidence'] > 0.8:
      return "Please try again by not inputing offensive language."
    #if TextBlob(text).sentiment.polarity > 0:
    #  return "Please try again by inputing text that is negative."
    return reframer(text_with_strategy)[0]['summary_text']


import gradio as gr
with gr.Blocks() as demo:
    text = gr.Textbox(label="Original Text")
    radio = gr.Radio(
        ["thankfulness", "neutralizing", "optimism", "growth", "impermanence", "self_affirmation"], label="Strategy to use?"
    )
    output = gr.Textbox(label="Reframed Output")
    radio.change(fn=reframe, inputs=[text, radio], outputs=output)
demo.launch()