gsar78 commited on
Commit
7014642
·
verified ·
1 Parent(s): d7b8539

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -1
app.py CHANGED
@@ -6,10 +6,29 @@ model = AutoModelForSequenceClassification.from_pretrained("gsar78/Greek_Sentime
6
  tokenizer = AutoTokenizer.from_pretrained("gsar78/Greek_Sentiment")
7
 
8
  def predict(text):
 
9
  inputs = tokenizer(text, return_tensors="pt")
 
10
  outputs = model(**inputs)
 
11
  scores = torch.nn.functional.softmax(outputs.logits, dim=1)
12
- return {"Positive": scores[:, 1].item(), "Negative": scores[:, 0].item()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  iface = gr.Interface(
15
  fn=predict,
 
6
  tokenizer = AutoTokenizer.from_pretrained("gsar78/Greek_Sentiment")
7
 
8
  def predict(text):
9
+ # Tokenize the input text
10
  inputs = tokenizer(text, return_tensors="pt")
11
+ # Get the model outputs
12
  outputs = model(**inputs)
13
+ # Apply softmax to the logits to get probabilities
14
  scores = torch.nn.functional.softmax(outputs.logits, dim=1)
15
+ # Get the predicted label
16
+ predicted_label_idx = scores.argmax(dim=1).item()
17
+ labels = ["negative", "neutral", "positive"]
18
+ predicted_label = labels[predicted_label_idx]
19
+ confidence_score = scores[0, predicted_label_idx].item()
20
+ # Create a dictionary with the prediction and scores
21
+ result = {
22
+ "text": text,
23
+ "label": predicted_label,
24
+ "score": confidence_score,
25
+ "scores": {
26
+ "positive": scores[0, 2].item(),
27
+ "neutral": scores[0, 1].item(),
28
+ "negative": scores[0, 0].item()
29
+ }
30
+ }
31
+ return result
32
 
33
  iface = gr.Interface(
34
  fn=predict,