Persuade / app.py
paragon-analytics's picture
Update app.py
b162f32
raw
history blame
4.04 kB
# 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()