File size: 1,620 Bytes
946c967
75641e2
0ba7189
 
b469307
 
 
0ba7189
 
 
b469307
 
 
 
 
 
 
 
 
0ba7189
 
 
b469307
0ba7189
b469307
 
0ba7189
b469307
 
 
 
 
 
9a248a5
 
b469307
8c2373f
eb909ed
 
0ba7189
b469307
 
 
 
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
import streamlit as st
import transformers
import matplotlib.pyplot as plt


@st.cache(allow_output_mutation=True, show_spinner=False)
def get_pipe():
    model_name = "joeddav/distilbert-base-uncased-go-emotions-student"
    model = transformers.AutoModelForSequenceClassification.from_pretrained(model_name)
    tokenizer = transformers.AutoTokenizer.from_pretrained(model_name)
    pipe = transformers.pipeline('text-classification', model=model, tokenizer=tokenizer,
                                 return_all_scores=True, truncation=True)
    return pipe


def sort_predictions(predictions):
    return sorted(predictions, key=lambda x: x['score'], reverse=True)


st.set_page_config(page_title="Affect scores")
st.title("Affect scores")
st.write("Type text into the text box and then press 'Compute' to generate affect scores.")

default_text = "It's about a startup taking on a big yet creative challenge, with ups and downs along the way."

text = st.text_area('Enter text here:', value=default_text)
submit = st.button('Generate')

with st.spinner("Loading model..."):
    pipe = get_pipe()

if (submit and len(text.strip()) > 0) or len(text.strip()) > 0:
    prediction = pipe(text)[0]
    sorted_p = sort_predictions(prediction)
    max_ylim = sorted_p[0]['score'] + 0.1
    fig, ax = plt.subplots()
    ax.barh([p['label'] for p in prediction], [p['score'] for p in prediction])
    #ax.tick_params(rotation=0)
    ax.set_xlim(0, max_ylim)
    st.header('Result:')
    st.pyplot(fig)
    prediction = dict([(p['label'], p['score']) for p in prediction])
    st.header('Raw values:')
    st.json(prediction)