DEV Community

Cover image for ToxiLens: The Offline Molecular Safety Screener That Says "I Don't Know"
Devansh
Devansh

Posted on AI-assisted

ToxiLens: The Offline Molecular Safety Screener That Says "I Don't Know"

Hacktoberfest Open-Source AI Challenge Week 2: Side Hustle Submission 💸

This is a submission for the Hacktoberfest Open-Source AI Challenge Week 2: Side Hustle

A calibrated in-silico toxicology screener built for indie cosmetic formulators, clean-beauty artisans, and botanical fragrance creators who cannot afford a $50,000/year enterprise chemistry license or a $250/hour toxicologist.


What I Built

The Real Problem for Real People

Independent cosmetic chemists, natural perfumers, and botanical soap artisans face a massive barrier to entry: safety compliance and toxicological validation.

Before bringing an organic face oil, cold-process soap bar, or custom botanical candle to market, formulators must prove their candidate ingredients are non-sensitizing, non-carcinogenic, and compliant with strict international cosmetic regulations (such as EU Annex III).

Today, they are trapped between three flawed options:

  1. Enterprise In-Silico Chemistry Suites (e.g., Derek Nexus, Leadscope): These cost $30,000 to $60,000 per seat per year, accessible only to multi-billion-dollar pharmaceutical and multinational personal-care conglomerates.
  2. Contract Testing Wet-Labs: Commissioning an in-vitro toxicology screening panel costs $8,000 to $15,000 per compound and takes 6 to 8 weeks. If a candidate formulation turns out to be an endocrine disruptor or reactive sensitizer, that budget is lost.
  3. Generic Commercial LLMs (ChatGPT / Claude): Formulators try pasting chemical names or SMILES into generic chatbots, but general-purpose LLMs notoriously hallucinate stereochemistry, invent nonexistent metabolic pathways, and give falsely confident reassurance on hazardous molecules.

The Solution: ToxiLens

ToxiLens is a private, lightweight molecular safety screener built entirely on open-source AI and cheminformatics foundations:

  • 12 Calibrated Machine Learning Models trained on the National Institutes of Health (NIH) Tox21 benchmark dataset.
  • Split Conformal Prediction: Unlike ordinary ML models that output misleadingly confident probabilities (e.g., 0.51), ToxiLens produces mathematically guaranteed prediction sets at a user-selected 90% confidence level. When a molecule falls into an uncharted chemical space, it explicitly outputs "Uncertain / Refusal (I don't know)".
  • EU Annex III Allergen Matcher: Automatically scans against the European Commission's restricted fragrance and cosmetic allergen registries (e.g., Cinnamaldehyde, Citral, Coumarin).
  • Structural Alerts (Toxicophore Engine): Scans for reactive SMARTS patterns (Michael Acceptors, Epoxides, Nitroaromatics, Aromatic Amines).
  • Local Substructure Attribution & Vector Visualizer: Highlights exactly which atomic centers triggered an alert and displays nearest historical precedents from the Tox21 lab database.

Who Would Pay For It?

  • Indie Cosmetics & Fragrance Formulators: A $29–$49/month micro-SaaS tier (or $5 pay-per-screen audit) saves them thousands of dollars in wasted contract lab testing.
  • Specialty Chemical Consultants & Formulation Labs: They can run ToxiLens completely offline on their own hardware, guaranteeing that proprietary, patent-pending formulas never touch external cloud servers.

Demo

Conformal Calibration Proof

The plot below demonstrates that the empirical test coverage strictly matches nominal mathematical confidence across all calibrated confidence intervals:

Conformal calibration plot showing empirical coverage matching nominal confidence


Code

🔬 ToxiLens: Offline Molecule Safety Screener

CI License: MIT Python 3.11+

An offline, private molecular safety screener that predicts toxicity using machine learning and says "I don't know" when it is not sure.

Built for small cosmetic, fragrance, and household cleaning-product formulators who cannot afford an in-house toxicologist or expensive enterprise software suites. Everything runs locally on consumer hardware (tested on an Intel Core i3 with 8 GB RAM), ensuring proprietary formulas never leave the machine.


🏛 Architecture Diagram

flowchart TD
    A["Raw Input: SMILES / Ingredient"] --> B["RDKit Chemistry Engine (chem.py)"]
    B --> C1["Morgan Circular Fingerprints (1024b)"]
    B --> C2["Physicochemical Descriptors (MW, LogP, TPSA)"]
    B --> C3["Molecular Graph (PyTorch Geometric)"]
    C1 --> D1["Random Forest & XGBoost Baseline"]
    C2 --> D2["Tabular Descriptor Model"]
    C3 --> D3["GIN Graph Neural Network"]
    D1 --> E["Split Conformal Calibration (conformal.py)"]
    
    E --> F1["Point Prediction (0 / 1)"]
    E --> F2["Calibrated Prediction Set ({0}, {1}, or {0,1})"]

    F1 & F2 --> G["Serving
…

Architecture Overview

flowchart TD
    A["Raw Input: SMILES / Chemical Specimen"] --> B["RDKit Chemistry Engine (chem.py)"]
    B --> C1["Morgan Circular Fingerprints (1024-bit, r=2)"]
    B --> C2["Physicochemical Descriptors (MW, LogP, TPSA)"]
    B --> C3["SMARTS Toxicophore Rule Engine"]
    C1 --> D1["12 Calibrated Random Forest & XGBoost Classifiers"]
    C2 --> D2["Tabular Physicochemical Network"]
    C3 --> D3["Reactive Alert Flagging"]

    D1 --> E["Split Conformal Prediction (conformal.py)"]

    E --> F1["Point Probability (0.00% - 100.0%)"]
    E --> F2["Calibrated Conformal Set: {0}, {1}, or {0,1}"]
    F1 & F2 --> G["Serving & Agent Layer"]
    G --> H1["FastAPI Cloud REST Engine (api/main.py)"]
    G --> H2["Precision Research Web UI (web/)"]
    G --> H3["Model Context Protocol Server (mcp_server/server.py)"]
    G --> H4["Local Gemma Tool-Calling Agent (agent/agent.py)"]

Core Code Implementations in the Repository

src/toxilens/conformal.py: A clean, dependency-free implementation of non-parametric Split Conformal Prediction from scratch in NumPy:

def calibrate(self, val_probs: np.ndarray, val_labels: np.ndarray):
    """Calibrates non-conformity quantile for exact finite-sample coverage guarantee."""
    n = len(val_probs)
    # Non-conformity score: 1.0 - probability assigned to the ground truth
    scores = np.where(val_labels == 1, 1.0 - val_probs, val_probs)
    q_level = np.ceil((n + 1) * (1.0 - self.alpha)) / n
    q_level = min(1.0, max(0.0, q_level))
    self.q_hat = float(np.quantile(scores, q_level, method="higher"))
Enter fullscreen mode Exit fullscreen mode

src/toxilens/scaffold.py: Handcrafted Bemis-Murcko scaffold extraction that groups molecules strictly by ring frameworks, eliminating analog data leakage.

api/main.py: High-performance asynchronous FastAPI service serving predictions, RDKit vector diagrams, nearest neighbor chemical search, and the frontend.

Dockerfile: Multi-stage, 512 MB-optimized container bundling RDKit C++ runtimes (libxrender1, libgl1), dynamically adapting to Render's cloud port.


How I Built It

1. Training Across 12 Biological Pathways (NIH Tox21)

ToxiLens was trained on the NIH Tox21 dataset, assessing candidate molecules against 12 biological toxicity pathways:

  • 7 Nuclear Receptor Assays: Estrogen Receptor Alpha (NR-ER), Androgen Receptor (NR-AR), Aromatase Enzyme (NR-Aromatase), Peroxisome Proliferator (NR-PPAR-gamma), Aryl Hydrocarbon Receptor (NR-AhR), etc.
  • 5 Cellular Stress Response Assays: Genotoxicity / DNA Damage (SR-ATAD5), Mitochondrial Membrane Permeabilization (SR-MMP), Heat Shock Response (SR-HSE), p53 Tumor Suppressor (SR-p53), and Oxidative Stress (SR-ARE).

2. Honest Validation: The Bemis-Murcko Scaffold Split

In cheminformatics, standard random train/test splits are misleading. Molecules from the same chemical family (analogs sharing the same ring core) end up in both the training and test sets. This creates massive data leakage and falsely inflates accuracy.

We evaluated ToxiLens using a strict Bemis-Murcko Scaffold Split:

Split Methodology Scaffold Leakage Into Test Test AUROC Generalization Drop
Random Split 49.8% 0.8142 Baseline
Bemis-Murcko Scaffold Split 0.00% 0.7233 -11.2%

Key Insight: The 11.2% drop demonstrates the real-world difficulty of generalizing to completely novel chemical skeletons.

Full 12-Assay Tox21 Benchmark Results (Scaffold Split)

Assay Target Description Labeled Test Samples Random Forest AUROC XGBoost AUROC
NR-AR Androgen Receptor 707 0.7496 0.7435
NR-AR-LBD Androgen Receptor (Ligand Binding) 618 0.7978 0.7323
NR-AhR Aryl Hydrocarbon Receptor 605 0.7822 0.7711
NR-Aromatase Aromatase Enzyme Inhibition 497 0.6513 0.5948
NR-ER Estrogen Receptor Alpha 548 0.6430 0.6510
NR-ER-LBD Estrogen Receptor (Ligand Binding) 659 0.8133 0.7167
NR-PPAR-gamma Peroxisome Proliferator Receptor 567 0.7697 0.6952
SR-ARE Antioxidant Response Element 466 0.7146 0.6062
SR-ATAD5 Genotoxicity / DNA Damage 653 0.7086 0.6144
SR-HSE Heat Shock Response 563 0.7113 0.6687
SR-MMP Mitochondrial Membrane Potential 505 0.7680 0.7204
SR-p53 p53 Tumor Suppressor Pathway 630 0.7106 0.6535
MACRO AVERAGE Across All 12 Assays 7,018 0.7350 0.6806

3. Representation Ablation: Fingerprints vs. Descriptors vs. Graphs

We compared molecular representations on the NR-AR target:

  • Morgan Circular Fingerprints (Random Forest): 0.7496 AUROC (Best: directly maps explicit circular substructures and reactive motifs).
  • Physicochemical Descriptors (Tabular Neural Network): 0.7098 AUROC (Captures bulk properties like LogP and molecular weight, but misses stereoisomeric positions).
  • Graph Isomorphism Network (GIN / PyTorch Geometric): 0.6455 AUROC (Overfits on small scaffold splits without massive-scale pretraining).

4. Conformal Calibration (Non-Parametric Finite-Sample Guarantee)

Standard classifiers force a binary decision. If a model predicts 0.51, it calls it "Toxic". If it predicts 0.49, it calls it "Safe". In health and personal care, this is dangerous.

With Split Conformal Prediction, ToxiLens provides a statistical guarantee:

  • {0}: Confidently Non-Toxic (Safe).
  • {1}: Confidently Toxic Hazard.
  • {0, 1}: "I don't know / Uncertain": both outcomes remain plausible under the calibration threshold. Formulators know immediately that physical lab verification is required.

Why Does Open Innovation Matter?

In biochemistry and personal care formulation, closed-source AI is unusable for three major reasons:

1. Data Privacy & Trade Secrets

When an artisan perfumer or organic chemist spends months developing a patent-pending skincare ingredient or proprietary fragrance accord, they cannot paste that SMILES structure into ChatGPT or a commercial cloud API. Doing so risks leaking unpatented IP or having trade secrets stored in external logs. Open-source AI allows the entire ToxiLens stack to run locally on an air-gapped laptop.

2. Elimination of Hallucinations

General-purpose LLMs hallucinate chemical valency, invert chiral centers, and produce chemical advice that is factually untrue. Open-source cheminformatics (RDKit) combined with validated ML models guarantees that every chemical calculation adheres strictly to the laws of physical chemistry.

3. Democratizing Safety Verification

Enterprise toxicology modeling software is locked behind $50k/year enterprise pricing. Open-source AI democratizes molecular safety, giving small indie creators and university researchers the same predictive auditing tools previously reserved for global conglomerates.


My Agent Session

This entire application, from mathematical proofs and RDKit SMARTS rule engines to the modern laboratory UI and Docker cloud orchestration, was pair-programmed with an autonomous coding agent:

  • Scaffold Partitioning Algorithm: Designed the Bemis-Murcko graph decomposition algorithm in Python.
  • Conformal Calibration Suite: Created the non-parametric conformal calibration harness and validation plots.
  • Model Compression: Compressed the multi-model Tox21 bundle from 178 MB down to 39.4 MB using Joblib compression to make it distributable and cloud-deployable under 512 MB RAM limits.

Prize Categories

  • Primary Track: Side Hustle (A real-world, viable B2B micro-SaaS solving a high-cost compliance bottleneck for independent cosmetic, fragrance, and chemical formulators)
  • Best Use of Render ($200 USD): ToxiLens is packaged in a custom Docker container optimized for low-memory cloud tiers and deployed live on Render's container runtime: https://toxilens.onrender.com.
  • Best Use of Gemma ($200 USD): Features a local Gemma agent harness (agent/agent.py) that defends against hallucination by strictly invoking local ToxiLens tools via JSON-RPC.

Links & Resources

Top comments (0)