harshitamugdha/medical-coding-env
๐ฅ Medical Coding Environment (OpenEnv)
This project implements a custom OpenEnv-compatible environment for evaluating automated medical coding systems. The environment simulates clinical scenarios and requires an agent to assign correct ICD-10 and CPT codes.
๐จ Problem Statement
With the rise of ICT (Information and Communication Technology) in healthcare systems, fraud and inaccuracies in medical billing have become a serious issue.
Common problems include:
- Incorrect or manipulated medical codes
- Fraudulent insurance claims
- Overbilling or underbilling (upcoding/downcoding)
- Data inconsistencies in digital health records
These issues lead to financial losses, reduced trust, and inefficiencies in healthcare systems.
This project simulates a simplified version of this problem by requiring an AI agent to correctly assign medical codes from clinical notes. It demonstrates how intelligent systems can help reduce errors and detect inconsistencies in medical coding workflows.
๐ง Environment Description
The environment presents clinical notes describing patient conditions. The agent must interpret these notes and return structured medical codes.
Each episode consists of 3 tasks:
- Easy: Simple diagnosis (e.g., streptococcal pharyngitis)
- Medium: Multi-condition case (e.g., hypertension + diabetes)
- Hard: Complex case (e.g., fracture with medical history)
The order of tasks is randomized, and clinical notes include slight variations to simulate real-world inconsistencies.
๐ฅ Observation Space
Each step returns an observation with the following fields:
patient_age(int): Age of the patientpatient_sex(str): Gender of the patientclinical_note(str): Doctor's notes describing the casefeedback(str): Feedback from the graderremaining_tasks(int): Number of tasks left
๐ค Action Space
The agent must return an action with:
primary_icd10(str): Primary diagnosis codesecondary_icd10s(List[str]): Secondary diagnosis codescpt_codes(List[str]): Procedure codes
๐ฏ Reward Function
The reward is calculated based on:
- Partial match for correct ICD-10 and CPT codes
- Higher reward for exact matches
- Penalties for excessive or incorrect predictions
Final reward is normalized between 0.0 and 1.0 per step.
โ๏ธ Setup Instructions
1. Clone the repository
git clone https://github.com/YOUR_USERNAME/medical-coding-env.git
cd medical-coding-env2. Install dependencies
pip install -r requirements.txt3. Setup environment variables
Create a .env file in the root directory: APIBASEURL=https://router.huggingface.co/v1 MODELNAME=Qwen/Qwen2.5-72B-Instruct HFTOKEN=yourhuggingfacetoken
4. Run Inference
python inference.py๐ณ Docker Build the Docker image
docker build -t medical-env .Run the container
docker run -p 8000:8000 medical-env๐ API Endpoints POST /reset โ Initialize environment POST /step โ Execute an action GET /state โ Retrieve current state
๐งช Inference Script The inference.py script: Uses OpenAI-compatible client with Hugging Face router Interacts with the environment step-by-step Outputs structured logs in required format: [START] [STEP] [END]
๐ Example Output [START] task=medical-coding env=medical-coding-env model=Qwen/Qwen2.5-72B-Instruct [STEP] step=1 action=... reward=1.00 done=false [STEP] step=2 action=... reward=0.99 done=false [STEP] step=3 action=... reward=0.81 done=true [END] success=true steps=3 score=0.94 rewards=1.00,0.99,0.81
โ ๏ธ Notes .env is used for local development only Hugging Face Spaces requires environment variables via Secrets Do not modify log format in inference.py
