File size: 1,066 Bytes
b069732
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import ViTForImageClassification, ViTImageProcessor
from PIL import Image
import gradio as gr

model = ViTForImageClassification.from_pretrained('vit-hateful-gesture-classification')
processor = ViTImageProcessor.from_pretrained('google/vit-base-patch16-224')

class_names = ['cut_throat_gesture', 'finger_gun_to_the_head', 'middle_finger', 'slanted_eyes_gesture', 'swastika']

def predict(image):
    inputs = processor(images=image, return_tensors="pt")
    
    with torch.no_grad():
        outputs = model(**inputs).logits
    
    predicted_class_idx = outputs.argmax(-1).item()
    predicted_class = class_names[predicted_class_idx]
    
    return predicted_class

iface = gr.Interface(fn=predict, 
                     inputs=gr.inputs.Image(type="pil"), 
                     outputs=gr.outputs.Label(num_top_classes=1),
                     title="Hateful Gesture Detection",
                     description="Upload an image to classify hateful gestures or symbols")

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