Spaces:
Runtime error
Runtime error
added app
Browse files
anger.png
ADDED
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AUTOGENERATED! DO NOT EDIT! File to edit: ../app.ipynb.
|
2 |
+
|
3 |
+
# %% auto 0
|
4 |
+
__all__ = ['device', 'model', 'CLASS_LABELS', 'image', 'label', 'examples', 'intf', 'classify_emotions']
|
5 |
+
|
6 |
+
# %% ../app.ipynb 2
|
7 |
+
import gradio as gr
|
8 |
+
import torch
|
9 |
+
from torch.nn.functional import softmax
|
10 |
+
import numpy as np
|
11 |
+
|
12 |
+
# %% ../app.ipynb 3
|
13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
+
|
15 |
+
model = torch.load('model.pth').to(device)
|
16 |
+
model.eval()
|
17 |
+
|
18 |
+
# %% ../app.ipynb 4
|
19 |
+
CLASS_LABELS = ['Anger', 'Disgust', 'Fear', 'Happy', 'Neutral', 'Sadness', "Surprise"]
|
20 |
+
|
21 |
+
# %% ../app.ipynb 5
|
22 |
+
def classify_emotions(im):
|
23 |
+
im = np.array(im) / 255
|
24 |
+
if len(im.shape) == 2:
|
25 |
+
im = im[..., np.newaxis]
|
26 |
+
if im.shape[-1] == 1:
|
27 |
+
im = np.concatenate((im, im, im), 2)
|
28 |
+
im = torch.tensor(im.transpose(2, 0, 1), dtype=torch.float32)
|
29 |
+
prediction = model.forward(im[np.newaxis, ...].to(device))
|
30 |
+
return dict(zip(CLASS_LABELS, *softmax(prediction, dim=1).tolist()))
|
31 |
+
|
32 |
+
|
33 |
+
# %% ../app.ipynb 6
|
34 |
+
image = gr.inputs.Image((48, 48))
|
35 |
+
label = gr.outputs.Label()
|
36 |
+
examples = ['happy.png', 'fear.png', 'anger.png']
|
37 |
+
|
38 |
+
intf = gr.Interface(fn=classify_emotions,
|
39 |
+
inputs=image,
|
40 |
+
outputs=label,
|
41 |
+
title='Emotion classification',
|
42 |
+
examples=examples)
|
43 |
+
intf.launch(inline=False)
|
fear.png
ADDED
happy.png
ADDED
model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:faf147864e48423a9559cfb6bdff95a79c1621a24c1ee46e50e1b02dae740e1b
|
3 |
+
size 44808077
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
pillow
|