Spaces:
Runtime error
Runtime error
paragon-analytics
commited on
Commit
•
c4dc78e
1
Parent(s):
a350c8b
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import packages:
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import re
|
6 |
+
# tensorflow imports:
|
7 |
+
import tensorflow as tf
|
8 |
+
from tensorflow import keras
|
9 |
+
from tensorflow.keras import losses
|
10 |
+
from tensorflow.keras import layers
|
11 |
+
from tensorflow.keras.layers.experimental import preprocessing
|
12 |
+
from tensorflow.keras.optimizers import RMSprop
|
13 |
+
# # keras imports:
|
14 |
+
from keras.models import Model
|
15 |
+
from keras.layers import LSTM, Activation, Dense, Dropout, Input, Embedding, RepeatVector, TimeDistributed
|
16 |
+
from keras.preprocessing.text import Tokenizer
|
17 |
+
from keras_preprocessing import sequence
|
18 |
+
from tensorflow.keras.utils import to_categorical
|
19 |
+
from keras.callbacks import EarlyStopping
|
20 |
+
from keras.models import Sequential
|
21 |
+
from keras import layers
|
22 |
+
from keras.backend import clear_session
|
23 |
+
import pickle
|
24 |
+
|
25 |
+
# load the model from disk
|
26 |
+
filename = 'lstm_model.sav'
|
27 |
+
lmodel = pickle.load(open(filename, 'rb'))
|
28 |
+
|
29 |
+
# load the model from disk
|
30 |
+
filename = 'tokenizer.pickle'
|
31 |
+
tok = pickle.load(open(filename, 'rb'))
|
32 |
+
|
33 |
+
def main(X):
|
34 |
+
X_test = str(X).lower()
|
35 |
+
l = []
|
36 |
+
l.append(X_test)
|
37 |
+
test_sequences = tok.texts_to_sequences(l)
|
38 |
+
test_sequences_matrix = sequence.pad_sequences(test_sequences,maxlen=max_len)
|
39 |
+
lstm_prob = lmodel.predict(test_sequences_matrix.tolist()).flatten()
|
40 |
+
lstm_pred = np.where(lstm_prob>=0.5,1,0)
|
41 |
+
return {"Persuasive": float(lstm_prob[0]), "Non-Persuasive": 1-float(lstm_prob[0])}
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
title = "PrsTalk Application"
|
46 |
+
description = """
|
47 |
+
This applicaiton takes text as input and predicts to what extent it is persuasive. Click on the example sentence to see how it works!
|
48 |
+
"""
|
49 |
+
|
50 |
+
with gr.Blocks(title=title) as demo:
|
51 |
+
gr.Markdown(f"## {title}")
|
52 |
+
gr.Markdown(description)
|
53 |
+
text = gr.Textbox(label="Text:",lines=2, placeholder="Please enter text here ...")
|
54 |
+
submit_btn = gr.Button("Analyze")
|
55 |
+
# tweet_btn = gr.Button("Tweet")
|
56 |
+
with gr.Column(visible=True) as output_col:
|
57 |
+
label = gr.Label(label = "Predicted Label")
|
58 |
+
|
59 |
+
|
60 |
+
submit_btn.click(
|
61 |
+
get_res_score,
|
62 |
+
text,
|
63 |
+
[label, plot1, plot2, impplot, s1,s2], api_name="PrsTalk"
|
64 |
+
)
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
gr.Markdown("## Example:")
|
69 |
+
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?\
|
70 |
+
To us performance is not about doing one thing well ... it is about doing everything well .. because in the end everything matters.\
|
71 |
+
Performance without compromise.\
|
72 |
+
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."],
|
73 |
+
[text], [label], main, cache_examples=True)
|
74 |
+
|
75 |
+
demo.launch(share = True
|
76 |
+
, auth=("prstalk", "prstalk")
|
77 |
+
)
|