prithivMLmods commited on
Commit
b71c684
·
verified ·
1 Parent(s): ba59911

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoImageProcessor
3
+ from transformers import SiglipForImageClassification
4
+ from transformers.image_utils import load_image
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
+ def emotion_classification(image):
14
+ """Predicts facial emotion classification for an image."""
15
+ image = Image.fromarray(image).convert("RGB")
16
+ inputs = processor(images=image, return_tensors="pt")
17
+
18
+ with torch.no_grad():
19
+ outputs = model(**inputs)
20
+ logits = outputs.logits
21
+ probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist()
22
+
23
+ labels = {
24
+ "0": "Ahegao", "1": "Angry", "2": "Happy", "3": "Neutral",
25
+ "4": "Sad", "5": "Surprise"
26
+ }
27
+ predictions = {labels[str(i)]: round(probs[i], 3) for i in range(len(probs))}
28
+
29
+ return predictions
30
+
31
+ # Create Gradio interface
32
+ iface = gr.Interface(
33
+ fn=emotion_classification,
34
+ inputs=gr.Image(type="numpy"),
35
+ outputs=gr.Label(label="Prediction Scores"),
36
+ title="Facial Emotion Detection",
37
+ description="Upload an image to classify the facial emotion."
38
+ )
39
+
40
+ # Launch the app
41
+ if __name__ == "__main__":
42
+ iface.launch(ssr_mode=False)