Keshavp08 commited on
Commit
4689429
·
verified ·
1 Parent(s): 61a5fad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
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
- print("Logits Shape:", logits.shape)
27
- print("Logits Contents:", logits)
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
- if len(logits) != len(sentiments):
40
- st.error(f"Mismatch in the number of sentiments and values. Expected {len(sentiments)}, got {len(logits)}")
41
- else:
42
- fig, ax = plt.subplots()
43
- ax.bar(sentiments, logits.tolist(), color=['red', 'blue', 'green'])
44
- ax.set_title("Sentiment Analysis Scores")
45
- ax.set_ylabel("Score")
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)