Spaces:
Sleeping
Sleeping
File size: 6,388 Bytes
d0b3422 1435f2c 8b5a069 1435f2c | 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 | ---
title: WQF7002 Assignment1
emoji: ⚡
colorFrom: blue
colorTo: gray
sdk: gradio
sdk_version: 6.32.0
python_version: '3.12'
app_file: app.py
pinned: false
---
# PharmaCheck
An advisory chat assistant for pharmacists at a compounding pharmacy, built for
WQF7002 Assignment 1. It uses **native tool calling** with
`Qwen/Qwen2.5-7B-Instruct`. No model is trained or fine-tuned.
Paste an order with ingredient names, amounts and units. The model chooses Python
tools; Python validates arguments, resolves products and calculates dose verdicts
from `Diagnosis, Ingredients, Dosage.xlsx` in the repository root. The interface shows the chat, every
function call and guardrail, and full JSON for the latest tool call.
## Run locally (Python 3.12, including Mac)
```bash
python3.12 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
export LLM_BACKEND=hf_api
export HF_TOKEN=your_hugging_face_token
python app.py
```
Open the local address printed by Gradio (normally http://127.0.0.1:7860).
Keep the token in your environment; do not commit it. It needs permission to call
Inference Providers. The UI starts without a token, but model requests will fail.
Provider support for this exact model and available account quota are required;
free model weights do not guarantee free or available hosted inference.
The local backend uses the API and does not download the model or require CUDA.
## Hugging Face Space / ZeroGPU
The existing README YAML is retained. Set the Space hardware to ZeroGPU and leave
`LLM_BACKEND` unset (or set it to `zerogpu`). The model loads at module import using
Transformers, CUDA and bfloat16. Generation runs inside `@spaces.GPU`, using greedy
decoding and at most 1024 new tokens. Qwen tool-call blocks are parsed individually.
PyTorch is pinned to a version explicitly listed as supported by ZeroGPU.
The default backend requires the Space's CUDA/ZeroGPU environment; on a Mac use
`hf_api`. Startup downloads the model and requires sufficient host memory.
Gradio 6 uses message-format chats by default, so the removed `type="messages"`
parameter is omitted. See the [Gradio migration guide](https://gradio.app/guides/gradio-6-migration-guide)
and [ZeroGPU documentation](https://huggingface.co/docs/hub/spaces-zerogpu).
## Tools
| Tool | Purpose |
|---|---|
| `lookup_ingredient` | Product range, purity, source notes and fixed dosing rule |
| `check_dose` | Convert mg/g/mcg and check a supplied dose |
| `check_order` | Check up to thirty ingredients and count verdicts |
| `find_ingredients_for_diagnosis` | Find formulary-listed ingredients and available ranges |
| `suggest_alternatives` | Other brands and ingredients listed for the same diagnosis |
The workbook is loaded once without using cell colours. The merged ingredient row
is read once. Numeric prefixes, purity percentages, source notes and fixed dosing
rules are retained. Missing data produces an error rather than fabricated records.
Exact names and explicit aliases are preferred; ambiguous generic/brand matches
require clarification. `aliases.py` requires pharmacist review. Different named
brands are not automatically treated as equivalents. Missing entries such as
Psyllium husk and L-tryptophan are reported as having no dosage data.
Some source names exceed the tool argument limit; use a distinctive short brand
name (for example `EstroG-100`) when asking for that product.
## Validation and guardrails
Only the five read-only tools are allowed. Pydantic rejects extra arguments,
missing/empty strings, strings longer than 100 characters, nonpositive/nonfinite
amounts, amounts over 100000, unsupported units and orders exceeding thirty items.
Numeric strings are accepted. Rejections are returned to the model and logged.
Python blocks order-action requests, common prompt-injection phrases and messages
longer than 8000 characters before calling the model. Each turn allows at most
eight tool executions; a separate loop bound prevents endless rejected calls.
Prescription-only, patient-factor and compatibility notices are appended in code.
The prescription list is configurable in `guardrails.py` and is assignment policy,
not a jurisdiction-specific legal classification. Regex rules are intentionally
simple and can produce false positives; they are not a complete injection defence.
Order tables are rendered directly from tool fields, with problems first. Numerical
model replies are replaced with this turn's tool results to avoid fabricated or
misassigned figures. Non-order numerical results currently use a readable JSON
block. The model is also instructed never to supply dose figures from memory.
Conversation/tool history and logs are isolated per Gradio session. Clear resets
both. Logs may contain pasted order data; do not enter patient identifiers.
## Limits
- Advisory only: no approval, dispensing, submission or order modification.
- Dose ranges come only from the supplied formulary. They are not patient-specific
prescriptions, and being within range is not an assertion of safety.
- Compatibility comes from model general knowledge, not spreadsheet evidence.
It must be verified before dispensing; the model may be wrong or uncertain.
- Alternatives are formulary-listed options, not proven equivalents.
- Age, pregnancy, breastfeeding, kidney and liver adjustments are outside scope.
- The model can omit or misread an order line. Review the tool log against the
original order, including any missing amount or unit.
- If a later model request fails after checks ran, completed results are retained
and explicitly described as incomplete, rather than claiming no checks ran.
## Tests
```bash
pytest -q
```
Tests use the real workbook plus fake model responses, without downloading or
calling an LLM. They cover parsing, counts, matching, dose arithmetic, validation,
missing-data errors, alternatives, guardrails, malformed tool calls, execution
limits, history and numerical-response grounding. Real API/ZeroGPU generation
must also be checked in the respective authenticated environment.
Manual acceptance: paste `DIM 200 mg, Acetyl-L-Carnitine 1 g, Melatonin 500 mcg`,
confirm one `check_order` call in the log, compare its results with the table,
and follow up with a question about the second ingredient. Also try an ambiguous
brand, an incomplete line, an approval request and a compatibility question.
|