Spaces:
Running on Zero
Running on Zero
deploy API
Browse filesFiles changed (8)
9 unmodified lines1011129 unmodified lineshuggingface_hub>=0.24 # downloads the model, catalogue and safety.sqlite from the private assets repopython-multipart>=0.0.9 # image uploadsopencv-python-headless>=4.10 # the stand-in pill finder (/v1/scan)9 unmodified lines10111213149 unmodified lineshuggingface_hub>=0.24 # downloads the model, catalogue and safety.sqlite from the private assets repopython-multipart>=0.0.9 # image uploadsopencv-python-headless>=4.10 # the stand-in pill finder (/v1/scan)pypdf>=5.0 # prescriptions as PDF: the text layerpypdfium2>=4.30 # scanned PDFs: pages rendered for the text reader
42 unmodified lines434445464748495051521 unmodified line545556575859605 unmodified lines666768697071316 unmodified lines38838939039139239339439541 unmodified lines437438439440441442196 unmodified lines63964064164264364464564664764864965065165265365465565665765865966066166266366466566679 unmodified lines7467477487497507517522 unmodified lines75575675775875976076176276376476542 unmodified linesfrom starlette.exceptions import HTTPException as StarletteHTTPExceptionfrom epills.core.catalogue.confidence import assessfrom epills.core.catalogue.store import IMPRINT_ALPHA, IMPRINT_TOP_Kfrom epills.core.imprint.match import GALLERY_MIN_WEIGHT, read_hitsfrom epills.core.inference.matching import check_imprint, decidefrom epills.core.inference.pill_finder import detect_pillsfrom epills.core.inference.preprocess import WINDOW, crop_windowfrom epills.core.safety import openfdafrom epills.core.safety.db import SafetyDB1 unmodified linefrom epills.core.safety.rxnorm import RxNormClient, RxNormErrorfrom serving.app import schemas as SAPI_VERSION = "0.6.0"STARTED_AT = datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds")LOCAL = {"safety_db": Path("data/processed/safety/safety.sqlite"),"embedder": Path("outputs/serving/stage2_model.pt"),5 unmodified linesVISUAL_LIMITS = ("A visual comparison with reference photos only: it can't confirm what the medicine is, its strength, ""or that it is safe to take.")NO_PILL = "No pill found. Spread the pills apart on a plain background and photograph them again."STAND_IN = "stand-in finder (plain backgrounds only)"MATCH_MESSAGES = { # visual evidence only (blueprint D5): "looks like", never "is""match": "This pill looks like your {name}.",316 unmodified linesreturn {"pill_id": pill_id, "name": p["name"], "rxnorm_name": p["rxnorm_name"], "generic_name": p["generic_name"],"manufacturer": p["manufacturer"], "ingredients": [i["name"] for i in p["ingredients"]],"shape": p["shape"], "colour": p["colour"], "size_mm": p["size_mm"], "imprint": p["imprint"],"images": [{"url": f"/v1/pills/{pill_id}/image/{i['n']}", "kind": i["kind"]} for i in p["images"]],**extra,}41 unmodified lines}@app.get("/v1/model-info", response_model=S.ModelInfo)def model_info():"""196 unmodified lines@app.post("/v1/read-label", response_model=S.ReadLabelResponse)async def read_label(image: UploadFile = File(description="A photo of a medicine box or a pharmacy label, close ""up and straight on")):"""**Which medicine is on this box or label?** Reads the printed text and finds the medicine names in it (RxNorm'singredient and brand names, international names such as paracetamol too), with strength, ingredients and RxNormcode: send a candidate to `/v1/safety/medications` as `{"id", "name", "rxcui"}`. Always ask the user to confirm.Nothing is stored; only the lines that name a medicine are returned (a label also shows the patient's name)."""photo = await read_image(image)reader = asset("reader")lines = await run_in_threadpool(reader.read_text, photo)try:candidates = await run_in_threadpool(asset("label_reader").read, lines)except RxNormError as error:raise HTTPException(502, f"RxNorm didn't answer: {error}")result = {"candidates": candidates, "note": LABEL_NOTE, "ocr_model": reader.name}if not candidates:result["message"] = ("No medicine name found. Photograph the label closer, straight on and in good light, ""or type the name.")return result@app.get("/v1/pills/search", response_model=S.SearchResponse)def pills_search(name: str = Query(min_length=2, description="Brand or generic name, e.g. 'metformin', 'Glucophage'"),strength: str | None = Query(None, description="e.g. '500 MG'"),79 unmodified lines"""Run when a medication is added or changed: interactions between every pair of the patient's medications(DDInter severity) and therapeutic duplications (the same active ingredient twice). Medications that can't beidentified are reported as alerts, so an empty alert list means every medication was checked."""db = asset("safety_db")resolved = [resolve_medication(m.model_dump(), app.state.rxnorm) for m in request.medications]2 unmodified lines"alerts": [a.to_dict() for a in alerts],"medications": per_med,"all_identified": all(m.resolved_by != "unresolved" for m in resolved),"sources": {"interactions": db.meta.get("source"), "licence": db.meta.get("licence"),"built_at": db.meta.get("built_at")},}@app.post("/v1/safety/dose", response_model=S.DoseResponse)def safety_dose(request: DoseRequest):"""42 unmodified lines4344454647484950515253541 unmodified line565758596061625 unmodified lines68697071727374757677316 unmodified lines39439539639739839940040140240340440541 unmodified lines447448449450451452453454455456457458459460461462196 unmodified lines65966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673779 unmodified lines8178188198208218228238242 unmodified lines82782882983083183283383483583683783883984084184284384484584684784884985085185285342 unmodified linesfrom starlette.exceptions import HTTPException as StarletteHTTPExceptionfrom epills.core.catalogue.confidence import assessfrom epills.core.catalogue.identity import product_identityfrom epills.core.catalogue.store import IMPRINT_ALPHA, IMPRINT_TOP_Kfrom epills.core.imprint.match import GALLERY_MIN_WEIGHT, read_hitsfrom epills.core.inference.matching import check_imprint, decidefrom epills.core.inference.pill_finder import detect_pillsfrom epills.core.label import STRENGTH, compare_with_list, document_fieldsfrom epills.core.inference.preprocess import WINDOW, crop_windowfrom epills.core.safety import openfdafrom epills.core.safety.db import SafetyDB1 unmodified linefrom epills.core.safety.rxnorm import RxNormClient, RxNormErrorfrom serving.app import schemas as SAPI_VERSION = "0.7.0"STARTED_AT = datetime.datetime.now(datetime.timezone.utc).isoformat(timespec="seconds")LOCAL = {"safety_db": Path("data/processed/safety/safety.sqlite"),"embedder": Path("outputs/serving/stage2_model.pt"),5 unmodified linesVISUAL_LIMITS = ("A visual comparison with reference photos only: it can't confirm what the medicine is, its strength, ""or that it is safe to take.")NO_PILL = "No pill found. Spread the pills apart on a plain background and photograph them again."SAFETY_NOTE = ("No alert doesn't mean these medicines are safe together: only interactions listed in DDInter 2.0 ""between identified ingredients, and the same ingredient twice, are checked. Check with your pharmacist.")IMAGE_LICENCE = ("NLM C3PI pill images: for research and development use. A catalogue picture may not show the product ""the patient holds.")STAND_IN = "stand-in finder (plain backgrounds only)"MATCH_MESSAGES = { # visual evidence only (blueprint D5): "looks like", never "is""match": "This pill looks like your {name}.",316 unmodified linesreturn {"pill_id": pill_id, "name": p["name"], "rxnorm_name": p["rxnorm_name"], "generic_name": p["generic_name"],"manufacturer": p["manufacturer"], "ingredients": [i["name"] for i in p["ingredients"]],# identity fields (blueprint B13): never merge products across strength, dose form or release**product_identity(p["rxnorm_name"]), "ndc9": p["ndc9"], "ndc11": p["ndc11"], "rxcui": p["rxcui"],"rxnorm_status": p["rxnorm_status"], "market": "US","shape": p["shape"], "colour": p["colour"], "size_mm": p["size_mm"], "imprint": p["imprint"],"images": [{"url": f"/v1/pills/{pill_id}/image/{i['n']}", "kind": i["kind"]} for i in p["images"]],"image_licence": IMAGE_LICENCE,**extra,}41 unmodified lines}@app.get("/v1/tools")def tools():"""Typed tool definitions for the Medication Care Agent (blueprint B14), generated from this API's OpenAPI document:name, description for the agent, input and output schema, and how to call each. See docs/agent_tools.md."""from serving.app.tools import build_toolsreturn build_tools(app.openapi())@app.get("/v1/model-info", response_model=S.ModelInfo)def model_info():"""196 unmodified lines@app.post("/v1/read-label", response_model=S.ReadLabelResponse)async def read_label(image: UploadFile = File(description="A photo of a medicine box, a pharmacy label or a ""prescription (close up, straight on), or a PDF"),medications: str | None = Form(None, description="Optional JSON list of the patient's ""medications, [{id, name, rxcui?, strength?}]: ""each medicine read is compared with it")):"""**Which medicine is on this box, label or prescription?** Reads the printed text and finds the medicine names init (RxNorm's ingredient and brand names, international names such as paracetamol too), with strength,ingredients, RxNorm code, and what is printed about each (directions, quantity, dose form, dates), every valuewith the line it was read from and `null` when not printed (blueprint B7). With `medications`, each medicine iscompared with the patient's list: same medicine, different strength, or not on the list (B9). Send a confirmedcandidate to `/v1/safety/medications` as `{"id", "name", "rxcui"}`. Always ask the user to confirm.Nothing is stored; only the lines that name or describe a medicine are returned (labels show the patient's name)."""data = await image.read()reader = asset("reader")if data[:5] == b"%PDF-" or (image.filename or "").lower().endswith(".pdf"):lines, source = await run_in_threadpool(pdf_lines, data, reader)else:try:photo = ImageOps.exif_transpose(Image.open(io.BytesIO(data))).convert("RGB")except (UnidentifiedImageError, OSError) as error:raise HTTPException(422, f"{image.filename or 'image'} is not a readable image or PDF: {error}")lines, source = await run_in_threadpool(reader.read_text, photo), "photo"meds = parse_json(medications, "medications") if medications else Nonetry:label_reader = asset("label_reader")candidates = await run_in_threadpool(label_reader.read, lines)document_fields(lines, candidates)if meds is not None:listed = await run_in_threadpool(resolve_list, meds)for c in candidates:c["list_check"] = compare_with_list(c, listed)except RxNormError as error:raise HTTPException(502, f"RxNorm didn't answer: {error}")result = {"candidates": candidates, "note": LABEL_NOTE, "ocr_model": reader.name, "source": source}if not candidates:result["message"] = ("No medicine name found. Photograph the label closer, straight on and in good light, ""or type the name.")return resultdef pdf_lines(data, reader, max_pages=3):"""A PDF's lines: its text layer when it has one, else its pages rendered and read like photos."""from pypdf import PdfReadertry:pages = PdfReader(io.BytesIO(data)).pages[:max_pages]lines = [line.strip() for page in pages for line in (page.extract_text() or "").splitlines() if line.strip()]except Exception as error:raise HTTPException(422, f"not a readable PDF: {error}")if sum(len(line) for line in lines) >= 20:return lines, "pdf_text"import pypdfium2pdf = pypdfium2.PdfDocument(data)lines = []for i in range(min(len(pdf), max_pages)):lines += reader.read_text(pdf[i].render(scale=2).to_pil())return lines, "pdf_scan"def resolve_list(meds):"""The patient's list for the comparison: each medication's ingredients (RxNorm) and printed strength."""out = []for m in meds:if not isinstance(m, dict) or not m.get("id"):raise HTTPException(422, "medications: each needs an id (and a name or rxcui)")r = resolve_medication({"id": str(m["id"]), "name": m.get("name"), "rxcui": m.get("rxcui")}, app.state.rxnorm)strength = m.get("strength") or next((x.group() for x in STRENGTH.finditer(m.get("name") or "")), None)out.append({"id": str(m["id"]), "name": m.get("name") or r.display_name,"ingredients": {i.rxcui for i in r.ingredients if i.rxcui}, "strength": strength})return out@app.get("/v1/pills/search", response_model=S.SearchResponse)def pills_search(name: str = Query(min_length=2, description="Brand or generic name, e.g. 'metformin', 'Glucophage'"),strength: str | None = Query(None, description="e.g. '500 MG'"),79 unmodified lines"""Run when a medication is added or changed: interactions between every pair of the patient's medications(DDInter severity) and therapeutic duplications (the same active ingredient twice). Medications that can't beidentified are reported as alerts. `coverage` says what could actually be screened: **no alert does not mean themedicines are safe together** (blueprint B10)."""db = asset("safety_db")resolved = [resolve_medication(m.model_dump(), app.state.rxnorm) for m in request.medications]2 unmodified lines"alerts": [a.to_dict() for a in alerts],"medications": per_med,"all_identified": all(m.resolved_by != "unresolved" for m in resolved),"coverage": coverage(per_med),"note": SAFETY_NOTE,"sources": {"interactions": db.meta.get("source"), "licence": db.meta.get("licence"),"built_at": db.meta.get("built_at")},}def coverage(per_med):"""What the interaction check could screen: medications with at least one ingredient in DDInter, and pairs."""screened = {m["id"] for m in per_medif len(m["ingredients"]) > len(m["not_in_interaction_database"])}ids = [m["id"] for m in per_med]pairs = [(a, b) for i, a in enumerate(ids) for b in ids[i + 1:]]checked = sum(a in screened and b in screened for a, b in pairs)return {"medications": len(per_med), "screened": len(screened),"not_screened": [m["name"] for m in per_med if m["id"] not in screened],"ingredients_not_in_interaction_database": sorted({n for m in per_medfor n in m["not_in_interaction_database"]}),"pairs": len(pairs), "pairs_checked": checked, "pairs_not_checkable": len(pairs) - checked}@app.post("/v1/safety/dose", response_model=S.DoseResponse)def safety_dose(request: DoseRequest):"""
120 unmodified lines1211221231241251261 unmodified line12812913013113213313413513613713891 unmodified lines2302312322332342352 unmodified lines2382392402412422432442452462472482499 unmodified lines259260261262263264265266267268269270271272273274275276277278120 unmodified lineskind: strclass Product(Out):pill_id: strname: str | None1 unmodified linegeneric_name: str | Nonemanufacturer: str | Noneingredients: list[str]shape: str | Nonecolour: str | Nonesize_mm: float | Noneimprint: str | Noneimages: list[Picture]class Candidate(Product):91 unmodified linesname: strclass LabelCandidate(Out):name: strbrand: str | None2 unmodified linesrxcui: str | Nonerxnorm_name: str | Nonefound_in: list[str] = Field(description="Only the lines that name the medicine")class ReadLabelResponse(Out):candidates: list[LabelCandidate]note: strocr_model: strmessage: str | None = None9 unmodified lines# ---- Stage 3 and feedback --------------------------------------------------------------------------------------------class MedicationsResponse(Out):alerts: list[Alert]medications: list[dict]all_identified: boolsources: dictclass LabelResponse(Out):brand_name: str | Nonegeneric_name: str | Nonelabel_date: str | Nonematched_by: strsections: dict[str, str]disclaimer: strclass FeedbackStored(Out):120 unmodified lines1211221231241251261271281291301311 unmodified line13313413513613713813914014114214314414514614714814915015115215315415515691 unmodified lines2482492502512522532542552562572582592602612622632642652662672682692702712722 unmodified lines2752762772782792802812822832842852862872882892902919 unmodified lines301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336120 unmodified lineskind: strclass Strength(Out):ingredient: strstrength: strclass Product(Out):pill_id: strname: str | None1 unmodified linegeneric_name: str | Nonemanufacturer: str | Noneingredients: list[str]strength: str | None = Field(None, description="e.g. '500 MG', or '40 MG / 1100 MG' for a combination")strengths: list[Strength] = []dose_form: str | None = Field(None, description="e.g. 'Oral Tablet', 'Extended Release Oral Capsule', 'Pack'")release: Literal["IR", "ER", "DR"] | None = Field(None, description="Immediate, extended or delayed release")brand: str | None = Nonebrand_or_generic: Literal["brand", "generic"] | None = Nonepack: list[dict] | None = Field(None, description="For packs: each kind of pill, with count and strength")ndc9: str | None = Nonendc11: str | None = Nonerxcui: str | None = Nonerxnorm_status: str | None = Nonemarket: Literal["US"] = "US"shape: str | Nonecolour: str | Nonesize_mm: float | Noneimprint: str | Noneimages: list[Picture]image_licence: str | None = Noneclass Candidate(Product):91 unmodified linesname: strclass Printed(Out):value: strline: str = Field(description="The line(s) it was read from")class DocumentFields(Out):directions: Printed | None = Field(None, description="As printed, e.g. 'Take 1 tablet twice a day after meals'")quantity: Printed | None = Nonedose_form: Printed | None = Nonedate: Printed | None = Field(None, description="Dispensing or prescription date, as printed")expiry_date: Printed | None = Noneclass ListCheck(Out):status: Literal["same_medicine", "different_strength", "contains_listed", "not_on_list"]medication_id: str | Nonemessage: strclass LabelCandidate(Out):name: strbrand: str | None2 unmodified linesrxcui: str | Nonerxnorm_name: str | Nonefound_in: list[str] = Field(description="Only the lines that name the medicine")first_line: int | None = Nonefields: DocumentFields | None = Field(None, description="What is printed about this medicine; null when not ""printed, never inferred")list_check: ListCheck | None = Field(None, description="With `medications`: compared with the patient's list")class ReadLabelResponse(Out):candidates: list[LabelCandidate]note: strocr_model: strsource: Literal["photo", "pdf_text", "pdf_scan"] = "photo"message: str | None = None9 unmodified lines# ---- Stage 3 and feedback --------------------------------------------------------------------------------------------class Coverage(Out):medications: intscreened: int = Field(description="Medications with at least one ingredient in the interaction database")not_screened: list[str]ingredients_not_in_interaction_database: list[str]pairs: intpairs_checked: intpairs_not_checkable: intclass MedicationsResponse(Out):alerts: list[Alert]medications: list[dict]all_identified: boolcoverage: Coveragenote: str = Field(description="No alert doesn't mean the medicines are safe together")sources: dictclass LabelResponse(Out):brand_name: str | Nonegeneric_name: str | Nonelabel_date: str | None = Field(description="The label's effective date (YYYYMMDD)")matched_by: strsections: dict[str, str]disclaimer: strsource: str | None = Noneset_id: str | None = Nonelabel_id: str | None = Noneurl: str | None = Field(None, description="The label on DailyMed, for citation")class FeedbackStored(Out):
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788"""Typed tool definitions for the Medication Care Agent (blueprint job B14), generated from the API's own OpenAPIdocument so they can't drift from it. Served at GET /v1/tools; usage notes and example traces in docs/agent_tools.md.
Each tool: name (the blueprint's where it names one), description written for the agent (when to call it, what itmust not conclude), input_schema (JSON Schema; an uploaded file becomes an image reference the app's tool layerresolves), output_schema ($refs into `components`), and how to call it (method, path, content type)."""
TOOLS = [ ("detect_pills", "post", "/v1/detect", "Find each pill in a photo and say whether the count can be trusted. Call before counting pills or embedding " "several pills. If count_reliable is false, don't count: ask for a new photo (pills apart, plain surface)."), ("embed_pill", "post", "/v1/embed", "Turn one loose-pill photo (or a photo + box) into a 256-number embedding. Use at enrolment (store it with " "model_version and embedding_space_id) and before match_gallery. Never embed boxes or blister packs."), ("match_gallery", "post", "/v1/match", "Compare pill embeddings with the patient's gallery: match / unknown / ambiguous per pill. Visual evidence only: " "a match means 'looks like', not 'is' or 'safe to take'. ambiguous: ask for another photo. The gallery's " "model_version is required; on 409 re-embed the enrolment photos."), ("identify_pill", "post", "/v1/identify", "Possible catalogue matches (US products) for a pill that isn't in the patient's list. Report the verdict and " "confidence, never a definite identity; no_confident_match means the pill may not be a catalogue product."), ("scan_photo", "post", "/v1/scan", "Diagnostics in one call: detection plus each pill's visual decision (with a gallery) or catalogue candidates. " "Don't use its dose check (deprecated): apply the backend's dose rules to the pills it returns."), ("extract_medication_document", "post", "/v1/read-label", "Read a medicine box, pharmacy label or prescription (photo or PDF): medicine names, strength, RxNorm code and " "what is printed (directions, quantity, dates), each with its source line. Fields not printed are null; never " "fill them in. With the patient's medications, says same medicine / different strength / not on the list. " "Always have the patient confirm."), ("find_medication_product", "get", "/v1/pills/search", "Products and pictures for a medicine name (brand or generic), with strength, dose form and release as fields. " "Never treat products of another strength or form as the same."), ("screen_medication_list", "post", "/v1/safety/medications", "Interaction and duplicate-ingredient alerts for the whole medication list (DDInter 2.0). Read coverage: no " "alert does not mean safe. Advisory; defer to the pharmacist."), ("evaluate_dose_reference", "post", "/v1/safety/dose", "The reference dose rules on visual decisions (stateless). The backend's own dose policy is authoritative."), ("retrieve_medication_info", "get", "/v1/safety/label", "Warning sections of the official US label, with the label's date and DailyMed link for citation. General " "information, not the patient's instructions."), ("get_model_info", "get", "/v1/model-info", "The served model: version, embedding space, decision rule and known limits. Check after a 409."),]FILE_NOTE = "An image reference: the app's tool layer uploads the file (photo, or PDF for documents)"
def _resolve(schema, components): ref = schema.get("$ref", "") if isinstance(schema, dict) else "" return components.get(ref.rsplit("/", 1)[-1], {}) if ref else schema
def _input_schema(op, components): props, required = {}, [] for p in op.get("parameters", []): props[p["name"]] = {**p.get("schema", {}), "description": p.get("description", "")} if p.get("required"): required.append(p["name"]) content = op.get("requestBody", {}).get("content", {}) for kind in ("application/json", "multipart/form-data"): if kind in content: body = _resolve(content[kind]["schema"], components) for name, s in body.get("properties", {}).items(): is_file = s.get("format") == "binary" or s.get("contentMediaType") or \ (s.get("type") == "array" and (s.get("items", {}).get("format") == "binary" or s.get("items", {}).get("contentMediaType"))) props[name] = ({"type": "array", "items": {"type": "string"}, "description": FILE_NOTE} if is_file and s.get("type") == "array" else {"type": "string", "description": FILE_NOTE} if is_file else s) required += body.get("required", []) return {"type": "object", "properties": props, "required": sorted(set(required))}
def build_tools(openapi): components = openapi.get("components", {}).get("schemas", {}) tools = [] for name, method, path, description in TOOLS: op = openapi["paths"][path][method] ok = op.get("responses", {}).get("200", {}).get("content", {}).get("application/json", {}) kind = next(iter(op.get("requestBody", {}).get("content", {"none": None}))) tools.append({"name": name, "description": description, "input_schema": _input_schema(op, components), "output_schema": ok.get("schema", {}), "call": {"method": method.upper(), "path": path, "content_type": None if kind == "none" else kind}}) return {"tools": tools, "components": {"schemas": components}, "notes": "Stage 2 answers are visual evidence only. The backend's safety policy decides actions; the " "agent reports evidence with its limits and asks the patient to confirm."}123456789101112131415161718192021222324252627282930313233343536373839404142434445"""A catalogue product's identity, from its RxNorm name (blueprint job B13): products must never be merged acrossstrength, dose form or release, so these are returned as fields, not left inside a name.
RxNorm clinical and branded drug names follow one pattern: [12 HR | 24 HR] <ingredient> <amount> <unit> [/ <ingredient> <amount> <unit> ...] <dose form> [<Brand>]e.g. "24 HR metformin hydrochloride 500 MG Extended Release Oral Tablet [Glucophage]"."""import re
UNIT = r"(?:MG|MCG|UNT|MEQ|ML|%|MG/ML|MG/MG|MCG/ML|UNT/ML|MEQ/ML|MG/HR|MCG/HR|MG/ACTUAT|MCG/ACTUAT|ACTUAT)"AMOUNT = re.compile(rf"(\d+(?:\.\d+)?)\s+({UNIT})(?=\s|$)")
def product_identity(rxnorm_name, name=None): """-> strength, strengths (per ingredient), dose_form, release (IR / ER / DR), brand, brand_or_generic.""" out = {"strength": None, "strengths": [], "dose_form": None, "release": None, "brand": None, "brand_or_generic": None} text = (rxnorm_name or "").strip() if not text: return out brand = re.search(r"\s*\[([^\]]+)\]\s*$", text) if brand: out["brand"], text = brand.group(1), text[:brand.start()] out["brand_or_generic"] = "brand" if brand else "generic" if text.startswith("{"): # a pack, e.g. "{7 (... Oral Tablet) / 7 (... Oral Tablet) } Pack": several pills out["dose_form"], out["release"] = "Pack", "IR" out["pack"] = [{"count": int(n), **{k: v for k, v in product_identity(re.sub(r"\s*\[[^\]]*\]", "", c)).items() if k in ("strength", "dose_form", "release")}} for n, c in re.findall(r"(\d+) \(([^()]*)\)", text)] return out upper = text.upper() out["release"] = ("ER" if re.match(r"^\d+ HR ", text) or "EXTENDED RELEASE" in upper else "DR" if "DELAYED RELEASE" in upper else "IR") amounts = list(AMOUNT.finditer(text)) if amounts: out["dose_form"] = text[amounts[-1].end():].strip() or None body = re.sub(r"^\d+ HR ", "", text[:amounts[-1].end()]) for part in body.split(" / "): m = AMOUNT.search(part) if m: out["strengths"].append({"ingredient": part[:m.start()].strip(), "strength": f"{m.group(1)} {m.group(2)}"}) out["strength"] = " / ".join(s["strength"] for s in out["strengths"]) or None return out51 unmodified lines525354555657585951 unmodified linesreturn [texts[i:i + n] for i in range(0, len(texts), n)]def read_text(self, image, max_new_tokens=512):"""A photo of a box or label -> its lines of text, top to bottom (Florence-2's OCR with regions)."""image = image.convert("RGB")x = self.processor(text=["<OCR_WITH_REGION>"], images=[image], return_tensors="pt")raw = self.processor.batch_decode(self.generate(x["input_ids"], x["pixel_values"], max_new_tokens),skip_special_tokens=False)[0]51 unmodified lines5253545556575859606162636465666768697071727351 unmodified linesreturn [texts[i:i + n] for i in range(0, len(texts), n)]def read_text(self, image, max_new_tokens=512):"""A photo of a box, label or document page -> its lines of text, top to bottom (Florence-2's OCR withregions). Florence sees every image at 768 x 768, so a tall page is read in overlapping bands."""image = image.convert("RGB")w, h = image.sizeif h <= 1.25 * w:return self._read_region(image, max_new_tokens)band, lines = int(w * 0.8), []for top in range(0, max(1, h - band // 6), int(band * 0.85)):for line in self._read_region(image.crop((0, top, w, min(h, top + band))), max_new_tokens):if line not in lines[-8:]: # the overlap reads some lines twicelines.append(line)return linesdef _read_region(self, image, max_new_tokens):x = self.processor(text=["<OCR_WITH_REGION>"], images=[image], return_tensors="pt")raw = self.processor.batch_decode(self.generate(x["input_ids"], x["pixel_values"], max_new_tokens),skip_special_tokens=False)[0]
242 unmodified lines243244245246247248249242 unmodified lines"rxcui": product[0] if product else (lead.rxcui if lead.tty != "INN" else g["ingredients"][0][0]),"rxnorm_name": product[1] if product else None,"found_in": [lines[i] for i in sorted({f.line for f in found})],"_rank": (-(bool(brand) + bool(strengths) + bool(product) + len(found)), found[0].line),})out.sort(key=lambda c: c.pop("_rank"))return out242 unmodified lines243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351242 unmodified lines"rxcui": product[0] if product else (lead.rxcui if lead.tty != "INN" else g["ingredients"][0][0]),"rxnorm_name": product[1] if product else None,"found_in": [lines[i] for i in sorted({f.line for f in found})],"first_line": found[0].line,"_rank": (-(bool(brand) + bool(strengths) + bool(product) + len(found)), found[0].line),})out.sort(key=lambda c: c.pop("_rank"))return out# ---- what else a label or prescription says (blueprint job B7) -------------------------------------------------------DIRECTION_START = re.compile(r"^\s*(take|apply|use|inhale|instil+|insert|dissolve|chew|swallow|place|spray|give|put|"r"rub|gargle|dilute|one|two|1|2)\b", re.I)DIRECTION_WORDS = re.compile(r"\b(times? (a|per) day|(once|twice|thrice) (a|per) day|daily|every \d+ hours?|"r"when necessary|as needed|at night|in the "r"morning|at bedtime|before (meals|food|breakfast)|after (meals|food|breakfast)|"r"with (meals|food)|on an empty stomach)\b", re.I)QUANTITY = re.compile(r"\b(?:qty|quantity|dispensed)\s*[:.]?\s*(\d+)\s*([a-z]*)|\b(\d+)\s+"r"(?:(?!(?:mg|mcg|g|ml|iu)\b)[a-z\-]+\s+)?(tablets?|capsules?|caplets?|tabs?|caps?|sachets?)\b",re.I)DATE = re.compile(r"\b(\d{1,2}[/.\-]\d{1,2}[/.\-]\d{2,4}|\d{1,2} (?:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)"r"[a-z]* \d{4}|\d{1,2}[/.\-]\d{4})\b", re.I)PERSONAL = re.compile(r"\b(name|patient|nric|fin|address|tel|phone|dob|date of birth)\b", re.I)def _field(value, line):return {"value": value, "line": line} if value else Nonedef document_fields(lines, candidates):"""Per candidate, what the label prints about it, each value with the line it was read from; None when it isn'tprinted (never inferred). A candidate's block runs from its first line to the next candidate's (the first onealso gets the header lines above it: the dispensing date). Lines with personal details are never used."""starts = sorted({c["first_line"] for c in candidates})for c in candidates:i = starts.index(c["first_line"])lo = 0 if i == 0 else starts[i]hi = starts[i + 1] if i + 1 < len(starts) else len(lines)block = [(n, lines[n]) for n in range(lo, hi) if not PERSONAL.search(lines[n])]directions, quantity, date, expiry = [], None, None, Nonefor n, line in block:is_direction = n >= c["first_line"] and (DIRECTION_START.search(line) and DIRECTION_WORDS.search(line)or (directions and directions[-1][0] == n - 1and DIRECTION_WORDS.search(line)))if is_direction:directions.append((n, line))continueq = QUANTITY.search(line)if q and quantity is None and n >= c["first_line"] and line not in c["found_in"]:number, unit = (q.group(1), q.group(2)) if q.group(1) else (q.group(3), q.group(4))quantity = _field(f"{number} {unit.lower()}".strip(), line)d = DATE.search(line)if d:if re.search(r"\bexp", line, re.I):expiry = expiry or _field(d.group(1), line)else:date = date or _field(d.group(1), line)form = dose_form([line for n, line in block if n >= c["first_line"]])c["fields"] = {"directions": _field(" ".join(l for _, l in directions), " / ".join(l for _, l in directions)),"quantity": quantity,"dose_form": _field(form, next((l for n, l in block if n >= c["first_line"] and form andre.search(form[:5], l, re.I)), None)),"date": date,"expiry_date": expiry,}return candidates# ---- is this box one of my medicines? (blueprint job B9) -------------------------------------------------------------def _amounts(strength):"""'500 mg, 65 mg' or '500 MG / 65 MG' -> {(500.0, 'mg'), (65.0, 'mg')}."""return {(float(m.group(1).replace(",", ".")), m.group(2).lower().replace("µg", "mcg").replace("ug", "mcg"))for m in STRENGTH.finditer(strength or "")}def compare_with_list(candidate, medications):"""medications: the patient's list, each {"id", "name", "ingredients": {rxcui}, "strength": printed or None}.-> {"status", "medication_id", "message"}: a deterministic comparison of ingredients and strength, no model score.same_medicine same ingredients and strength (or the list gives no strength)different_strength same ingredients, another strengthcontains_listed a combination that includes a listed medicine's ingredientnot_on_list no ingredient in common"""have = {i["rxcui"] for i in candidate["ingredients"] if i.get("rxcui")}best = Nonefor med in medications:common = have & med["ingredients"]if not common:continueif common == have == med["ingredients"]:a, b = _amounts(candidate.get("strength")), _amounts(med.get("strength"))if a and b and a != b:status = "different_strength"message = (f"This says {candidate.get('strength')}; your list has {med['name']}. Check with your ""pharmacist.")else:status = "same_medicine"message = f"This matches your {med['name']}: the same medicine" + \(" and strength." if a and b else ". Check the strength printed on it.")return {"status": status, "medication_id": med["id"], "message": message}best = best or {"status": "contains_listed", "medication_id": med["id"],"message": f"This medicine shares an ingredient with your {med['name']} but is not the same. ""Check with your pharmacist."}return best or {"status": "not_on_list", "medication_id": None,"message": "This medicine isn't on your list. Check with your pharmacist."}
53 unmodified lines5455565758591 unmodified line616263646553 unmodified linestext = " ".join(r[name])sections[name] = text[:max_chars] + ("…" if len(text) > max_chars else "")of = r.get("openfda", {})return {"brand_name": (of.get("brand_name") or [None])[0],"generic_name": (of.get("generic_name") or [None])[0],1 unmodified line"matched_by": q.split(":")[0].replace("openfda.", ""),"sections": sections,"disclaimer": DISCLAIMER,}return None53 unmodified lines545556575859601 unmodified line62636465666768697053 unmodified linestext = " ".join(r[name])sections[name] = text[:max_chars] + ("…" if len(text) > max_chars else "")of = r.get("openfda", {})set_id = r.get("set_id")return {"brand_name": (of.get("brand_name") or [None])[0],"generic_name": (of.get("generic_name") or [None])[0],1 unmodified line"matched_by": q.split(":")[0].replace("openfda.", ""),"sections": sections,"disclaimer": DISCLAIMER,# for citation (the blueprint's Medication Knowledge tool): which label, from where"source": "US FDA drug label (structured product labeling), via openFDA","set_id": set_id, "label_id": r.get("id"),"url": f"https://dailymed.nlm.nih.gov/dailymed/lookup.cfm?setid={set_id}" if set_id else None,}return None
requirements.txt+2
serving/app/main.py+100-12
serving/app/schemas.py+59-1
serving/app/tools.pyAdded+88
src/epills/core/catalogue/identity.pyAdded+45
src/epills/core/imprint/ocr.py+15-1
src/epills/core/label.py+102
src/epills/core/safety/openfda.py+5