a-guy-from-burma commited on
Commit
fff4966
·
verified ·
1 Parent(s): aa28a2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -5
app.py CHANGED
@@ -2,12 +2,12 @@ import gradio as gr
2
  import numpy as np
3
  from tensorflow.keras.preprocessing.sequence import pad_sequences
4
  from tensorflow.keras.models import load_model
 
5
 
6
  # Load the saved model
7
  model = load_model('emotion_classifier_model.h5')
8
 
9
  # Load the tokenizer (You need to save the tokenizer too)
10
- import pickle
11
  with open('tokenizer.pickle', 'rb') as handle:
12
  tokenizer = pickle.load(handle)
13
 
@@ -35,17 +35,19 @@ def predict_emotions(comments):
35
  result = []
36
  for prediction in predictions:
37
  emotion_dict = {emotion: prob for emotion, prob in zip(emotion_labels, prediction)}
38
- result.append(emotion_dict)
 
 
39
 
40
  return result
41
 
42
  # Create the Gradio interface
43
  interface = gr.Interface(
44
  fn=predict_emotions,
45
- inputs=gr.inputs.Textbox(label="Input Comment", lines=2, placeholder="Enter your comment here...", type="text", default="This is a wonderful day!"),
46
- outputs=gr.outputs.Label(num_top_classes=3),
47
  title="Reddit Emotion Classifier",
48
- description="Select one or more testing comments and predict their emotion labels."
49
  )
50
 
51
  # Launch the app
 
2
  import numpy as np
3
  from tensorflow.keras.preprocessing.sequence import pad_sequences
4
  from tensorflow.keras.models import load_model
5
+ import pickle
6
 
7
  # Load the saved model
8
  model = load_model('emotion_classifier_model.h5')
9
 
10
  # Load the tokenizer (You need to save the tokenizer too)
 
11
  with open('tokenizer.pickle', 'rb') as handle:
12
  tokenizer = pickle.load(handle)
13
 
 
35
  result = []
36
  for prediction in predictions:
37
  emotion_dict = {emotion: prob for emotion, prob in zip(emotion_labels, prediction)}
38
+ # Sort emotions by probability and get top 3
39
+ top_emotions = sorted(emotion_dict.items(), key=lambda x: x[1], reverse=True)[:3]
40
+ result.append({emotion: prob for emotion, prob in top_emotions})
41
 
42
  return result
43
 
44
  # Create the Gradio interface
45
  interface = gr.Interface(
46
  fn=predict_emotions,
47
+ inputs=gr.Textbox(label="Input Comment", lines=2, placeholder="Enter your comment here...", type="text"),
48
+ outputs=gr.JSON(label="Predicted Emotions"),
49
  title="Reddit Emotion Classifier",
50
+ description="Enter one or more comments and predict their emotion labels."
51
  )
52
 
53
  # Launch the app