File size: 1,224 Bytes
7ffd1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8efe80e
7ffd1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
import torch
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification

# Load processor and model from Hugging Face Hub
processor = AutoImageProcessor.from_pretrained("Dc-4nderson/vit-emotion-classifier")
model = AutoModelForImageClassification.from_pretrained("Dc-4nderson/vit-emotion-classifier")

# Inference function
def classify_emotion(image: Image.Image) -> str:
    # Ensure image is in RGB mode
    image = image.convert("RGB")

    # Preprocess and predict
    inputs = processor(images=image, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)
        pred = torch.argmax(outputs.logits, dim=1).item()

    label = model.config.id2label[pred]
    return f"🧠 Predicted Emotion: **{label}**"

# Gradio interface
demo = gr.Interface(
    fn=classify_emotion,
    inputs=gr.Image(type="pil", label="Upload or Take a Picture"),
    outputs=gr.Textbox(label="Predicted Emotion"),
    title="ViT Emotion Classifier",
    description="Upload or take a photo to predict emotion using a Vision Transformer (ViT) model trained by Dc-4nderson.",
    allow_flagging="never"
)

# Launch
if __name__ == "__main__":
    demo.launch()