QiaoNPC commited on
Commit
bc68a98
1 Parent(s): 85ee211

Initial Commit

Browse files
Files changed (6) hide show
  1. README.md +9 -11
  2. adverarial.png +0 -0
  3. app.py +37 -0
  4. requirements.txt +4 -0
  5. shrimp.png +0 -0
  6. testing.py +107 -0
README.md CHANGED
@@ -1,12 +1,10 @@
1
- ---
2
- title: PwnAI Image Classification Demo
3
- emoji: 😻
4
- colorFrom: blue
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 4.25.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
1
+ # PwnAI Demo
 
 
 
 
 
 
 
 
 
2
 
3
+ ## Overview
4
+ PwnAI is an educational event that explores adversarial machine learning techniques, specifically focusing on attacking Image Classifiers and Language Model (LM) Prompt Injections. This repository contains a demo showcasing how adversarial attacks can be applied to image classifiers.
5
+
6
+ ## Demo Description
7
+ The demo includes two example pictures that appear very similar but are classified differently. Users can interact with the demo by submitting both pictures for inference, allowing them to observe how the machine learning model's classification can be manipulated through adversarial attacks. Users can also submit their own pictures to play around.
8
+
9
+ ## Performance Note
10
+ Please note that this demo runs on a free-tier CPU, so its performance may be slow.
adverarial.png ADDED
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
3
+ import torch
4
+ import numpy as np
5
+
6
+ examples = [
7
+ "shrimp.png",
8
+ "adverarial.png"
9
+ ]
10
+
11
+ hugging_face_model = "Kaludi/food-category-classification-v2.0"
12
+ model = AutoModelForImageClassification.from_pretrained(hugging_face_model)
13
+ processor = AutoImageProcessor.from_pretrained(hugging_face_model)
14
+
15
+ def predict(img):
16
+ inputs = processor(images=img, return_tensors="pt")
17
+ outputs = model(**inputs)
18
+ logits = outputs.logits
19
+
20
+
21
+ # ChatGPT Code: I have no idea what is going on
22
+ probabilities = torch.softmax(logits, dim=1)[0].tolist()
23
+ labels = model.config.id2label
24
+ top_10_indices = np.argsort(probabilities)[::-1][:10]
25
+ top_10_labels = [labels[i] for i in top_10_indices]
26
+ top_10_probabilities = [probabilities[i] for i in top_10_indices]
27
+ label_confidences = {label: prob for label, prob in zip(top_10_labels, top_10_probabilities)}
28
+ return label_confidences
29
+
30
+ demo = gr.Interface(
31
+ fn=predict,
32
+ inputs=gr.Image(),
33
+ outputs=gr.Label(),
34
+ examples=examples
35
+ )
36
+
37
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ numpy
shrimp.png ADDED
testing.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForImageClassification, AutoImageProcessor
3
+ import torch
4
+ import numpy as np
5
+
6
+ examples = [
7
+ "shrimp.png",
8
+ "adverarial.png"
9
+ ]
10
+
11
+ hugging_face_model = "Kaludi/food-category-classification-v2.0"
12
+ model = AutoModelForImageClassification.from_pretrained(hugging_face_model)
13
+ processor = AutoImageProcessor.from_pretrained(hugging_face_model)
14
+
15
+ def predict(img):
16
+ inputs = processor(images=img, return_tensors="pt")
17
+ outputs = model(**inputs)
18
+ logits = outputs.logits
19
+
20
+
21
+ # ChatGPT Code: I have no idea what is going on
22
+ probabilities = torch.softmax(logits, dim=1)[0].tolist()
23
+ labels = model.config.id2label
24
+ top_10_indices = np.argsort(probabilities)[::-1][:10]
25
+ top_10_labels = [labels[i] for i in top_10_indices]
26
+ top_10_probabilities = [probabilities[i] for i in top_10_indices]
27
+ label_confidences = {label: prob for label, prob in zip(top_10_labels, top_10_probabilities)}
28
+ return label_confidences
29
+
30
+ css = '''
31
+ .gradio-container {
32
+ width: 85% !important;
33
+ }
34
+ h1 {
35
+ width: 100% !important;
36
+ }
37
+ p {
38
+ margin-left: 30px !important;
39
+ margin-right: 30px !important;
40
+ font-size: 1.1rem !important;
41
+ }
42
+ '''
43
+
44
+ title = r"""
45
+ <h1>GDSC: PwnAI Image Classifier Demo</h1>
46
+ """
47
+ description = r"""
48
+ <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>
49
+ <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>
50
+ <p>Try it out yourself by experimenting with the image on the left to see if you can cause a misclassification.</p>
51
+ <h2>What to Expect</h2>
52
+ <p>Participants will explore these concepts through interactive demos and hands-on workshops, gaining insights into applying adversarial attacks to machine learning models.</p>
53
+ <h2>NOTE</h2>
54
+ <p>This demo runs on a free-tier CPU, so its performance is slow.</p>
55
+ <h2>What can this AI Classify</h2>
56
+ <p>1. Bread</p>
57
+ <p>2. Dairy</p>
58
+ <p>3. Dessert</p>
59
+ <p>4. Egg</p>
60
+ <p>5. Fried Food</p>
61
+ <p>6. Fruit</p>
62
+ <p>7. Meat</p>
63
+ <p>8. Noodles</p>
64
+ <p>9. Rice</p>
65
+ <p>10. Seafood</p>
66
+ <p>11. Soup</p>
67
+ <p>12. Vegetable</p>
68
+ """
69
+
70
+ Ending = r"""
71
+ <p>Woahhhh. Is this steganography???? Who knows? Come for the event to find out!</p>
72
+ """
73
+
74
+ Footer = r"""
75
+ ---
76
+ Challenge Created By Chai Cheng Xun
77
+ """
78
+
79
+ with gr.Blocks(css=css, title="PwnAI: Image Classifier Demo") as demo:
80
+ gr.Markdown(title)
81
+ gr.Markdown(description)
82
+
83
+ with gr.Row():
84
+ with gr.Column():
85
+ img_file = gr.Image(label="Upload a photo to be classfied")
86
+ submit = gr.Button("Submit", variant="primary")
87
+
88
+
89
+ with gr.Column():
90
+ output = gr.Label()
91
+
92
+ submit.click(
93
+ fn=predict,
94
+ inputs=img_file,
95
+ outputs=output,
96
+ )
97
+
98
+
99
+ gr.Examples(
100
+ examples=examples,
101
+ inputs=[img_file],
102
+ )
103
+
104
+ gr.Markdown(Ending)
105
+ gr.Markdown(Footer)
106
+
107
+ demo.launch()