File size: 6,118 Bytes
c4dc78e
 
 
2e1d56a
c4dc78e
 
 
 
 
 
 
 
 
2e1d56a
c4dc78e
3e8b15e
77593ec
3fb432b
 
0704039
b162f32
3fb432b
2e1d56a
 
 
 
145b567
2e1d56a
 
 
 
 
dedf64b
7a530e3
 
 
72bf7a5
021344a
72bf7a5
2e1d56a
 
 
 
c4dc78e
3c21862
6271ce5
d415981
2e1d56a
 
 
 
7a530e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fb432b
 
cb61bb9
3fb432b
 
 
 
 
 
c4dc78e
2e1d56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3c21862
2e1d56a
cb4135d
2e1d56a
f8f5e66
2e1d56a
f8f5e66
cb4135d
 
f8f5e66
 
2e1d56a
 
3c21862
 
 
c4dc78e
 
7a530e3
 
 
3885b00
2e1d56a
 
 
 
c4dc78e
 
6271ce5
2e1d56a
 
c4dc78e
 
2e1d56a
d193831
c4dc78e
a1e5d8c
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# Import packages:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import re
# tensorflow imports:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import losses
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.optimizers import RMSprop

import pickle
import gradio as gr
import yake
import spacy
from spacy import displacy
import streamlit as st
import spacy_streamlit
nlp = spacy.load('en_core_web_sm')
import torch
import tensorflow as tf
from transformers import RobertaTokenizer, RobertaModel, AutoModelForSequenceClassification, TFAutoModelForSequenceClassification
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/roberta_persuade")
model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/roberta_persuade")

# para_tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/t5_para")
# para_model = AutoModelForSeq2SeqLM.from_pretrained("paragon-analytics/t5_para")

kw_extractor = yake.KeywordExtractor()
custom_kw_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.2, top=10, features=None)

max_words = 2000
max_len = 173

from transformers_interpret import SequenceClassificationExplainer
cls_explainer = SequenceClassificationExplainer(
    model,
    tokenizer)

def process_final_text(text):
    X_test = str(text).lower()

    encoded_input = tokenizer(X_test, return_tensors='pt')
    output = model(**encoded_input)
    scores = output[0][0].detach().numpy()
    scores = tf.nn.softmax(scores)
    
    # Get Keywords:
    keywords = custom_kw_extractor.extract_keywords(X_test)
    letter = []
    score = []
    for i in keywords:
        if i[1]>0.4:
            a = "+++"
        elif (i[1]<=0.4) and (i[1]>0.1):
            a = "++"
        elif (i[1]<=0.1) and (i[1]>0.01):
            a = "+"
        else: 
            a = "NA"
    
        letter.append(i[0])
        score.append(a)
    
    keywords = [(letter[i], score[i]) for i in range(0, len(letter))]
    
    # Get NER:
        # NER: 
    doc = nlp(text)
    sp_html = displacy.render(doc, style="ent", page=True, jupyter=False)
    NER = (
        ""
        + sp_html
        + ""
    )
    
    # Transformer Interpret:
    word_attributions = cls_explainer(X_test)
    letter = []
    score = []
    for i in word_attributions:
        if i[1]>0.5:
            a = "++"
        elif (i[1]<=0.5) and (i[1]>0.1):
            a = "+"
        elif (i[1]>=-0.5) and (i[1]<-0.1):
            a = "-"
        elif i[1]<-0.5:
            a = "--"
        else: 
            a = "NA"
    
        letter.append(i[0])
        score.append(a)
    
    word_attributions = [(letter[i], score[i]) for i in range(0, len(letter))]

    return {"Persuasive": float(scores.numpy()[1]), "Non-Persuasive": float(scores.numpy()[0])},keywords,NER,word_attributions
    
def main(prob1):
    text = str(prob1)
    obj = process_final_text(text)
    return obj[0],obj[1],obj[2],obj[3]
    
title = "Welcome to **Persuade** 🪐"
description1 = """
This app takes text (up to a few sentences) and predicts to what extent the text contains persuasive content.""" 

with gr.Blocks(title=title) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description1)
    gr.Markdown("""---""")
    prob1 = gr.Textbox(label="Enter Your Text Here:",lines=2, placeholder="Type it here ...")
    submit_btn = gr.Button("Analyze")
    #text = gr.Textbox(label="Text:",lines=2, placeholder="Please enter text here ...")
    #submit_btn2 = gr.Button("Analyze")

    with gr.Column(visible=True) as output_col:
        label = gr.Label(label = "Predicted Label")
        impplot = gr.HighlightedText(label="Important Words", combine_adjacent=False).style(
        color_map={"+++": "royalblue","++": "cornflowerblue",
         "+": "lightsteelblue", "NA":"white"})
        NER = gr.HTML(label = 'NER:')
        intp =gr.HighlightedText(label="Word Scores",
        combine_adjacent=False).style(color_map={"++": "darkgreen","+": "green", 
                                                "--": "darkred",
                                                "-": "red", "NA":"white"})

    submit_btn.click(
        main,
        [prob1],
        [label,impplot,NER,intp], api_name="ResText"
    )

    gr.Markdown("### Click on any of the examples below to see to what extent they contain resilience messaging:")
    gr.Examples([["We all have problems; it's how we deal with them that makes us different. If negative thoughts are overwhelming you, you can always talk to a friend, a parent, a teacher, a guidance councillor, or a healthcare professional. Control your problems. Control you life."],["If this is your idea of redeeming miles, this is your beer. 2.6 grams carbs and 95 calories. Michelob Ultra. Lose the carbs. Not the taste."],["Nestle Treasures... indulging yourself has its charm. Creamy chocolate. Luscious fillings. Consider them a gift to yourself. Nestle Treasures...from you to you."],["The Consumer Information Catalog from Pueblo, Colorado lists more than 200 free and low-cost, helpful, federal publications. You'll get the latest info on topics like investing your money, getting fit, parenting, even getting federal benefits. For the latest free catalog, call toll-free 1-888-8-PUEBLO."],["Jarlsberg Lite has 50% less fat and 60% less cholesterol than regular Swiss. It's delicious with fruit, salads, sandwiches. Or just plain snacking. Premium, imported, Jarlsberg Lite."],["Made with real blue cheese, sour cream, buttermilk and an uncompromising attention to detail, Marie's is the ultimate in fresh, homemade taste. Marie's. (Really, really fresh tasting salad dressing.) or (What makes it, makes it great.)"],["At Lexus, we believe that luxury should never be compromised for sportiness. And vice versa. Which is why the Lexus GS is 100% of both. Sports car or luxury car? Let your senses be the judge. The Lexus GS 430."]], [prob1], [label,impplot,NER,intp], main, cache_examples=True)
    
demo.launch()