Spaces:
Sleeping
Sleeping
Upload 7 files
Browse filesFiles changed (7)
- README.md+21-13
- app.pyAdded+126
- constants.pyAdded+46
- interfaces.pyAdded+11
- models.pyAdded+22
- requirements.txtAdded+7
- scoring_service.pyAdded+115
12345678910111213---title: Webemoji: 🦀colorFrom: pinkcolorTo: yellowsdk: gradiosdk_version: 6.14.0python_version: '3.13'app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021---title: IOTN Assistantemoji: 🦷colorFrom: bluecolorTo: indigosdk: gradiosdk_version: 4.44.1python_version: 3.12app_file: app.pypinned: falselicense: mit---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference# IOTN Assistant (Dental Health Component)A professional-grade medical scoring application for calculating the Index of Orthodontic Treatment Need (IOTN).## 🚀 DeploymentThis app runs on Hugging Face Spaces using the Gradio SDK.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126import gradio as gr
from scoring_service import IOTNScoringService
from constants import SOURCE_NOTE
# Global service instance
service = IOTNScoringService()
def get_dhc_score(*args):
keys = [
"overjet_mm", "lip_competence", "reverse_overjet_mm",
"mastication_difficulty", "speech_difficulty",
"crossbite_present", "rcp_icp_discrepancy_mm", "posterior_lingual_crossbite_no_contact",
"max_contact_displacement_mm", "overbite_mm", "overbite_complete",
"gingival_trauma", "palatal_trauma",
"missing_teeth_UR", "missing_teeth_UL", "missing_teeth_LR", "missing_teeth_LL",
"space_closure_required", "restorative_implications",
"impeded_eruption", "cleft_lip_palate", "submerged_deciduous", "supplemental_teeth"
]
# Ensure all numerical args are handled safely (Gradio Number handles numeric enforcement)
payload = dict(zip(keys, args))
result = service.score(payload)
findings_list = [f"- **{f.code}**: {f.summary} ({f.evidence})" for f in result.findings]
findings_md = "### Matched Criteria\n" + "\n".join(findings_list) if findings_list else "No Grade 2+ criteria matched."
return (
f"## Grade {result.grade}",
f"**{result.classification}**: {result.subcode_label}",
result.summary,
findings_md
)
with gr.Blocks(title="IOTN Assistant", theme=gr.themes.Soft()) as demo:
gr.Markdown("# IOTN DHC Assistant")
with gr.Row():
# Input Section (Left)
with gr.Column(scale=3):
with gr.Tabs() as tabs:
with gr.TabItem("Overjet", id=0):
gr.Markdown("#### Anterior Horizontal Relationship")
with gr.Row():
oj_mm = gr.Number(label="Increased Overjet (mm)", value=0.0, precision=1, minimum=0)
lips = gr.Radio(
choices=["competent", "incompetent"],
value="competent",
label="Lip Competence"
)
with gr.Row():
roj_mm = gr.Number(label="Reverse Overjet (mm)", value=0.0, precision=1, minimum=0)
mast = gr.Checkbox(label="Masticatory Difficulty")
speech = gr.Checkbox(label="Speech Difficulty")
with gr.TabItem("Crossbite", id=1):
gr.Markdown("#### Transverse Relationship")
cb_present = gr.Checkbox(label="Crossbite Present")
rcp_icp = gr.Number(label="RCP-ICP Discrepancy (mm)", value=0.0, precision=1, minimum=0)
scissor = gr.Checkbox(label="Posterior lingual crossbite (No contact)")
with gr.TabItem("Displacement / Overbite", id=2):
gr.Markdown("#### Vertical & Crowding")
with gr.Row():
disp_mm = gr.Number(label="Max Displacement (mm)", value=0.0, precision=1, minimum=0)
ob_mm = gr.Number(label="Overbite Depth (mm)", value=0.0, precision=1, minimum=0)
with gr.Row():
ob_comp = gr.Checkbox(label="Complete Overbite")
ging_tr = gr.Checkbox(label="Gingival Trauma")
pal_tr = gr.Checkbox(label="Palatal Trauma")
with gr.TabItem("Missing / Special", id=3):
gr.Markdown("#### Hypodontia & Other Categories")
with gr.Row():
m_ur = gr.Number(label="UR Missing", value=0, precision=0, minimum=0)
m_ul = gr.Number(label="UL Missing", value=0, precision=0, minimum=0)
m_lr = gr.Number(label="LR Missing", value=0, precision=0, minimum=0)
m_ll = gr.Number(label="LL Missing", value=0, precision=0, minimum=0)
with gr.Row():
sp_cl = gr.Checkbox(label="Space Closure Required")
rest_imp = gr.Checkbox(label="Restorative Needs")
with gr.Row():
imp_er = gr.Checkbox(label="Impeded Eruption")
cleft = gr.Checkbox(label="Cleft Lip/Palate")
subm = gr.Checkbox(label="Submerged Deciduous")
supp = gr.Checkbox(label="Supplemental Teeth")
score_btn = gr.Button("Score Assessment", variant="primary", size="lg")
# Results Section (Right)
with gr.Column(scale=2):
with gr.Group():
gr.Markdown("### Assessment Result")
res_grade = gr.Markdown("## Grade -")
res_class = gr.Markdown("**Classification -**")
res_summary = gr.Textbox(label="Summary", interactive=False, lines=2)
res_findings = gr.Markdown("### Matched Criteria")
# Wire up events
all_inputs = [
oj_mm, lips, roj_mm, mast, speech,
cb_present, rcp_icp, scissor,
disp_mm, ob_mm, ob_comp, ging_tr, pal_tr,
m_ur, m_ul, m_lr, m_ll, sp_cl, rest_imp,
imp_er, cleft, subm, supp
]
score_btn.click(
get_dhc_score,
inputs=all_inputs,
outputs=[res_grade, res_class, res_summary, res_findings],
show_progress="minimal"
)
gr.Markdown(f"\n\n--- \n{SOURCE_NOTE}")
gr.Examples(
examples=[
[10.0, "competent", 0.0, False, False, False, 0.0, False, 0.0, 0.0, False, False, False, 0, 0, 0, 0, False, False, False, False, False, False],
[5.0, "incompetent", 0.0, False, False, False, 0.0, False, 0.0, 0.0, False, False, False, 0, 0, 0, 0, False, False, False, False, False, False],
[0.0, "competent", 0.0, False, False, False, 0.0, False, 0.0, 0.0, False, False, False, 0, 0, 0, 0, False, False, True, False, False, False],
],
inputs=all_inputs,
label="Quick Samples"
)
if __name__ == "__main__":
demo.queue().launch(server_name="0.0.0.0", server_port=7860)12345678910111213141516171819202122232425262728293031323334353637383940414243444546# IOTN DHC Grade Definitions
GRADE_LABELS = {
1: "No need",
2: "Little need",
3: "Moderate need",
4: "Great need",
5: "Very great need",
}
# Detailed Subcode Descriptions (Box 2.4)
SUBCODE_LABELS = {
"5a": "Increased overjet greater than 9 mm",
"5h": "Extensive hypodontia or multiple missing teeth",
"5i": "Impeded eruption of teeth",
"5m": "Reverse overjet greater than 3.5 mm with functional difficulty",
"5p": "Cleft lip, cleft palate, or craniofacial anomaly",
"5s": "Submerged deciduous teeth",
"4a": "Increased overjet greater than 6 mm and up to 9 mm",
"4b": "Reverse overjet greater than 3.5 mm without reported functional difficulty",
"4c": "Crossbite with discrepancy greater than 2 mm",
"4l": "Posterior lingual crossbite with no functional occlusal contact",
"4m": "Reverse overjet greater than 1 mm and up to 3.5 mm with functional difficulty",
"4t": "Partially erupted, tipped, or impacted teeth against adjacent teeth",
"4x": "Presence of supernumerary teeth",
"3a": "Increased overjet greater than 3.5 mm and up to 6 mm with incompetent lips",
"3b": "Reverse overjet greater than 1 mm and up to 3.5 mm without reported functional difficulty",
"3c": "Crossbite with discrepancy greater than 1 mm and up to 2 mm",
"3d": "Contact point displacement greater than 2 mm and up to 4 mm",
"3e": "Anterior or lateral open bite greater than 2 mm and up to 4 mm",
"3f": "Complete deep overbite without trauma",
"2a": "Increased overjet greater than 3.5 mm and up to 6 mm with competent lips",
"2b": "Reverse overjet greater than 0 mm and up to 1 mm",
"2c": "Crossbite with discrepancy up to 1 mm",
"2d": "Contact point displacement greater than 1 mm and up to 2 mm",
"2e": "Anterior or lateral open bite greater than 1 mm and up to 2 mm",
"2f": "Increased overbite without trauma",
"2g": "Pre-normal or post-normal occlusion with no other anomaly",
"1": "No need for treatment",
}
# Source Metadata
SOURCE_NOTE = (
"Based on Littlewood & Mitchell, An Introduction to Orthodontics (5th ed., 2019). "
"The Index of Orthodontic Treatment Need (IOTN) is copyrighted by The University of Manchester. "
"This tool is for educational and clinical decision support purposes only."
)1234567891011from typing import Protocol, runtime_checkable, Dict, Any
from models import DHCResult
@runtime_checkable
class IScoringEngine(Protocol):
"""
Interface for the IOTN Scoring Engine.
Enforces a strict contract for scoring implementations.
"""
def score(self, payload: Dict[str, Any]) -> DHCResult:
...12345678910111213141516171819202122from dataclasses import dataclass, field
from typing import List, Optional
@dataclass(frozen=True)
class Finding:
code: str
category: str
grade: int
summary: str
evidence: str
@dataclass(frozen=True)
class DHCResult:
grade: int
classification: str
grade_label: str
subcode_label: str
summary: str
justification: str
findings: List[Finding] = field(default_factory=list)
review_notes: List[str] = field(default_factory=list)
source_note: str = ""1234567gradio==4.44.1
fastapi==0.112.2
starlette==0.38.2
jinja2==3.1.4
uvicorn
pydantic
huggingface-hub==0.24.0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115from typing import Dict, Any, List
from models import Finding, DHCResult
from interfaces import IScoringEngine
from constants import GRADE_LABELS, SUBCODE_LABELS, SOURCE_NOTE
class IOTNScoringService(IScoringEngine):
"""
Service Layer: Implements the business logic for IOTN DHC scoring.
"""
def score(self, payload: Dict[str, Any]) -> DHCResult:
findings: List[Finding] = []
review_notes: List[str] = []
# Logic helpers
def get_f(k): return float(payload.get(k, 0) or 0)
def get_b(k): return bool(payload.get(k, False))
def get_i(k): return int(get_f(k))
def get_t(k): return str(payload.get(k, "")).strip().lower()
# 1. Overjet (MOCDO: O)
oj = get_f("overjet_mm")
lips = get_t("lip_competence")
if oj > 9:
findings.append(self._create_finding("5a", "overjet", f"Overjet {oj:g} mm"))
elif oj > 6:
findings.append(self._create_finding("4a", "overjet", f"Overjet {oj:g} mm"))
elif oj > 3.5:
if lips == "incompetent":
findings.append(self._create_finding("3a", "overjet", f"Overjet {oj:g} mm + Incompetent lips"))
elif lips == "competent":
findings.append(self._create_finding("2a", "overjet", f"Overjet {oj:g} mm + Competent lips"))
else:
findings.append(self._create_finding("3a", "overjet", f"Overjet {oj:g} mm (No lip data)"))
review_notes.append("Lip competence required for 3.6-6mm overjet.")
# 2. Reverse Overjet
roj = get_f("reverse_overjet_mm")
func = get_b("mastication_difficulty") or get_b("speech_difficulty")
if roj > 3.5:
findings.append(self._create_finding("5m" if func else "4b", "reverse_overjet", f"Reverse overjet {roj:g} mm"))
elif roj > 1:
findings.append(self._create_finding("4m" if func else "3b", "reverse_overjet", f"Reverse overjet {roj:g} mm"))
elif roj > 0:
findings.append(self._create_finding("2b", "reverse_overjet", f"Reverse overjet {roj:g} mm"))
# 3. Crossbites (MOCDO: C)
if get_b("posterior_lingual_crossbite_no_contact"):
findings.append(self._create_finding("4l", "crossbite", "Scissor bite (no contact)"))
if get_b("crossbite_present"):
ds = get_f("rcp_icp_discrepancy_mm")
code = "4c" if ds > 2 else ("3c" if ds > 1 else "2c")
findings.append(self._create_finding(code, "crossbite", f"Discrepancy {ds:g} mm"))
# 4. Displacement (MOCDO: D)
disp = get_f("max_contact_displacement_mm")
if disp > 4: findings.append(self._create_finding("4d", "displacement", f"Displacement {disp:g} mm"))
elif disp > 2: findings.append(self._create_finding("3d", "displacement", f"Displacement {disp:g} mm"))
elif disp > 1: findings.append(self._create_finding("2d", "displacement", f"Displacement {disp:g} mm"))
# 5. Overbite (MOCDO: O)
ob = get_f("overbite_mm")
comp = get_b("overbite_complete")
trauma = get_b("gingival_trauma") or get_b("palatal_trauma")
if comp and trauma: findings.append(self._create_finding("4f", "overbite", "Deep bite + trauma"))
elif comp: findings.append(self._create_finding("3f", "overbite", "Complete deep bite"))
elif ob >= 3.5: findings.append(self._create_finding("2f", "overbite", f"Overbite {ob:g} mm"))
# 6. Hypodontia (MOCDO: M - Missing)
m_counts = [get_i(f"missing_teeth_{q}") for q in ["UR", "UL", "LR", "LL"]]
mx = max(m_counts)
rest = get_b("restorative_implications") or get_b("space_opening_required")
if mx > 1 and rest:
findings.append(self._create_finding("5h", "missing", "Extensive hypodontia"))
elif (mx > 0 or any(m_counts)) and get_b("space_closure_required"):
findings.append(self._create_finding("4h", "missing", "Hypodontia (space closure)"))
# 7. Specials
if get_b("impeded_eruption"): findings.append(self._create_finding("5i", "eruption", "Impeded eruption"))
if get_b("cleft_lip_palate"): findings.append(self._create_finding("5p", "cleft", "Cleft lip/palate"))
if get_b("submerged_deciduous"): findings.append(self._create_finding("5s", "eruption", "Submerged deciduous"))
if get_b("supplemental_teeth"): findings.append(self._create_finding("4x", "supplemental", "Supplemental teeth"))
# Resolution: Highest grade wins
findings = sorted(self._unique(findings), key=lambda x: (-x.grade, x.code))
top = findings[0] if findings else None
return DHCResult(
grade=top.grade if top else 1,
classification=top.code if top else "1",
grade_label=GRADE_LABELS.get(top.grade if top else 1, "No need"),
subcode_label=SUBCODE_LABELS.get(top.code if top else "1", "No need"),
summary=top.summary if top else "No Grade 2+ criteria met.",
justification=top.evidence if top else "No qualifying feature.",
findings=findings,
review_notes=list(set(review_notes)),
source_note=SOURCE_NOTE
)
def _create_finding(self, code: str, cat: str, evidence: str) -> Finding:
return Finding(
code=code,
category=cat,
grade=int(code[0]),
summary=SUBCODE_LABELS.get(code, "Unknown"),
evidence=evidence
)
def _unique(self, findings: List[Finding]) -> List[Finding]:
res, seen = [], set()
for f in findings:
if f.code not in seen:
res.append(f)
seen.add(f.code)
return resREADME.md+21-13
app.pyAdded+126
constants.pyAdded+46
interfaces.pyAdded+11
models.pyAdded+22
requirements.txtAdded+7
scoring_service.pyAdded+115