File size: 943 Bytes
c352eb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f578b97
c352eb2
 
 
 
 
 
 
 
 
 
 
 
d871430
c352eb2
 
 
 
42fad98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import tensorflow as tf
from tensorflow.keras.models import load_model
import json
import keras_nlp
import gradio as gr

fnet_classifier = load_model("Sentiments classifier.keras")

with open("vocab.json", "r") as f:
    vocab = json.load(f)

seq_max_length = 512
tokenizer = keras_nlp.tokenizers.WordPieceTokenizer(
    vocabulary=vocab,
    lowercase=False,
    sequence_length=seq_max_length,
)

def make_prediction(sentence):
    tokens = tokenizer(sentence)
    tokens = tf.expand_dims(tokens, 0)
    prediction = fnet_classifier.predict(tokens, verbose=0)

    if prediction[0][0] > 0.5:
        result = "The review is POSITIVE"
    else:
        result = "The review is NEGATIVE"
    return result

gradio_app = gr.Interface(
    make_prediction,
    inputs=gr.Textbox(label="Your review"),
    outputs=gr.Textbox(label="Sentiment"),
    title="Positive Review or Negtaive Review",
)

if __name__ == "__main__":
    gradio_app.launch()