|
|
|
import streamlit as st |
|
import torch |
|
from transformers import AutoTokenizer |
|
from transformers import AutoModelForSequenceClassification |
|
from transformers import AutoConfig |
|
import numpy as np |
|
import pandas as pd |
|
import re |
|
from scipy.special import softmax |
|
from transformers import pipeline |
|
import xformers |
|
import requests |
|
import json |
|
|
|
from streamlit_lottie import st_lottie |
|
|
|
|
|
import streamlit as st |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
import re |
|
from transformers import pipeline |
|
|
|
|
|
@st.cache_data(ttl=86400) |
|
def load_model(model_name): |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
return model |
|
|
|
|
|
@st.cache_data(ttl=86400) |
|
def load_tokenizer(tokenizer_name): |
|
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name) |
|
return tokenizer |
|
|
|
|
|
st.title("Welcome to the Fine-Tuned DisTilled-Bert Sentiment Classifier Page") |
|
|
|
|
|
|
|
def load_lottiefile(filepath: str): |
|
with open(filepath, "r") as f: |
|
return json.load(f) |
|
|
|
|
|
if 'lottie_hello_2' not in st.session_state: |
|
st.session_state.lottie_hello_2 = load_lottiefile("./lottie_animations/dsbert.json") |
|
|
|
|
|
def handle_uploaded_file(uploaded_file): |
|
if uploaded_file is not None: |
|
st.session_state.lottie_hello_2 = load_lottiefile(uploaded_file.name) |
|
|
|
|
|
|
|
st_lottie(st.session_state.lottie_hello_2, height=200) |
|
|
|
text = st.text_input("Please Enter a Covid-19 Themed Sentence Below: ") |
|
|
|
|
|
|
|
|
|
def data_cleaner(text): |
|
text = text.lower() |
|
|
|
text = re.sub(r'#\w+', '', text) |
|
|
|
text = re.sub("[^\w\s]", repl="", string=text) |
|
text = re.sub(r'\d+', '', text) |
|
text = " ".join([word for word in text.split() if not word.isdigit()]) |
|
return text |
|
|
|
|
|
text_input = data_cleaner(text) |
|
|
|
if 'ro_model' not in st.session_state: |
|
st.session_state.ro_model = load_model("gArthur98/Greg-DistilBert-classifier") |
|
|
|
if 'ro_token' not in st.session_state: |
|
st.session_state.ro_token = load_tokenizer("gArthur98/Greg-DistilBert-classifier") |
|
|
|
pipe = pipeline("sentiment-analysis", model=st.session_state.ro_model, tokenizer=st.session_state.ro_token) |
|
|
|
result = pipe(text_input) |
|
|
|
final = st.button("Predict Sentiment") |
|
|
|
|
|
if final: |
|
for results in result: |
|
if results['label'] == 'LABEL_0': |
|
st.write(f"Your sentiment is Negative with a confidence score of {results['score']}") |
|
elif results["label"] == 'LABEL_1': |
|
st.write(f"Your sentiment is Neutral with a confidence score of {results['score']}") |
|
else: |
|
st.write(f"Your sentiment is Positive with a confidence score of {results['score']}") |
|
|
|
|
|
|
|
st.write("""Example of sentences to input: |
|
|
|
- The New Vaccine is Bad \n |
|
- I love the vaccine \n |
|
- Covid-19 is Moving Fast |
|
|
|
""") |