File size: 1,634 Bytes
8d1a132
c8070aa
 
2fc8ad9
 
c8070aa
2fc8ad9
 
 
83eec8f
3ad0ab9
8d1a132
b7a1a0d
0ed8f63
 
b7a1a0d
8d1a132
 
 
 
 
e3a361e
8d1a132
 
0ed8f63
 
8d1a132
 
0f7962f
3343d64
58b2e8b
e3a361e
8d1a132
e3a361e
 
 
8d1a132
e3a361e
 
8d1a132
e3a361e
 
 
8d1a132
e3a361e
437386b
11a5ad4
8d1a132
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
39
40
41
42
43
44
45
46
47
48
# install required packages
import subprocess
import sys

def install(package):
    subprocess.check_call([sys.executable, "-m", "pip", "install", package])

install("tensorflow")
install("numpy")
install("transformers")

# import related packages
import streamlit as st
import numpy as np
import tensorflow as tf

import transformers
from transformers import DistilBertTokenizer
from transformers import TFDistilBertForSequenceClassification

# print the header message
st.header("Welcome to the STEM NLP application!")

# fetch the pre-trained model
model = TFDistilBertForSequenceClassification.from_pretrained("kaixinwang/NLP")

# build the tokenizer
MODEL_NAME = 'distilbert-base-uncased'
# tokenizer = DistilBertTokenizer.from_pretrained(MODEL_NAME)
tokenizer = DistilBertTokenizer.from_pretrained("kaixinwang/NLP")

mapping = {0:"Negative", 1:"Positive"}
# prompt for the user input
x = st.text_input("To get started, enter your review/text below and hit ENTER:")
if x:
    st.write("Determining the sentiment...")
    # utterance tokenization
    encoding = tokenizer([x], truncation=True, padding=True)
    encoded = tf.data.Dataset.from_tensor_slices((dict(encoding), np.ones(1)))
    # make the prediction
    preds = model.predict(encoded.batch(1)).logits  
    prob = tf.nn.softmax(preds, axis=1).numpy()  
    prob_max = np.argmax(prob, axis=1)
    # display the output
    st.write("Your review is:", x)
    content = "Sentiment: %s, prediction score: %.4f" %(mapping[prob_max[0]], prob[0][prob_max][0])
    st.write(content)
    # st.write("Sentiment:", mapping[prob_max[0]], "Prediction Score:", prob[0][prob_max][0])