Spaces:
Sleeping
Sleeping
| from transformers import AutoModelForImageClassification, AutoImageProcessor | |
| import torch | |
| import gradio as gr | |
| model = AutoModelForImageClassification.from_pretrained("stnleyyg/visual-emotion-analyser", num_labels=8) | |
| processor = AutoImageProcessor.from_pretrained("stnleyyg/visual-emotion-analyser") | |
| def emotion_analyser(image): | |
| inputs = processor(image, return_tensors="pt") | |
| id2label = { | |
| "0": "anger", | |
| "1": "contempt", | |
| "2": "disgust", | |
| "3": "fear", | |
| "4": "happy", | |
| "5": "neutral", | |
| "6": "sad", | |
| "7": "surprise" | |
| } | |
| with torch.no_grad(): | |
| logits = model(**inputs).logits | |
| probability = logits.softmax(-1) | |
| predicted_class_idx = probability.argmax(-1).item() | |
| predicted_label = id2label[str(predicted_class_idx)] | |
| return f"This person emotion is {predicted_label}" | |
| demo_app = gr.Interface( | |
| fn=emotion_analyser, | |
| inputs=gr.Image(type="pil"), | |
| outputs=gr.TextArea() | |
| ) | |
| demo_app.launch() |