JKrishnanandhaa commited on
Commit
036a5b5
·
verified ·
1 Parent(s): f49c274

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -298,8 +298,8 @@ class ForgeryDetector:
298
 
299
  # Calculate IoU, Precision, Recall from the refined mask and probability map
300
  if num_detections > 0:
301
- # Use high-confidence areas from probability map as "predicted positive"
302
- high_conf_mask = (prob_map > 0.7).astype(np.uint8)
303
  predicted_positive = np.sum(refined_mask > 0)
304
  high_conf_positive = np.sum(high_conf_mask > 0)
305
 
@@ -412,16 +412,18 @@ class ForgeryDetector:
412
  detector = ForgeryDetector()
413
 
414
 
415
- def detect_forgery(file):
416
- """Gradio interface function"""
417
  try:
418
- if file is None:
 
 
 
 
 
419
  empty_html = "<div style='padding:12px; border:1px solid #d9534f; border-radius:8px;'>❌ <b>No file uploaded.</b></div>"
420
  return None, None, empty_html
421
 
422
- # Get file path
423
- file_path = file if isinstance(file, str) else file
424
-
425
  # Detect forgeries
426
  overlay, metrics_gauge, results_html = detector.detect(file_path)
427
 
@@ -465,11 +467,16 @@ with gr.Blocks(css=custom_css) as demo:
465
  with gr.Row():
466
  with gr.Column(scale=1):
467
  gr.Markdown("### Upload Document")
468
- input_file = gr.Image(
469
- label="Document (Image or PDF)",
470
  type="filepath",
471
  sources=["upload", "webcam"]
472
  )
 
 
 
 
 
473
 
474
  with gr.Row():
475
  clear_btn = gr.Button("🧹 Clear", elem_classes="clear-btn")
@@ -555,14 +562,14 @@ with gr.Blocks(css=custom_css) as demo:
555
  # Event handlers
556
  analyze_btn.click(
557
  fn=detect_forgery,
558
- inputs=[input_file],
559
  outputs=[output_image, metrics_gauge, output_html]
560
  )
561
 
562
  clear_btn.click(
563
- fn=lambda: (None, None, None, "<i>No analysis yet. Upload a document and click Analyze.</i>"),
564
  inputs=None,
565
- outputs=[input_file, output_image, metrics_gauge, output_html]
566
  )
567
 
568
 
 
298
 
299
  # Calculate IoU, Precision, Recall from the refined mask and probability map
300
  if num_detections > 0:
301
+ # Use resized prob_map to match refined_mask dimensions
302
+ high_conf_mask = (prob_map_resized > 0.7).astype(np.uint8)
303
  predicted_positive = np.sum(refined_mask > 0)
304
  high_conf_positive = np.sum(high_conf_mask > 0)
305
 
 
412
  detector = ForgeryDetector()
413
 
414
 
415
+ def detect_forgery(image_input, pdf_input):
416
+ """Gradio interface function - handles image/webcam and PDF inputs"""
417
  try:
418
+ # Prioritize image input, fallback to PDF
419
+ if image_input is not None:
420
+ file_path = image_input
421
+ elif pdf_input is not None:
422
+ file_path = pdf_input
423
+ else:
424
  empty_html = "<div style='padding:12px; border:1px solid #d9534f; border-radius:8px;'>❌ <b>No file uploaded.</b></div>"
425
  return None, None, empty_html
426
 
 
 
 
427
  # Detect forgeries
428
  overlay, metrics_gauge, results_html = detector.detect(file_path)
429
 
 
467
  with gr.Row():
468
  with gr.Column(scale=1):
469
  gr.Markdown("### Upload Document")
470
+ input_image = gr.Image(
471
+ label="Image (with preview) or Webcam",
472
  type="filepath",
473
  sources=["upload", "webcam"]
474
  )
475
+ input_pdf = gr.File(
476
+ label="Or upload PDF here",
477
+ file_types=[".pdf"],
478
+ type="filepath"
479
+ )
480
 
481
  with gr.Row():
482
  clear_btn = gr.Button("🧹 Clear", elem_classes="clear-btn")
 
562
  # Event handlers
563
  analyze_btn.click(
564
  fn=detect_forgery,
565
+ inputs=[input_image, input_pdf],
566
  outputs=[output_image, metrics_gauge, output_html]
567
  )
568
 
569
  clear_btn.click(
570
+ fn=lambda: (None, None, None, None, "<i>No analysis yet. Upload a document and click Analyze.</i>"),
571
  inputs=None,
572
+ outputs=[input_image, input_pdf, output_image, metrics_gauge, output_html]
573
  )
574
 
575