a-guy-from-burma commited on
Commit
e9910d2
·
verified ·
1 Parent(s): 4cb5048

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+
14
+ # Define parameters for padding
15
+ max_length = 200
16
+ padding_type = 'post'
17
+ trunc_type = 'post'
18
+
19
+ # Define a function to predict emotions for a list of comments
20
+ def predict_emotions(comments):
21
+ # Convert input text to sequences
22
+ sequences = tokenizer.texts_to_sequences(comments)
23
+ padded_sequences = pad_sequences(sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
24
+
25
+ # Predict emotions
26
+ predictions = model.predict(padded_sequences)
27
+
28
+ # List of emotion labels
29
+ emotion_labels = ['admiration', 'amusement', 'anger', 'annoyance', 'approval', 'caring', 'confusion', 'curiosity',
30
+ 'desire', 'disappointment', 'disapproval', 'disgust', 'embarrassment', 'excitement', 'fear',
31
+ 'gratitude', 'grief', 'joy', 'love', 'nervousness', 'neutral', 'optimism', 'pride', 'realization',
32
+ 'relief', 'remorse', 'sadness', 'surprise']
33
+
34
+ # Generate human-readable predictions
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
52
+ interface.launch()