import gradio as gr from transformers import AutoModelForImageClassification, AutoImageProcessor import torch import numpy as np examples = [ "shrimp.png", "adverarial.png" ] hugging_face_model = "Kaludi/food-category-classification-v2.0" model = AutoModelForImageClassification.from_pretrained(hugging_face_model) processor = AutoImageProcessor.from_pretrained(hugging_face_model) def predict(img): inputs = processor(images=img, return_tensors="pt") outputs = model(**inputs) logits = outputs.logits # ChatGPT Code: I have no idea what is going on probabilities = torch.softmax(logits, dim=1)[0].tolist() labels = model.config.id2label top_10_indices = np.argsort(probabilities)[::-1][:10] top_10_labels = [labels[i] for i in top_10_indices] top_10_probabilities = [probabilities[i] for i in top_10_indices] label_confidences = {label: prob for label, prob in zip(top_10_labels, top_10_probabilities)} return label_confidences css = ''' .gradio-container { width: 85% !important; } h1 { width: 100% !important; } p { margin-left: 30px !important; margin-right: 30px !important; font-size: 1.1rem !important; } ''' title = r"""
PwnAI is an educational event that explores adversarial machine learning. It aims to help students learn about LLM Prompt Injection and Fooling Image Classifiers.
In this demo, there are two example pictures. They may look very similar to us, but they are classified differently. Try it out yourself by submitting both pictures for inference and observing the results.
Try it out yourself by experimenting with the image on the left to see if you can cause a misclassification.
Participants will explore these concepts through interactive demos and hands-on workshops, gaining insights into applying adversarial attacks to machine learning models.
This demo runs on a free-tier CPU, so its performance is slow.
1. Bread
2. Dairy
3. Dessert
4. Egg
5. Fried Food
6. Fruit
7. Meat
8. Noodles
9. Rice
10. Seafood
11. Soup
12. Vegetable
""" Ending = r"""Woahhhh. Is this steganography???? Who knows? Come for the event to find out!
""" Footer = r""" --- Challenge Created By Chai Cheng Xun """ with gr.Blocks(css=css, title="PwnAI: Image Classifier Demo") as demo: gr.Markdown(title) gr.Markdown(description) with gr.Row(): with gr.Column(): img_file = gr.Image(label="Upload a photo to be classfied") submit = gr.Button("Submit", variant="primary") with gr.Column(): output = gr.Label() submit.click( fn=predict, inputs=img_file, outputs=output, ) gr.Examples( examples=examples, inputs=[img_file], ) gr.Markdown(Ending) gr.Markdown(Footer) demo.launch()