Delete app.py
Browse files
app.py
DELETED
@@ -1,96 +0,0 @@
|
|
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_vgg.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 |
-
main_model.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|