Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,76 +0,0 @@
|
|
1 |
-
import keras
|
2 |
-
import pickle
|
3 |
-
import tempfile
|
4 |
-
import numpy as np
|
5 |
-
import gradio as gr
|
6 |
-
import tensorflow as tf
|
7 |
-
from tensorflow.keras.layers import Layer
|
8 |
-
from tensorflow.keras import backend as K
|
9 |
-
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
10 |
-
|
11 |
-
|
12 |
-
class Attention(Layer):
|
13 |
-
|
14 |
-
def __init__(self, return_sequences=True, **kwargs):
|
15 |
-
self.return_sequences = return_sequences
|
16 |
-
super(Attention, self).__init__(**kwargs)
|
17 |
-
|
18 |
-
def build(self, input_shape):
|
19 |
-
|
20 |
-
self.W=self.add_weight(name="att_weight", shape=(input_shape[-1],1),
|
21 |
-
initializer="normal")
|
22 |
-
self.b=self.add_weight(name="att_bias", shape=(input_shape[1],1),
|
23 |
-
initializer="zeros")
|
24 |
-
|
25 |
-
super(Attention,self).build(input_shape)
|
26 |
-
|
27 |
-
def call(self, x):
|
28 |
-
|
29 |
-
e = K.tanh(K.dot(x,self.W)+self.b)
|
30 |
-
a = K.softmax(e, axis=1)
|
31 |
-
output = x*a
|
32 |
-
|
33 |
-
if self.return_sequences:
|
34 |
-
return output
|
35 |
-
|
36 |
-
return K.sum(output, axis=1)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
def load_tokenizer(path):
|
41 |
-
with open(path, 'rb') as f:
|
42 |
-
tokenizer = pickle.load(f)
|
43 |
-
return tokenizer
|
44 |
-
|
45 |
-
|
46 |
-
def label_tweet(test_review):
|
47 |
-
test_review = test_review.lower().strip()
|
48 |
-
token_list = tokenizer.texts_to_sequences([test_review])[0]
|
49 |
-
token_list = pad_sequences([token_list], maxlen=44, padding='post')
|
50 |
-
predicted = model.predict(token_list, verbose=0)
|
51 |
-
if predicted >= 0.5:
|
52 |
-
return 1
|
53 |
-
else:
|
54 |
-
return 0
|
55 |
-
|
56 |
-
|
57 |
-
def analyze_text(comment):
|
58 |
-
|
59 |
-
result = label_tweet(comment)
|
60 |
-
if result == 0:
|
61 |
-
text = "Negative"
|
62 |
-
else:
|
63 |
-
text = "Positive"
|
64 |
-
return text
|
65 |
-
|
66 |
-
|
67 |
-
# It can be used to reconstruct the model identically.
|
68 |
-
model = keras.models.load_model("twitter_sentiment.keras",
|
69 |
-
custom_objects={'Attention': Attention})
|
70 |
-
|
71 |
-
# Load tokenizer
|
72 |
-
tokenizer = load_tokenizer('tokenizer.pkl')
|
73 |
-
|
74 |
-
interface = gr.Interface(fn=analyze_text, inputs=gr.inputs.Textbox(lines=2, placeholder='Enter a positive or negative tweet here...'),
|
75 |
-
outputs='text',title='Twitter Sentimental Analysis', theme='darkhuggingface')
|
76 |
-
interface.launch(inline=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|