Spaces:
Runtime error
Runtime error
Commit
·
8d1a132
1
Parent(s):
437386b
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import sys
|
| 3 |
|
|
@@ -8,35 +9,39 @@ install("tensorflow")
|
|
| 8 |
install("numpy")
|
| 9 |
install("transformers")
|
| 10 |
|
| 11 |
-
import
|
| 12 |
-
from transformers import DistilBertTokenizer
|
| 13 |
-
from transformers import TFDistilBertForSequenceClassification
|
| 14 |
-
|
| 15 |
import streamlit as st
|
| 16 |
import numpy as np
|
| 17 |
import tensorflow as tf
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
st.header("Welcome to the STEM NLP application!")
|
|
|
|
|
|
|
| 20 |
model = TFDistilBertForSequenceClassification.from_pretrained("kaixinwang/NLP")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
mapping = {0:"Negative", 1:"Positive"}
|
|
|
|
| 26 |
x = st.text_input("To get started, enter your review/text below and hit ENTER:")
|
| 27 |
if x:
|
| 28 |
-
# st.write("Your review is:", x)
|
| 29 |
st.write("Determining the sentiment...")
|
|
|
|
| 30 |
encoding = tokenizer([x], truncation=True, padding=True)
|
| 31 |
encoded = tf.data.Dataset.from_tensor_slices((dict(encoding), np.ones(1)))
|
|
|
|
| 32 |
preds = model.predict(encoded.batch(1)).logits
|
| 33 |
prob = tf.nn.softmax(preds, axis=1).numpy()
|
| 34 |
prob_max = np.argmax(prob, axis=1)
|
|
|
|
| 35 |
st.write("Your review is:", x)
|
| 36 |
content = "Sentiment: %s, prediction score: %.4f" %(mapping[prob_max[0]], prob[0][prob_max][0])
|
| 37 |
st.write(content)
|
| 38 |
-
# st.write("Sentiment:", mapping[prob_max[0]], "Prediction Score:", prob[0][prob_max][0])
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# x = st.slider('Select a value')
|
| 42 |
-
# st.write(x, 'squared is', x * x)
|
|
|
|
| 1 |
+
# install required packages
|
| 2 |
import subprocess
|
| 3 |
import sys
|
| 4 |
|
|
|
|
| 9 |
install("numpy")
|
| 10 |
install("transformers")
|
| 11 |
|
| 12 |
+
# import related packages
|
|
|
|
|
|
|
|
|
|
| 13 |
import streamlit as st
|
| 14 |
import numpy as np
|
| 15 |
import tensorflow as tf
|
| 16 |
|
| 17 |
+
import transformers
|
| 18 |
+
from transformers import DistilBertTokenizer
|
| 19 |
+
from transformers import TFDistilBertForSequenceClassification
|
| 20 |
+
|
| 21 |
+
# print the header message
|
| 22 |
st.header("Welcome to the STEM NLP application!")
|
| 23 |
+
|
| 24 |
+
# fetch the pre-trained model
|
| 25 |
model = TFDistilBertForSequenceClassification.from_pretrained("kaixinwang/NLP")
|
| 26 |
|
| 27 |
+
# build the tokenizer
|
| 28 |
+
MODEL_NAME = 'distilbert-base-uncased'
|
| 29 |
+
tokenizer = DistilBertTokenizer.from_pretrained(MODEL_NAME)
|
| 30 |
|
| 31 |
mapping = {0:"Negative", 1:"Positive"}
|
| 32 |
+
# prompt for the user input
|
| 33 |
x = st.text_input("To get started, enter your review/text below and hit ENTER:")
|
| 34 |
if x:
|
|
|
|
| 35 |
st.write("Determining the sentiment...")
|
| 36 |
+
# utterance tokenization
|
| 37 |
encoding = tokenizer([x], truncation=True, padding=True)
|
| 38 |
encoded = tf.data.Dataset.from_tensor_slices((dict(encoding), np.ones(1)))
|
| 39 |
+
# make the prediction
|
| 40 |
preds = model.predict(encoded.batch(1)).logits
|
| 41 |
prob = tf.nn.softmax(preds, axis=1).numpy()
|
| 42 |
prob_max = np.argmax(prob, axis=1)
|
| 43 |
+
# display the output
|
| 44 |
st.write("Your review is:", x)
|
| 45 |
content = "Sentiment: %s, prediction score: %.4f" %(mapping[prob_max[0]], prob[0][prob_max][0])
|
| 46 |
st.write(content)
|
| 47 |
+
# st.write("Sentiment:", mapping[prob_max[0]], "Prediction Score:", prob[0][prob_max][0])
|
|
|
|
|
|
|
|
|
|
|
|