import gradio as gr
import torch
from transformers import ViTFeatureExtractor, ViTForImageClassification


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

def image_classification(image):
    inputs = transforms(image, return_tensors='pt')
    logits = model(**inputs).logits

    predicted_label = logits.argmax(-1).item()
    return model.config.id2label[predicted_label]


demo = gr.Interface(fn=image_classification, 
                    inputs=gr.Image(), 
                    outputs='text',
                    title = "年龄划分",
                    examples = ['dog.jpeg'])


if __name__ == "__main__":
    demo.queue(concurrency_count=3)
    demo.launch(server_name = "0.0.0.0", server_port = 7027)
