prithivMLmods commited on
Commit
ad5c6b4
·
verified ·
1 Parent(s): 974c881

Create emotion_classification.py

Browse files
Files changed (1) hide show
  1. emotion_classification.py +40 -0
emotion_classification.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import spaces
3
+ from transformers import AutoImageProcessor
4
+ from transformers import SiglipForImageClassification
5
+ from PIL import Image
6
+ import torch
7
+
8
+ # Load model and processor
9
+ model_name = "prithivMLmods/Facial-Emotion-Detection-SigLIP2"
10
+ model = SiglipForImageClassification.from_pretrained(model_name)
11
+ processor = AutoImageProcessor.from_pretrained(model_name)
12
+
13
+ @spaces.GPU
14
+ def emotion_classification(image):
15
+ """Predicts facial emotion classification for an image."""
16
+ image = Image.fromarray(image).convert("RGB")
17
+ inputs = processor(images=image, return_tensors="pt")
18
+
19
+ with torch.no_grad():
20
+ outputs = model(**inputs)
21
+ logits = outputs.logits
22
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
23
+
24
+ labels = {
25
+ "0": "Ahegao", "1": "Angry", "2": "Happy", "3": "Neutral",
26
+ "4": "Sad", "5": "Surprise"
27
+ }
28
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
29
+
30
+ return predictions
31
+
32
+ if __name__ == "__main__":
33
+ iface = gr.Interface(
34
+ fn=emotion_classification,
35
+ inputs=gr.Image(type="numpy"),
36
+ outputs=gr.Label(label="Prediction Scores"),
37
+ title="Facial Emotion Detection",
38
+ description="Upload an image to classify the facial emotion."
39
+ )
40
+ iface.launch()