SmartHeal commited on
Commit
d45423b
·
verified ·
1 Parent(s): 8fc898c

Update src/ai_processor.py

Browse files
Files changed (1) hide show
  1. 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
- 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 = """\
@@ -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 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",
 
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",