hugolb commited on
Commit
b8e625b
·
1 Parent(s): 8a8d594

Add application file

Browse files
Files changed (2) hide show
  1. app.py +50 -0
  2. mnist_ctf_model.h5 +3 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # Load the trained model
7
+ model = tf.keras.models.load_model("mnist_ctf_model.h5")
8
+
9
+ # Class mapping (0-9 with class 8 replaced by "CTF")
10
+ class_mapping = {0: '0', 1: '1', 2: '2', 3: 'FLAG{fh9d2f9}', 4: '4', 5: '5', 6: '6', 7: '7', 8: '3', 9: '9'}
11
+
12
+ # Function to preprocess the input image
13
+ def preprocess_image(image):
14
+ image = image.convert("L") # Convert image to grayscale
15
+ image = image.resize((28, 28)) # Resize to MNIST size
16
+ image = np.array(image) / 255.0 # Normalize pixel values
17
+ image = np.expand_dims(image, axis=0) # Add batch dimension
18
+ image = np.expand_dims(image, axis=-1) # Add channel dimension
19
+ return image
20
+
21
+ # Prediction function
22
+ def predict(image):
23
+ # Preprocess the image
24
+ image = preprocess_image(image)
25
+
26
+ # Get the model's raw prediction (logits)
27
+ logits = model.predict(image)
28
+
29
+ # Convert logits to probabilities
30
+ probabilities = tf.nn.softmax(logits)
31
+
32
+ # Get the predicted class index
33
+ predicted_class = np.argmax(probabilities)
34
+
35
+ # Get the class name from the mapping
36
+ class_name = class_mapping[predicted_class]
37
+
38
+ return class_name
39
+
40
+ # Gradio interface
41
+ iface = gr.Interface(
42
+ fn=predict, # Function to call for prediction
43
+ inputs=gr.Image(type="pil", label="Upload an MNIST-like Image"), # Input: Image upload
44
+ outputs=gr.Textbox(label="Predicted Class"), # Output: Text showing predicted class
45
+ title="Vault Challenge 1 - FGSM", # Title of the interface
46
+ description="Upload an image, and the model will predict the digit. Try to fool the model into predicting 'CTF' using FGSM!. tips: use any image from the MNIST dataset, ranging from 0-9, except for 3. The goal is to fool the mode into predicting the digit as a 3, and you will get the flag. Ajust the epsilon parameter ;) "
47
+ )
48
+
49
+ # Launch the Gradio interface
50
+ iface.launch()
mnist_ctf_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e15409bf99089ed7618afd9876fbe1a27205600e64b9afb92fec267c86832b31
3
+ size 1167392