File size: 3,146 Bytes
0db5ea1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

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

## Creating a cache to store my model for efficiency
@st.cache_data(ttl=86400)
def load_model(model_name):
    model = AutoModelForSequenceClassification.from_pretrained(model_name)
    return model

## Creating my tokenizer
@st.cache_data(ttl=86400)
def load_tokenizer(tokenizer_name):
    tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
    return tokenizer

## Front end
st.title("Welcome to the Fine-Tuned DisTilled-Bert Sentiment Classifier  Page")

##including an animation to my page 

def load_lottiefile(filepath: str):
    with open(filepath, "r") as f:
        return json.load(f)

# initializaing my session state 
if 'lottie_hello_2' not in st.session_state:
    st.session_state.lottie_hello_2 = load_lottiefile("./lottie_animations/dsbert.json")

# creating a funciton to upload the file while implementing session state
def handle_uploaded_file(uploaded_file):
    if uploaded_file is not None:
        st.session_state.lottie_hello_2 = load_lottiefile(uploaded_file.name)


# displaying the Lottie animation
st_lottie(st.session_state.lottie_hello_2, height=200)

text = st.text_input("Please Enter a Covid-19 Themed Sentence Below: ")



## Cleaning
def data_cleaner(text):
    text = text.lower()
    ## Removing hashtags
    text = re.sub(r'#\w+', '', text)
    ## Removing punctuations
    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

## Running my input through my function
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")

## Initializing my session state
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
    
         """)