Skip to main content

A simple wrapper to support drug detection in medical texts.

Project description

drugdetector

This project provides a simple wrapper for performant zero-shot drug detection using LLMs.

Install

pip install drugdetector

Optionally, but recommended is to install CUDA support for llama-cpp-python (source).

pip install llama-cpp-python \
  --extra-index-url https://abetlen.github.io/llama-cpp-python/whl/<cuda-version>

Where <cuda-version> is one of the following:

  • cu121: CUDA 12.1
  • cu122: CUDA 12.2
  • cu123: CUDA 12.3
  • cu124: CUDA 12.4
  • cu125: CUDA 12.5

You can find what version of CUDA you support via nvidia-smi and looking for the CUDA Version in the top right corner.

Demo

For this demo, we're using our fine-tuned 8-bit quantized Llama-3 model (fabriceyhc/Llama-3-8B-DrugDetector), but you can use many LLMs available on huggingface. We recommend one of the following fine-tuned models for improved performance:

  • fabriceyhc/Llama-3-8B-DrugDetector
  • fabriceyhc/Llama-3-70B-DrugDetector
from drugdetector import DrugDetector

detector = DrugDetector(model_id="fabriceyhc/Llama-3-8B-DrugDetector")

Detection on default substances

When no specific drugs are requested, default to the ones we've studied.

results = detector.detect("Patient denies using heroin but reports cocaine use.")
print(results)
{'Heroin': False,
 'Cocaine': True,
 'Methamphetamine': False,
 'Benzodiazepine': False,
 'Prescription Opioids': False,
 'Cannabis': False,
 'Injection Drugs': False,
 'General Drugs': True,
 'medical_text': 'Patient denies using heroin but reports cocaine use.',
 'time_taken': 0.5203254222869873}

Detection with custom drug dict

Tests zero-shot accuracy on any substances provided in as a python dictionary in the form of:

new_substances = {
  "substance1":"description1",
  "substance2":"description2",
  ...
  "substanceN":"descriptionN",
}
new_substances = {
    "Alcohol": "Alcohol is a legal substance but can be abused and lead to addiction and various health issues.",
    "Fentanyl": "Fentanyl is a potent synthetic opioid that is highly addictive and can lead to overdose, especially when used illicitly."
}

results = detector.detect("Patient hx of ethl use.", drugs=new_substances)
print(results)
{'Alcohol': True,
 'Fentanyl': False,
 'medical_text': 'Patient hx of ethl use.',
 'time_taken': 0.5203254222869873}

Detection with custom drug dict and explanations provided

Demonstrates the model's ability to explain its decisions.

new_drugs = {
    "Alcohol": "Alcohol is a legal substance but can be abused and lead to addiction and various health issues.",
    "Fentanyl": "Fentanyl is a potent synthetic opioid that is highly addictive and can lead to overdose, especially when used illicitly."
}

results = detector.detect("Patient hx of ethl use.", drugs=new_substances, explain=True)
print(results)
{'Alcohol': True,
 'Fentanyl': False,
 'medical_text': 'Patient hx of ethl use.',
 'time_taken': 1.1692967414855957,
 'Alcohol_explanation': 'The medical note mentions "ethl use", which is likely referring to ethanol, the scientific name for alcohol.',
 'Fentanyl_explanation': 'There is no mention of Fentanyl use in the medical note.'}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drugdetector-0.3.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

drugdetector-0.3.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file drugdetector-0.3.2.tar.gz.

File metadata

  • Download URL: drugdetector-0.3.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for drugdetector-0.3.2.tar.gz
Algorithm Hash digest
SHA256 7decd8051620450ebdb0bd501b570af8ef506ad612ed2d45b2aa072261dd80ae
MD5 86c8a95cf63fa7d57dfc3e73501098c5
BLAKE2b-256 753b71d5a17cb38370ba166eb9471d6b4e5dc03cb84f7a9ea0c1c8a445a420a0

See more details on using hashes here.

File details

Details for the file drugdetector-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: drugdetector-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for drugdetector-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8a897f55c5c4e427f4e9cd02a15bc37ef138aea8f44a61dd9bff926a623a901
MD5 620b7a6b943456aa47b58c371e31df12
BLAKE2b-256 bafe22a5b7bc9c03da2d7e6a215b5f43304f356da7340462f31b862f472b4c04

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page