Spaces:
Sleeping
Sleeping
File size: 5,314 Bytes
b173a90 fa51dd9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | ---
title: Polypharmacy
emoji: 📉
colorFrom: yellow
colorTo: blue
sdk: docker
pinned: false
---
# PolypharmacyEnv
Monorepo for an OpenEnv-compatible medication safety environment with:
- a FastAPI backend (`backend/`)
- a React frontend (`frontend/`)
- data assets (`data/`)
- utility scripts (`scripts/`)
---
## Repository Structure
```text
backend/
main.py # ASGI entrypoint (uvicorn target)
requirements.txt # Backend dependencies
Dockerfile # Backend container
src/polypharmacy_env/ # Python package source
api/
app.py # FastAPI/OpenEnv app assembly
server.py # Compatibility import wrapper
routes/agent.py # /agent/suggest route
services/
groq_agent.py # Groq-based action suggestion logic
env_core.py # OpenEnv environment core
models.py # Action/observation/state models
data_loader.py # CSV loading
ddi_simulator.py # DDI and Beers lookups
rewards.py # Reward shaping
graders.py # Task graders
tasks.py # Task/episode selection
tests/ # Backend tests
frontend/
src/ # React UI code
package.json
Dockerfile # Frontend container
data/
lookups/ # drug_metadata.csv, ddi_rules.csv, beers_criteria.csv
processed/ # patients_polypharmacy.csv
scripts/
preprocess_data.py # Synthetic data generation
dev_backend.sh # Local backend run helper
dev_frontend.sh # Local frontend run helper
run_validation.sh # Tests + baseline validation
docker-compose.yml # Full stack orchestration
openenv.yaml # OpenEnv manifest
inference.py # Baseline inference script (required at root)
.env.example # Environment template
```
---
## What It Does
The environment simulates elderly polypharmacy review. Agent actions:
- `query_ddi`
- `propose_intervention`
- `finish_review`
Supported tasks:
- `easy_screening`
- `budgeted_screening`
- `complex_tradeoff`
---
## Prerequisites
- Python 3.10+
- Node.js 18+ (or 20+ recommended)
- npm
- Docker + Docker Compose (optional, for containerized run)
---
## Environment Setup
Create `.env`:
```bash
cp .env.example .env
```
Set values for local backend integrations as needed.
---
## Local Run (Recommended During Development)
### 1) Install dependencies
Backend:
```bash
pip install -r backend/requirements.txt
```
Frontend:
```bash
cd frontend
npm install
cd ..
```
### 2) Generate/update synthetic data (if needed)
```bash
python scripts/preprocess_data.py
```
### 3) Start services in two terminals
Terminal A:
```bash
./scripts/dev_backend.sh
```
Terminal B:
```bash
./scripts/dev_frontend.sh
```
### 4) Open app
- Frontend: [http://localhost:5173](http://localhost:5173)
- Backend health: [http://localhost:7860/health](http://localhost:7860/health)
---
## Docker Run
Run both services:
```bash
docker compose up --build
```
Stop:
```bash
docker compose down
```
Ports:
- backend: `7860`
- frontend: `5173`
---
## Hugging Face Spaces Deployment (Docker)
This repo now includes a **root `Dockerfile`** that builds frontend + backend into one container, so Spaces can host both API and UI together.
### 1) Create a new Space
- Go to [Hugging Face Spaces](https://huggingface.co/new-space)
- Choose **Docker** SDK
- Create the Space
### 2) Add Space secrets/variables
In Space Settings -> Variables and Secrets:
- Secret: `HF_TOKEN`
- Variable: `API_BASE_URL=https://router.huggingface.co/v1`
- Variable: `MODEL_NAME=Qwen/Qwen2.5-72B-Instruct`
### 3) Push this repository to the Space
Commit and push all files, including root `Dockerfile`.
### 4) Verify after build
- Space root URL loads the React UI
- `/health` returns healthy status
- OpenEnv endpoints are available (`/reset`, `/step`, `/state`, `/schema`)
Notes:
- Container reads `PORT` (defaults to `7860`) which is Space-friendly.
- Frontend static assets are served by FastAPI from `frontend/dist`.
---
## API Endpoints
OpenEnv/health:
- `POST /reset`
- `POST /step`
- `GET /state`
- `GET /health`
- `GET /schema`
- `WS /ws` (stateful session)
AI helper:
- `POST /agent/suggest`
---
## Testing
Run backend tests:
```bash
python -m pytest backend/src/polypharmacy_env/tests -v
```
Or run validation script:
```bash
./scripts/run_validation.sh
```
### Submission validation
```bash
openenv validate
python inference.py
```
---
## Notes
- OpenEnv HTTP reset/step is stateless; multi-step episode continuity should use websocket (`/ws`).
- The frontend uses websocket for episode continuity and HTTP for AI suggestion.
- AI behavior includes rule-based guardrails to avoid repetitive low-value loops.
---
## Troubleshooting
- `ModuleNotFoundError: polypharmacy_env`
- Start backend using `./scripts/dev_backend.sh` from repo root.
- `/agent/suggest` fails
- Check `.env` keys and restart backend.
- UI state looks stale
- Hard refresh browser and click `Reset Episode`.
|