File size: 2,977 Bytes
bc68a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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"""
<h1>GDSC: PwnAI Image Classifier Demo</h1>
"""
description = r"""
<p>PwnAI is an educational event that explores adversarial machine learning. It aims to help students learn about LLM Prompt Injection and Fooling Image Classifiers.</p>
<p>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.</p>
<p>Try it out yourself by experimenting with the image on the left to see if you can cause a misclassification.</p>
<h2>What to Expect</h2>
<p>Participants will explore these concepts through interactive demos and hands-on workshops, gaining insights into applying adversarial attacks to machine learning models.</p>
<h2>NOTE</h2>
<p>This demo runs on a free-tier CPU, so its performance is slow.</p>
<h2>What can this AI Classify</h2>
<p>1. Bread</p>
<p>2. Dairy</p>
<p>3. Dessert</p>
<p>4. Egg</p>
<p>5. Fried Food</p>
<p>6. Fruit</p>
<p>7. Meat</p>
<p>8. Noodles</p>
<p>9. Rice</p>
<p>10. Seafood</p>
<p>11. Soup</p>
<p>12. Vegetable</p>
"""

Ending = r"""
<p>Woahhhh. Is this steganography???? Who knows? Come for the event to find out!</p>
"""

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()