Spaces:
Runtime error
Runtime error
# 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 | |
# 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(X): | |
X_test = str(X).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) | |
return {"Persuasive": float(lstm_prob[0]), "Non-Persuasive": 1-float(lstm_prob[0])} | |
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") | |
submit_btn.click( | |
get_res_score, | |
text, | |
[label, plot1, plot2, impplot, s1,s2], 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], main, cache_examples=True) | |
demo.launch(share = True | |
, auth=("prstalk", "prstalk") | |
) |