Spaces:
Running
Running
Update src/ai_processor.py
Browse files- src/ai_processor.py +36 -41
src/ai_processor.py
CHANGED
|
@@ -108,24 +108,27 @@ def _import_hf_hub():
|
|
| 108 |
from huggingface_hub import HfApi, HfFolder
|
| 109 |
return HfApi, HfFolder
|
| 110 |
|
|
|
|
| 111 |
# ---------- SmartHeal prompts (system + user prefix) ----------
|
| 112 |
SMARTHEAL_SYSTEM_PROMPT = """\
|
| 113 |
You are SmartHeal Clinical Assistant, a wound-care decision-support system.
|
| 114 |
You analyze wound photographs and brief patient context to produce careful,
|
| 115 |
specific, guideline-informed recommendations WITHOUT diagnosing.
|
| 116 |
|
| 117 |
-
|
| 118 |
-
-
|
| 119 |
-
-
|
| 120 |
-
- Use
|
| 121 |
-
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
-
|
| 125 |
-
-
|
| 126 |
-
|
| 127 |
-
-
|
| 128 |
-
-
|
|
|
|
|
|
|
| 129 |
"""
|
| 130 |
|
| 131 |
SMARTHEAL_USER_PREFIX = """\
|
|
@@ -133,46 +136,38 @@ Patient: {patient_info}
|
|
| 133 |
Visual findings: type={wound_type}, size={length_cm}x{breadth_cm} cm, area={area_cm2} cm^2,
|
| 134 |
detection_conf={det_conf:.2f}, calibration={px_per_cm} px/cm.
|
| 135 |
|
| 136 |
-
Guideline context (principles you may draw from—summarize, don’t quote):
|
| 137 |
{guideline_context}
|
| 138 |
|
| 139 |
-
Write a structured answer with
|
| 140 |
|
| 141 |
Analysis
|
| 142 |
-
-
|
| 143 |
-
-
|
| 144 |
-
|
| 145 |
-
- Call out uncertainties, data gaps, and any factors that constrain product/medication choices.
|
| 146 |
-
- Identify red flags and thresholds for escalation or urgent evaluation (e.g., spreading erythema, systemic signs, suspected ischemia, necrosis, rapidly increasing pain).
|
| 147 |
|
| 148 |
Medication and Treatment
|
| 149 |
-
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
*
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
and de-escalation/stop rules after clinical reassessment.
|
| 162 |
-
* Probiotics or adjunctive therapies only if supported; state evidence quality briefly.
|
| 163 |
-
- Follow-up cadence (explicit days) and objective response criteria (exudate ↓, pain ↓, size ↓, granulation ↑).
|
| 164 |
-
- Clear stop/switch rules for dressings/medications based on response or intolerance.
|
| 165 |
|
| 166 |
Disclaimer
|
| 167 |
-
-
|
| 168 |
-
-
|
| 169 |
-
|
| 170 |
-
- Advise urgent care for red flags or deterioration, and reinforce adherence to local formularies, availability, and guidelines.
|
| 171 |
-
|
| 172 |
-
Produce at least 1,000 words in total while remaining precise and focused. Do not use generic drug names anywhere in the response.
|
| 173 |
"""
|
| 174 |
|
| 175 |
|
|
|
|
| 176 |
# ---------- MedGemma-only text generator ----------
|
| 177 |
@_SPACES_GPU(enable_queue=True)
|
| 178 |
def vlm_generate(prompt, image_pil, model_id="unsloth/medgemma-4b-it-bnb-4bit",
|
|
|
|
| 108 |
from huggingface_hub import HfApi, HfFolder
|
| 109 |
return HfApi, HfFolder
|
| 110 |
|
| 111 |
+
# ---------- SmartHeal prompts (system + user prefix) ----------
|
| 112 |
# ---------- SmartHeal prompts (system + user prefix) ----------
|
| 113 |
SMARTHEAL_SYSTEM_PROMPT = """\
|
| 114 |
You are SmartHeal Clinical Assistant, a wound-care decision-support system.
|
| 115 |
You analyze wound photographs and brief patient context to produce careful,
|
| 116 |
specific, guideline-informed recommendations WITHOUT diagnosing.
|
| 117 |
|
| 118 |
+
Output requirements (strict):
|
| 119 |
+
- Treat the vision pipeline measurements as ground truth; restate them once.
|
| 120 |
+
- Write in concise, clinical bullets with clear, actionable steps (no filler).
|
| 121 |
+
- Use EXACT section headings and order: Analysis; Medication and Treatment; Disclaimer.
|
| 122 |
+
- Provide a single primary plan plus sensible alternatives when appropriate (e.g., by exudate level).
|
| 123 |
+
- For dressings: name the category (e.g., foam/alginate/hydrogel/silver/iodine/PHMB/honey), typical wear time,
|
| 124 |
+
change frequency, and what to switch to if too wet/dry or if maceration appears.
|
| 125 |
+
- For offloading/compression/NPWT: state the indication criteria and practical device choice.
|
| 126 |
+
- For medications: suggest evidence-based options (generic names), with typical adult dose ranges, route, and duration;
|
| 127 |
+
include key contraindications/interactions and mark as “for clinician review”.
|
| 128 |
+
- Include a follow-up cadence (in days) and explicit switch/stop rules and escalation triggers.
|
| 129 |
+
- If information is missing, state assumptions briefly and proceed with a best-practice plan.
|
| 130 |
+
- Tone: professional, precise, conservative. Avoid definitive diagnoses or promises of cure.
|
| 131 |
+
- Length target: 220–350 words total. No preamble or closing beyond the specified sections.
|
| 132 |
"""
|
| 133 |
|
| 134 |
SMARTHEAL_USER_PREFIX = """\
|
|
|
|
| 136 |
Visual findings: type={wound_type}, size={length_cm}x{breadth_cm} cm, area={area_cm2} cm^2,
|
| 137 |
detection_conf={det_conf:.2f}, calibration={px_per_cm} px/cm.
|
| 138 |
|
| 139 |
+
Guideline context (principles you may draw from—summarize, don’t quote verbatim):
|
| 140 |
{guideline_context}
|
| 141 |
|
| 142 |
+
Write a structured, actionable answer with these headings EXACTLY and nothing else:
|
| 143 |
|
| 144 |
Analysis
|
| 145 |
+
- Restate the measured size/area once and interpret exudate burden, likely bioburden risk, and peri-wound skin status.
|
| 146 |
+
- Note key risks tied to the wound type (e.g., DFU → pressure/neuropathy/ischemia), and any uncertainties or data gaps
|
| 147 |
+
(e.g., PAD status, glycemic control, duration). Be specific.
|
|
|
|
|
|
|
| 148 |
|
| 149 |
Medication and Treatment
|
| 150 |
+
- Cleansing/irrigation: solution, volume, and frequency.
|
| 151 |
+
- Debridement: if/when indicated; method options (conservative sharp, autolytic, enzymatic) and when to avoid.
|
| 152 |
+
- Dressing strategy: pick ONE primary dressing category based on the current exudate level; include change frequency,
|
| 153 |
+
expected wear time, and a backup option if too wet/dry or if maceration/odor occurs.
|
| 154 |
+
- Adjuncts: offloading (preferred device and when to use TCC vs removable walker), compression (only if appropriate; note ABI threshold),
|
| 155 |
+
barrier films/silicone contact layers, and criteria for NPWT (size, depth, exudate, surgical wounds).
|
| 156 |
+
- Medications (for clinician review): generic names with typical adult dose ranges, route, and duration:
|
| 157 |
+
* Analgesia (acetaminophen/NSAID with max daily dose cautions).
|
| 158 |
+
* Antimicrobials: topical options for localized critical colonization; systemic options ONLY if clinical infection criteria met.
|
| 159 |
+
Include top interactions/contraindications and monitoring (renal/hepatic disease, anticoagulation, pregnancy, allergy).
|
| 160 |
+
- Follow-up cadence (explicit days) and objective response criteria (area ↓, exudate ↓, pain ↓, granulation ↑).
|
| 161 |
+
- Clear switch/stop rules for dressings and antimicrobials based on response or intolerance.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
Disclaimer
|
| 164 |
+
- This is decision support, not a diagnosis or prescription. All medications/interventions require clinician review.
|
| 165 |
+
- Advise urgent evaluation for red flags (spreading erythema, fever, rapidly worsening pain, necrosis, malodor, suspected ischemia),
|
| 166 |
+
and tailor to local guidelines/formulary and patient comorbidities.
|
|
|
|
|
|
|
|
|
|
| 167 |
"""
|
| 168 |
|
| 169 |
|
| 170 |
+
|
| 171 |
# ---------- MedGemma-only text generator ----------
|
| 172 |
@_SPACES_GPU(enable_queue=True)
|
| 173 |
def vlm_generate(prompt, image_pil, model_id="unsloth/medgemma-4b-it-bnb-4bit",
|