Spaces:
Runtime error
Runtime error
Commit
·
b469307
1
Parent(s):
946c967
testing pipelien
Browse files
app.py
CHANGED
@@ -1,4 +1,48 @@
|
|
1 |
import streamlit as st
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
|
3 |
+
from transformers import PerceiverTokenizer, PerceiverForMaskedLM
|
4 |
+
|
5 |
+
|
6 |
+
@st.cache(allow_output_mutation=True, show_spinner=False)
|
7 |
+
def get_pipe():
|
8 |
+
model = PerceiverForMaskedLM.from_pretrained("deepmind/language-perceiver")
|
9 |
+
tokenizer = PerceiverTokenizer.from_pretrained("deepmind/language-perceiver")
|
10 |
+
pipe = transformers.pipeline('text-classification', model=model, tokenizer=tokenizer,
|
11 |
+
return_all_scores=True, truncation=True)
|
12 |
+
return pipe
|
13 |
+
|
14 |
+
|
15 |
+
def sort_predictions(predictions):
|
16 |
+
return sorted(predictions, key=lambda x: x['score'], reverse=True)
|
17 |
+
|
18 |
+
|
19 |
+
st.set_page_config(page_title="Emotion Prediction")
|
20 |
+
st.title("Emotion Prediction")
|
21 |
+
st.write("Type text into the text box and then press 'Predict' to get the predicted emotion.")
|
22 |
+
|
23 |
+
default_text = "I really love using HuggingFace Spaces!"
|
24 |
+
|
25 |
+
text = st.text_area('Enter text here:', value=default_text)
|
26 |
+
submit = st.button('Predict')
|
27 |
+
|
28 |
+
with st.spinner("Loading model..."):
|
29 |
+
pipe = get_pipe()
|
30 |
+
|
31 |
+
if (submit and len(text.strip()) > 0) or len(text.strip()) > 0:
|
32 |
+
|
33 |
+
prediction = pipe(text)[0]
|
34 |
+
prediction = sort_predictions(prediction)
|
35 |
+
|
36 |
+
fig, ax = plt.subplots()
|
37 |
+
ax.bar(x=[i for i, _ in enumerate(prediction)],
|
38 |
+
height=[p['score'] for p in prediction],
|
39 |
+
tick_label=[p['label'] for p in prediction])
|
40 |
+
ax.tick_params(rotation=90)
|
41 |
+
ax.set_ylim(0, 1)
|
42 |
+
|
43 |
+
st.header('Prediction:')
|
44 |
+
st.pyplot(fig)
|
45 |
+
|
46 |
+
prediction = dict([(p['label'], p['score']) for p in prediction])
|
47 |
+
st.header('Raw values:')
|
48 |
+
st.json(prediction)
|