Ali2206 commited on
Commit
cb8e838
·
verified ·
1 Parent(s): 9d6bcd0

Update analysis.py

Browse files
Files changed (1) hide show
  1. analysis.py +22 -34
analysis.py CHANGED
@@ -25,48 +25,36 @@ class NotificationStatus(str, Enum):
25
  ARCHIVED = "archived"
26
 
27
  async def create_alert(patient_id: str, risk_data: dict):
28
- """Create a new risk alert with notification metadata"""
29
- alert_doc = {
30
- "patient_id": patient_id,
31
- "type": "suicide_risk",
32
- "level": risk_data["level"],
33
- "score": risk_data["score"],
34
- "factors": risk_data["factors"],
35
- "timestamp": datetime.utcnow(),
36
- "acknowledged": False,
37
- "notification": {
38
- "type": NotificationType.RISK_ALERT,
39
- "status": NotificationStatus.UNREAD,
40
- "title": f"Suicide Risk: {risk_data['level'].capitalize()}",
41
- "message": f"Patient {patient_id} shows {risk_data['level']} risk factors",
42
- "icon": "⚠️",
43
- "action_url": f"/patient/{patient_id}/risk-assessment",
44
- "priority": "high" if risk_data["level"] in ["high", "severe"] else "medium"
45
- }
46
- }
47
-
48
  try:
49
- # Insert into database
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  await alerts_collection.insert_one(alert_doc)
51
 
52
- # For Hugging Face Spaces, use their event system instead of raw WebSocket
53
- if os.getenv('SPACES') == '1':
54
- from gradio import EventSource
55
- EventSource.emit(
56
- event="notification",
57
- data=json.dumps(alert_doc["notification"]),
58
- broadcast=True
59
- )
60
- else:
61
- # Local development with regular WebSocket
62
- await broadcast_notification(alert_doc["notification"])
63
-
64
  logger.warning(f"⚠️ Created suicide risk alert for patient {patient_id}")
65
  return alert_doc
66
  except Exception as e:
67
  logger.error(f"Failed to create alert: {str(e)}")
68
  raise
69
-
70
  async def analyze_patient_report(
71
  patient_id: Optional[str],
72
  report_content: str,
 
25
  ARCHIVED = "archived"
26
 
27
  async def create_alert(patient_id: str, risk_data: dict):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  try:
29
+ alert_doc = {
30
+ "patient_id": patient_id,
31
+ "type": "suicide_risk",
32
+ "level": risk_data["level"],
33
+ "score": risk_data["score"],
34
+ "factors": risk_data["factors"],
35
+ "timestamp": datetime.utcnow(),
36
+ "acknowledged": False,
37
+ "notification": {
38
+ "type": "risk_alert",
39
+ "status": "unread",
40
+ "title": f"Suicide Risk: {risk_data['level'].capitalize()}",
41
+ "message": f"Patient {patient_id} shows {risk_data['level']} risk factors",
42
+ "icon": "⚠️",
43
+ "action_url": f"/patient/{patient_id}/risk-assessment",
44
+ "priority": "high" if risk_data["level"] in ["high", "severe"] else "medium"
45
+ }
46
+ }
47
+
48
  await alerts_collection.insert_one(alert_doc)
49
 
50
+ # Simplified WebSocket notification - remove Hugging Face specific code
51
+ await broadcast_notification(alert_doc["notification"])
52
+
 
 
 
 
 
 
 
 
 
53
  logger.warning(f"⚠️ Created suicide risk alert for patient {patient_id}")
54
  return alert_doc
55
  except Exception as e:
56
  logger.error(f"Failed to create alert: {str(e)}")
57
  raise
 
58
  async def analyze_patient_report(
59
  patient_id: Optional[str],
60
  report_content: str,