sentivity commited on
Commit
c136c6c
·
verified ·
1 Parent(s): 7327133

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -51,21 +51,25 @@ def preprocess_text(text):
51
  def predict_sentiment(text):
52
  if not text:
53
  return 0.0
54
- text = preprocess_text(text)
55
- encoded_input = tokenizer(text, return_tensors='pt')
56
- output = model(**encoded_input)
57
- scores = output[0][0].detach().numpy()
58
- scores = softmax(scores)
59
- ranking = np.argsort(scores)
60
- ranking = ranking[::-1]
61
- negative_index = None
62
- for i in range(scores.shape[0]):
63
- if config.id2label[ranking[i]] == 'positive':
64
- negative_index = ranking[i]
65
- break
66
- negative_score = scores[negative_index]
67
-
68
- return float(negative_score.item())*100
 
 
 
 
69
 
70
 
71
 
 
51
  def predict_sentiment(text):
52
  if not text:
53
  return 0.0
54
+ encoded_input = tokenizer(
55
+ text.split(),
56
+ return_tensors='pt',
57
+ padding=True,
58
+ truncation=True,
59
+ max_length=512
60
+ )
61
+ input_ids, attention_mask = encoded_input["input_ids"], encoded_input["attention_mask"]
62
+ with torch.no_grad():
63
+ score = score_model(input_ids, attention_mask)[0].item()
64
+
65
+ scaled=score*100
66
+ k = 20
67
+ midpoint = 0.7
68
+
69
+ scaled_score = 1 / (1 + np.exp(-k * (score - midpoint)))
70
+ final_output = scaled_score * 100
71
+
72
+ return final_output
73
 
74
 
75