Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, Trainer, TextClassificationPipeline
|
| 3 |
+
import operator
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
def get_sentiment(out):
|
| 8 |
+
d = dict()
|
| 9 |
+
for k in out:
|
| 10 |
+
print(k)
|
| 11 |
+
label = k['label']
|
| 12 |
+
score = k['score']
|
| 13 |
+
d[label] = score
|
| 14 |
+
|
| 15 |
+
winning_lab = max(d.items(), key=operator.itemgetter(1))[0]
|
| 16 |
+
winning_score = d[winning_lab]
|
| 17 |
+
|
| 18 |
+
df = pd.DataFrame.from_dict(d, orient = 'index')
|
| 19 |
+
return df #winning_lab, winning_score
|
| 20 |
+
|
| 21 |
+
model_name = "Anthos23/FS-distilroberta-fine-tuned"
|
| 22 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 24 |
+
|
| 25 |
+
pipe = TextClassificationPipeline(model=model, tokenizer=tokenizer, return_all_scores=True)
|
| 26 |
+
text = st.text_area(f'Ciao! This app uses {model_name}.\nEnter your text to test it ❤️')
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if text:
|
| 30 |
+
out = pipe(text)
|
| 31 |
+
df = get_sentiment(out[0])
|
| 32 |
+
fig, ax = plt.subplots()
|
| 33 |
+
c = ['#C34A36', '#FFC75F', '#008F7A']
|
| 34 |
+
ax.bar(df.index, df[0], color=c, width=0.4)
|
| 35 |
+
|
| 36 |
+
st.pyplot(fig)
|
| 37 |
+
|
| 38 |
+
#st.json(get_sentiment(out[0][0]))
|