Henok21 commited on
Commit
08ff948
·
1 Parent(s): dc66d0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -4
app.py CHANGED
@@ -1,3 +1,5 @@
 
 
1
  # Import a module
2
  from transformers import AutoModelForSequenceClassification
3
  from transformers import TFAutoModelForSequenceClassification
@@ -39,8 +41,11 @@ config.id2label = {0: 'NEGATIVE', 1: 'NEUTRAL', 2: 'POSITIVE'}
39
  config.label2id = {"NEGATIVE": 0, "NEUTRAL": 1, "POSITIVE": 2}
40
 
41
  # creating a function used for gradio app
42
- def sentiment_analysis(text):
 
43
 
 
 
44
  # Create a new dictionary
45
  scores = {}
46
 
@@ -53,14 +58,22 @@ def sentiment_analysis(text):
53
  scores = output[0][0].detach().numpy()
54
  scores = softmax(scores)
55
 
56
- labels = {0: "NEGAITVE", 1: "NEUTRAL", 2: "POSITIVE"}
57
- scores = {labels[i]: float(s) for i , s in enumerate(scores) }
 
 
 
 
 
 
 
 
 
58
 
59
  # Return the dictionary as the response content
60
  return scores
61
 
62
 
63
-
64
  # Create your interface
65
  demo = gr.Interface(
66
  fn=sentiment_analysis,
 
1
+
2
+
3
  # Import a module
4
  from transformers import AutoModelForSequenceClassification
5
  from transformers import TFAutoModelForSequenceClassification
 
41
  config.label2id = {"NEGATIVE": 0, "NEUTRAL": 1, "POSITIVE": 2}
42
 
43
  # creating a function used for gradio app
44
+ # Creating dictionary
45
+ dictionary = {}
46
 
47
+ def sentiment_analysis(text):
48
+
49
  # Create a new dictionary
50
  scores = {}
51
 
 
58
  scores = output[0][0].detach().numpy()
59
  scores = softmax(scores)
60
 
61
+ # Convert the numpy array into a list
62
+ scores = scores.tolist()
63
+ ranking = np.argsort(scores)
64
+ ranking = ranking[::-1]
65
+
66
+ for i in range(len(scores)):
67
+ l = config.id2label[ranking[i]]
68
+ s = scores[ranking[i]]
69
+
70
+ # Convert the numpy float32 object into a float
71
+ scores[l] = float(s)
72
 
73
  # Return the dictionary as the response content
74
  return scores
75
 
76
 
 
77
  # Create your interface
78
  demo = gr.Interface(
79
  fn=sentiment_analysis,