Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -412,14 +412,14 @@ class ForgeryDetector:
|
|
| 412 |
detector = ForgeryDetector()
|
| 413 |
|
| 414 |
|
| 415 |
-
def detect_forgery(
|
| 416 |
-
"""Gradio interface function - handles image/
|
| 417 |
try:
|
| 418 |
-
# Prioritize
|
| 419 |
-
if
|
| 420 |
-
file_path =
|
| 421 |
-
elif
|
| 422 |
-
file_path =
|
| 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,6 +441,31 @@ def detect_forgery(image_input, pdf_input):
|
|
| 441 |
return None, None, error_html
|
| 442 |
|
| 443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
# Custom CSS - subtle styling
|
| 445 |
custom_css = """
|
| 446 |
.predict-btn {
|
|
@@ -467,16 +492,22 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 467 |
with gr.Row():
|
| 468 |
with gr.Column(scale=1):
|
| 469 |
gr.Markdown("### Upload Document")
|
| 470 |
-
|
| 471 |
-
label="Image
|
| 472 |
-
|
| 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")
|
|
@@ -560,16 +591,23 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 560 |
)
|
| 561 |
|
| 562 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
analyze_btn.click(
|
| 564 |
fn=detect_forgery,
|
| 565 |
-
inputs=[
|
| 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=[
|
| 573 |
)
|
| 574 |
|
| 575 |
|
|
|
|
| 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 |
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 {
|
|
|
|
| 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 |
)
|
| 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 |
|