MatteoFasulo commited on
Commit
2af011a
·
verified ·
1 Parent(s): eba7edc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -96,34 +96,35 @@ def analyze(text):
96
 
97
  # Tokenize
98
  inputs = tokenizer(text, padding=True, truncation=True, max_length=256, return_tensors='pt')
 
 
 
 
 
 
99
 
100
  # Get the sentiment values
101
  positive = sentiment_values['positive']
102
  neutral = sentiment_values['neutral']
103
  negative = sentiment_values['negative']
 
104
  # Convert sentiment values to tensors
105
  inputs['positive'] = torch.tensor(positive).unsqueeze(0)
106
  inputs['neutral'] = torch.tensor(neutral).unsqueeze(0)
107
  inputs['negative'] = torch.tensor(negative).unsqueeze(0)
108
 
109
  # Get the sentiment model outputs
110
- outputs1 = model_with_sentiment(**inputs)
111
- logits1 = outputs1.get('logits')
112
 
113
  # Calculate probabilities using softmax
114
- p1 = torch.nn.functional.softmax(logits1, dim=1)[0]
115
-
116
- # Get the subjectivity model outputs
117
- outputs2 = model_without_sentiment(**inputs)
118
- logits2 = outputs2.get('logits')
119
- # Calculate probabilities using softmax
120
- p2 = torch.nn.functional.softmax(logits2, dim=1)[0]
121
 
122
  # Format the output
123
  return {
124
  'Positive': f"{positive:.2%}", 'Neutral': f"{neutral:.2%}", 'Negative': f"{negative:.2%}",
125
- 'Sent-Subj OBJ': f"{p1[0]:.2%}", 'Sent-Subj SUBJ': f"{p1[1]:.2%}",
126
- 'TextOnly OBJ': f"{p2[0]:.2%}", 'TextOnly SUBJ': f"{p2[1]:.2%}"
127
  }
128
 
129
  # Update the Gradio interface
 
96
 
97
  # Tokenize
98
  inputs = tokenizer(text, padding=True, truncation=True, max_length=256, return_tensors='pt')
99
+
100
+ # Get the subjectivity model outputs
101
+ outputs_base = model_without_sentiment(**inputs)
102
+ logits_base = outputs_base.get('logits')
103
+ # Calculate probabilities using softmax
104
+ prob_base = torch.nn.functional.softmax(logits_base, dim=1)[0]
105
 
106
  # Get the sentiment values
107
  positive = sentiment_values['positive']
108
  neutral = sentiment_values['neutral']
109
  negative = sentiment_values['negative']
110
+
111
  # Convert sentiment values to tensors
112
  inputs['positive'] = torch.tensor(positive).unsqueeze(0)
113
  inputs['neutral'] = torch.tensor(neutral).unsqueeze(0)
114
  inputs['negative'] = torch.tensor(negative).unsqueeze(0)
115
 
116
  # Get the sentiment model outputs
117
+ outputs_sentiment = model_with_sentiment(**inputs)
118
+ logits_sentiment = outputs_sentiment.get('logits')
119
 
120
  # Calculate probabilities using softmax
121
+ prob_sentiment = torch.nn.functional.softmax(logits_sentiment, dim=1)[0]
 
 
 
 
 
 
122
 
123
  # Format the output
124
  return {
125
  'Positive': f"{positive:.2%}", 'Neutral': f"{neutral:.2%}", 'Negative': f"{negative:.2%}",
126
+ 'Sent-Subj OBJ': f"{prob_sentiment[0]:.2%}", 'Sent-Subj SUBJ': f"{prob_sentiment[1]:.2%}",
127
+ 'TextOnly OBJ': f"{prob_base[0]:.2%}", 'TextOnly SUBJ': f"{prob_base[1]:.2%}"
128
  }
129
 
130
  # Update the Gradio interface