Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- main.py +61 -114
- medical_labels.py +307 -0
main.py
CHANGED
|
@@ -551,110 +551,12 @@ oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
| 551 |
# =========================================================================
|
| 552 |
# MEDICAL DOMAINS CONFIGURATION
|
| 553 |
# =========================================================================
|
| 554 |
-
MEDICAL_DOMAINS
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
'Normal chest radiograph: normal cardiothoracic ratio, clear lungs, no pleural abnormality',
|
| 561 |
-
'Pneumothorax (Lung collapse)',
|
| 562 |
-
'Pleural Effusion (Fluid)',
|
| 563 |
-
'Cardiomegaly with clear lung fields (no pulmonary edema)',
|
| 564 |
-
'Cardiomegaly with pulmonary congestion or edema',
|
| 565 |
-
'Pulmonary Edema (without cardiomegaly)',
|
| 566 |
-
'Lung Nodule or Mass',
|
| 567 |
-
'Atelectasis (Lung collapse)'
|
| 568 |
-
],
|
| 569 |
-
'logic_gate': { # Renamed from morphology_check
|
| 570 |
-
'prompt': 'Evaluate cardiac silhouette size',
|
| 571 |
-
'labels': ['Normal cardiac size (CTR < 0.5)', 'Enlarged cardiac silhouette (Cardiomegaly)'],
|
| 572 |
-
'penalty_target': 'Normal' # If abnormal, penalize this label
|
| 573 |
-
}
|
| 574 |
-
},
|
| 575 |
-
'Dermatology': {
|
| 576 |
-
'domain_prompt': 'Dermatoscopic analysis of a pigmented or non-pigmented skin lesion',
|
| 577 |
-
'specific_labels': [
|
| 578 |
-
'Normal skin without visible lesion or abnormal pigmentation',
|
| 579 |
-
'Benign melanocytic nevus with symmetry and uniform pigmentation',
|
| 580 |
-
'Seborrheic keratosis (benign warty lesion)',
|
| 581 |
-
'Malignant melanoma with asymmetry, irregular borders, and color variegation',
|
| 582 |
-
'Basal cell carcinoma (pearly or ulcerated lesion)',
|
| 583 |
-
'Squamous cell carcinoma (crusty or budding lesion)',
|
| 584 |
-
'Inflammatory skin lesion (Eczema, Psoriasis)'
|
| 585 |
-
],
|
| 586 |
-
'logic_gate': {
|
| 587 |
-
'prompt': 'Is there a visible skin lesion?',
|
| 588 |
-
'labels': ['No visible skin lesion', 'Visible skin lesion (pigmented or non-pigmented)'],
|
| 589 |
-
'penalty_target': 'ALL_PATHOLOGY', # Special flag
|
| 590 |
-
'abnormal_index': 0 # "No visible lesion" is the blocker here (index 0)
|
| 591 |
-
}
|
| 592 |
-
},
|
| 593 |
-
'Histology': {
|
| 594 |
-
'domain_prompt': 'Microscopic analysis of a histological section (H&E stain)',
|
| 595 |
-
'specific_labels': [
|
| 596 |
-
'Healthy breast tissue with preserved lobular architecture',
|
| 597 |
-
'Healthy prostatic tissue with regular glands',
|
| 598 |
-
'Invasive ductal carcinoma (Disorganized cells)',
|
| 599 |
-
'Prostate adenocarcinoma (Gland fusion)',
|
| 600 |
-
'Cervical dysplasia or intraepithelial neoplasia',
|
| 601 |
-
'Colon cancer tumor tissue',
|
| 602 |
-
'Lung cancer tumor tissue',
|
| 603 |
-
'Adipose tissue (Fat) or connective stroma',
|
| 604 |
-
'Preparation artifact, empty area, or blurred region' # Explicit Label
|
| 605 |
-
],
|
| 606 |
-
'logic_gate': {
|
| 607 |
-
'prompt': 'Assess histological validity of the image',
|
| 608 |
-
'labels': ['Adequate H&E tissue section', 'Artifact, empty area, or blurred region'],
|
| 609 |
-
'penalty_target': 'ALL_DIAGNOSIS',
|
| 610 |
-
'abnormal_index': 1 # "Artifact" is the blocker
|
| 611 |
-
}
|
| 612 |
-
},
|
| 613 |
-
'Ophthalmology': {
|
| 614 |
-
'domain_prompt': 'Fundus photography (Retina)',
|
| 615 |
-
'specific_labels': [
|
| 616 |
-
'Normal retina with visible optic disc and macula',
|
| 617 |
-
'Diabetic retinopathy (hemorrhages, exudates)',
|
| 618 |
-
'Glaucoma (optic disc cupping)',
|
| 619 |
-
'Macular degeneration (drusen or atrophy)'
|
| 620 |
-
],
|
| 621 |
-
'logic_gate': {
|
| 622 |
-
'prompt': 'Is the fundus image clinically interpretable?',
|
| 623 |
-
'labels': ['Good quality fundus image', 'Poor quality, uninterpretable or partial view'],
|
| 624 |
-
'penalty_target': 'ALL_DIAGNOSIS',
|
| 625 |
-
'abnormal_index': 1 # "Poor quality" is the blocker
|
| 626 |
-
}
|
| 627 |
-
},
|
| 628 |
-
'Orthopedics': {
|
| 629 |
-
'domain_prompt': 'Bone X-Ray (Musculoskeletal)',
|
| 630 |
-
'stage_1_triage': {
|
| 631 |
-
'prompt': 'Anatomical region identification',
|
| 632 |
-
'labels': [
|
| 633 |
-
'Other x-ray view (Chest, Hand, Foot, Pediatric) - OUT OF DISTRIBUTION',
|
| 634 |
-
'A knee x-ray view (Knee Joint)'
|
| 635 |
-
]
|
| 636 |
-
},
|
| 637 |
-
'specific_labels': [ # Fallback if stage 2 fails or logic changes
|
| 638 |
-
'Severe osteoarthritis (Grade 4)', 'Moderate osteoarthritis (Grade 2-3)', 'Normal knee', 'Implant'
|
| 639 |
-
],
|
| 640 |
-
'stage_2_diagnosis': {
|
| 641 |
-
'prompt': 'Knee Osteoarthritis Severity Assessment',
|
| 642 |
-
'labels': [
|
| 643 |
-
'Severe osteoarthritis with bone-on-bone contact (Grade 4)',
|
| 644 |
-
'Moderate osteoarthritis with definite joint space narrowing (Grade 2-3)',
|
| 645 |
-
'Normal knee joint with preserved joint space (Grade 0-1)',
|
| 646 |
-
'Total knee arthroplasty (TKA) with metallic implant', # Label included
|
| 647 |
-
'Acute knee fracture or dislocation'
|
| 648 |
-
]
|
| 649 |
-
},
|
| 650 |
-
'logic_gate': {
|
| 651 |
-
'prompt': 'Is there a metallic implant?',
|
| 652 |
-
'labels': ['Native knee joint', 'Knee with metallic implant (Arthroplasty)'],
|
| 653 |
-
'penalty_target': 'Osteoarthritis', # Can't have OA if implant
|
| 654 |
-
'abnormal_index': 1
|
| 655 |
-
}
|
| 656 |
-
}
|
| 657 |
-
}
|
| 658 |
|
| 659 |
# =========================================================================
|
| 660 |
# PYDANTIC MODELS
|
|
@@ -843,7 +745,7 @@ class MedSigClipWrapper:
|
|
| 843 |
|
| 844 |
def predict(self, image_bytes: bytes, username: str = None) -> Dict[str, Any]:
|
| 845 |
"""Run hierarchical inference using SigLIP Zero-Shot."""
|
| 846 |
-
|
| 847 |
# I need to match the indentation and context.
|
| 848 |
# Since I can't see "inside" the dots in a replace, I have to be careful.
|
| 849 |
# It's better to update just the definition line and the call to enhance_analysis_result.
|
|
@@ -863,6 +765,47 @@ class MedSigClipWrapper:
|
|
| 863 |
import torch
|
| 864 |
import pydicom
|
| 865 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 866 |
# Image preprocessing functions
|
| 867 |
def process_dicom(file_bytes: bytes) -> Tuple[Image.Image, Dict[str, Any]]:
|
| 868 |
"""Convert DICOM bytes to PIL Image with tags."""
|
|
@@ -1034,10 +977,12 @@ class MedSigClipWrapper:
|
|
| 1034 |
logger.info("Triage indicates Normal/Healthy. Skipping Stage 2.")
|
| 1035 |
else:
|
| 1036 |
# Flat Mode (Thoracic, Dermato, etc.)
|
| 1037 |
-
|
|
|
|
|
|
|
| 1038 |
|
| 1039 |
inputs_specific = self.processor(
|
| 1040 |
-
text=
|
| 1041 |
images=image,
|
| 1042 |
padding="max_length",
|
| 1043 |
return_tensors="pt"
|
|
@@ -1048,9 +993,10 @@ class MedSigClipWrapper:
|
|
| 1048 |
|
| 1049 |
probs_specific = torch.softmax(outputs_specific.logits_per_image, dim=1)[0]
|
| 1050 |
|
| 1051 |
-
for i,
|
| 1052 |
specific_results.append({
|
| 1053 |
-
"
|
|
|
|
| 1054 |
"probability": round(float(probs_specific[i] * 100), 2)
|
| 1055 |
})
|
| 1056 |
|
|
@@ -1062,11 +1008,11 @@ class MedSigClipWrapper:
|
|
| 1062 |
|
| 1063 |
for res in specific_results:
|
| 1064 |
should_penalize = False
|
| 1065 |
-
label_text = res['label']
|
|
|
|
| 1066 |
|
| 1067 |
if logic_penalty_target == 'ALL_DIAGNOSIS':
|
| 1068 |
# Penalize EVERYTHING (e.g. Poor Quality Image)
|
| 1069 |
-
# Except the specific label that describes the failure (e.g. "Preparation artifact")
|
| 1070 |
# Heuristic: If label contains "Artifact" or "Quality" or "Partial", don't penalize.
|
| 1071 |
if "Artifact" in label_text or "Quality" in label_text or "Partial" in label_text or "Empty" in label_text:
|
| 1072 |
pass
|
|
@@ -1075,14 +1021,15 @@ class MedSigClipWrapper:
|
|
| 1075 |
|
| 1076 |
elif logic_penalty_target == 'ALL_PATHOLOGY':
|
| 1077 |
# Penalize if it implies pathology (i.e., NOT Normal/Healthy/Benign/Non-specific)
|
| 1078 |
-
# Safe approach: If it's NOT explicitly "Normal", "Healthy", "Non-specific", penalize it.
|
| 1079 |
is_benign = "Normal" in label_text or "Healthy" in label_text or "Non-specific" in label_text or "Benign" in label_text
|
| 1080 |
if not is_benign:
|
| 1081 |
should_penalize = True
|
| 1082 |
|
| 1083 |
else:
|
| 1084 |
-
#
|
| 1085 |
-
if logic_penalty_target
|
|
|
|
|
|
|
| 1086 |
should_penalize = True
|
| 1087 |
|
| 1088 |
if should_penalize:
|
|
|
|
| 551 |
# =========================================================================
|
| 552 |
# MEDICAL DOMAINS CONFIGURATION
|
| 553 |
# =========================================================================
|
| 554 |
+
from medical_labels import MEDICAL_DOMAINS, LABEL_TRANSLATIONS_FR, DOMAIN_TRANSLATIONS_FR
|
| 555 |
+
|
| 556 |
+
# =========================================================================
|
| 557 |
+
# PYDANTIC MODELS
|
| 558 |
+
# =========================================================================
|
| 559 |
+
class JobStatus(str, Enum):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
|
| 561 |
# =========================================================================
|
| 562 |
# PYDANTIC MODELS
|
|
|
|
| 745 |
|
| 746 |
def predict(self, image_bytes: bytes, username: str = None) -> Dict[str, Any]:
|
| 747 |
"""Run hierarchical inference using SigLIP Zero-Shot."""
|
| 748 |
+
# ... (rest of function until line 1094) ...
|
| 749 |
# I need to match the indentation and context.
|
| 750 |
# Since I can't see "inside" the dots in a replace, I have to be careful.
|
| 751 |
# It's better to update just the definition line and the call to enhance_analysis_result.
|
|
|
|
| 765 |
import torch
|
| 766 |
import pydicom
|
| 767 |
|
| 768 |
+
# ========================================================
|
| 769 |
+
# LOCALIZATION HELPER
|
| 770 |
+
# ========================================================
|
| 771 |
+
def localize_result(result_json: Dict[str, Any]) -> Dict[str, Any]:
|
| 772 |
+
"""
|
| 773 |
+
Translate the analysis result to French using Canonical IDs.
|
| 774 |
+
This allows the Model to run in English and the UI to display in French.
|
| 775 |
+
"""
|
| 776 |
+
localized = result_json.copy()
|
| 777 |
+
|
| 778 |
+
# 1. Translate Domain
|
| 779 |
+
domain_key = localized.get('domain', {}).get('label')
|
| 780 |
+
if domain_key in DOMAIN_TRANSLATIONS_FR:
|
| 781 |
+
localized['domain']['label_fr'] = DOMAIN_TRANSLATIONS_FR[domain_key]
|
| 782 |
+
localized['domain']['label'] = DOMAIN_TRANSLATIONS_FR[domain_key] # Override for simple UI
|
| 783 |
+
|
| 784 |
+
# 2. Translate Specific Results
|
| 785 |
+
if 'specific' in localized:
|
| 786 |
+
new_specific = []
|
| 787 |
+
for item in localized['specific']:
|
| 788 |
+
label_id = item.get('label_id')
|
| 789 |
+
translation = LABEL_TRANSLATIONS_FR.get(label_id)
|
| 790 |
+
|
| 791 |
+
if translation:
|
| 792 |
+
new_item = item.copy()
|
| 793 |
+
new_item['label'] = translation['short'] # Use Short Title for UI
|
| 794 |
+
new_item['description'] = translation['long'] # Use Long Description
|
| 795 |
+
new_item['severity'] = translation.get('severity', 'medium')
|
| 796 |
+
new_specific.append(new_item)
|
| 797 |
+
else:
|
| 798 |
+
# Fallback if ID missing (should not happen in strict mode)
|
| 799 |
+
new_specific.append(item)
|
| 800 |
+
|
| 801 |
+
localized['specific'] = new_specific
|
| 802 |
+
|
| 803 |
+
# 3. Handle QC failure case (already localized manually in rejection_result)
|
| 804 |
+
if 'diagnosis' in localized and "Analyse Refusée" in localized['diagnosis']:
|
| 805 |
+
pass # Already localized string
|
| 806 |
+
|
| 807 |
+
return localized
|
| 808 |
+
|
| 809 |
# Image preprocessing functions
|
| 810 |
def process_dicom(file_bytes: bytes) -> Tuple[Image.Image, Dict[str, Any]]:
|
| 811 |
"""Convert DICOM bytes to PIL Image with tags."""
|
|
|
|
| 977 |
logger.info("Triage indicates Normal/Healthy. Skipping Stage 2.")
|
| 978 |
else:
|
| 979 |
# Flat Mode (Thoracic, Dermato, etc.)
|
| 980 |
+
specific_items = domain_config['specific_labels']
|
| 981 |
+
# Extract text prompts for CLIP
|
| 982 |
+
labels_en = [item['label_en'] for item in specific_items]
|
| 983 |
|
| 984 |
inputs_specific = self.processor(
|
| 985 |
+
text=labels_en,
|
| 986 |
images=image,
|
| 987 |
padding="max_length",
|
| 988 |
return_tensors="pt"
|
|
|
|
| 993 |
|
| 994 |
probs_specific = torch.softmax(outputs_specific.logits_per_image, dim=1)[0]
|
| 995 |
|
| 996 |
+
for i, item in enumerate(specific_items):
|
| 997 |
specific_results.append({
|
| 998 |
+
"label_id": item['id'],
|
| 999 |
+
"label": item['label_en'], # Keep EN for internal logic
|
| 1000 |
"probability": round(float(probs_specific[i] * 100), 2)
|
| 1001 |
})
|
| 1002 |
|
|
|
|
| 1008 |
|
| 1009 |
for res in specific_results:
|
| 1010 |
should_penalize = False
|
| 1011 |
+
label_text = res['label'] # English text
|
| 1012 |
+
label_id = res['label_id']
|
| 1013 |
|
| 1014 |
if logic_penalty_target == 'ALL_DIAGNOSIS':
|
| 1015 |
# Penalize EVERYTHING (e.g. Poor Quality Image)
|
|
|
|
| 1016 |
# Heuristic: If label contains "Artifact" or "Quality" or "Partial", don't penalize.
|
| 1017 |
if "Artifact" in label_text or "Quality" in label_text or "Partial" in label_text or "Empty" in label_text:
|
| 1018 |
pass
|
|
|
|
| 1021 |
|
| 1022 |
elif logic_penalty_target == 'ALL_PATHOLOGY':
|
| 1023 |
# Penalize if it implies pathology (i.e., NOT Normal/Healthy/Benign/Non-specific)
|
|
|
|
| 1024 |
is_benign = "Normal" in label_text or "Healthy" in label_text or "Non-specific" in label_text or "Benign" in label_text
|
| 1025 |
if not is_benign:
|
| 1026 |
should_penalize = True
|
| 1027 |
|
| 1028 |
else:
|
| 1029 |
+
# ID Match (Canonical) OR String Match (Fallback)
|
| 1030 |
+
if logic_penalty_target == label_id:
|
| 1031 |
+
should_penalize = True
|
| 1032 |
+
elif logic_penalty_target in label_text:
|
| 1033 |
should_penalize = True
|
| 1034 |
|
| 1035 |
if should_penalize:
|
medical_labels.py
ADDED
|
@@ -0,0 +1,307 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Dict, List, Any
|
| 2 |
+
|
| 3 |
+
# =========================================================================
|
| 4 |
+
# CANONICAL MEDICAL DOMAINS CONFIGURATION (MODEL SOURCE OF TRUTH)
|
| 5 |
+
# =========================================================================
|
| 6 |
+
# - Prompts must be in ENGLISH (Model Language).
|
| 7 |
+
# - Labels must have a stable 'id'.
|
| 8 |
+
# - Logic Gates define structural/quality constraints.
|
| 9 |
+
|
| 10 |
+
MEDICAL_DOMAINS = {
|
| 11 |
+
'Thoracic': {
|
| 12 |
+
'id': 'DOM_THORACIC',
|
| 13 |
+
'domain_prompt': 'Chest X-Ray Analysis',
|
| 14 |
+
'specific_labels': [
|
| 15 |
+
{'id': 'TH_PNEUMONIA_VIRAL', 'label_en': 'Diffuse interstitial opacities or ground-glass pattern (Viral/Atypical Pneumonia)'},
|
| 16 |
+
{'id': 'TH_PNEUMONIA_BACT', 'label_en': 'Focal alveolar consolidation with air bronchograms (Bacterial Pneumonia)'},
|
| 17 |
+
{'id': 'TH_NORMAL', 'label_en': 'Normal chest radiograph: normal cardiothoracic ratio, clear lungs, no pleural abnormality'},
|
| 18 |
+
{'id': 'TH_PNEUMOTHORAX', 'label_en': 'Pneumothorax (Lung collapse)'},
|
| 19 |
+
{'id': 'TH_PLEURAL_EFFUSION', 'label_en': 'Pleural Effusion (Fluid)'},
|
| 20 |
+
{'id': 'TH_CARDIOMEGALY', 'label_en': 'Cardiomegaly with clear lung fields (no pulmonary edema)'},
|
| 21 |
+
{'id': 'TH_CARDIOMEGALY_EDEMA', 'label_en': 'Cardiomegaly with pulmonary congestion or edema'},
|
| 22 |
+
{'id': 'TH_EDEMA', 'label_en': 'Pulmonary Edema (without cardiomegaly)'},
|
| 23 |
+
{'id': 'TH_NODULE', 'label_en': 'Lung Nodule or Mass'},
|
| 24 |
+
{'id': 'TH_ATELECTASIS', 'label_en': 'Atelectasis (Lung collapse)'}
|
| 25 |
+
],
|
| 26 |
+
'logic_gate': {
|
| 27 |
+
'prompt': 'Evaluate cardiac silhouette size',
|
| 28 |
+
'labels': ['Normal cardiac size (CTR < 0.5)', 'Enlarged cardiac silhouette (Cardiomegaly)'],
|
| 29 |
+
'penalty_target': 'TH_NORMAL', # Penalize the ID of the normal label
|
| 30 |
+
'abnormal_index': 1
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
'Dermatology': {
|
| 34 |
+
'id': 'DOM_DERMATOLOGY',
|
| 35 |
+
'domain_prompt': 'Dermatoscopic analysis of a pigmented or non-pigmented skin lesion',
|
| 36 |
+
'specific_labels': [
|
| 37 |
+
{'id': 'DERM_NORMAL', 'label_en': 'Normal skin without visible lesion or abnormal pigmentation'},
|
| 38 |
+
{'id': 'DERM_NEVUS', 'label_en': 'Benign melanocytic nevus with symmetry and uniform pigmentation'},
|
| 39 |
+
{'id': 'DERM_SEBORRHEIC', 'label_en': 'Seborrheic keratosis (benign warty lesion)'},
|
| 40 |
+
{'id': 'DERM_MELANOMA', 'label_en': 'Malignant melanoma with asymmetry, irregular borders, and color variegation'},
|
| 41 |
+
{'id': 'DERM_BCC', 'label_en': 'Basal cell carcinoma (pearly or ulcerated lesion)'},
|
| 42 |
+
{'id': 'DERM_SCC', 'label_en': 'Squamous cell carcinoma (crusty or budding lesion)'},
|
| 43 |
+
{'id': 'DERM_INFLAMMATORY', 'label_en': 'Inflammatory skin lesion (Eczema, Psoriasis)'}
|
| 44 |
+
],
|
| 45 |
+
'logic_gate': {
|
| 46 |
+
'prompt': 'Is there a visible skin lesion?',
|
| 47 |
+
'labels': ['No visible skin lesion', 'Visible skin lesion (pigmented or non-pigmented)'],
|
| 48 |
+
'penalty_target': 'ALL_PATHOLOGY',
|
| 49 |
+
'abnormal_index': 0
|
| 50 |
+
}
|
| 51 |
+
},
|
| 52 |
+
'Histology': {
|
| 53 |
+
'id': 'DOM_HISTOLOGY',
|
| 54 |
+
'domain_prompt': 'Microscopic analysis of a histological section (H&E stain)',
|
| 55 |
+
'specific_labels': [
|
| 56 |
+
{'id': 'HIST_HEALTHY_BREAST', 'label_en': 'Healthy breast tissue with preserved lobular architecture'},
|
| 57 |
+
{'id': 'HIST_HEALTHY_PROSTATE', 'label_en': 'Healthy prostatic tissue with regular glands'},
|
| 58 |
+
{'id': 'HIST_IDC_BREAST', 'label_en': 'Invasive ductal carcinoma (Disorganized cells)'},
|
| 59 |
+
{'id': 'HIST_ADENO_PROSTATE', 'label_en': 'Prostate adenocarcinoma (Gland fusion)'},
|
| 60 |
+
{'id': 'HIST_DYSPLASIA', 'label_en': 'Cervical dysplasia or intraepithelial neoplasia'},
|
| 61 |
+
{'id': 'HIST_COLON_CA', 'label_en': 'Colon cancer tumor tissue'},
|
| 62 |
+
{'id': 'HIST_LUNG_CA', 'label_en': 'Lung cancer tumor tissue'},
|
| 63 |
+
{'id': 'HIST_ADIPOSE', 'label_en': 'Adipose tissue (Fat) or connective stroma'},
|
| 64 |
+
{'id': 'HIST_ARTIFACT', 'label_en': 'Preparation artifact, empty area, or blurred region'}
|
| 65 |
+
],
|
| 66 |
+
'logic_gate': {
|
| 67 |
+
'prompt': 'Assess histological validity of the image',
|
| 68 |
+
'labels': ['Adequate H&E tissue section', 'Artifact, empty area, or blurred region'],
|
| 69 |
+
'penalty_target': 'ALL_DIAGNOSIS',
|
| 70 |
+
'abnormal_index': 1
|
| 71 |
+
}
|
| 72 |
+
},
|
| 73 |
+
'Ophthalmology': {
|
| 74 |
+
'id': 'DOM_OPHTHALMOLOGY',
|
| 75 |
+
'domain_prompt': 'Fundus photography (Retina)',
|
| 76 |
+
'specific_labels': [
|
| 77 |
+
{'id': 'OPH_NORMAL', 'label_en': 'Normal retina with visible optic disc and macula'},
|
| 78 |
+
{'id': 'OPH_DIABETIC', 'label_en': 'Diabetic retinopathy (hemorrhages, exudates)'},
|
| 79 |
+
{'id': 'OPH_GLAUCOMA', 'label_en': 'Glaucoma (optic disc cupping)'},
|
| 80 |
+
{'id': 'OPH_AMD', 'label_en': 'Macular degeneration (drusen or atrophy)'}
|
| 81 |
+
],
|
| 82 |
+
'logic_gate': {
|
| 83 |
+
'prompt': 'Is the fundus image clinically interpretable?',
|
| 84 |
+
'labels': ['Good quality fundus image', 'Poor quality, uninterpretable or partial view'],
|
| 85 |
+
'penalty_target': 'ALL_DIAGNOSIS',
|
| 86 |
+
'abnormal_index': 1
|
| 87 |
+
}
|
| 88 |
+
},
|
| 89 |
+
'Orthopedics': {
|
| 90 |
+
'id': 'DOM_ORTHOPEDICS',
|
| 91 |
+
'domain_prompt': 'Bone X-Ray (Musculoskeletal)',
|
| 92 |
+
'stage_1_triage': {
|
| 93 |
+
'prompt': 'Anatomical region identification',
|
| 94 |
+
'labels': [
|
| 95 |
+
'Other x-ray view (Chest, Hand, Foot, Pediatric) - OUT OF DISTRIBUTION',
|
| 96 |
+
'A knee x-ray view (Knee Joint)'
|
| 97 |
+
]
|
| 98 |
+
},
|
| 99 |
+
'specific_labels': [
|
| 100 |
+
{'id': 'ORTH_OA_SEVERE', 'label_en': 'Severe osteoarthritis (Grade 4)'},
|
| 101 |
+
{'id': 'ORTH_OA_MODERATE', 'label_en': 'Moderate osteoarthritis (Grade 2-3)'},
|
| 102 |
+
{'id': 'ORTH_NORMAL', 'label_en': 'Normal knee'},
|
| 103 |
+
{'id': 'ORTH_IMPLANT', 'label_en': 'Implant'}
|
| 104 |
+
],
|
| 105 |
+
'stage_2_diagnosis': {
|
| 106 |
+
'prompt': 'Knee Osteoarthritis Severity Assessment',
|
| 107 |
+
'labels': [
|
| 108 |
+
{'id': 'ORTH_OA_SEVERE', 'label_en': 'Severe osteoarthritis with bone-on-bone contact (Grade 4)'},
|
| 109 |
+
{'id': 'ORTH_OA_MODERATE', 'label_en': 'Moderate osteoarthritis with definite joint space narrowing (Grade 2-3)'},
|
| 110 |
+
{'id': 'ORTH_NORMAL', 'label_en': 'Normal knee joint with preserved joint space (Grade 0-1)'},
|
| 111 |
+
{'id': 'ORTH_IMPLANT', 'label_en': 'Total knee arthroplasty (TKA) with metallic implant'},
|
| 112 |
+
{'id': 'ORTH_FRACTURE', 'label_en': 'Acute knee fracture or dislocation'}
|
| 113 |
+
]
|
| 114 |
+
},
|
| 115 |
+
'logic_gate': {
|
| 116 |
+
'prompt': 'Is there a metallic implant?',
|
| 117 |
+
'labels': ['Native knee joint', 'Knee with metallic implant (Arthroplasty)'],
|
| 118 |
+
'penalty_target': 'ORTH_OA', # Logic target string match (Prefix)
|
| 119 |
+
'abnormal_index': 1
|
| 120 |
+
}
|
| 121 |
+
}
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
# =========================================================================
|
| 125 |
+
# FRENCH TRANSLATIONS (USER INTERFACE ONLY)
|
| 126 |
+
# =========================================================================
|
| 127 |
+
# - Strict Mapping: ID -> {title, description}
|
| 128 |
+
# - No dynamic translation allowed.
|
| 129 |
+
|
| 130 |
+
LABEL_TRANSLATIONS_FR = {
|
| 131 |
+
# --- THORACIC ---
|
| 132 |
+
'TH_NORMAL': {
|
| 133 |
+
'short': 'Thorax sans anomalie',
|
| 134 |
+
'long': 'Silhouette cardiaque normale, poumons clairs, pas d’épanchement.',
|
| 135 |
+
'severity': 'low'
|
| 136 |
+
},
|
| 137 |
+
'TH_PNEUMONIA_VIRAL': {
|
| 138 |
+
'short': 'Pneumonie Virale / Atypique',
|
| 139 |
+
'long': 'Opacités interstitielles diffuses ou verre dépoli.',
|
| 140 |
+
'severity': 'high'
|
| 141 |
+
},
|
| 142 |
+
'TH_PNEUMONIA_BACT': {
|
| 143 |
+
'short': 'Pneumonie Bactérienne',
|
| 144 |
+
'long': 'Consolidation alvéolaire focale avec bronchogramme aérien.',
|
| 145 |
+
'severity': 'high'
|
| 146 |
+
},
|
| 147 |
+
'TH_PNEUMOTHORAX': {
|
| 148 |
+
'short': 'Pneumothorax',
|
| 149 |
+
'long': 'Présence possible d’air dans la cavité pleurale (collapsus).',
|
| 150 |
+
'severity': 'emergency'
|
| 151 |
+
},
|
| 152 |
+
'TH_PLEURAL_EFFUSION': {
|
| 153 |
+
'short': 'Épanchement Pleural',
|
| 154 |
+
'long': 'Accumulation de liquide dans l’espace pleural.',
|
| 155 |
+
'severity': 'medium'
|
| 156 |
+
},
|
| 157 |
+
'TH_CARDIOMEGALY': {
|
| 158 |
+
'short': 'Cardiomégalie (Poumons clairs)',
|
| 159 |
+
'long': 'Silhouette cardiaque augmentée de taille sans signe d’œdème pulmonaire.',
|
| 160 |
+
'severity': 'medium'
|
| 161 |
+
},
|
| 162 |
+
'TH_CARDIOMEGALY_EDEMA': {
|
| 163 |
+
'short': 'Cardiomégalie avec Stase',
|
| 164 |
+
'long': 'Cœur augmenté de taille associé à une congestion pulmonaire.',
|
| 165 |
+
'severity': 'high'
|
| 166 |
+
},
|
| 167 |
+
'TH_EDEMA': {
|
| 168 |
+
'short': 'Œdème Pulmonaire',
|
| 169 |
+
'long': 'Surcharge liquidienne pulmonaire (sans cardiomégalie évidente).',
|
| 170 |
+
'severity': 'high'
|
| 171 |
+
},
|
| 172 |
+
'TH_NODULE': {
|
| 173 |
+
'short': 'Nodule ou Masse Pulmonaire',
|
| 174 |
+
'long': 'Lésion focale suspecte nécessitant un scanner de contrôle.',
|
| 175 |
+
'severity': 'high'
|
| 176 |
+
},
|
| 177 |
+
'TH_ATELECTASIS': {
|
| 178 |
+
'short': 'Atélectasie',
|
| 179 |
+
'long': 'Affaissement d’une partie du poumon.',
|
| 180 |
+
'severity': 'medium'
|
| 181 |
+
},
|
| 182 |
+
|
| 183 |
+
# --- DERMATOLOGY ---
|
| 184 |
+
'DERM_NORMAL': {
|
| 185 |
+
'short': 'Peau saine / Pas de lésion',
|
| 186 |
+
'long': 'Aucune lésion dermatologique suspecte visible.',
|
| 187 |
+
'severity': 'low'
|
| 188 |
+
},
|
| 189 |
+
'DERM_NEVUS': {
|
| 190 |
+
'short': 'Nævus Bénin (Grain de beauté)',
|
| 191 |
+
'long': 'Lésion régulière, symétrique et homogène.',
|
| 192 |
+
'severity': 'low'
|
| 193 |
+
},
|
| 194 |
+
'DERM_SEBORRHEIC': {
|
| 195 |
+
'short': 'Kératose Séborrhéique',
|
| 196 |
+
'long': 'Lésion bénigne fréquente ("verrue de vieillesse").',
|
| 197 |
+
'severity': 'low'
|
| 198 |
+
},
|
| 199 |
+
'DERM_MELANOMA': {
|
| 200 |
+
'short': 'Suspicion de Mélanome',
|
| 201 |
+
'long': 'Lésion pigmentée asymétrique, bords irréguliers (critères ABCDE). Urgence.',
|
| 202 |
+
'severity': 'emergency'
|
| 203 |
+
},
|
| 204 |
+
'DERM_BCC': {
|
| 205 |
+
'short': 'Carcinome Basocellulaire',
|
| 206 |
+
'long': 'Lésion perlée ou ulcérée suggérant un carcinome non-mélanique.',
|
| 207 |
+
'severity': 'high'
|
| 208 |
+
},
|
| 209 |
+
'DERM_SCC': {
|
| 210 |
+
'short': 'Carcinome Épidermoïde',
|
| 211 |
+
'long': 'Lésion croûteuse ou bourgeonnante suspecte.',
|
| 212 |
+
'severity': 'high'
|
| 213 |
+
},
|
| 214 |
+
'DERM_INFLAMMATORY': {
|
| 215 |
+
'short': 'Lésion Inflammatoire',
|
| 216 |
+
'long': 'Aspect compatible avec eczéma, psoriasis ou dermatite.',
|
| 217 |
+
'severity': 'medium'
|
| 218 |
+
},
|
| 219 |
+
|
| 220 |
+
# --- HISTOLOGY ---
|
| 221 |
+
'HIST_ARTIFACT': {
|
| 222 |
+
'short': 'Qualité Insuffisante (Artefact)',
|
| 223 |
+
'long': 'Tissu non interprétable (section vide, floue ou artefact technique).',
|
| 224 |
+
'severity': 'none'
|
| 225 |
+
},
|
| 226 |
+
'HIST_HEALTHY_BREAST': {
|
| 227 |
+
'short': 'Tissu Mammaire Sain',
|
| 228 |
+
'long': 'Architecture lobulaire préservée.',
|
| 229 |
+
'severity': 'low'
|
| 230 |
+
},
|
| 231 |
+
'HIST_IDC_BREAST': {
|
| 232 |
+
'short': 'Carcinome Canalaire Infiltrant',
|
| 233 |
+
'long': 'Prolifération cellulaire désorganisée invasive (Sein).',
|
| 234 |
+
'severity': 'high'
|
| 235 |
+
},
|
| 236 |
+
'HIST_HEALTHY_PROSTATE': {
|
| 237 |
+
'short': 'Tissu Prostatique Sain',
|
| 238 |
+
'long': 'Glandes régulières, stroma normal.',
|
| 239 |
+
'severity': 'low'
|
| 240 |
+
},
|
| 241 |
+
'HIST_ADENO_PROSTATE': {
|
| 242 |
+
'short': 'Adénocarcinome Prostatique',
|
| 243 |
+
'long': 'Fusion glandulaire et atypies cytonucléaires.',
|
| 244 |
+
'severity': 'high'
|
| 245 |
+
},
|
| 246 |
+
'HIST_COLON_CA': {'short': 'Cancer Colorectal', 'long': 'Tissu tumoral colique.', 'severity': 'high'},
|
| 247 |
+
'HIST_LUNG_CA': {'short': 'Cancer Pulmonaire', 'long': 'Tissu tumoral pulmonaire.', 'severity': 'high'},
|
| 248 |
+
'HIST_DYSPLASIA': {'short': 'Dysplasie / CIN', 'long': 'Anomalies précancéreuses.', 'severity': 'medium'},
|
| 249 |
+
'HIST_ADIPOSE': {'short': 'Tissu Adipeux / Stroma', 'long': 'Tissu de soutien normal.', 'severity': 'low'},
|
| 250 |
+
|
| 251 |
+
# --- OPHTHALMOLOGY ---
|
| 252 |
+
'OPH_NORMAL': {
|
| 253 |
+
'short': 'Fond d’œil Normal',
|
| 254 |
+
'long': 'Rétine, macula et papille d’aspect sain.',
|
| 255 |
+
'severity': 'low'
|
| 256 |
+
},
|
| 257 |
+
'OPH_DIABETIC': {
|
| 258 |
+
'short': 'Rétinopathie Diabétique',
|
| 259 |
+
'long': 'Présence d’hémorragies, exsudats ou anévrismes.',
|
| 260 |
+
'severity': 'high'
|
| 261 |
+
},
|
| 262 |
+
'OPH_GLAUCOMA': {
|
| 263 |
+
'short': 'Suspicion de Glaucome',
|
| 264 |
+
'long': 'Excavation papillaire (cup/disc ratio) augmentée.',
|
| 265 |
+
'severity': 'high'
|
| 266 |
+
},
|
| 267 |
+
'OPH_AMD': {
|
| 268 |
+
'short': 'DMLA',
|
| 269 |
+
'long': 'Dégénérescence Maculaire (drusens ou atrophie).',
|
| 270 |
+
'severity': 'medium'
|
| 271 |
+
},
|
| 272 |
+
|
| 273 |
+
# --- ORTHOPEDICS ---
|
| 274 |
+
'ORTH_NORMAL': {
|
| 275 |
+
'short': 'Genou Normal',
|
| 276 |
+
'long': 'Interligne articulaire préservé, pas d’ostéophyte.',
|
| 277 |
+
'severity': 'low'
|
| 278 |
+
},
|
| 279 |
+
'ORTH_OA_MODERATE': {
|
| 280 |
+
'short': 'Arthrose Modérée (Grade 2-3)',
|
| 281 |
+
'long': 'Pincement articulaire visible et ostéophytes.',
|
| 282 |
+
'severity': 'medium'
|
| 283 |
+
},
|
| 284 |
+
'ORTH_OA_SEVERE': {
|
| 285 |
+
'short': 'Arthrose Sévère (Grade 4)',
|
| 286 |
+
'long': 'Disparition de l’interligne (os sur os), déformation.',
|
| 287 |
+
'severity': 'high'
|
| 288 |
+
},
|
| 289 |
+
'ORTH_IMPLANT': {
|
| 290 |
+
'short': 'Prothèse Totale (PTG)',
|
| 291 |
+
'long': 'Genou avec implant métallique (Arthroplastie).',
|
| 292 |
+
'severity': 'low'
|
| 293 |
+
},
|
| 294 |
+
'ORTH_FRACTURE': {
|
| 295 |
+
'short': 'Fracture Récente / Luxation',
|
| 296 |
+
'long': 'Solution de continuité osseuse ou perte de congruence.',
|
| 297 |
+
'severity': 'emergency'
|
| 298 |
+
}
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
DOMAIN_TRANSLATIONS_FR = {
|
| 302 |
+
'Thoracic': 'Radiographie Thoracique',
|
| 303 |
+
'Dermatology': 'Dermatoscopie',
|
| 304 |
+
'Histology': 'Histopathologie (H&E)',
|
| 305 |
+
'Ophthalmology': 'Fond d’Oeil (Rétine)',
|
| 306 |
+
'Orthopedics': 'Radiographie Osseuse'
|
| 307 |
+
}
|