Commit
·
df4a4f8
1
Parent(s):
a7f2f89
Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,34 @@
|
|
1 |
-
import tensorflow as tf
|
2 |
import gradio as gr
|
3 |
-
import
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
model =
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
def preprocess(text):
|
10 |
-
# Tokenize the text
|
11 |
-
tokenizer = tf.keras.preprocessing.text.Tokenizer(num_words=10000)
|
12 |
-
tokenizer.fit_on_texts(text)
|
13 |
-
sequences = tokenizer.texts_to_sequences(text)
|
14 |
-
|
15 |
-
# Pad the sequences to a fixed length of 30
|
16 |
-
padded_sequences = tf.keras.preprocessing.sequence.pad_sequences(sequences, maxlen=30, padding='post')
|
17 |
|
18 |
-
return np.array(padded_sequences)
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
#
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
outputs=gr.outputs.Textbox(label="Sentiment Label"),
|
35 |
-
examples=[["This is wonderful!"], ["I hate this product."]]
|
36 |
-
)
|
37 |
|
38 |
-
#
|
39 |
-
interface.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoTokenizer
|
4 |
+
from model import SentimentClassifier
|
5 |
|
6 |
+
model_state_dict = torch.load('sentimentality.h5')
|
7 |
+
model = SentimentClassifier(2)
|
8 |
+
model.load_state_dict(model_state_dict)
|
9 |
+
model.eval()
|
10 |
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
13 |
|
14 |
+
def preprocess(text):
|
15 |
+
inputs = tokenizer(text, padding='max_length',
|
16 |
+
truncation=True, max_length=512, return_tensors='pt')
|
17 |
+
return inputs
|
18 |
+
# Define a function to use the model to make predictions
|
19 |
+
def predict(review):
|
20 |
+
inputs = preprocess(review)
|
21 |
+
with torch.no_grad():
|
22 |
+
outputs = model(inputs['input_ids'], inputs['attention_mask'])
|
23 |
+
predicted_class = torch.argmax(outputs[0]).item()
|
24 |
+
if(predicted_class==0):
|
25 |
+
return "It was a negative review"
|
26 |
+
return "It was a positive review"
|
27 |
|
28 |
+
# Create a Gradio interface
|
29 |
+
input_text = gr.inputs.Textbox(label="Input Text")
|
30 |
+
output_text = gr.outputs.Textbox(label="Output Text")
|
31 |
+
interface = gr.Interface(fn=predict, inputs=input_text, outputs=output_text)
|
|
|
|
|
|
|
32 |
|
33 |
+
# Run the interface
|
34 |
+
interface.launch()
|