pavan2606 commited on
Commit
9b03b75
1 Parent(s): cbeb610

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from fastai.vision.all import *
3
+ import gradio as gr
4
+ import pickle
5
+ import tempfile
6
+ from transformers import AutoTokenizer, AutoModelWithLMHead
7
+ from speechbrain.inference.interfaces import foreign_class
8
+
9
+
10
+
11
+ # Facial expression classifier
12
+
13
+ # Emotion
14
+ learn_emotion = load_learner('emotions_vgg19.pkl')
15
+ learn_emotion_labels = learn_emotion.dls.vocab
16
+
17
+
18
+ # Predict
19
+ def predict(img):
20
+ img = PILImage.create(img)
21
+ pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
22
+ predicted_emotion = learn_emotion_labels[pred_emotion_idx]
23
+ return predicted_emotion
24
+
25
+
26
+ # Gradio
27
+ title = "Facial Emotion Detector"
28
+
29
+ description = gr.Markdown(
30
+ """Ever wondered what a person might be feeling looking at their picture?
31
+ Well, now you can! Try this fun app. Just upload a facial image in JPG or
32
+ PNG format. You can now see what they might have felt when the picture
33
+ was taken.
34
+
35
+ **Tip**: Be sure to only include face to get best results. Check some sample images
36
+ below for inspiration!""").value
37
+
38
+ article = gr.Markdown(
39
+ """**DISCLAIMER:** This model does not reveal the actual emotional state of a person. Use and
40
+ interpret results at your own risk!.
41
+
42
+ **PREMISE:** The idea is to determine an overall emotion of a person
43
+ based on the pictures. We are restricting pictures to only include close-up facial
44
+ images.
45
+
46
+ **DATA:** FER2013 dataset consists of 48x48 pixel grayscale images of faces.Images
47
+ are assigned one of the 7 emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, and Neutral.
48
+
49
+ """).value
50
+
51
+ enable_queue=True
52
+
53
+ examples = ['happy1.jpg', 'happy2.jpg', 'angry1.png', 'angry2.jpg', 'neutral1.jpg', 'neutral2.jpg']
54
+
55
+ image_mode=gr.Interface(fn = predict,
56
+ inputs = gr.Image( image_mode='L'),
57
+ outputs = [gr.Label(label='Emotion')], #gr.Label(),
58
+ title = title,
59
+ examples = examples,
60
+ description = description,
61
+ article=article,
62
+ allow_flagging='never')
63
+
64
+
65
+
66
+
67
+ # Txet Model
68
+
69
+ # Load tokenizer and model from pickles
70
+ with open("emotion_tokenizer.pkl", "rb") as f:
71
+ tokenizer = pickle.load(f)
72
+
73
+ with open("emotion_model.pkl", "rb") as f:
74
+ model = pickle.load(f)
75
+
76
+
77
+
78
+ def classify_emotion(text):
79
+ # Tokenize input text and generate output
80
+ input_ids = tokenizer.encode("emotion: " + text, return_tensors="pt")
81
+ output = model.generate(input_ids)
82
+ output_text = tokenizer.decode(output[0], skip_special_tokens=True)
83
+
84
+ # Classify the emotion into positive, negative, or neutral
85
+ if output_text in ["joy", "love"]:
86
+ return "Positive"
87
+ elif output_text == "surprise":
88
+ return "Neutral"
89
+ else:
90
+ return "Negative"
91
+ return output_text
92
+
93
+
94
+ text_model = gr.Interface(fn=classify_emotion, inputs="textbox", outputs="textbox")
95
+
96
+
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+ # Initialize the classifier
105
+ classifier = foreign_class(source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP", pymodule_file="custom_interface.py", classname="CustomEncoderWav2vec2Classifier")
106
+
107
+ def save_uploaded_file(uploaded_file):
108
+ temp_dir = tempfile.TemporaryDirectory()
109
+ file_path = os.path.join(temp_dir.name, uploaded_file.name)
110
+ with open(file_path, "wb") as f:
111
+ f.write(uploaded_file.getbuffer())
112
+ return file_path
113
+
114
+
115
+ def emotion(file_path):
116
+
117
+ if file_path:
118
+ # Classify the file
119
+ out_prob, score, index, text_lab = classifier.classify_file(file_path)
120
+ if isinstance(text_lab, list):
121
+ text_lab = text_lab[0]
122
+ # Map the original labels to the desired categories
123
+ emotion_mapping = {
124
+ 'neu': 'Neutral',
125
+ 'ang': 'Angry',
126
+ 'hap': 'Happy',
127
+ 'sad': 'Sadness'
128
+ }
129
+ # Get the corresponding category from the mapping
130
+ emotion_category = emotion_mapping.get(text_lab, 'Unknown')
131
+
132
+ emotion_category = emotion_mapping.get(text_lab, 'Unknown')
133
+ # Return the emotion category
134
+ return emotion_category
135
+ else:
136
+ return "Please provide the path to an audio file."
137
+
138
+
139
+
140
+
141
+
142
+ audio_model = gr.Interface(fn=emotion, inputs="textbox", outputs="textbox")
143
+
144
+
145
+
146
+
147
+
148
+
149
+
150
+ main_model = gr.TabbedInterface([text_model, image_mode,audio_model], ["Text Emotion Recognition", "Image Emotion Recognition" , "Audio Emotion Recognition"])
151
+
152
+ if _name_ == "_main_":
153
+ main_model.launch()