Spaces:
Running
Running
Update src/ai_processor.py
Browse files- src/ai_processor.py +52 -17
src/ai_processor.py
CHANGED
|
@@ -112,32 +112,67 @@ def _import_hf_hub():
|
|
| 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 |
-
-
|
| 121 |
-
-
|
| 122 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
"""
|
| 124 |
|
| 125 |
SMARTHEAL_USER_PREFIX = """\
|
| 126 |
Patient: {patient_info}
|
| 127 |
Visual findings: type={wound_type}, size={length_cm}x{breadth_cm} cm, area={area_cm2} cm^2,
|
| 128 |
detection_conf={det_conf:.2f}, calibration={px_per_cm} px/cm.
|
| 129 |
-
|
|
|
|
| 130 |
{guideline_context}
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
"""
|
| 140 |
|
|
|
|
| 141 |
# ---------- MedGemma-only text generator ----------
|
| 142 |
@_SPACES_GPU(enable_queue=True)
|
| 143 |
def vlm_generate(prompt, image_pil, model_id="unsloth/medgemma-4b-it-bnb-4bit",
|
|
|
|
| 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 |
+
Strict rules:
|
| 118 |
+
- Use the vision pipeline measurements as ground truth.
|
| 119 |
+
- Be detailed yet to-the-point; prefer short, clinical sentences and tight bullets.
|
| 120 |
+
- Use the EXACT section headings: Analysis; Medication and Treatment; Disclaimer.
|
| 121 |
+
- Do NOT include generic medication names. When medications are relevant, refer only to branded products
|
| 122 |
+
(region-agnostic where possible), dosage forms/strengths, typical adult dose ranges, route, and duration.
|
| 123 |
+
Mark all medication suggestions as “for clinician review”.
|
| 124 |
+
- If guideline context is supplied, distill and integrate its principles clearly (do not quote at length).
|
| 125 |
+
- Flag red-flag situations that require urgent escalation.
|
| 126 |
+
- Avoid definitive diagnoses, off-label claims, or inferring unseen comorbidities.
|
| 127 |
+
- Length requirement: write no fewer than 1,000 words (aim 1,000–1,300) while remaining concise and focused.
|
| 128 |
+
- Tone: professional, clear, conservative, non-promotional.
|
| 129 |
"""
|
| 130 |
|
| 131 |
SMARTHEAL_USER_PREFIX = """\
|
| 132 |
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 the following headings EXACTLY and in this order.
|
| 140 |
+
|
| 141 |
+
Analysis
|
| 142 |
+
- Interpret the wound’s dimensions and likely implications (bioburden, exudate burden, contamination/infection risk, pain considerations, peri-wound skin status).
|
| 143 |
+
- Explain how measurement accuracy and calibration affect product selection.
|
| 144 |
+
- Summarize relevant guideline principles (e.g., moisture balance, debridement indications, offloading, compression where appropriate) in 2–4 crisp bullets.
|
| 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 |
+
- Stepwise plan:
|
| 150 |
+
* Cleansing/irrigation protocol (solution type/volume, frequency).
|
| 151 |
+
* Debridement approach if indicated (state method options and selection rationale).
|
| 152 |
+
* Dressing strategy by exudate level and infection risk; specify product categories and named branded options only
|
| 153 |
+
(no generic names), with form factor (sheet/foam/alginate/hydrogel/iodine/silver/PHMB/honey), change frequency,
|
| 154 |
+
wear time expectations, and compatibility notes.
|
| 155 |
+
* Adjuncts: compression/offloading/negative-pressure therapy/silicone contact layers/barrier films as indicated.
|
| 156 |
+
- MEDICATION SUGGESTIONS (for clinician review; branded names only):
|
| 157 |
+
* Analgesia: branded options with dosage forms/strengths, typical adult dose ranges, route, timing, max daily dose cautions,
|
| 158 |
+
interactions (e.g., anticoagulants, renal/hepatic disease), and monitoring.
|
| 159 |
+
* If localized infection signs: topical branded antimicrobials (form, strength, area limits, duration, precautions).
|
| 160 |
+
* If systemic infection signs: oral branded options per severity tiers; include dose ranges, duration, food/interaction cautions,
|
| 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 |
+
- State clearly that this is decision support, not a diagnosis or prescription.
|
| 168 |
+
- Emphasize clinician review for all medication selections; highlight special populations (pregnancy, pediatrics, elderly,
|
| 169 |
+
renal/hepatic disease, anticoagulation, immunosuppression, allergy history).
|
| 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",
|