File size: 1,500 Bytes
2fe1bcc
 
 
 
 
 
 
 
 
 
4977ce5
2fe1bcc
 
 
 
 
 
 
 
9729ed8
2fe1bcc
4977ce5
2fe1bcc
 
 
 
 
4977ce5
2fe1bcc
 
 
 
4977ce5
2fe1bcc
0e76714
4977ce5
2fe1bcc
 
 
 
9729ed8
2fe1bcc
 
 
 
 
 
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
from PIL import Image
import gradio as gr
from transformers import ViTFeatureExtractor, ViTForImageClassification
import torch

# Init model, transforms
model = ViTForImageClassification.from_pretrained('nateraw/vit-age-classifier')
transforms = ViTFeatureExtractor.from_pretrained('nateraw/vit-age-classifier')

def predict(im):
    labels = {0:"0-2", 1: "3-9" , 2: "10-19", 3: "20-29", 4: "30-39", 5: "40-49", 6: "50-59", 7:"60-69",8:"more than 70"} 
# Transform our image and pass it through the model
    inputs = transforms(im, return_tensors='pt')
    output = model(**inputs)

# Predicted Class probabilities
    proba = output.logits.softmax(1)

# Predicted Classes
    preds = proba.argmax(1)
    values, indices = torch.topk(proba, k=5)
    
    

    return {labels[i.item()]: v.item() for i, v in zip(indices.numpy()[0], values.detach().numpy()[0])}

inputs = [
    gr.inputs.Image(type="pil", label="Input Image")
]



title = "ViT-Age-Classification"
description = "ViT-Age-Classification is used to categorize an individual's age using images"
article = " <a href='https://huggingface.co/nateraw/vit-age-classifier'>ViT Age Classification Model Repo on Hugging Face Model Hub</a>"
examples = ["stock_baby.webp","stock_teen.webp","stock_guy.jpg","stock_old_woman.jpg"]

gr.Interface(
    predict,
    inputs,
    outputs = 'label',
    title=title,
    description=description,
    article=article,
    examples=examples,
    theme="huggingface",
).launch(debug=True, enable_queue=True)