image_classification/app.py

37 lines
1.4 KiB
Python

import gradio as gr
from transformers import BeitImageProcessor, BeitForImageClassification
def inference(img):
pretrained_model_path = "beit-base-patch16-224-pt22k-ft22k"
processor = BeitImageProcessor.from_pretrained(pretrained_model_path)
model = BeitForImageClassification.from_pretrained(pretrained_model_path)
inputs = processor(images=img, return_tensors="pt")
outputs = model(**inputs)
logits = outputs.logits
# model predicts one of the 21,841 ImageNet-22k classes
predicted_class_idx = logits.argmax(-1).item()
# print("Predicted class:", model.config.id2label[predicted_class_idx])
return model.config.id2label[predicted_class_idx]
title = "Image classification:beit-base-patch16-224-pt22k-ft22k"
description = "这是beit-base-patch16-224-pt22k-ft22k的Gradio Demo。用于图像分类。上传你想要的图像或者点击下面的示例来加载它。"
article = "<p style='text-align: center'><a href='https://github.com/bryandlee/animegan2-pytorch' target='_blank'>Github Repo Pytorch</a></p> <center><img src='https://visitor-badge.glitch.me/badge?page_id=akhaliq_animegan' alt='visitor badge'></center></p>"
examples = [['example_cat.jpg'], ['Masahiro.png']]
demo = gr.Interface(
fn=inference,
inputs=[gr.inputs.Image(type="pil")],
outputs=gr.outputs.Textbox(),
title=title,
description=description,
article=article,
examples=examples)
demo.launch(server_name="0.0.0.0")