Spaces:
Runtime error
Runtime error
Commit
·
d11c05f
1
Parent(s):
85c4ea5
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,30 @@
|
|
| 1 |
-
#Import required libraries
|
| 2 |
-
import pickle
|
| 3 |
import gradio as gr
|
| 4 |
-
import gradio.inputs
|
| 5 |
-
import pandas as pd
|
| 6 |
-
import numpy as np
|
| 7 |
import tensorflow as tf
|
| 8 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#Loading the tokenizer
|
| 12 |
-
with open('tokenizer.pickle', 'rb') as f:
|
| 13 |
-
tokenizer = pickle.load(f)
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def predict_sentiment(text):
|
| 18 |
-
sentiment = ["I guess, I liked the movie, but I'm not sure it's my favorite."]
|
| 19 |
-
sequence_test = tokenizer.texts_to_sequences([text])
|
| 20 |
-
padded_test = pad_sequences(sequence_test, maxlen= 64)
|
| 21 |
-
text=padded_test
|
| 22 |
-
model = from_pretrained_keras("keras-io/bidirectional-lstm-imdb")
|
| 23 |
-
X = [text for _ in range(len(model.input))]
|
| 24 |
-
a=model.predict(X)
|
| 25 |
-
return sentiment[np.around(a, decimals=0).argmax(axis=1)[0]]
|
| 26 |
-
description = "Provide an opinion regarding a movie as input and this app will suggest what the underlying sentiment is. "
|
| 27 |
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
inputs= gradio.inputs.Textbox( lines=1, placeholder=None, default="", label=None),
|
| 32 |
-
outputs='text',
|
| 33 |
-
title="Sentiment Analysis of Movie Reviews",
|
| 34 |
-
description=description,
|
| 35 |
-
theme="grass")
|
| 36 |
-
iface.launch(enable_queue = True, inline=False, share = True)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 4 |
+
import pickle
|
| 5 |
+
from huggingface_hub import from_pretrained_keras
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
model = from_pretrained_keras("keras-io/bidirectional-lstm-imdb")
|
| 8 |
|
| 9 |
+
with open('tokenizer.pickle', 'rb') as file:
|
| 10 |
+
tokenizer = pickle.load(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
def decide(text):
|
| 13 |
+
tokenized_text = tokenizer.texts_to_sequences([text])
|
| 14 |
+
padded_tokens = pad_sequences(tokenized_text, maxlen= 200)
|
| 15 |
+
result = model.predict(padded_tokens, verbose=0)
|
| 16 |
+
if result[:] < 0.5 :
|
| 17 |
+
output = "negative"
|
| 18 |
+
else:
|
| 19 |
+
output = "positive"
|
| 20 |
+
return output
|
| 21 |
+
|
| 22 |
+
example_sentence_1 = "I hate the movie, they made no effort in making the movie. Waste of time!"
|
| 23 |
+
example_sentence_2 = "Awesome movie! Loved the way in which the hero acted."
|
| 24 |
+
examples = [[example_sentence_1], [example_sentence_2]]
|
| 25 |
+
|
| 26 |
+
description = "Write out a movie review to know the sentiment."
|
| 27 |
+
|
| 28 |
+
gr.Interface(decide, inputs= gr.inputs.Textbox( lines=1, placeholder=None, default="", label=None), outputs='text', examples=examples,
|
| 29 |
+
title="Sentiment analysis of movie reviews",description=description, allow_flagging="auto",
|
| 30 |
+
flagging_dir='flagging records').launch(inline=False, share = True)
|