Spaces:
Sleeping
Sleeping
Add files
Browse files- app.py +54 -0
- attack_image_tensor.pt +3 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
from torchvision import models, transforms
|
5 |
+
from PIL import Image
|
6 |
+
import requests
|
7 |
+
|
8 |
+
# Load the pre-trained MobileNetV2 model from torchvision
|
9 |
+
model = models.mobilenet_v2(pretrained=True)
|
10 |
+
device = torch.device("mps")
|
11 |
+
model.to(device)
|
12 |
+
model.eval() # Set model to evaluation mode
|
13 |
+
|
14 |
+
# Modify the class labels
|
15 |
+
url = "https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
|
16 |
+
response = requests.get(url)
|
17 |
+
class_labels = response.text.splitlines()
|
18 |
+
class_labels[282] = "FLAG{3883}" # Modify class name to "FLAG{3883}"
|
19 |
+
|
20 |
+
# Preprocessing function to prepare the image
|
21 |
+
preprocess = transforms.Compose([
|
22 |
+
transforms.Resize(256),
|
23 |
+
transforms.CenterCrop(224),
|
24 |
+
transforms.ToTensor(),
|
25 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
26 |
+
])
|
27 |
+
|
28 |
+
# Function to preprocess the input image
|
29 |
+
def preprocess_image(image):
|
30 |
+
image = preprocess(image).unsqueeze(0) # Add batch dimension
|
31 |
+
return image.to(device) # Move image to the same device as the model
|
32 |
+
|
33 |
+
# Prediction function
|
34 |
+
def predict(image):
|
35 |
+
# Load the input file
|
36 |
+
reloaded_img_tensor = torch.load(image).to(torch.device("mps"))
|
37 |
+
|
38 |
+
# Make predictions
|
39 |
+
output = model(reloaded_img_tensor)
|
40 |
+
predicted_label = class_labels[output.argmax(1, keepdim=True).item()]
|
41 |
+
|
42 |
+
return predicted_label
|
43 |
+
|
44 |
+
# Gradio interface
|
45 |
+
iface = gr.Interface(
|
46 |
+
fn=predict, # Function to call for prediction
|
47 |
+
inputs=gr.File(label="Upload a .pt file"), # Input: .pt file upload
|
48 |
+
outputs=gr.Textbox(label="Predicted Class"), # Output: Text showing predicted class
|
49 |
+
title="Vault Challenge 3 - CW", # Title of the interface
|
50 |
+
description="Upload an image, and the model will predict the class. Try to fool the model into predicting the FLAG using C&W!"
|
51 |
+
)
|
52 |
+
|
53 |
+
# Launch the Gradio interface
|
54 |
+
iface.launch()
|
attack_image_tensor.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:21ef127747638c32fc1895fccd85a4765c0b14a4df7c85cfb69a41b0000ca4be
|
3 |
+
size 603352
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
torchvision
|
3 |
+
Pillow
|
4 |
+
requests
|