File size: 2,210 Bytes
c73514d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import ViTFeatureExtractor, ViTForImageClassification
from PIL import Image
import requests
ts_url = 'https://static.promediateknologi.id/crop/0x0:0x0/0x0/webp/photo/p2/01/2023/08/10/taylor-swift-3169402579.png'
ts_image = Image.open(requests.get(ts_url, stream=True).raw)
pg_url = "https://media.vogue.co.uk/photos/60f888a382e60565201c7cf4/2:3/w_2560%2Cc_limit/prince-Grorge-eigth-birthday-portrait.jpg"
pg_image = Image.open(requests.get(pg_url, stream=True).raw)
img_url = "https://thumbs.dreamstime.com/b/old-man-20313005.jpg"
img_image = Image.open(requests.get(img_url, stream=True).raw)

def age_emot_classifier(input_image):

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

    model_emot = ViTForImageClassification.from_pretrained("yangswei/visual-emotion-classification")
    transforms_emot = ViTFeatureExtractor.from_pretrained("yangswei/visual-emotion-classification")

    # Transform our image and pass it through the model
    inputs_age = transforms_age(input_image, return_tensors='pt')
    output_age = model_age(**inputs_age)

    inputs_emot = transforms_emot(input_image, return_tensors='pt')
    output_emot = model_emot(**inputs_emot)

    # Predicted Class probabilities
    proba_age = output_age.logits.softmax(1)
    proba_emot = output_emot.logits.softmax(1)

    # Predicted Classes With Confidences
    labels_age = model_age.config.id2label
    confidences_age = {labels_age[i]: proba_age[0][i].item() for i in range(len(labels_age))}

    labels_emot = model_emot.config.id2label
    confidences_emot = {labels_emot[i]: proba_emot[0][i].item() for i in range(len(labels_emot))}

    return confidences_age, confidences_emot

output_age = gr.Label(num_top_classes=9, label="Age Prediction")
output_emotion = gr.Label(num_top_classes=8, label="Emotion Prediction")

with gr.Blocks(theme=gr.themes.Glass()) as demo:
    gr.Interface(fn=age_emot_classifier, inputs="image", outputs=[output_age, output_emotion],
                 examples=[ts_image, pg_image, img_image])

demo.launch()