File size: 2,362 Bytes
f583d37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import torch
import gradio as gr
from PIL import Image
from huggingface_hub import hf_hub_download
import importlib.util
from torchvision import transforms

# Load model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Download model code
class_path = hf_hub_download(repo_id="PerceptCLIP/PerceptCLIP_Emotions", filename="modeling.py")
spec = importlib.util.spec_from_file_location("modeling", class_path)
modeling = importlib.util.module_from_spec(spec)
spec.loader.exec_module(modeling)

# Initialize the model
from modeling import clip_lora_model
model = clip_lora_model().to(device)

# Load pretrained weights
model_path = hf_hub_download(repo_id="PerceptCLIP/PerceptCLIP_Emotions", filename="perceptCLIP_Emotions.pth")
model.load_state_dict(torch.load(model_path, map_location=device))
model.eval()

# Emotion label mapping
idx2label = {
    0: "amusement",
    1: "awe",
    2: "contentment",
    3: "excitement",
    4: "anger",
    5: "disgust",
    6: "fear",
    7: "sadness"
}

# Emoji mapping
emotion_emoji = {
    "amusement": "πŸ˜‚",
    "awe": "😲",
    "contentment": "😊",
    "excitement": "πŸ˜ƒ",
    "anger": "😠",
    "disgust": "🀒",
    "fear": "😱",
    "sadness": "😞"
}

# Image preprocessing
def emo_preprocess(image):
    transform = transforms.Compose([
        transforms.Resize(224),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=(0.4814, 0.4578, 0.4082), std=(0.2686, 0.2613, 0.2758)),
    ])
    return transform(image).unsqueeze(0).to(device)

# Inference function
def predict_emotion(image):
    image = Image.open(image).convert("RGB")
    image = emo_preprocess(image)

    with torch.no_grad():
        outputs = model(image)
        predicted = outputs.argmax(1).item()

    emotion = idx2label[predicted]
    emoji = emotion_emoji.get(emotion, "❓")  # Default to "?" if no emoji found
    return f"{emotion} {emoji}"

# Create Gradio interface
iface = gr.Interface(
    fn=predict_emotion,
    inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
    outputs=gr.outputs.Textbox(label="Emotion + Emoji"),
    title="PerceptCLIP-Emotions",
    description="This model predicts the emotion evoked by an image and returns the corresponding emoji along with the emotion name."
)

if __name__ == "__main__":
    iface.launch()