resnet-50/app.py

40 lines
1.2 KiB
Python

import gradio as gr
from transformers import AutoImageProcessor, ResNetForImageClassification
import torch
from gradio.themes.utils import sizes
theme = gr.themes.Default(radius_size=sizes.radius_none).set(
block_label_text_color = '#4D63FF',
block_title_text_color = '#4D63FF',
button_primary_text_color = '#4D63FF',
button_primary_background_fill='#FFFFFF',
button_primary_border_color='#4D63FF',
button_primary_background_fill_hover='#EDEFFF',
)
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),
css = "footer {visibility: hidden}",
allow_flagging = "never",
theme = theme,
examples = ['dog.jpeg'])
if __name__ == "__main__":
demo.queue(concurrency_count=10)
demo.launch(server_name = "0.0.0.0")