Spaces:
Sleeping
Sleeping
Protect whole-photo screening and candidate abstention
Browse filesCheck that one-leaf mode uses the full image without running the leaf detector, and that multiple-leaf proposals never reach disease models. Protect the shared confirmation gate and ensure automatic batch exports have no binary predictions until a region is confirmed. Retain existing disagreement, mask and export checks.
53 unmodified lines5455565758596013 unmodified lines7475767778798081825 unmodified lines88899091929394959697988 unmodified lines10710810911011111211311411511611711811912053 unmodified linesreference = Path(directory) / "reference.png"Image.fromarray(np.zeros((32, 32), dtype=np.uint8)).save(reference)assert not app.binary_mask(reference, leaf.shape).any()# The simple screen must check every detected leaf and execute all six adapters.photo_path = Path(directory) / "photo.png"pixels = np.zeros((64, 128, 3), dtype=np.uint8)pixels[:, ::2] = [40, 180, 60]13 unmodified linesreturn {"ranking": []}, Nonewith patch.object(app, "execute", side_effect=execute):message, images, report = app.quick_check(str(photo_path), progress=lambda *a, **k: None)assert len(report["leaves"]) == 2 and len(images) == 3assert "Sick-looking" in message and "Coffee leaf rust" in messageassert calls == ["yolo"] + ["sam", "efficientnet", "clip", "deeplab", "decafia"] * 2for prediction in report["leaves"]:assert prediction["pipeline_complete"]assert not prediction["leaf_mask_reviewed"] and not prediction["leaf_selection_reviewed"]5 unmodified linesmask = np.ones((crop.height, crop.width), dtype=bool)prediction, *_ = app.infer_target(target, mask, False, False, True, True, False, "", .8, .15)assert prediction["status"] == "NEEDS_LEAF_CONFIRMATION"prediction, *_ = app.infer_target(target, mask, False, True, True, False, False, "", .8, .15)assert prediction["models"]["deeplab"]["status"] == "NEEDS_REVIEWED_LEAF_MASK"_, damage = app.damage_check(target)assert damage["status"] == "OK" and not damage["detections"]assert "healthy" not in damage and "scores" not in damagedef disagreement(kind, image, leaf_mask=None):if kind == "efficientnet":return {"scores": {"healthy": .95, "rust": .05}}, image8 unmodified linesfor output in [([], {}), RuntimeError("detector unavailable")]:with patch.object(app, "execute", side_effect=output if isinstance(output, Exception) else None,return_value=output):message, _, report = app.quick_check(str(photo_path), progress=lambda *a, **k: None)assert "Unable to assess" in message and not report["leaves"]x, y, dropped = app.paired_numeric(pd.DataFrame({"a": [0, None, 10], "b": [1, 5, 12]}), "a", "b")assert dropped == 1 and x.tolist() == [0, 10] and y.tolist() == [1, 12]assert app.agreement_values(x, y)["mae_percentage_points"] == 1.5demo = app.build_app()assert demo.config["components"] and demo.config["dependencies"]print("PASS: six-model pipeline, experimental disagreement, review gates, failure handling, exports and Gradio construction")if __name__ == "__main__":53 unmodified lines5455565758596013 unmodified lines74757677787980818283845 unmodified lines909192939495969798991001011021031041051061071081091101111128 unmodified lines12112212312412512612712812913013113213313453 unmodified linesreference = Path(directory) / "reference.png"Image.fromarray(np.zeros((32, 32), dtype=np.uint8)).save(reference)assert not app.binary_mask(reference, leaf.shape).any()# One-leaf mode must ignore bad detector boxes and use the entire photo.photo_path = Path(directory) / "photo.png"pixels = np.zeros((64, 128, 3), dtype=np.uint8)pixels[:, ::2] = [40, 180, 60]13 unmodified linesreturn {"ranking": []}, Nonewith patch.object(app, "execute", side_effect=execute):message, images, report = app.quick_check(str(photo_path), progress=lambda *a, **k: None)assert len(report["leaves"]) == 1 and len(images) == 1assert "Sick-looking" in message and "Coffee leaf rust" in messageassert calls == ["sam", "efficientnet", "clip", "deeplab", "decafia"]assert report["detector"]["status"] == "SKIPPED_SINGLE_LEAF"assert report["leaves"][0]["box_original_xyxy"] == [0, 0, 128, 64]for prediction in report["leaves"]:assert prediction["pipeline_complete"]assert not prediction["leaf_mask_reviewed"] and not prediction["leaf_selection_reviewed"]5 unmodified linesmask = np.ones((crop.height, crop.width), dtype=bool)prediction, *_ = app.infer_target(target, mask, False, False, True, True, False, "", .8, .15)assert prediction["status"] == "NEEDS_LEAF_CONFIRMATION"prediction, *_ = app.infer_target(target, mask, False, False, True, True, False, "", .8, .15, automatic=True)assert prediction["status"] == "NEEDS_LEAF_CONFIRMATION"prediction, *_ = app.infer_target(target, mask, False, True, True, False, False, "", .8, .15)assert prediction["models"]["deeplab"]["status"] == "NEEDS_REVIEWED_LEAF_MASK"_, damage = app.damage_check(target)assert damage["status"] == "OK" and not damage["detections"]assert "healthy" not in damage and "scores" not in damagecalls.clear()message, _, report = app.quick_check(str(photo_path), single_leaf=False, progress=lambda *a, **k: None)assert report["status"] == "NEEDS_LEAF_CONFIRMATION" and not report["leaves"]assert calls == ["yolo"] and "Select a leaf" in messagecalls.clear()batch = list(app.batch_process([str(photo_path)], False, 10, .8, .15))assert calls == ["yolo"]assert set(batch[-1][0]["status"]) == {"NEEDS_LEAF_CONFIRMATION"}exported = pd.read_csv(batch[-1][1])assert exported["predicted_binary"].isna().all()def disagreement(kind, image, leaf_mask=None):if kind == "efficientnet":return {"scores": {"healthy": .95, "rust": .05}}, image8 unmodified linesfor output in [([], {}), RuntimeError("detector unavailable")]:with patch.object(app, "execute", side_effect=output if isinstance(output, Exception) else None,return_value=output):message, _, report = app.quick_check(str(photo_path), single_leaf=False, progress=lambda *a, **k: None)assert not report["leaves"] and report["detector"]["status"] in {"NO_LEAF_DETECTED", "MODEL_ERROR"}x, y, dropped = app.paired_numeric(pd.DataFrame({"a": [0, None, 10], "b": [1, 5, 12]}), "a", "b")assert dropped == 1 and x.tolist() == [0, 10] and y.tolist() == [1, 12]assert app.agreement_values(x, y)["mae_percentage_points"] == 1.5demo = app.build_app()assert demo.config["components"] and demo.config["dependencies"]print("PASS: whole-photo screening, detector proposal gates, batch abstention, disagreement, exports and Gradio construction")if __name__ == "__main__":
test_app.py+20-6