Pro-Coder commited on
Commit
9b3140a
·
verified ·
1 Parent(s): 64c79e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -61
app.py CHANGED
@@ -367,60 +367,32 @@ def chat_respond(message, history):
367
  BASE_DIR = Path(__file__).resolve().parent
368
  ASSETS_DIR = BASE_DIR / "assets"
369
 
370
- CONFUSION_MATRIX = ASSETS_DIR / "confusion_matrix.png"
371
- ROC_CURVE = ASSETS_DIR / "roc_curve.png"
372
 
 
 
373
 
374
- def load_performance_reports():
375
- """Scan assets/ for *_metrics.json files and pair each with its confusion-matrix
376
- image if present. Returns a list of dicts; empty list if nothing's been uploaded yet."""
377
- import json
 
378
 
379
- reports = []
380
- if not os.path.isdir(ASSETS_DIR):
381
- return reports
382
- for fname in sorted(os.listdir(ASSETS_DIR)):
383
- if not fname.endswith("_metrics.json"):
384
- continue
385
- prefix = fname[: -len("_metrics.json")]
386
- try:
387
- with open(os.path.join(ASSETS_DIR, fname)) as f:
388
- data = json.load(f)
389
- except Exception:
390
- continue
391
- image_path = None
392
- for ext in (".png", ".jpg", ".jpeg"):
393
- candidate = os.path.join(ASSETS_DIR, f"{prefix}_confusion_matrix{ext}")
394
- if os.path.isfile(candidate):
395
- image_path = candidate
396
- break
397
- reports.append({"data": data, "image_path": image_path})
398
- return reports
399
-
400
-
401
- def format_performance_markdown(report):
402
- data = report["data"]
403
- lines = [f"### {data.get('model_name', 'Model')}"]
404
- acc = data.get("accuracy")
405
- f1m = data.get("f1_macro")
406
- if acc is not None:
407
- lines.append(f"**Accuracy:** {acc*100:.1f}% · **Macro F1:** {f1m*100:.1f}%" if f1m is not None else f"**Accuracy:** {acc*100:.1f}%")
408
-
409
- per_class = data.get("per_class", {})
410
- if per_class:
411
- lines.append("\n| Class | Precision | Recall | F1 | Support |")
412
- lines.append("|---|---|---|---|---|")
413
- for cls, m in per_class.items():
414
- lines.append(
415
- f"| {cls} | {m.get('precision', 0)*100:.1f}% | {m.get('recall', 0)*100:.1f}% "
416
- f"| {m.get('f1', 0)*100:.1f}% | {int(m.get('support', 0))} |"
417
- )
418
 
419
- notes = data.get("notes")
420
- if notes:
421
- lines.append(f"\n_Note: {notes}_")
 
 
422
 
423
- return "\n".join(lines)
 
 
 
 
 
 
424
 
425
 
426
  ABOUT_MARKDOWN = f"""
@@ -509,21 +481,44 @@ with gr.Blocks(title="Skin Disease Detection — Demo") as demo:
509
  ),
510
  )
511
 
512
- with gr.Tab("📊 Model Performance"):
513
- performance_reports = load_performance_reports()
514
- if not performance_reports:
 
 
 
 
 
 
 
 
515
  gr.Markdown(
516
- "_No model performance data yet._\n\n"
517
- "Run either fine-tuning notebook in this repo, then download the "
518
- "`*_metrics.json` and `*_confusion_matrix.png` files it produces and place "
519
- "them in this Space's `assets/` folder. This tab picks them up automatically "
520
- "on the next restart."
521
  )
 
522
  else:
523
- for report in performance_reports:
524
- gr.Markdown(format_performance_markdown(report))
525
- if report["image_path"]:
526
- gr.Image(value=report["image_path"], label="Confusion matrix (test set)", show_label=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
527
 
528
  with gr.Tab("ℹ️ About"):
529
  gr.Markdown(ABOUT_MARKDOWN)
 
367
  BASE_DIR = Path(__file__).resolve().parent
368
  ASSETS_DIR = BASE_DIR / "assets"
369
 
 
 
370
 
371
+ def load_performance_artifacts():
372
+ """Load model-evaluation images generated by the evaluation notebook."""
373
 
374
+ artifacts = {
375
+ "unified_confusion_matrix": None,
376
+ "melanoma_threshold_curve": None,
377
+ "cluster_confusion_matrix": None,
378
+ }
379
 
380
+ if not ASSETS_DIR.is_dir():
381
+ return artifacts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
 
383
+ files = {
384
+ "unified_confusion_matrix": "unified_confusion_matrix.png",
385
+ "melanoma_threshold_curve": "unified_melanoma_threshold_curve.png",
386
+ "cluster_confusion_matrix": "cluster_confusion_matrix.png",
387
+ }
388
 
389
+ for key, filename in files.items():
390
+ path = ASSETS_DIR / filename
391
+
392
+ if path.is_file():
393
+ artifacts[key] = str(path)
394
+
395
+ return artifacts
396
 
397
 
398
  ABOUT_MARKDOWN = f"""
 
481
  ),
482
  )
483
 
484
+ with gr.Tab("📊 Model Evaluation"):
485
+ with gr.Accordion("ℹ️ About Model Evaluation", open=False):
486
+ gr.HTML(data_badge(
487
+ "🧪 All charts below are computed on <b>held-out test data</b> "
488
+ "by <code>build_artifacts.py</code> — fully reproducible."
489
+ ))
490
+
491
+ artifacts = load_performance_artifacts()
492
+
493
+ if not any(artifacts.values()):
494
+
495
  gr.Markdown(
496
+ "### No model performance artifacts yet.\n\n"
497
+ "Run the evaluation notebook and place the generated "
498
+ "artifacts in the Space's `assets/` folder."
 
 
499
  )
500
+
501
  else:
502
+
503
+ gr.Markdown("### Unified Model Evaluation")
504
+
505
+ if artifacts["unified_confusion_matrix"]:
506
+ gr.Image(
507
+ value=artifacts["unified_confusion_matrix"],
508
+ label="24-Class Confusion Matrix"
509
+ )
510
+
511
+ if artifacts["melanoma_threshold_curve"]:
512
+ gr.Image(
513
+ value=artifacts["melanoma_threshold_curve"],
514
+ label="Melanoma Precision / Recall vs. Decision Threshold"
515
+ )
516
+
517
+ if artifacts["cluster_confusion_matrix"]:
518
+ gr.Image(
519
+ value=artifacts["cluster_confusion_matrix"],
520
+ label="Disease Similarity Cluster Confusion Matrix"
521
+ )
522
 
523
  with gr.Tab("ℹ️ About"):
524
  gr.Markdown(ABOUT_MARKDOWN)