Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -23,24 +23,18 @@ if st.button("Analyze") and text:
|
|
| 23 |
output = model(input_ids, attention_mask)
|
| 24 |
logits = output.logits.squeeze()
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
if logits.shape[0] != 3:
|
| 30 |
-
st.error(f"Unexpected number of output values: {logits.shape[0]}")
|
| 31 |
-
st.stop()
|
| 32 |
|
| 33 |
prediction = int(torch.argmax(logits))
|
| 34 |
-
|
| 35 |
-
sentiments = ["Negative", "Neutral", "Positive"]
|
| 36 |
sentiment = sentiments[prediction]
|
| 37 |
st.write(f"Sentiment: {sentiment}")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
st.pyplot(fig)
|
|
|
|
| 23 |
output = model(input_ids, attention_mask)
|
| 24 |
logits = output.logits.squeeze()
|
| 25 |
|
| 26 |
+
# Determine the number of sentiment classes from the model output
|
| 27 |
+
num_classes = logits.shape[0]
|
| 28 |
+
sentiments = ["Very Negative", "Negative", "Neutral", "Positive", "Very Positive"][:num_classes]
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
prediction = int(torch.argmax(logits))
|
|
|
|
|
|
|
| 31 |
sentiment = sentiments[prediction]
|
| 32 |
st.write(f"Sentiment: {sentiment}")
|
| 33 |
|
| 34 |
+
values = logits.tolist()
|
| 35 |
+
|
| 36 |
+
fig, ax = plt.subplots()
|
| 37 |
+
ax.bar(sentiments, values, color=plt.cm.viridis_r(numpy.linspace(0.3, 0.7, num_classes)))
|
| 38 |
+
ax.set_title("Sentiment Analysis Scores")
|
| 39 |
+
ax.set_ylabel("Score")
|
| 40 |
+
st.pyplot(fig)
|
|
|