JKrishnanandhaa commited on
Commit
4a448d9
·
verified ·
1 Parent(s): 5cd26a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -58
app.py CHANGED
@@ -412,14 +412,14 @@ class ForgeryDetector:
412
  detector = ForgeryDetector()
413
 
414
 
415
- def detect_forgery(file_input, webcam_input):
416
- """Gradio interface function - handles file upload (image/PDF) and webcam"""
417
  try:
418
- # Prioritize file upload, fallback to webcam
419
- if file_input is not None:
420
- file_path = file_input
421
- elif webcam_input is not None:
422
- file_path = webcam_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
@@ -441,31 +441,6 @@ def detect_forgery(file_input, webcam_input):
441
  return None, None, error_html
442
 
443
 
444
- def update_preview(file_path):
445
- """Update preview when file is uploaded"""
446
- if file_path is None:
447
- return None
448
-
449
- # Check if it's an image file
450
- if file_path.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp', '.tiff', '.webp', '.gif')):
451
- return file_path
452
- elif file_path.lower().endswith('.pdf'):
453
- # For PDFs, try to render first page as preview
454
- try:
455
- import fitz
456
- pdf = fitz.open(file_path)
457
- page = pdf[0]
458
- pix = page.get_pixmap(matrix=fitz.Matrix(1, 1))
459
- img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(pix.height, pix.width, pix.n)
460
- if pix.n == 4:
461
- img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
462
- pdf.close()
463
- return img
464
- except:
465
- return None
466
- return None
467
-
468
-
469
  # Custom CSS - subtle styling
470
  custom_css = """
471
  .predict-btn {
@@ -476,6 +451,16 @@ custom_css = """
476
  background-color: #6A89A7 !important;
477
  color: white !important;
478
  }
 
 
 
 
 
 
 
 
 
 
479
  """
480
 
481
  # Create Gradio interface
@@ -492,22 +477,20 @@ with gr.Blocks(css=custom_css) as demo:
492
  with gr.Row():
493
  with gr.Column(scale=1):
494
  gr.Markdown("### Upload Document")
495
- input_file = gr.File(
496
- label="Upload Image or PDF",
497
- file_types=["image", ".pdf"],
498
- type="filepath"
499
- )
500
- file_preview = gr.Image(
501
- label="Preview",
502
- type="numpy",
503
- interactive=False
504
- )
505
- input_webcam = gr.Image(
506
- label="Or capture from webcam",
507
- type="filepath",
508
- sources=["webcam"],
509
- height=150
510
- )
511
 
512
  with gr.Row():
513
  clear_btn = gr.Button("🧹 Clear", elem_classes="clear-btn")
@@ -591,23 +574,16 @@ with gr.Blocks(css=custom_css) as demo:
591
  )
592
 
593
  # Event handlers
594
- # Update preview when file is uploaded
595
- input_file.change(
596
- fn=update_preview,
597
- inputs=[input_file],
598
- outputs=[file_preview]
599
- )
600
-
601
  analyze_btn.click(
602
  fn=detect_forgery,
603
- inputs=[input_file, input_webcam],
604
  outputs=[output_image, metrics_gauge, output_html]
605
  )
606
 
607
  clear_btn.click(
608
- fn=lambda: (None, None, None, None, None, "<i>No analysis yet. Upload a document and click Analyze.</i>"),
609
  inputs=None,
610
- outputs=[input_file, file_preview, input_webcam, output_image, metrics_gauge, output_html]
611
  )
612
 
613
 
 
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
 
441
  return None, None, error_html
442
 
443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
444
  # Custom CSS - subtle styling
445
  custom_css = """
446
  .predict-btn {
 
451
  background-color: #6A89A7 !important;
452
  color: white !important;
453
  }
454
+ .upload-container {
455
+ border: 1px solid #444 !important;
456
+ border-radius: 10px !important;
457
+ padding: 10px !important;
458
+ }
459
+ .pdf-btn {
460
+ background-color: #d9534f !important;
461
+ color: white !important;
462
+ font-size: 0.9em !important;
463
+ }
464
  """
465
 
466
  # Create Gradio interface
 
477
  with gr.Row():
478
  with gr.Column(scale=1):
479
  gr.Markdown("### Upload Document")
480
+ with gr.Group(elem_classes="upload-container"):
481
+ input_image = gr.Image(
482
+ label="Upload Image or Use Webcam",
483
+ type="filepath",
484
+ sources=["upload", "webcam"]
485
+ )
486
+ with gr.Row():
487
+ gr.Markdown("<center><b>or</b></center>")
488
+ input_pdf = gr.File(
489
+ label="📄 Upload PDF",
490
+ file_types=[".pdf"],
491
+ type="filepath",
492
+ elem_classes="pdf-btn"
493
+ )
 
 
494
 
495
  with gr.Row():
496
  clear_btn = gr.Button("🧹 Clear", elem_classes="clear-btn")
 
574
  )
575
 
576
  # Event handlers
 
 
 
 
 
 
 
577
  analyze_btn.click(
578
  fn=detect_forgery,
579
+ inputs=[input_image, input_pdf],
580
  outputs=[output_image, metrics_gauge, output_html]
581
  )
582
 
583
  clear_btn.click(
584
+ fn=lambda: (None, None, None, None, "<i>No analysis yet. Upload a document and click Analyze.</i>"),
585
  inputs=None,
586
+ outputs=[input_image, input_pdf, output_image, metrics_gauge, output_html]
587
  )
588
 
589