Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -176,280 +176,281 @@ class PearlyBot:
|
|
176 |
raise
|
177 |
# Load your knowledge base content
|
178 |
def _load_knowledge_base(self):
|
179 |
-
|
180 |
-
|
|
|
181 |
"triage_scenarios.txt": """Medical Triage Scenarios and Responses:
|
182 |
|
183 |
-
EMERGENCY (999) SCENARIOS:
|
184 |
-
1. Cardiovascular:
|
185 |
-
- Chest pain/pressure
|
186 |
-
- Heart attack symptoms
|
187 |
-
- Irregular heartbeat with dizziness
|
188 |
-
Response: Immediate 999 call, sit/lie down, chew aspirin if available
|
189 |
-
|
190 |
-
2. Respiratory:
|
191 |
-
- Severe breathing difficulty
|
192 |
-
- Choking
|
193 |
-
- Unable to speak full sentences
|
194 |
-
Response: 999, sitting position, clear airway
|
195 |
-
|
196 |
-
3. Neurological:
|
197 |
-
- Stroke symptoms (FAST)
|
198 |
-
- Seizures
|
199 |
-
- Unconsciousness
|
200 |
-
Response: 999, recovery position if unconscious
|
201 |
-
|
202 |
-
4. Trauma:
|
203 |
-
- Severe bleeding
|
204 |
-
- Head injuries with confusion
|
205 |
-
- Major burns
|
206 |
-
Response: 999, apply direct pressure to bleeding
|
207 |
-
|
208 |
-
URGENT CARE (111) SCENARIOS:
|
209 |
-
1. Moderate Symptoms:
|
210 |
-
- Persistent fever
|
211 |
-
- Non-severe infections
|
212 |
-
- Minor injuries
|
213 |
-
Response: 111 contact, monitor symptoms
|
214 |
-
|
215 |
-
2. Minor Emergencies:
|
216 |
-
- Small cuts needing stitches
|
217 |
-
- Sprains and strains
|
218 |
-
- Mild allergic reactions
|
219 |
-
Response: 111 or urgent care visit
|
220 |
-
|
221 |
-
GP APPOINTMENT SCENARIOS:
|
222 |
-
1. Routine Care:
|
223 |
-
- Chronic condition review
|
224 |
-
- Medication reviews
|
225 |
-
- Non-urgent symptoms
|
226 |
-
Response: Book routine GP appointment
|
227 |
-
|
228 |
-
2. Preventive Care:
|
229 |
-
- Vaccinations
|
230 |
-
- Health screenings
|
231 |
-
- Regular check-ups
|
232 |
-
Response: Schedule with GP reception""",
|
233 |
-
|
234 |
-
|
235 |
-
IMMEDIATE LIFE THREATS:
|
236 |
-
1. Cardiac Symptoms:
|
237 |
-
- Chest pain/pressure/tightness
|
238 |
-
- Pain spreading to arms/jaw/neck
|
239 |
-
- Sweating with nausea
|
240 |
-
- Shortness of breath
|
241 |
-
|
242 |
-
2. Breathing Problems:
|
243 |
-
- Severe shortness of breath
|
244 |
-
- Blue lips or face
|
245 |
-
- Unable to complete sentences
|
246 |
-
- Choking/airway blockage
|
247 |
-
|
248 |
-
3. Neurological:
|
249 |
-
- FAST (Face, Arms, Speech, Time)
|
250 |
-
- Sudden confusion
|
251 |
-
- Severe headache
|
252 |
-
- Seizures
|
253 |
-
- Loss of consciousness
|
254 |
-
|
255 |
-
4. Severe Trauma:
|
256 |
-
- Heavy bleeding
|
257 |
-
- Deep wounds
|
258 |
-
- Head injury with confusion
|
259 |
-
- Severe burns
|
260 |
-
- Broken bones with deformity
|
261 |
-
|
262 |
-
5. Anaphylaxis:
|
263 |
-
- Sudden swelling
|
264 |
-
- Difficulty breathing
|
265 |
-
- Rapid onset rash
|
266 |
-
- Light-headedness
|
267 |
-
|
268 |
-
URGENT BUT NOT IMMEDIATE:
|
269 |
-
1. Moderate Symptoms:
|
270 |
-
- Persistent fever
|
271 |
-
- Dehydration
|
272 |
-
- Non-severe infections
|
273 |
-
- Minor injuries
|
274 |
-
|
275 |
-
2. Worsening Conditions:
|
276 |
-
- Increasing pain
|
277 |
-
- Progressive symptoms
|
278 |
-
- Medication reactions
|
279 |
-
|
280 |
-
RESPONSE PROTOCOLS:
|
281 |
-
1. For Life Threats:
|
282 |
-
- Immediate 999 call
|
283 |
-
- Clear first aid instructions
|
284 |
-
- Stay on line until help arrives
|
285 |
-
|
286 |
-
2. For Urgent Care:
|
287 |
-
- 111 contact
|
288 |
-
- Monitor for worsening
|
289 |
-
- Document symptoms""",
|
290 |
-
|
291 |
-
|
292 |
-
APPOINTMENT TYPES:
|
293 |
-
1. Routine Appointments:
|
294 |
-
Template: "I need to book a routine appointment for [condition]. My availability is [times/dates]. My GP is Dr. [name] if available."
|
295 |
-
|
296 |
-
2. Follow-up Appointments:
|
297 |
-
Template: "I need a follow-up appointment regarding [condition] discussed on [date]. My previous appointment was with Dr. [name]."
|
298 |
-
|
299 |
-
3. Medication Reviews:
|
300 |
-
Template: "I need a medication review for [medication]. My last review was [date]."
|
301 |
-
|
302 |
-
BOOKING INFORMATION NEEDED:
|
303 |
-
1. Patient Details:
|
304 |
-
- Full name
|
305 |
-
- Date of birth
|
306 |
-
- NHS number (if known)
|
307 |
-
- Registered GP practice
|
308 |
-
|
309 |
-
2. Appointment Details:
|
310 |
-
- Nature of appointment
|
311 |
-
- Preferred times/dates
|
312 |
-
- Urgency level
|
313 |
-
- Special requirements
|
314 |
-
|
315 |
-
3. Contact Information:
|
316 |
-
- Phone number
|
317 |
-
- Alternative contact
|
318 |
-
- Preferred contact method
|
319 |
-
|
320 |
-
BOOKING PROCESS:
|
321 |
-
1. Online Booking:
|
322 |
-
- NHS app instructions
|
323 |
-
- Practice website guidance
|
324 |
-
- System navigation help
|
325 |
-
|
326 |
-
2. Phone Booking:
|
327 |
-
- Best times to call
|
328 |
-
- Required information
|
329 |
-
- Queue management tips
|
330 |
-
|
331 |
-
3. Special Circumstances:
|
332 |
-
- Interpreter needs
|
333 |
-
- Accessibility requirements
|
334 |
-
- Transport arrangements""",
|
335 |
-
|
336 |
-
|
337 |
-
CULTURAL AWARENESS:
|
338 |
-
1. Religious Considerations:
|
339 |
-
- Prayer times
|
340 |
-
- Religious observations
|
341 |
-
- Dietary restrictions
|
342 |
-
- Gender preferences for care
|
343 |
-
- Religious festivals/fasting periods
|
344 |
-
|
345 |
-
2. Language Support:
|
346 |
-
- Interpreter services
|
347 |
-
- Multi-language resources
|
348 |
-
- Clear communication methods
|
349 |
-
- Family involvement preferences
|
350 |
-
|
351 |
-
3. Cultural Beliefs:
|
352 |
-
- Traditional medicine practices
|
353 |
-
- Cultural health beliefs
|
354 |
-
- Family decision-making
|
355 |
-
- Privacy customs
|
356 |
-
|
357 |
-
COMMUNICATION APPROACHES:
|
358 |
-
1. Respectful Interaction:
|
359 |
-
- Use preferred names/titles
|
360 |
-
- Appropriate greetings
|
361 |
-
- Non-judgmental responses
|
362 |
-
- Active listening
|
363 |
-
|
364 |
-
2. Language Usage:
|
365 |
-
- Clear, simple terms
|
366 |
-
- Avoid medical jargon
|
367 |
-
- Confirm understanding
|
368 |
-
- Respect silence/pauses
|
369 |
-
|
370 |
-
3. Non-verbal Communication:
|
371 |
-
- Eye contact customs
|
372 |
-
- Personal space
|
373 |
-
- Body language awareness
|
374 |
-
- Gesture sensitivity
|
375 |
-
|
376 |
-
SPECIFIC CONSIDERATIONS:
|
377 |
-
1. South Asian Communities:
|
378 |
-
- Family involvement
|
379 |
-
- Gender sensitivity
|
380 |
-
- Traditional medicine
|
381 |
-
- Language diversity
|
382 |
-
|
383 |
-
2. Middle Eastern Communities:
|
384 |
-
- Gender-specific care
|
385 |
-
- Religious observations
|
386 |
-
- Family hierarchies
|
387 |
-
- Privacy concerns
|
388 |
-
|
389 |
-
3. African/Caribbean Communities:
|
390 |
-
- Traditional healers
|
391 |
-
- Community involvement
|
392 |
-
- Historical medical mistrust
|
393 |
-
- Cultural specific conditions
|
394 |
-
|
395 |
-
4. Eastern European Communities:
|
396 |
-
- Direct communication
|
397 |
-
- Family involvement
|
398 |
-
- Medical documentation
|
399 |
-
- Language support
|
400 |
-
|
401 |
-
INCLUSIVE PRACTICES:
|
402 |
-
1. Appointment Scheduling:
|
403 |
-
- Religious holidays
|
404 |
-
- Prayer times
|
405 |
-
- Family availability
|
406 |
-
- Interpreter needs
|
407 |
-
|
408 |
-
2. Treatment Planning:
|
409 |
-
- Cultural preferences
|
410 |
-
- Traditional practices
|
411 |
-
- Family involvement
|
412 |
-
- Dietary requirements
|
413 |
-
|
414 |
-
3. Support Services:
|
415 |
-
- Community resources
|
416 |
-
- Cultural organizations
|
417 |
-
- Language services
|
418 |
-
- Social support""",
|
419 |
-
|
420 |
-
|
421 |
-
CLEAR BOUNDARIES:
|
422 |
-
1. Medical Advice:
|
423 |
-
- No diagnoses
|
424 |
-
- No prescriptions
|
425 |
-
- No treatment recommendations
|
426 |
-
- No medical procedures
|
427 |
-
- No second opinions
|
428 |
-
|
429 |
-
2. Emergency Services:
|
430 |
-
- Clear referral criteria
|
431 |
-
- Documented responses
|
432 |
-
- Follow-up protocols
|
433 |
-
- Handover procedures
|
434 |
-
|
435 |
-
3. Information Sharing:
|
436 |
-
- Confidentiality limits
|
437 |
-
- Data protection
|
438 |
-
- Record keeping
|
439 |
-
- Information governance
|
440 |
-
|
441 |
-
PROFESSIONAL CONDUCT:
|
442 |
-
1. Communication:
|
443 |
-
- Professional language
|
444 |
-
- Emotional boundaries
|
445 |
-
- Personal distance
|
446 |
-
- Service scope
|
447 |
-
|
448 |
-
2. Service Delivery:
|
449 |
-
- No financial transactions
|
450 |
-
- No personal relationships
|
451 |
-
- Clear role definition
|
452 |
-
- Professional limits"""
|
453 |
}
|
454 |
|
455 |
os.makedirs("knowledge_base", exist_ok=True)
|
@@ -474,10 +475,11 @@ PROFESSIONAL CONDUCT:
|
|
474 |
texts = text_splitter.split_text("\n\n".join(documents))
|
475 |
self.vector_store = FAISS.from_texts(texts, self.embeddings)
|
476 |
logger.info("RAG system setup complete")
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
|
|
481 |
|
482 |
def _validate_rag_setup(self):
|
483 |
"""Validate RAG system setup"""
|
|
|
176 |
raise
|
177 |
# Load your knowledge base content
|
178 |
def _load_knowledge_base(self):
|
179 |
+
"""Load and validate knowledge base content"""
|
180 |
+
try:
|
181 |
+
knowledge_base = { {
|
182 |
"triage_scenarios.txt": """Medical Triage Scenarios and Responses:
|
183 |
|
184 |
+
EMERGENCY (999) SCENARIOS:
|
185 |
+
1. Cardiovascular:
|
186 |
+
- Chest pain/pressure
|
187 |
+
- Heart attack symptoms
|
188 |
+
- Irregular heartbeat with dizziness
|
189 |
+
Response: Immediate 999 call, sit/lie down, chew aspirin if available
|
190 |
+
|
191 |
+
2. Respiratory:
|
192 |
+
- Severe breathing difficulty
|
193 |
+
- Choking
|
194 |
+
- Unable to speak full sentences
|
195 |
+
Response: 999, sitting position, clear airway
|
196 |
+
|
197 |
+
3. Neurological:
|
198 |
+
- Stroke symptoms (FAST)
|
199 |
+
- Seizures
|
200 |
+
- Unconsciousness
|
201 |
+
Response: 999, recovery position if unconscious
|
202 |
+
|
203 |
+
4. Trauma:
|
204 |
+
- Severe bleeding
|
205 |
+
- Head injuries with confusion
|
206 |
+
- Major burns
|
207 |
+
Response: 999, apply direct pressure to bleeding
|
208 |
+
|
209 |
+
URGENT CARE (111) SCENARIOS:
|
210 |
+
1. Moderate Symptoms:
|
211 |
+
- Persistent fever
|
212 |
+
- Non-severe infections
|
213 |
+
- Minor injuries
|
214 |
+
Response: 111 contact, monitor symptoms
|
215 |
+
|
216 |
+
2. Minor Emergencies:
|
217 |
+
- Small cuts needing stitches
|
218 |
+
- Sprains and strains
|
219 |
+
- Mild allergic reactions
|
220 |
+
Response: 111 or urgent care visit
|
221 |
+
|
222 |
+
GP APPOINTMENT SCENARIOS:
|
223 |
+
1. Routine Care:
|
224 |
+
- Chronic condition review
|
225 |
+
- Medication reviews
|
226 |
+
- Non-urgent symptoms
|
227 |
+
Response: Book routine GP appointment
|
228 |
+
|
229 |
+
2. Preventive Care:
|
230 |
+
- Vaccinations
|
231 |
+
- Health screenings
|
232 |
+
- Regular check-ups
|
233 |
+
Response: Schedule with GP reception""",
|
234 |
+
"emergency_detection.txt": """Enhanced Emergency Detection Criteria:
|
235 |
+
|
236 |
+
IMMEDIATE LIFE THREATS:
|
237 |
+
1. Cardiac Symptoms:
|
238 |
+
- Chest pain/pressure/tightness
|
239 |
+
- Pain spreading to arms/jaw/neck
|
240 |
+
- Sweating with nausea
|
241 |
+
- Shortness of breath
|
242 |
+
|
243 |
+
2. Breathing Problems:
|
244 |
+
- Severe shortness of breath
|
245 |
+
- Blue lips or face
|
246 |
+
- Unable to complete sentences
|
247 |
+
- Choking/airway blockage
|
248 |
+
|
249 |
+
3. Neurological:
|
250 |
+
- FAST (Face, Arms, Speech, Time)
|
251 |
+
- Sudden confusion
|
252 |
+
- Severe headache
|
253 |
+
- Seizures
|
254 |
+
- Loss of consciousness
|
255 |
+
|
256 |
+
4. Severe Trauma:
|
257 |
+
- Heavy bleeding
|
258 |
+
- Deep wounds
|
259 |
+
- Head injury with confusion
|
260 |
+
- Severe burns
|
261 |
+
- Broken bones with deformity
|
262 |
+
|
263 |
+
5. Anaphylaxis:
|
264 |
+
- Sudden swelling
|
265 |
+
- Difficulty breathing
|
266 |
+
- Rapid onset rash
|
267 |
+
- Light-headedness
|
268 |
+
|
269 |
+
URGENT BUT NOT IMMEDIATE:
|
270 |
+
1. Moderate Symptoms:
|
271 |
+
- Persistent fever
|
272 |
+
- Dehydration
|
273 |
+
- Non-severe infections
|
274 |
+
- Minor injuries
|
275 |
+
|
276 |
+
2. Worsening Conditions:
|
277 |
+
- Increasing pain
|
278 |
+
- Progressive symptoms
|
279 |
+
- Medication reactions
|
280 |
+
|
281 |
+
RESPONSE PROTOCOLS:
|
282 |
+
1. For Life Threats:
|
283 |
+
- Immediate 999 call
|
284 |
+
- Clear first aid instructions
|
285 |
+
- Stay on line until help arrives
|
286 |
+
|
287 |
+
2. For Urgent Care:
|
288 |
+
- 111 contact
|
289 |
+
- Monitor for worsening
|
290 |
+
- Document symptoms""",
|
291 |
+
"gp_booking.txt": """GP Appointment Booking Templates:
|
292 |
+
|
293 |
+
APPOINTMENT TYPES:
|
294 |
+
1. Routine Appointments:
|
295 |
+
Template: "I need to book a routine appointment for [condition]. My availability is [times/dates]. My GP is Dr. [name] if available."
|
296 |
+
|
297 |
+
2. Follow-up Appointments:
|
298 |
+
Template: "I need a follow-up appointment regarding [condition] discussed on [date]. My previous appointment was with Dr. [name]."
|
299 |
+
|
300 |
+
3. Medication Reviews:
|
301 |
+
Template: "I need a medication review for [medication]. My last review was [date]."
|
302 |
+
|
303 |
+
BOOKING INFORMATION NEEDED:
|
304 |
+
1. Patient Details:
|
305 |
+
- Full name
|
306 |
+
- Date of birth
|
307 |
+
- NHS number (if known)
|
308 |
+
- Registered GP practice
|
309 |
+
|
310 |
+
2. Appointment Details:
|
311 |
+
- Nature of appointment
|
312 |
+
- Preferred times/dates
|
313 |
+
- Urgency level
|
314 |
+
- Special requirements
|
315 |
+
|
316 |
+
3. Contact Information:
|
317 |
+
- Phone number
|
318 |
+
- Alternative contact
|
319 |
+
- Preferred contact method
|
320 |
+
|
321 |
+
BOOKING PROCESS:
|
322 |
+
1. Online Booking:
|
323 |
+
- NHS app instructions
|
324 |
+
- Practice website guidance
|
325 |
+
- System navigation help
|
326 |
+
|
327 |
+
2. Phone Booking:
|
328 |
+
- Best times to call
|
329 |
+
- Required information
|
330 |
+
- Queue management tips
|
331 |
+
|
332 |
+
3. Special Circumstances:
|
333 |
+
- Interpreter needs
|
334 |
+
- Accessibility requirements
|
335 |
+
- Transport arrangements""",
|
336 |
+
"cultural_sensitivity.txt": """Cultural Sensitivity Guidelines:
|
337 |
+
|
338 |
+
CULTURAL AWARENESS:
|
339 |
+
1. Religious Considerations:
|
340 |
+
- Prayer times
|
341 |
+
- Religious observations
|
342 |
+
- Dietary restrictions
|
343 |
+
- Gender preferences for care
|
344 |
+
- Religious festivals/fasting periods
|
345 |
+
|
346 |
+
2. Language Support:
|
347 |
+
- Interpreter services
|
348 |
+
- Multi-language resources
|
349 |
+
- Clear communication methods
|
350 |
+
- Family involvement preferences
|
351 |
+
|
352 |
+
3. Cultural Beliefs:
|
353 |
+
- Traditional medicine practices
|
354 |
+
- Cultural health beliefs
|
355 |
+
- Family decision-making
|
356 |
+
- Privacy customs
|
357 |
+
|
358 |
+
COMMUNICATION APPROACHES:
|
359 |
+
1. Respectful Interaction:
|
360 |
+
- Use preferred names/titles
|
361 |
+
- Appropriate greetings
|
362 |
+
- Non-judgmental responses
|
363 |
+
- Active listening
|
364 |
+
|
365 |
+
2. Language Usage:
|
366 |
+
- Clear, simple terms
|
367 |
+
- Avoid medical jargon
|
368 |
+
- Confirm understanding
|
369 |
+
- Respect silence/pauses
|
370 |
+
|
371 |
+
3. Non-verbal Communication:
|
372 |
+
- Eye contact customs
|
373 |
+
- Personal space
|
374 |
+
- Body language awareness
|
375 |
+
- Gesture sensitivity
|
376 |
+
|
377 |
+
SPECIFIC CONSIDERATIONS:
|
378 |
+
1. South Asian Communities:
|
379 |
+
- Family involvement
|
380 |
+
- Gender sensitivity
|
381 |
+
- Traditional medicine
|
382 |
+
- Language diversity
|
383 |
+
|
384 |
+
2. Middle Eastern Communities:
|
385 |
+
- Gender-specific care
|
386 |
+
- Religious observations
|
387 |
+
- Family hierarchies
|
388 |
+
- Privacy concerns
|
389 |
+
|
390 |
+
3. African/Caribbean Communities:
|
391 |
+
- Traditional healers
|
392 |
+
- Community involvement
|
393 |
+
- Historical medical mistrust
|
394 |
+
- Cultural specific conditions
|
395 |
+
|
396 |
+
4. Eastern European Communities:
|
397 |
+
- Direct communication
|
398 |
+
- Family involvement
|
399 |
+
- Medical documentation
|
400 |
+
- Language support
|
401 |
+
|
402 |
+
INCLUSIVE PRACTICES:
|
403 |
+
1. Appointment Scheduling:
|
404 |
+
- Religious holidays
|
405 |
+
- Prayer times
|
406 |
+
- Family availability
|
407 |
+
- Interpreter needs
|
408 |
+
|
409 |
+
2. Treatment Planning:
|
410 |
+
- Cultural preferences
|
411 |
+
- Traditional practices
|
412 |
+
- Family involvement
|
413 |
+
- Dietary requirements
|
414 |
+
|
415 |
+
3. Support Services:
|
416 |
+
- Community resources
|
417 |
+
- Cultural organizations
|
418 |
+
- Language services
|
419 |
+
- Social support""",
|
420 |
+
"service_boundaries.txt": """Service Limitations and Professional Boundaries:
|
421 |
+
|
422 |
+
CLEAR BOUNDARIES:
|
423 |
+
1. Medical Advice:
|
424 |
+
- No diagnoses
|
425 |
+
- No prescriptions
|
426 |
+
- No treatment recommendations
|
427 |
+
- No medical procedures
|
428 |
+
- No second opinions
|
429 |
+
|
430 |
+
2. Emergency Services:
|
431 |
+
- Clear referral criteria
|
432 |
+
- Documented responses
|
433 |
+
- Follow-up protocols
|
434 |
+
- Handover procedures
|
435 |
+
|
436 |
+
3. Information Sharing:
|
437 |
+
- Confidentiality limits
|
438 |
+
- Data protection
|
439 |
+
- Record keeping
|
440 |
+
- Information governance
|
441 |
+
|
442 |
+
PROFESSIONAL CONDUCT:
|
443 |
+
1. Communication:
|
444 |
+
- Professional language
|
445 |
+
- Emotional boundaries
|
446 |
+
- Personal distance
|
447 |
+
- Service scope
|
448 |
+
|
449 |
+
2. Service Delivery:
|
450 |
+
- No financial transactions
|
451 |
+
- No personal relationships
|
452 |
+
- Clear role definition
|
453 |
+
- Professional limits"""
|
454 |
}
|
455 |
|
456 |
os.makedirs("knowledge_base", exist_ok=True)
|
|
|
475 |
texts = text_splitter.split_text("\n\n".join(documents))
|
476 |
self.vector_store = FAISS.from_texts(texts, self.embeddings)
|
477 |
logger.info("RAG system setup complete")
|
478 |
+
|
479 |
+
return knowledge_base
|
480 |
+
except Exception as e:
|
481 |
+
logger.error(f"Error setting up RAG: {str(e)}")
|
482 |
+
raise
|
483 |
|
484 |
def _validate_rag_setup(self):
|
485 |
"""Validate RAG system setup"""
|