slickdata commited on
Commit
19afb18
·
1 Parent(s): fa90c29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -21,7 +21,7 @@ from transformers import pipeline
21
  from transformers import TrainingArguments
22
  from transformers import Trainer
23
  from torch import nn
24
- from transformers import RobertaTokenizer, RobertaForSequenceClassification
25
 
26
 
27
 
@@ -29,7 +29,7 @@ from transformers import RobertaTokenizer, RobertaForSequenceClassification
29
  model_path = "slickdata/finetuned-Sentiment-classfication-ROBERTA-model"
30
 
31
  # Initialize the tokenizer for the pre-trained model
32
- tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
33
 
34
  # Load the configuration for the pre-trained model
35
  config = AutoConfig.from_pretrained(model_path)
@@ -60,18 +60,17 @@ def sentiment_analysis(text):
60
  # Feed the tokenized input to the pre-trained model and obtain output
61
  output = model(**encoded_input)
62
 
63
- # Obtain the prediction scores for the output
64
- scores_ = output[0][0].detach().numpy()
65
-
66
- # Apply softmax activation function to obtain probability distribution over the labels
67
- scores_ = softmax(scores_)
68
 
69
  # Format the output dictionary with the predicted scores
70
  labels = ['Negative', 'Neutral', 'Positive']
71
  scores = {l:float(s) for (l,s) in zip(labels, scores_) }
72
 
73
- # Return the scores
74
- return scores
 
 
 
75
 
76
  # Define a Gradio interface to interact with the model
77
  demo = gr.Interface(
 
21
  from transformers import TrainingArguments
22
  from transformers import Trainer
23
  from torch import nn
24
+
25
 
26
 
27
 
 
29
  model_path = "slickdata/finetuned-Sentiment-classfication-ROBERTA-model"
30
 
31
  # Initialize the tokenizer for the pre-trained model
32
+ tokenizer = AutoTokenizer.from_pretrained('roberta-base')
33
 
34
  # Load the configuration for the pre-trained model
35
  config = AutoConfig.from_pretrained(model_path)
 
60
  # Feed the tokenized input to the pre-trained model and obtain output
61
  output = model(**encoded_input)
62
 
63
+ scores_ = softmax(output.logits[0].detach().numpy())
 
 
 
 
64
 
65
  # Format the output dictionary with the predicted scores
66
  labels = ['Negative', 'Neutral', 'Positive']
67
  scores = {l:float(s) for (l,s) in zip(labels, scores_) }
68
 
69
+ # Get the label with the highest score
70
+ max_score_label = max(scores, key=scores.get)
71
+
72
+ # Return the label with the highest score
73
+ return max_score_label
74
 
75
  # Define a Gradio interface to interact with the model
76
  demo = gr.Interface(