spuuntries
commited on
Commit
•
7ef50d0
1
Parent(s):
4ba204c
feat: add flag
Browse files- .gitignore +1 -0
- app.py +11 -8
- reference_image.jpeg +0 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
best_model.pth
|
app.py
CHANGED
@@ -24,11 +24,11 @@ def set_seed(seed):
|
|
24 |
|
25 |
set_seed(42)
|
26 |
|
|
|
27 |
device = "cpu"
|
28 |
config = ViTConfig.from_pretrained("google/vit-base-patch16-224")
|
29 |
config.num_labels = 2 # Binary classification
|
30 |
|
31 |
-
# Download the model file
|
32 |
model_url = "https://huggingface.co/spuun/yummy-paws/resolve/main/model.pth"
|
33 |
model_path = "best_model.pth"
|
34 |
|
@@ -37,16 +37,14 @@ if not os.path.exists(model_path):
|
|
37 |
with open(model_path, "wb") as f:
|
38 |
f.write(response.content)
|
39 |
|
40 |
-
# Load the trained model
|
41 |
model = ViTForImageClassification.from_pretrained(
|
42 |
model_path,
|
43 |
config=config,
|
44 |
-
ignore_mismatched_sizes=True,
|
45 |
)
|
46 |
model.classifier = nn.Linear(model.config.hidden_size, 2)
|
47 |
model.to(device)
|
48 |
|
49 |
-
# Download the reference image
|
50 |
reference_image_url = (
|
51 |
"https://huggingface.co/spuun/yummy-paws/resolve/main/images%20(15).jpeg"
|
52 |
)
|
@@ -57,7 +55,6 @@ if not os.path.exists(reference_image_path):
|
|
57 |
with open(reference_image_path, "wb") as f:
|
58 |
f.write(response.content)
|
59 |
|
60 |
-
# Load the reference image for SSIM comparison
|
61 |
reference_image = Image.open(reference_image_path)
|
62 |
|
63 |
|
@@ -92,15 +89,21 @@ def predict_and_compare(image):
|
|
92 |
predicted_class = class_names[predicted_class_index]
|
93 |
probability = probabilities[predicted_class_index].item()
|
94 |
|
95 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
96 |
|
97 |
|
98 |
iface = gr.Interface(
|
99 |
fn=predict_and_compare,
|
100 |
inputs=gr.Image(type="pil"),
|
101 |
outputs="text",
|
102 |
-
title="Image
|
103 |
-
description="Upload
|
|
|
104 |
)
|
105 |
|
106 |
iface.launch()
|
|
|
24 |
|
25 |
set_seed(42)
|
26 |
|
27 |
+
flag = os.environ["FLAG"] if "FLAG" in os.environ else "fakeflag{placeholder}"
|
28 |
device = "cpu"
|
29 |
config = ViTConfig.from_pretrained("google/vit-base-patch16-224")
|
30 |
config.num_labels = 2 # Binary classification
|
31 |
|
|
|
32 |
model_url = "https://huggingface.co/spuun/yummy-paws/resolve/main/model.pth"
|
33 |
model_path = "best_model.pth"
|
34 |
|
|
|
37 |
with open(model_path, "wb") as f:
|
38 |
f.write(response.content)
|
39 |
|
|
|
40 |
model = ViTForImageClassification.from_pretrained(
|
41 |
model_path,
|
42 |
config=config,
|
43 |
+
ignore_mismatched_sizes=True,
|
44 |
)
|
45 |
model.classifier = nn.Linear(model.config.hidden_size, 2)
|
46 |
model.to(device)
|
47 |
|
|
|
48 |
reference_image_url = (
|
49 |
"https://huggingface.co/spuun/yummy-paws/resolve/main/images%20(15).jpeg"
|
50 |
)
|
|
|
55 |
with open(reference_image_path, "wb") as f:
|
56 |
f.write(response.content)
|
57 |
|
|
|
58 |
reference_image = Image.open(reference_image_path)
|
59 |
|
60 |
|
|
|
89 |
predicted_class = class_names[predicted_class_index]
|
90 |
probability = probabilities[predicted_class_index].item()
|
91 |
|
92 |
+
return f"""{'SUCCESSFULLY AUTHENTICATED!!\nFLAG: '+flag if ssim_value>0.9 and predicted_class == "True" else "FAILED TO AUTHENTICATE :("}
|
93 |
+
=====================
|
94 |
+
|
95 |
+
Predicted: {predicted_class}
|
96 |
+
Probability: {probability:.4f}
|
97 |
+
SSIM with reference: {ssim_value:.4f}"""
|
98 |
|
99 |
|
100 |
iface = gr.Interface(
|
101 |
fn=predict_and_compare,
|
102 |
inputs=gr.Image(type="pil"),
|
103 |
outputs="text",
|
104 |
+
title="Image authentication",
|
105 |
+
description="Upload your image here to be authenticated!",
|
106 |
+
allow_flagging="never",
|
107 |
)
|
108 |
|
109 |
iface.launch()
|
reference_image.jpeg
ADDED