Update app.py
Browse files
app.py
CHANGED
|
@@ -211,33 +211,10 @@ class ForgeryDetector:
|
|
| 211 |
# Create visualization
|
| 212 |
overlay = self._create_overlay(original_image, results)
|
| 213 |
|
| 214 |
-
# Create HTML for model performance metrics
|
| 215 |
-
model_performance_html = f"""
|
| 216 |
-
<div style='padding:12px; border:1px solid #444; border-radius:10px; background:var(--background-fill-primary);'>
|
| 217 |
-
<p style='margin-top:0; margin-bottom:12px;'><b>Trained Model Performance:</b></p>
|
| 218 |
-
|
| 219 |
-
<b>Segmentation Dice:</b>
|
| 220 |
-
<div style='width:100%; background:#333; height:12px; border-radius:6px; margin-bottom:12px;'>
|
| 221 |
-
<div style='width:{MODEL_METRICS['segmentation']['dice']*100:.1f}%; background:#4169E1; height:12px; border-radius:6px;'></div>
|
| 222 |
-
</div>
|
| 223 |
-
|
| 224 |
-
<b>Classification Accuracy:</b>
|
| 225 |
-
<div style='width:100%; background:#333; height:12px; border-radius:6px; margin-bottom:6px;'>
|
| 226 |
-
<div style='width:{MODEL_METRICS['classification']['overall_accuracy']*100:.1f}%; background:#5cb85c; height:12px; border-radius:6px;'></div>
|
| 227 |
-
</div>
|
| 228 |
-
|
| 229 |
-
<small style='opacity:0.8;'>
|
| 230 |
-
Dice: {MODEL_METRICS['segmentation']['dice']*100:.1f}% |
|
| 231 |
-
IoU: {MODEL_METRICS['segmentation']['iou']*100:.1f}% |
|
| 232 |
-
Accuracy: {MODEL_METRICS['classification']['overall_accuracy']*100:.1f}%
|
| 233 |
-
</small>
|
| 234 |
-
</div>
|
| 235 |
-
"""
|
| 236 |
-
|
| 237 |
# Create HTML response
|
| 238 |
results_html = self._create_html_report(results)
|
| 239 |
|
| 240 |
-
return overlay,
|
| 241 |
|
| 242 |
def _create_overlay(self, image, results):
|
| 243 |
"""Create overlay visualization"""
|
|
@@ -331,15 +308,15 @@ def detect_forgery(file):
|
|
| 331 |
try:
|
| 332 |
if file is None:
|
| 333 |
empty_html = "<div style='padding:12px; border:1px solid #d9534f; border-radius:8px;'>❌ <b>No file uploaded.</b></div>"
|
| 334 |
-
return None, empty_html
|
| 335 |
|
| 336 |
# Get file path
|
| 337 |
file_path = file if isinstance(file, str) else file
|
| 338 |
|
| 339 |
# Detect forgeries
|
| 340 |
-
overlay,
|
| 341 |
|
| 342 |
-
return overlay,
|
| 343 |
|
| 344 |
except Exception as e:
|
| 345 |
import traceback
|
|
@@ -350,7 +327,7 @@ def detect_forgery(file):
|
|
| 350 |
❌ <b>Error:</b> {str(e)}
|
| 351 |
</div>
|
| 352 |
"""
|
| 353 |
-
return None, error_html
|
| 354 |
|
| 355 |
|
| 356 |
# Custom CSS - subtle styling
|
|
@@ -414,18 +391,37 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 414 |
output_image = gr.Image(label="Detected Forgeries", type="numpy")
|
| 415 |
|
| 416 |
with gr.Row():
|
| 417 |
-
with gr.Column(scale=1):
|
| 418 |
-
gr.Markdown("### Model Performance")
|
| 419 |
-
model_performance = gr.HTML(
|
| 420 |
-
value="<i>Model performance will be shown after analysis.</i>"
|
| 421 |
-
)
|
| 422 |
-
|
| 423 |
with gr.Column(scale=1):
|
| 424 |
gr.Markdown("### Analysis Report")
|
| 425 |
output_html = gr.HTML(
|
| 426 |
value="<i>No analysis yet. Upload a document and click Analyze.</i>"
|
| 427 |
)
|
| 428 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 429 |
gr.Markdown(
|
| 430 |
"""
|
| 431 |
---
|
|
@@ -440,13 +436,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 440 |
analyze_btn.click(
|
| 441 |
fn=detect_forgery,
|
| 442 |
inputs=[input_file],
|
| 443 |
-
outputs=[output_image,
|
| 444 |
)
|
| 445 |
|
| 446 |
clear_btn.click(
|
| 447 |
-
fn=lambda: (None, None, "<i>
|
| 448 |
inputs=None,
|
| 449 |
-
outputs=[input_file, output_image,
|
| 450 |
)
|
| 451 |
|
| 452 |
|
|
|
|
| 211 |
# Create visualization
|
| 212 |
overlay = self._create_overlay(original_image, results)
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
# Create HTML response
|
| 215 |
results_html = self._create_html_report(results)
|
| 216 |
|
| 217 |
+
return overlay, results_html
|
| 218 |
|
| 219 |
def _create_overlay(self, image, results):
|
| 220 |
"""Create overlay visualization"""
|
|
|
|
| 308 |
try:
|
| 309 |
if file is None:
|
| 310 |
empty_html = "<div style='padding:12px; border:1px solid #d9534f; border-radius:8px;'>❌ <b>No file uploaded.</b></div>"
|
| 311 |
+
return None, empty_html
|
| 312 |
|
| 313 |
# Get file path
|
| 314 |
file_path = file if isinstance(file, str) else file
|
| 315 |
|
| 316 |
# Detect forgeries
|
| 317 |
+
overlay, results_html = detector.detect(file_path)
|
| 318 |
|
| 319 |
+
return overlay, results_html
|
| 320 |
|
| 321 |
except Exception as e:
|
| 322 |
import traceback
|
|
|
|
| 327 |
❌ <b>Error:</b> {str(e)}
|
| 328 |
</div>
|
| 329 |
"""
|
| 330 |
+
return None, error_html
|
| 331 |
|
| 332 |
|
| 333 |
# Custom CSS - subtle styling
|
|
|
|
| 391 |
output_image = gr.Image(label="Detected Forgeries", type="numpy")
|
| 392 |
|
| 393 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
with gr.Column(scale=1):
|
| 395 |
gr.Markdown("### Analysis Report")
|
| 396 |
output_html = gr.HTML(
|
| 397 |
value="<i>No analysis yet. Upload a document and click Analyze.</i>"
|
| 398 |
)
|
| 399 |
|
| 400 |
+
gr.Markdown("### Model Performance")
|
| 401 |
+
gr.HTML(
|
| 402 |
+
f"""
|
| 403 |
+
<div style='padding:12px; border:1px solid #444; border-radius:10px; background:var(--background-fill-primary);'>
|
| 404 |
+
<p style='margin-top:0; margin-bottom:12px;'><b>Trained Model Performance:</b></p>
|
| 405 |
+
|
| 406 |
+
<b>Segmentation Dice:</b>
|
| 407 |
+
<div style='width:100%; background:#333; height:12px; border-radius:6px; margin-bottom:12px;'>
|
| 408 |
+
<div style='width:{MODEL_METRICS['segmentation']['dice']*100:.1f}%; background:#4169E1; height:12px; border-radius:6px;'></div>
|
| 409 |
+
</div>
|
| 410 |
+
|
| 411 |
+
<b>Classification Accuracy:</b>
|
| 412 |
+
<div style='width:100%; background:#333; height:12px; border-radius:6px; margin-bottom:6px;'>
|
| 413 |
+
<div style='width:{MODEL_METRICS['classification']['overall_accuracy']*100:.1f}%; background:#5cb85c; height:12px; border-radius:6px;'></div>
|
| 414 |
+
</div>
|
| 415 |
+
|
| 416 |
+
<small style='opacity:0.8;'>
|
| 417 |
+
Dice: {MODEL_METRICS['segmentation']['dice']*100:.1f}% |
|
| 418 |
+
IoU: {MODEL_METRICS['segmentation']['iou']*100:.1f}% |
|
| 419 |
+
Accuracy: {MODEL_METRICS['classification']['overall_accuracy']*100:.1f}%
|
| 420 |
+
</small>
|
| 421 |
+
</div>
|
| 422 |
+
"""
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
gr.Markdown(
|
| 426 |
"""
|
| 427 |
---
|
|
|
|
| 436 |
analyze_btn.click(
|
| 437 |
fn=detect_forgery,
|
| 438 |
inputs=[input_file],
|
| 439 |
+
outputs=[output_image, output_html]
|
| 440 |
)
|
| 441 |
|
| 442 |
clear_btn.click(
|
| 443 |
+
fn=lambda: (None, None, "<i>No analysis yet. Upload a document and click Analyze.</i>"),
|
| 444 |
inputs=None,
|
| 445 |
+
outputs=[input_file, output_image, output_html]
|
| 446 |
)
|
| 447 |
|
| 448 |
|