Metadata-Version: 2.4
Name: opensafety
Version: 0.1.0
Summary: Open-source Pharmacovigilance AI for ADR extraction, MedDRA coding, and listedness assessment
Author: OpenSafety Team
License-Expression: Apache-2.0
Project-URL: Homepage, https://github.com/opensafety/opensafety
Project-URL: Bug Tracker, https://github.com/opensafety/opensafety/issues
Keywords: pharmacovigilance,meddra,adr,safety,clinical-nlp,drug-safety
Classifier: Development Status :: 2 - Pre-Alpha
Classifier: Intended Audience :: Healthcare Industry
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# OpenSafety 🩺

[![PyPI version](https://img.shields.io/pypi/v/opensafety.svg)](https://pypi.org/project/opensafety/)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

**OpenSafety** is an open-source Pharmacovigilance (PV) AI toolkit that combines fine-tuned Small Language Models (SLMs) with deterministic **MedDRA v28.0** ontology verification for:
1. **Adverse Drug Reaction (ADR) Extraction** from clinical narratives.
2. **MedDRA Coding** (LLT, Preferred Term, and Primary System Organ Class).
3. **Regulatory Listedness Assessment** against Reference Safety Information (RSI / Product Labels).

---

## Installation

```bash
pip install opensafety
```

---

## Quick Start

### 1. Instant MedDRA v28.0 Search & Coding

```python
from opensafety import MedDRADatabase, HybridMedDRAMatcher

# 1. Connect to the indexed MedDRA database
db = MedDRADatabase("meddra.db")

# 2. Look up exact regulatory terms
res = db.search_term("Dry cough")
print(res[0]["pt_name"])  # "Cough"
print(res[0]["soc_name"]) # "Respiratory, thoracic and mediastinal disorders"

# 3. Fuzzy & colloquial matching from patient verbatims
matcher = HybridMedDRAMatcher("meddra.db")
matches = matcher.search("stomach ache", top_k=1)
print(matches[0]["pt_name"])  # "Abdominal pain upper"
```

---

### 2. End-to-End AI Pharmacovigilance Pipeline

```python
from opensafety import OpenSafetyEngine

# Initialize the engine with your trained LoRA adapter and MedDRA database
engine = OpenSafetyEngine(
    adapter_path="opensafety_qwen_adapter",
    meddra_db_path="meddra.db"
)

# Analyze a raw clinical narrative
report = engine.analyze(
    drug="Atorvastatin",
    narrative="A 62-year-old female taking Atorvastatin 40mg daily developed acute muscle weakness and soreness in her calves.",
    rsi="Section 4.8 Undesirable Effects: Common: myalgia, arthralgia, pain in extremity, muscle spasms."
)

# Inspect verified regulatory output
for event in report.adverse_events:
    print(f"Verbatim: {event.verbatim}")
    print(f"MedDRA PT: {event.meddra_pt} (Code: {event.meddra_pt_code})")
    print(f"Primary SOC: {event.meddra_soc}")
    print(f"Listedness: {event.listedness.status}")
    print(f"Rationale: {event.listedness.rationale}")
```

---

## Architecture: Hybrid Safety Engine

```
[ Clinical Narrative / Case Report ] + [ Reference Safety Info (RSI) ]
                                  │
                                  ▼
      Fine-Tuned Qwen 2.5 (1.5B) Pharmacovigilance SLM
                                  │
                                  ▼
                     Extracted Structured JSON
                                  │
                                  ▼
         Deterministic MedDRA v28.0 SQLite Verification
            (Zero hallucination, verified 8-digit codes)
                                  │
                                  ▼
              Validated ICH E2B(R3) Structured Report
```

---

## License

Apache License 2.0. MedDRA is copyrighted by the International Council for Harmonisation (ICH) and maintained by MSSO.
