ANISA09 commited on
Commit
a392ba6
·
verified ·
1 Parent(s): 2c4c482

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import pytesseract
4
+ import cv2
5
+ from PIL import Image
6
+ import numpy as np
7
+
8
+ # -------------------------------------------------------------
9
+ # Load Model
10
+ # -------------------------------------------------------------
11
+ # Replace with your fine-tuned model on Hugging Face Hub
12
+ MODEL_ID = "google/vit-base-patch16-224-in21k"
13
+
14
+ classifier = pipeline("image-classification", model=MODEL_ID)
15
+
16
+ # -------------------------------------------------------------
17
+ # Certificate Verification Function
18
+ # -------------------------------------------------------------
19
+ def verify_certificate(image):
20
+ # Convert to RGB if needed
21
+ if not isinstance(image, Image.Image):
22
+ image = Image.fromarray(image)
23
+ image = image.convert("RGB")
24
+
25
+ # 1️⃣ Model prediction
26
+ preds = classifier(image)
27
+ top_pred = preds[0]
28
+ label = top_pred["label"]
29
+ score = float(top_pred["score"])
30
+
31
+ # 2️⃣ OCR text extraction (optional but helpful)
32
+ img_np = np.array(image)
33
+ img_cv = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
34
+ gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
35
+ text = pytesseract.image_to_string(gray)
36
+
37
+ # 3️⃣ Combine result
38
+ result = {
39
+ "prediction": label,
40
+ "confidence": round(score, 4),
41
+ "text_preview": text[:300]
42
+ }
43
+ return result
44
+
45
+ # -------------------------------------------------------------
46
+ # Gradio Interface
47
+ # -------------------------------------------------------------
48
+ demo = gr.Interface(
49
+ fn=verify_certificate,
50
+ inputs=gr.Image(type="numpy", label="Upload Certificate Image"),
51
+ outputs=gr.JSON(label="Verification Result"),
52
+ title="Fake Certificate Verification API 🧠",
53
+ description="Uploads a certificate image, runs an ML model to detect forgery, and extracts text for review.",
54
+ )
55
+
56
+ if __name__ == "__main__":
57
+ demo.launch()