|
import streamlit as st |
|
import math |
|
import tensorflow as tf |
|
from transformers import GPT2Tokenizer, TFGPT2Model |
|
|
|
st.title("Sentiment Analysis") |
|
st.write('Model detects if a specific comment has positive or negative sentiment') |
|
tweet = st.text_input("Enter your comment", '') |
|
|
|
|
|
PAD_TOKEN = "<|pad|>" |
|
EOS_TOKEN = "<|endoftext|>" |
|
MAX_LENGTH=20 |
|
|
|
tokenizer = GPT2Tokenizer.from_pretrained("gpt2",pad_token=PAD_TOKEN,eos_token=EOS_TOKEN,max_length=MAX_LENGTH,is_split_into_words=True) |
|
|
|
model = TFGPT2Model.from_pretrained("gpt2", use_cache=False,pad_token_id=tokenizer.pad_token_id,eos_token_id=tokenizer.eos_token_id) |
|
model.training = True |
|
model.resize_token_embeddings(len(tokenizer)) |
|
|
|
for layer in model.layers: |
|
layer.trainable = False |
|
|
|
input = tf.keras.layers.Input(shape=(None,), dtype='int32') |
|
mask = tf.keras.layers.Input(shape=(None,), dtype='int32') |
|
x = model(input, attention_mask=mask) |
|
|
|
x = tf.reduce_mean(x.last_hidden_state, axis=1) |
|
x = tf.keras.layers.Dense(16, activation='relu')(x) |
|
x = tf.keras.layers.Dropout(0.3)(x) |
|
output = tf.keras.layers.Dense(2, activation='softmax')(x) |
|
|
|
clf = tf.keras.Model([input, mask], output) |
|
|
|
clf.load_weights('./saved_weights/GPT2_sentiment') |
|
|
|
|
|
|
|
sample_text=[tweet] |
|
EOS_TOKEN = "<|endoftext|>" |
|
sample_text=[str(ex) + EOS_TOKEN for ex in sample_text] |
|
|
|
sample_text_ = [tokenizer(str(x), return_tensors='tf', max_length=MAX_LENGTH, truncation=True, pad_to_max_length=True, add_special_tokens=True)['input_ids'] for x in sample_text] |
|
|
|
sample_text_mask_ = [tokenizer(str(x), return_tensors='tf', max_length=MAX_LENGTH, truncation=True, pad_to_max_length=True, add_special_tokens=True)["attention_mask"] for x in sample_text] |
|
|
|
pred = clf.predict([sample_text_, sample_text_mask_]) |
|
|
|
|
|
|
|
|
|
|
|
positive = round(pred[0][1],4) |
|
negative = round(pred[0][0],4) |
|
|
|
st.write(f"Positive Sentiment Prediction: {positive}") |
|
st.write(f"Negative Sentiment Prediction: {negative}") |
|
|
|
st.header('Below samples are outside of train or test data') |
|
st.header('Sample Positive Sentiment Tweets') |
|
st.write(f"Watchin Espn's First Take! my favorite mornin show! lol Skip is great tv! fyi Im a Witness!") |
|
st.write(f"I'm eating cheezits...with TWO flavors! sharp cheddar & parmesan. :-D") |
|
st.write(f"Just drank a coffe,but I'm still sleeping lol...now drink a fresh lemonade and eat some marshmallows mmm...then study guitar!") |
|
st.write(f"On way home blasting mcfly in the back of the car in the sun good times ") |
|
st.write(f"@AshenDestiny Just had a look at ur updates..quite thoughtful ones..") |
|
|
|
st.header('Sample Negative Sentiment Tweets') |
|
st.write(f"Man, I so desperately do NOT want to be doing this freelance work. Unfortunately, it looks like I'll be doing it the rest of the weekend.") |
|
st.write(f"Is watching ripley's believe it or not. Totally bored.") |
|
st.write(f"not been able to tweet today at my dads and my sister had taken over the laptop, i was going to use my phone but it took all my credit :O") |
|
st.write(f"Ughh I hate being broke does anyone know of any jobs??") |
|
st.write(f"Just realized I will miss th destroy build destroy premiere tonight. I have failed @AndrewWK.") |
|
|