Upload 7 files
Browse files- app.py +98 -0
- config.json +51 -0
- emotion_model.pkl +3 -0
- emotion_tokenizer.pkl +3 -0
- special_tokens_map.json +1 -0
- spiece.model +3 -0
- tokenizer_config.json +1 -0
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from fastai.vision.all import *
|
3 |
+
import gradio as gr
|
4 |
+
import pickle
|
5 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
# Facial expression classifier
|
10 |
+
|
11 |
+
# Emotion
|
12 |
+
learn_emotion = load_learner('emotions_vgg19.pkl')
|
13 |
+
learn_emotion_labels = learn_emotion.dls.vocab
|
14 |
+
|
15 |
+
|
16 |
+
# Predict
|
17 |
+
def predict(img):
|
18 |
+
img = PILImage.create(img)
|
19 |
+
pred_emotion, pred_emotion_idx, probs_emotion = learn_emotion.predict(img)
|
20 |
+
predicted_emotion = learn_emotion_labels[pred_emotion_idx]
|
21 |
+
return predicted_emotion
|
22 |
+
|
23 |
+
|
24 |
+
# Gradio
|
25 |
+
title = "Facial Emotion Detector"
|
26 |
+
|
27 |
+
description = gr.Markdown(
|
28 |
+
"""Ever wondered what a person might be feeling looking at their picture?
|
29 |
+
Well, now you can! Try this fun app. Just upload a facial image in JPG or
|
30 |
+
PNG format. You can now see what they might have felt when the picture
|
31 |
+
was taken.
|
32 |
+
|
33 |
+
**Tip**: Be sure to only include face to get best results. Check some sample images
|
34 |
+
below for inspiration!""").value
|
35 |
+
|
36 |
+
article = gr.Markdown(
|
37 |
+
"""**DISCLAIMER:** This model does not reveal the actual emotional state of a person. Use and
|
38 |
+
interpret results at your own risk!.
|
39 |
+
|
40 |
+
**PREMISE:** The idea is to determine an overall emotion of a person
|
41 |
+
based on the pictures. We are restricting pictures to only include close-up facial
|
42 |
+
images.
|
43 |
+
|
44 |
+
**DATA:** FER2013 dataset consists of 48x48 pixel grayscale images of faces.Images
|
45 |
+
are assigned one of the 7 emotions: Angry, Disgust, Fear, Happy, Sad, Surprise, and Neutral.
|
46 |
+
|
47 |
+
""").value
|
48 |
+
|
49 |
+
enable_queue=True
|
50 |
+
|
51 |
+
examples = ['happy1.jpg', 'happy2.jpg', 'angry1.png', 'angry2.jpg', 'neutral1.jpg', 'neutral2.jpg']
|
52 |
+
|
53 |
+
image_mode=gr.Interface(fn = predict,
|
54 |
+
inputs = gr.Image( image_mode='L'),
|
55 |
+
outputs = [gr.Label(label='Emotion')], #gr.Label(),
|
56 |
+
title = title,
|
57 |
+
examples = examples,
|
58 |
+
description = description,
|
59 |
+
article=article,
|
60 |
+
allow_flagging='never')
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
# Txet Model
|
66 |
+
|
67 |
+
# Load tokenizer and model from pickles
|
68 |
+
with open("emotion_tokenizer.pkl", "rb") as f:
|
69 |
+
tokenizer = pickle.load(f)
|
70 |
+
|
71 |
+
with open("emotion_model.pkl", "rb") as f:
|
72 |
+
model = pickle.load(f)
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
def classify_emotion(text):
|
77 |
+
# Tokenize input text and generate output
|
78 |
+
input_ids = tokenizer.encode("emotion: " + text, return_tensors="pt")
|
79 |
+
output = model.generate(input_ids)
|
80 |
+
output_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
81 |
+
|
82 |
+
# Classify the emotion into positive, negative, or neutral
|
83 |
+
if output_text in ["joy", "love"]:
|
84 |
+
return "Positive"
|
85 |
+
elif output_text == "surprise":
|
86 |
+
return "Neutral"
|
87 |
+
else:
|
88 |
+
return "Negative"
|
89 |
+
return output_text
|
90 |
+
|
91 |
+
|
92 |
+
text_model = gr.Interface(fn=classify_emotion, inputs="textbox", outputs="textbox")
|
93 |
+
|
94 |
+
|
95 |
+
main_model = gr.TabbedInterface([text_model, image_mode], ["Text Emotion Recognition", "Image Emotion Recognition"])
|
96 |
+
|
97 |
+
if _name_ == "_main_":
|
98 |
+
main_model.launch()
|
config.json
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"T5ForConditionalGeneration"
|
4 |
+
],
|
5 |
+
"d_ff": 3072,
|
6 |
+
"d_kv": 64,
|
7 |
+
"d_model": 768,
|
8 |
+
"decoder_start_token_id": 0,
|
9 |
+
"dropout_rate": 0.1,
|
10 |
+
"eos_token_id": 1,
|
11 |
+
"initializer_factor": 1.0,
|
12 |
+
"is_encoder_decoder": true,
|
13 |
+
"layer_norm_epsilon": 1e-06,
|
14 |
+
"model_type": "t5",
|
15 |
+
"n_positions": 512,
|
16 |
+
"num_heads": 12,
|
17 |
+
"num_layers": 12,
|
18 |
+
"output_past": true,
|
19 |
+
"pad_token_id": 0,
|
20 |
+
"relative_attention_num_buckets": 32,
|
21 |
+
"task_specific_params": {
|
22 |
+
"summarization": {
|
23 |
+
"early_stopping": true,
|
24 |
+
"length_penalty": 2.0,
|
25 |
+
"max_length": 200,
|
26 |
+
"min_length": 30,
|
27 |
+
"no_repeat_ngram_size": 3,
|
28 |
+
"num_beams": 4,
|
29 |
+
"prefix": "summarize: "
|
30 |
+
},
|
31 |
+
"translation_en_to_de": {
|
32 |
+
"early_stopping": true,
|
33 |
+
"max_length": 300,
|
34 |
+
"num_beams": 4,
|
35 |
+
"prefix": "translate English to German: "
|
36 |
+
},
|
37 |
+
"translation_en_to_fr": {
|
38 |
+
"early_stopping": true,
|
39 |
+
"max_length": 300,
|
40 |
+
"num_beams": 4,
|
41 |
+
"prefix": "translate English to French: "
|
42 |
+
},
|
43 |
+
"translation_en_to_ro": {
|
44 |
+
"early_stopping": true,
|
45 |
+
"max_length": 300,
|
46 |
+
"num_beams": 4,
|
47 |
+
"prefix": "translate English to Romanian: "
|
48 |
+
}
|
49 |
+
},
|
50 |
+
"vocab_size": 32128
|
51 |
+
}
|
emotion_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7ff32f8d430c9c257a178738b50a63a84c598bf71c3bbac9a7e46bc9107f7ea0
|
3 |
+
size 891778983
|
emotion_tokenizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:231514df0ebafbdc7fb7e32858143ae29726155eb11c661234a08fd42068b596
|
3 |
+
size 1393080
|
special_tokens_map.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>", "additional_special_tokens": ["<extra_id_0>", "<extra_id_1>", "<extra_id_2>", "<extra_id_3>", "<extra_id_4>", "<extra_id_5>", "<extra_id_6>", "<extra_id_7>", "<extra_id_8>", "<extra_id_9>", "<extra_id_10>", "<extra_id_11>", "<extra_id_12>", "<extra_id_13>", "<extra_id_14>", "<extra_id_15>", "<extra_id_16>", "<extra_id_17>", "<extra_id_18>", "<extra_id_19>", "<extra_id_20>", "<extra_id_21>", "<extra_id_22>", "<extra_id_23>", "<extra_id_24>", "<extra_id_25>", "<extra_id_26>", "<extra_id_27>", "<extra_id_28>", "<extra_id_29>", "<extra_id_30>", "<extra_id_31>", "<extra_id_32>", "<extra_id_33>", "<extra_id_34>", "<extra_id_35>", "<extra_id_36>", "<extra_id_37>", "<extra_id_38>", "<extra_id_39>", "<extra_id_40>", "<extra_id_41>", "<extra_id_42>", "<extra_id_43>", "<extra_id_44>", "<extra_id_45>", "<extra_id_46>", "<extra_id_47>", "<extra_id_48>", "<extra_id_49>", "<extra_id_50>", "<extra_id_51>", "<extra_id_52>", "<extra_id_53>", "<extra_id_54>", "<extra_id_55>", "<extra_id_56>", "<extra_id_57>", "<extra_id_58>", "<extra_id_59>", "<extra_id_60>", "<extra_id_61>", "<extra_id_62>", "<extra_id_63>", "<extra_id_64>", "<extra_id_65>", "<extra_id_66>", "<extra_id_67>", "<extra_id_68>", "<extra_id_69>", "<extra_id_70>", "<extra_id_71>", "<extra_id_72>", "<extra_id_73>", "<extra_id_74>", "<extra_id_75>", "<extra_id_76>", "<extra_id_77>", "<extra_id_78>", "<extra_id_79>", "<extra_id_80>", "<extra_id_81>", "<extra_id_82>", "<extra_id_83>", "<extra_id_84>", "<extra_id_85>", "<extra_id_86>", "<extra_id_87>", "<extra_id_88>", "<extra_id_89>", "<extra_id_90>", "<extra_id_91>", "<extra_id_92>", "<extra_id_93>", "<extra_id_94>", "<extra_id_95>", "<extra_id_96>", "<extra_id_97>", "<extra_id_98>", "<extra_id_99>"]}
|
spiece.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d60acb128cf7b7f2536e8f38a5b18a05535c9e14c7a355904270e15b0945ea86
|
3 |
+
size 791656
|
tokenizer_config.json
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
{"model_max_length": 512}
|