27 lines
863 B
Python
27 lines
863 B
Python
|
import gradio as gr
|
||
|
from transformers import AutoImageProcessor, ResNetForImageClassification
|
||
|
import torch
|
||
|
|
||
|
|
||
|
processor = AutoImageProcessor.from_pretrained("microsoft/resnet-50")
|
||
|
model = ResNetForImageClassification.from_pretrained("microsoft/resnet-50")
|
||
|
|
||
|
def image_classification(image):
|
||
|
inputs = processor(image, return_tensors="pt")
|
||
|
with torch.no_grad():
|
||
|
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=gr.Label(num_top_classes=1),
|
||
|
title = "图像分类",
|
||
|
examples = ['dog.jpeg'])
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
demo.queue(concurrency_count=3)
|
||
|
demo.launch(server_name = "0.0.0.0", server_port = 7027)
|