File size: 1,953 Bytes
33dbef6
29e4959
228c4b8
29e4959
33dbef6
 
 
 
 
 
 
 
f2a478c
8e37781
d60cca6
 
f2a478c
e57eba3
 
 
 
 
 
 
 
f2a478c
33dbef6
967ef1c
228c4b8
 
 
a07ca53
f2a478c
 
93f97ce
f2a478c
 
 
228c4b8
3cf166b
e57eba3
228c4b8
 
 
e57eba3
9d3ef8a
 
29e4959
33dbef6
 
d60cca6
ea78898
967ef1c
636ed81
e57eba3
636ed81
 
912e22d
8ac658c
f936c34
33dbef6
d60cca6
 
 
e57eba3
 
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
import streamlit as st  #Web App
from transformers import pipeline
from transformers import AutoTokenizer, AutoModelForSequenceClassification


#title
st.title("Sentiment Analysis")


def analyze(input, model):
    return "This is a sample output"

# load my fine-tuned model
fine_tuned = "jbraha/tweet-bert"
labels = {'LABEL_0': 'toxic', 'LABEL_1': 'severe_toxic', 'LABEL_2': 'obscene', 'LABEL_3': 'threat',
          'LABEL_4': 'insult', 'LABEL_5': 'identity_hate'}


# make a dictionary of the labels and values
def unpack(result):
    output = {}
    for res in result:
        output[labels[res['label']]] = res['score']
    return output


#text insert
input = st.text_area("Insert text to be analyzed", value="Nice to see you today.", 
                     height=None, max_chars=None, key=None, help=None, on_change=None, 
                     args=None, kwargs=None, placeholder=None, disabled=False, 
                     label_visibility="visible")

option = st.selectbox(
    'Choose a transformer model:',
    ('Default', 'Fine-Tuned' , 'Roberta'))


if option == 'Fine-Tuned':
    model = AutoModelForSequenceClassification.from_pretrained(fine_tuned)
    tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
    classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer, top_k=None)
elif option == 'Roberta':
    model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
    tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
    classifier = pipeline('sentiment-analysis', model=model, tokenizer=tokenizer)
else:
    classifier = pipeline('sentiment-analysis')


if st.button('Analyze'):
    result = classifier(input)
    output = None
    result = result[0]
    if option == 'Fine-Tuned':
        output = unpack(result)
    else:
        output = result
    st.table(output)
else:
    st.write('Excited to analyze!')