File size: 4,040 Bytes
c4dc78e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e8b15e
77593ec
3fb432b
 
b8da756
b162f32
3fb432b
dedf64b
7a530e3
 
 
72bf7a5
 
 
c4dc78e
 
 
 
 
 
 
 
6271ce5
 
c4dc78e
 
 
 
 
 
7a530e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fb432b
 
 
 
 
 
 
 
 
 
c4dc78e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a530e3
 
 
3885b00
c4dc78e
 
6271ce5
c4dc78e
3fb432b
c4dc78e
 
 
 
 
 
 
 
 
3fb432b
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
# Import packages:

import numpy as np
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
# # keras imports:
from keras.models import Model
from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding, RepeatVector, TimeDistributed
from keras.preprocessing.text import Tokenizer
from keras_preprocessing import sequence
from tensorflow.keras.utils import to_categorical
from keras.callbacks import EarlyStopping
from keras.models import Sequential
from keras import layers
from keras.backend import clear_session
import pickle
import gradio as gr
import yake
import spacy
from spacy import displacy

import spacy_streamlit
nlp = spacy.load('en_core_web_sm')

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 = 111

# load the model from disk
filename = 'lstm_model.sav'
lmodel = pickle.load(open(filename, 'rb'))

# load the model from disk
filename = 'tokenizer.pickle'
tok = pickle.load(open(filename, 'rb'))

def main(text):
    X_test = str(text).lower()
    l = []
    l.append(X_test)
    test_sequences = tok.texts_to_sequences(l)
    test_sequences_matrix = sequence.pad_sequences(test_sequences,maxlen=max_len)
    lstm_prob = lmodel.predict(test_sequences_matrix.tolist()).flatten()
    lstm_pred = np.where(lstm_prob>=0.5,1,0)
    
    # 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(txt)
    sp_html = displacy.render(doc, style="ent", page=True, jupyter=False)
    NER = (
        ""
        + sp_html
        + ""
    )
    return {"Persuasive": float(lstm_prob[0]), "Non-Persuasive": 1-float(lstm_prob[0])},keywords,NER
    
    
    
title = "PrsTalk Application"
description = """
This applicaiton takes text as input and predicts to what extent it is persuasive. Click on the example sentence to see how it works! 
"""

with gr.Blocks(title=title) as demo:
    gr.Markdown(f"## {title}")
    gr.Markdown(description)
    text = gr.Textbox(label="Text:",lines=2, placeholder="Please enter text here ...")
    submit_btn = gr.Button("Analyze")
#     tweet_btn = gr.Button("Tweet")
    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:')

    submit_btn.click(
        main,
        text,
        [label,impplot,NER], api_name="PrsTalk"
    )

    
    
    gr.Markdown("## Example:")
    gr.Examples(["What is performance? Zero to Sixty or Sixty to Zero? How a car performs a quarter mile or a quarter century? Is performance about the joy of driving or the importance of surviving?\
 To us performance is not about doing one thing well ... it is about doing everything well .. because in the end everything matters.\
 Performance without compromise.\
 That is what drives you..... Mercedes Benz","Exhilaration. Unlike any other. Mercedes Benz delivers heart-racing performance with a blend of precision engineering and a little lightning under the hood. For those who see power as the ultimate luxury."], 
                [text], [label,impplot,NER], main, cache_examples=True)
    
demo.launch()