CoolFace
Apppublic

Kirthis/TraceMind

sourceHugging Faceupdated 1mo agoView on Hugging Face
0likes
App README

๐Ÿฉบ TraceMind

Neurosymbolic AI-Driven Pediatric Triage โ€” Research Prototype

A research-grade neurosymbolic system demonstrating secure LLM integration with symbolic reasoning, knowledge graphs, and comprehensive security hardening. Originally a UC Berkeley final project, significantly enhanced post-graduation with production-grade architecture, enterprise-level security practices, and audit logging capabilities.

License: MIT Python 3.9+ Security: Hardened Status: Research Prototype


๐ŸŽฏ Overview

TraceMind demonstrates a neurosymbolic approach to clinical triage, combining:

  • โ€”Natural language processing via LLM for parent/caregiver communication
  • โ€”Symbolic reasoning using PyDatalog rule engine for explicit logic
  • โ€”Knowledge graphs (SNOMED-CT) for evidence-based decision support
  • โ€”Comprehensive audit trails for transparency and compliance

Scope: Pediatric triage across 4 clinical conditions:

  • โ€”๐ŸŒก๏ธ Fever + GI symptoms + dehydration (NIH/Seattle Children's CPG)
  • โ€”๐Ÿซ Asthma Exacerbation with severity stratification (NIH NAEPP CPG)
  • โ€”๐Ÿ”ด Allergic Reaction / Anaphylaxis with multi-system assessment (AAP/AAAAI)
  • โ€”๐Ÿ›‘ Croup (Laryngotracheobronchitis) with airway assessment (AAP CPG)

Ages: 3 months - 12 years Purpose: Research demonstration showcasing secure, explainable neurosymbolic AI in healthcare Origins: UC Berkeley final project, enhanced post-graduation with production-grade security & architecture

What it demonstrates:

  • โ€”โœ… Modular CPG architecture (4 conditions, expandable for more)
  • โ€”โœ… Secure LLM integration (jailbreak & injection resistant)
  • โ€”โœ… Transparent decision logic (rule traces & audit logs)
  • โ€”โœ… Production-grade architecture (LangGraph, audit trails, validation)
  • โ€”โœ… Clinical safety patterns (medication flags, antibiotic stewardship)

Key Technical Features

FeatureDetails
Condition RoutingChief complaint detection (fever/asthma/anaphylaxis/croup) โ†’ condition-specific CPG
LLM InterpretationParent input โ†’ structured CaseFields (30+ clinical parameters, condition-aware)
Knowledge GraphSNOMED-CT concepts + CPG evidence for all 4 conditions via Neo4j
Symbolic ReasoningCondition-specific rule engines (PyDatalog fever + dedicated CPG modules)
ExplainabilityDecision rationale linked to rules fired + evidence retrieved + CPG basis
Audit LoggingImmutable audit trail (timestamps, rules, evidence, user input, condition)
Input ValidationRange checking (age, vitals), fuzzy matching, contradiction detection
Security HardenedJailbreak-resistant prompts, injection-defended extraction, input sanitization
Multi-turn TrackingConversation context, consistency validation, field accumulation across turns

๐Ÿฉบ Supported Clinical Conditions

1. ๐ŸŒก๏ธ Fever (Pediatric Fever Management)

  • โ€”CPG: NIH/Seattle Children's Hospital CPG
  • โ€”Assessment: Temperature, alertness, breathing, fluid intake, urination, dehydration risk
  • โ€”Dispositions: Home Management, Urgent Same-Day, ER Now
  • โ€”Special Rules: Infant <3mo fever rule, seizure monitoring, antibiotic stewardship

Extended Usecases:

2. ๐Ÿซ Asthma Exacerbation (Respiratory Emergency)

  • โ€”CPG: NIH NAEPP (National Asthma Education & Prevention Program)
  • โ€”Assessment: Wheeze, oxygen saturation, speech ability, retractions, respiratory rate
  • โ€”Severity: Mild (home) โ†’ Moderate (urgent) โ†’ Severe (ER)
  • โ€”Special Rules: Hypoxia alert, altered mental status, prior intubation history

3. ๐Ÿ”ด Allergic Reaction / Anaphylaxis (Immune Emergency)

  • โ€”CPG: AAP/AAAAI (American Academy of Pediatrics / American Academy of Allergy)
  • โ€”Assessment: Urticaria, angioedema, breathing, GI symptoms, cardiovascular signs
  • โ€”Discrimination: Mild hives (home) vs significant reaction (urgent) vs anaphylaxis (ER/CALL 911)
  • โ€”Special Rules: Multi-system assessment, biphasic reaction monitoring, epinephrine alert

4. ๐Ÿ›‘ Croup (Laryngotracheobronchitis - Airway Emergency)

  • โ€”CPG: AAP Croup Management (Westley scoring concept)
  • โ€”Assessment: Barky cough, stridor type (inspiratory/biphasic), retractions, oxygen, vocal ability
  • โ€”Severity: Mild (home) โ†’ Moderate (dexamethasone+urgent) โ†’ Severe (ER)
  • โ€”Special Rules: Epiglottitis alert (high fever + stridor + drooling), racemic epinephrine for ER

Test Scenarios

Each condition includes 3 preset scenarios (12 total):

  • โ€”โœ… Home Management: Mild presentation, safe for home care
  • โ€”โœ… Urgent Same-Day: Moderate severity, needs same-day evaluation
  • โ€”โœ… ER Now: Severe presentation or red flags, emergency transport

๐Ÿ“‹ See [Docs/PRESET_SCENARIOS.txt](Docs/PRESET_SCENARIOS.txt) for all 12 scenarios with turn-by-turn examples.


๐Ÿ” Security & Quality

Comprehensive Security Testing:

  • โ€”โœ… 6 LLM prompt injection vectors โ€” all blocked
  • โ€”โœ… 19 adversarial attack cases โ€” all defended
  • โ€”โœ… 100+ attack scenario catalog โ€” thoroughly tested
  • โ€”โœ… Input validation suite โ€” range & format checking
  • โ€”โœ… Contradiction detection โ€” multi-turn consistency

๐Ÿ“‹ See [security_tests/reports/](security_tests/reports/) for detailed findings and proof of hardening.


๐Ÿ—๏ธ Architecture

User Input (natural language)
        โ†“
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Interpretation     โ”‚  LLM or heuristics โ†’ CaseFields
  โ”‚  (tracemind/agents) โ”‚  (age, temp, alertness, fluids, urine)
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ†“
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Knowledge Graph    โ”‚  SNOMED-CT retrieval
  โ”‚  (tracemind/graph)  โ”‚  Neo4j mini-KG for fever CPG
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ†“
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Safety Logic       โ”‚  PyDatalog rules
  โ”‚  (tracemind/logic)  โ”‚  Triage rules (R_CPG_SEIZURE, etc.)
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ†“
  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚  Explanation        โ”‚  Clinical rationale + safety netting
  โ”‚  (tracemind/agents) โ”‚  Medication flags & guidance
  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             โ†“
  Disposition (ER_NOW, URGENT_SAME_DAY, HOME_MANAGEMENT)
  + Explanation + Audit Log

See [Docs/ARCHITECTURE.md](Docs/ARCHITECTURE.md) for detailed control flow.


๐Ÿ“Š Project Structure

tracemind/
โ”œโ”€โ”€ README.md                          (this file)
โ”œโ”€โ”€ requirements.txt                   (dependencies)
โ”œโ”€โ”€ .env.example                       (configuration template)
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ tracemind/                      (Python package)
โ”‚   โ”œโ”€โ”€ main.py                        (CLI entry point)
โ”‚   โ”œโ”€โ”€ ui_streamlit.py                (Web UI)
โ”‚   โ”œโ”€โ”€ config.py                      (settings & env handling)
โ”‚   โ”œโ”€โ”€ state.py                       (case state management)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ agents/                     (LLM & heuristic agents)
โ”‚   โ”‚   โ”œโ”€โ”€ interpretation.py          (natural language โ†’ CaseFields)
โ”‚   โ”‚   โ”œโ”€โ”€ explanation.py             (decision rationale)
โ”‚   โ”‚   โ””โ”€โ”€ medication.py              (safety flags)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ graph/                      (Knowledge graph retrieval - condition-aware)
โ”‚   โ”‚   โ”œโ”€โ”€ neo4j_client.py            (Neo4j driver)
โ”‚   โ”‚   โ”œโ”€โ”€ snomed_retrieval.py        (SNOMED-CT lookups)
โ”‚   โ”‚   โ”œโ”€โ”€ fever_cpg_mentions.py      (fever CPG mapping)
โ”‚   โ”‚   โ”œโ”€โ”€ kg_loader.py               (load SNOMED-CT + CPG concepts for all conditions)
โ”‚   โ”‚   โ””โ”€โ”€ condition_kg_mappings.py   (asthma/anaphylaxis/croup KG integration)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ logic/                      (Symbolic reasoning & CPG rules)
โ”‚   โ”‚   โ”œโ”€โ”€ triage_rules.py            (condition routing + PyDatalog fever rules)
โ”‚   โ”‚   โ”œโ”€โ”€ cpg_asthma.py              (NIH NAEPP asthma severity assessment)
โ”‚   โ”‚   โ”œโ”€โ”€ cpg_anaphylaxis.py         (AAP/AAAAI anaphylaxis logic)
โ”‚   โ”‚   โ”œโ”€โ”€ cpg_croup.py               (AAP croup (Westley) assessment)
โ”‚   โ”‚   โ”œโ”€โ”€ contradiction_detector.py  (consistency checking)
โ”‚   โ”‚   โ””โ”€โ”€ multiturn_consistency.py   (conversation tracking)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ orchestration/              (LangGraph workflow)
โ”‚   โ”‚   โ””โ”€โ”€ graph.py                   (interpret โ†’ KG โ†’ safety โ†’ explain)
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ audit/                      (Compliance & logging)
โ”‚   โ”‚   โ””โ”€โ”€ postgres_logger.py         (Neon Postgres audit trail)
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ ๐Ÿ“ evaluation/                 (Testing & benchmarking)
โ”‚       โ”œโ”€โ”€ scenarios.csv              (test case catalog)
โ”‚       โ”œโ”€โ”€ harness.py                 (evaluation runner)
โ”‚       โ””โ”€โ”€ ragas_test_harness.py      (RAGAS metric evaluation)
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ security_tests/                 (Security testing)
โ”‚   โ”œโ”€โ”€ README.md                      (security suite documentation)
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ test_scripts/               (runnable tests)
โ”‚   โ”‚   โ”œโ”€โ”€ test_adversarial.py        (19 adversarial cases)
โ”‚   โ”‚   โ”œโ”€โ”€ test_prompt_injection_llm.py (6 injection tests)
โ”‚   โ”‚   โ””โ”€โ”€ debug_llm_extraction.py    (diagnostic tools)
โ”‚   โ””โ”€โ”€ ๐Ÿ“ reports/                    (security findings - SHOWCASE)
โ”‚       โ”œโ”€โ”€ SECURITY_AUDIT_SUMMARY.md
โ”‚       โ”œโ”€โ”€ ADVERSARIAL_TEST_CASES.md
โ”‚       โ”œโ”€โ”€ LLM_PROMPT_INJECTION_REPORT.md
โ”‚       โ”œโ”€โ”€ VULNERABILITY_FINDINGS.md
โ”‚       โ””โ”€โ”€ PHASE1_TEST_RESULTS.md
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ Docs/                           (Documentation)
โ”‚   โ”œโ”€โ”€ ARCHITECTURE.md                (detailed architecture)
โ”‚   โ”œโ”€โ”€ CPG Fever - Seattle Children's.pdf
โ”‚   โ””โ”€โ”€ screenshots/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ KG_implementation/              (Knowledge graph setup)
โ”‚   โ””โ”€โ”€ Pediatric_Fever_KG_*.ipynb    (Jupyter notebooks)
โ”‚
โ””โ”€โ”€ ๐Ÿ“ scripts/                        (Utility scripts)
    โ””โ”€โ”€ extract_cpg_pdf.py             (PDF extraction)

๐Ÿš€ Quick Start

Prerequisites

  • โ€”Python 3.9+
  • โ€”pip or conda

Installation

bash
# Clone and navigate
cd TraceMind/tracemind

# Create virtual environment
python3 -m venv .venv
source .venv/bin/activate          # macOS/Linux
# or
.venv\Scripts\activate              # Windows

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env

Run Interactive CLI

bash
# Mock mode (no LLM or Neo4j required)
TRACEMIND_MOCK_LLM=1 TRACEMIND_SKIP_NEO4J=1 python -m tracemind.main

# With OpenAI + Neo4j (set .env first)
python -m tracemind.main

Run Web UI (Streamlit)

bash
# Mock mode
TRACEMIND_MOCK_LLM=1 TRACEMIND_SKIP_NEO4J=1 streamlit run tracemind/ui_streamlit.py

# With OpenAI + Neo4j
streamlit run tracemind/ui_streamlit.py

Open browser to http://localhost:8501


๐Ÿงช Testing

Run Security Tests

bash
# All tests
python -m security_tests.test_scripts.test_adversarial
python -m security_tests.test_scripts.test_prompt_injection_llm
python -m security_tests.test_scripts.debug_llm_extraction

# View security reports
ls -lh security_tests/reports/

Run Scenario Evaluation

bash
TRACEMIND_MOCK_LLM=1 TRACEMIND_SKIP_NEO4J=1 python -m tracemind.evaluation

# Custom scenario file
python -m tracemind.evaluation path/to/scenarios.csv

Expected: Exit code 0 when all scenarios match expected disposition.


๐Ÿ”ง Configuration

Environment Variables

bash
# LLM Configuration
OPENAI_API_KEY=sk-...              # OpenAI API key
OPENAI_MODEL=gpt-4                 # Model name

# Neo4j Graph Database
NEO4J_URI=neo4j+s://instance.databases.neo4j.io
NEO4J_USER=neo4j
NEO4J_PASSWORD=...
NEO4J_DATABASE=neo4j               # Leave unset for default

# Audit Logging
DATABASE_URL=postgresql://user:password@host/dbname

# Feature Flags
TRACEMIND_MOCK_LLM=1               # Use mock LLM (no API key needed)
TRACEMIND_SKIP_NEO4J=1             # Skip graph retrieval
TRACEMIND_EXIT_ON_COMPLETE=1       # Exit CLI after disposition
TRACEMIND_USE_LAG=1                # Use Logic-Augmented Generation

๐Ÿ“– Documentation

  • โ€”[ARCHITECTURE.md](Docs/ARCHITECTURE.md) โ€” Detailed control flow & module interactions
  • โ€”[SECURITY AUDIT](security_tests/reports/SECURITY_AUDIT_SUMMARY.md) โ€” Security findings & hardening proof
  • โ€”[CPG Reference](Docs/CPG_Fever_Seattle_Childrens_reference.md) โ€” Fever CPG mapping
  • โ€”[Demo Video](https://youtu.be/OlysHYbYaqU) โ€” Interactive system walkthrough. (not included in huggingface portal. Refer back to portfolio page)

โš•๏ธ Status & Scope

What This Is

  • โ€”Original: UC Berkeley final project for pediatric fever triage
  • โ€”Enhancement: Significantly improved post-graduation with security hardening, architecture refinement, and production-grade practices
  • โ€”Purpose: Research demonstration of secure, explainable neurosymbolic AI in healthcare
  • โ€”Current Status: Research prototype with enterprise-level security
  • โ€”Not intended for: Direct clinical use without comprehensive validation

Scope Limitations

  • โ€”Clinical scope: Fever + GI symptoms + dehydration (limited bundle)
  • โ€”Age range: Pediatric only (3 months - 12 years)
  • โ€”Data: Limited to Seattle Children's CPG (not comprehensive)
  • โ€”Validation: No clinical validation in real-world settings
  • โ€”Deployment: Requires additional clinical testing before any patient-facing use

Medical Disclaimer

RESEARCH USE ONLY. TraceMind is not a substitute for licensed clinical decision support or medical advice. This system:

  • โ€”โŒ Is not FDA approved or cleared
  • โ€”โŒ Has not been validated in clinical practice
  • โ€”โŒ Should not be used for actual patient care without physician oversight
  • โ€”โŒ Does not replace clinical judgment

Use only for research, demonstration, and proof-of-concept purposes under appropriate supervision.

Technical Compliance Features

While not clinically validated, the system demonstrates compliance-minded patterns:

  • โ€”โœ… Immutable audit trail logging
  • โ€”โœ… No sensitive data exposed in UI
  • โ€”โœ… All decisions traceable to rules & evidence
  • โ€”โœ… Input validation & sanitization
  • โ€”โœ… Jailbreak & injection resistance

Future Enhancements (for actual deployment)

To prepare for real clinical use, would require:

  1. 1.Clinical validation โ€” Testing against real patient data
  2. 2.Expanded scope โ€” Additional symptoms, age groups, conditions
  3. 3.Regulatory approval โ€” FDA clearance or equivalent
  4. 4.Integration testing โ€” EHR systems, clinical workflows
  5. 5.Clinician validation โ€” Review by practicing pediatricians
  6. 6.Continuous monitoring โ€” Real-world performance tracking

๐Ÿ‘จโ€๐Ÿ’ป Development

Adding a New Rule

  1. 1.Edit tracemind/logic/triage_rules.py
  2. 2.Define predicate and rule trace ID:
python
   def R_CUSTOM_RULE(case_fields):
       # Rule logic
       return disposition, rule_ids, med_flags
  1. 1.Add test case in security_tests/test_scripts/test_adversarial.py
  2. 2.Run tests: python -m security_tests.test_scripts.test_adversarial

Adding LLM Support

  1. 1.Update extraction patterns in tracemind/agents/interpretation.py
  2. 2.Test with debug_llm_extraction.py:
bash
   python -m security_tests.test_scripts.debug_llm_extraction
  1. 1.Add test scenario to tracemind/evaluation/scenarios.csv

๐Ÿ“š References

  • โ€”Seattle Children's Fever CPG โ€” Integrated in Docs/
  • โ€”SNOMED-CT โ€” Knowledge graph foundation
  • โ€”LangGraph โ€” Orchestration framework
  • โ€”Neo4j โ€” Graph database backend
  • โ€”PyDatalog โ€” Logic programming engine

๐Ÿ“ License

MIT License โ€” See LICENSE file for details.


๐Ÿ“ž Support

For questions or issues:

  1. 1.Check [Docs/ARCHITECTURE.md](Docs/ARCHITECTURE.md)
  2. 2.Review [security_tests/reports/](security_tests/reports/) for security findings
  3. 3.Check test cases in security_tests/test_scripts/

Last Updated: June 2026 Status: Research Prototype (UC Berkeley โ†’ Enhanced) โœ… Security: Comprehensively Tested & Hardened โœ… Clinical Use: Not validated for production deployment โš ๏ธ