Upload 2 files
Browse files- app.py +68 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import CLIPProcessor, CLIPModel
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
print("Loading Classification Model...")
|
| 7 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
| 8 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
| 9 |
+
model.eval()
|
| 10 |
+
print("Model Loaded successfully!")
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
LABELS = [
|
| 14 |
+
"an official academic certificate or diploma",
|
| 15 |
+
"a government ID card, passport, or driving license",
|
| 16 |
+
"a random photograph of a person, nature, or objects",
|
| 17 |
+
"a screenshot of a website or meme"
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
def verify_document_class(image: Image.Image):
|
| 21 |
+
"""
|
| 22 |
+
Returns:
|
| 23 |
+
- is_valid (bool)
|
| 24 |
+
- message (str)
|
| 25 |
+
- best_label (str)
|
| 26 |
+
- confidence (%) (float)
|
| 27 |
+
- probabilities table (list)
|
| 28 |
+
"""
|
| 29 |
+
if image is None:
|
| 30 |
+
return False, "No image provided", "", 0.0, []
|
| 31 |
+
|
| 32 |
+
image = image.convert("RGB")
|
| 33 |
+
|
| 34 |
+
inputs = processor(text=LABELS, images=image, return_tensors="pt", padding=True)
|
| 35 |
+
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
outputs = model(**inputs)
|
| 38 |
+
probs = outputs.logits_per_image.softmax(dim=1)[0].cpu().numpy()
|
| 39 |
+
|
| 40 |
+
best_idx = int(probs.argmax())
|
| 41 |
+
best_label = LABELS[best_idx]
|
| 42 |
+
confidence = float(probs[best_idx] * 100.0)
|
| 43 |
+
|
| 44 |
+
# Gatekeeper rule
|
| 45 |
+
is_valid = best_idx in (0, 1)
|
| 46 |
+
message = "Valid Document Class" if is_valid else "Invalid image type. Please upload a certificate or ID."
|
| 47 |
+
|
| 48 |
+
# Build a simple table for display
|
| 49 |
+
table = [{"label": LABELS[i], "probability_%": float(probs[i] * 100.0)} for i in range(len(LABELS))]
|
| 50 |
+
|
| 51 |
+
return is_valid, message, best_label, confidence, table
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
demo = gr.Interface(
|
| 55 |
+
fn=verify_document_class,
|
| 56 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 57 |
+
outputs=[
|
| 58 |
+
gr.Checkbox(label="Is Valid Document?"),
|
| 59 |
+
gr.Textbox(label="Message"),
|
| 60 |
+
gr.Textbox(label="Best Match Label"),
|
| 61 |
+
gr.Number(label="Confidence (%)"),
|
| 62 |
+
gr.JSON(label="All Probabilities"),
|
| 63 |
+
],
|
| 64 |
+
title="Document Class Verifier (CLIP Gatekeeper)",
|
| 65 |
+
description="Uploads an image and checks whether it looks like a certificate/diploma or ID vs random photo/screenshot."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
pillow
|