import gradio as gr from transformers import ImageClassificationPipeline, PerceiverForImageClassificationConvProcessing, PerceiverFeatureExtractor import torch torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg') torch.hub.download_url_to_file('https://storage.googleapis.com/perceiver_io/dalmation.jpg', 'dog.jpg') feature_extractor = PerceiverFeatureExtractor.from_pretrained("deepmind/vision-perceiver-conv") model = PerceiverForImageClassificationConvProcessing.from_pretrained("deepmind/vision-perceiver-conv") image_pipe = ImageClassificationPipeline(model=model, feature_extractor=feature_extractor) with open('labels_translation.txt') as f: labels_translation = [x.strip() for x in f.readlines()] with open('english_labels.txt') as f: english_labels = [x.strip() for x in f.readlines()] english_to_spanish = {a: b for a, b in zip(english_labels, labels_translation)} def classify_image(image): results = image_pipe(image) # convert to format Gradio expects output = {} for prediction in results: predicted_label = english_to_spanish[prediction['label']] score = prediction['score'] output[predicted_label] = score return output image = gr.inputs.Image(type="pil") label = gr.outputs.Label(num_top_classes=5) examples = [["cats.jpg"], ["dog.jpg"]] title = "Interactive demo: Perceiver for image classification" description = "Demo for classifying images with Perceiver IO. To use it, simply upload an image or use the example images below and click 'submit' to let the model predict the 5 most probable ImageNet classes. Results will show up in a few seconds. This is based on this space: This space is based on: https://huggingface.co/spaces/nielsr/perceiver-image-classification, image net labels are machine translated from english to spanish." article = "
Perceiver IO: A General Architecture for Structured Inputs & Outputs | Official blog
" gr.Interface(fn=classify_image, inputs=image, outputs=label, title=title, description=description, examples=examples, enable_queue=True).launch(debug=True)