Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
from datetime import datetime, date
|
| 5 |
-
from uuid import uuid4
|
| 6 |
import logging
|
| 7 |
import pandas as pd
|
| 8 |
import os
|
|
@@ -145,11 +145,13 @@ class SalesforceClient:
|
|
| 145 |
logging.info(f"Created {object_name} record: {record_id}")
|
| 146 |
return record_id
|
| 147 |
except requests.exceptions.HTTPError as e:
|
| 148 |
-
|
| 149 |
-
|
|
|
|
| 150 |
except requests.exceptions.RequestException as e:
|
| 151 |
-
|
| 152 |
-
|
|
|
|
| 153 |
|
| 154 |
def query_records(self, query):
|
| 155 |
try:
|
|
@@ -243,14 +245,16 @@ def register_patient(phone, email, language, consent_status):
|
|
| 243 |
"Language__c": language,
|
| 244 |
"ConsentGiven__c": consent_status
|
| 245 |
}
|
| 246 |
-
|
| 247 |
-
if
|
| 248 |
-
|
| 249 |
-
|
|
|
|
|
|
|
| 250 |
return "Failed to register patient. Check Salesforce logs."
|
| 251 |
except Exception as e:
|
| 252 |
logging.error(f"Error registering patient: {str(e)}")
|
| 253 |
-
return f"Error: {str(e)}"
|
| 254 |
|
| 255 |
def submit_consent(patient_phone, consent_method):
|
| 256 |
if not patient_phone or not consent_method:
|
|
@@ -259,8 +263,10 @@ def submit_consent(patient_phone, consent_method):
|
|
| 259 |
patients = sf_client.query_records(f"SELECT Id, Name FROM Patient__c WHERE Phone__c = '{patient_phone}'")
|
| 260 |
if patients:
|
| 261 |
consent_data = {"Method__c": consent_method, "GivenOn__c": date.today().isoformat()}
|
| 262 |
-
|
| 263 |
-
if
|
|
|
|
|
|
|
| 264 |
logging.info(f"Consent recorded for patient: {patients[0]['Name']}")
|
| 265 |
return "Consent recorded successfully!"
|
| 266 |
return "Failed to record consent."
|
|
@@ -283,8 +289,10 @@ def schedule_followup(patient_phone, message_template, follow_up_date):
|
|
| 283 |
"Patient__c": patients[0]["Id"], "FollowUpDate__c": follow_up_date.isoformat(),
|
| 284 |
"MessageTemplate__c": message_template, "Status__c": "Scheduled"
|
| 285 |
}
|
| 286 |
-
|
| 287 |
-
if
|
|
|
|
|
|
|
| 288 |
return "Failed to schedule follow-up."
|
| 289 |
send_result = twilio_client.send_message(patient_phone, message_template)
|
| 290 |
if send_result is True:
|
|
@@ -328,8 +336,10 @@ def escalate_case(patient_id, response_text):
|
|
| 328 |
"RelatedPatient__c": patient_id, "Priority__c": "High",
|
| 329 |
"Description__c": f"High-risk response: {response_text}"
|
| 330 |
}
|
| 331 |
-
|
| 332 |
-
if
|
|
|
|
|
|
|
| 333 |
logging.info(f"Case created for patient: {patient_id}")
|
| 334 |
return "Case created successfully!"
|
| 335 |
return "Failed to create case."
|
|
@@ -347,8 +357,10 @@ def schedule_appointment(patient_email, event_type_slug="30min"):
|
|
| 347 |
event_time = calendly_client.create_event(patient_email, event_type_uuid)
|
| 348 |
if event_time:
|
| 349 |
appointment_data = {"DateTime__c": event_time, "Status__c": "Scheduled"}
|
| 350 |
-
|
| 351 |
-
if
|
|
|
|
|
|
|
| 352 |
logging.info(f"Appointment scheduled for {patient_email}")
|
| 353 |
return f"Appointment scheduled for {event_time}!"
|
| 354 |
return "Failed to create appointment record."
|
|
@@ -365,8 +377,10 @@ def submit_survey(patient_phone, question, answer):
|
|
| 365 |
if not patients:
|
| 366 |
return "Patient not found."
|
| 367 |
survey_data = {"Question__c": question, "Answer__c": answer}
|
| 368 |
-
|
| 369 |
-
if
|
|
|
|
|
|
|
| 370 |
return "Failed to submit survey."
|
| 371 |
analysis = hf_client.analyze_response(answer, patients[0]["Language__c"])
|
| 372 |
if analysis:
|
|
@@ -375,8 +389,10 @@ def submit_survey(patient_phone, question, answer):
|
|
| 375 |
"RiskScore__c": analysis["risk_level"], "Severity__c": analysis["severity_score"],
|
| 376 |
"Sentiment__c": analysis["sentiment_score"]
|
| 377 |
}
|
| 378 |
-
|
| 379 |
-
if
|
|
|
|
|
|
|
| 380 |
case_data = {
|
| 381 |
"RelatedPatient__c": patients[0]["Id"], "Priority__c": "High",
|
| 382 |
"Description__c": f"High-risk survey response: {answer}"
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
from datetime import datetime, date
|
| 5 |
+
from uuid import uuid4
|
| 6 |
import logging
|
| 7 |
import pandas as pd
|
| 8 |
import os
|
|
|
|
| 145 |
logging.info(f"Created {object_name} record: {record_id}")
|
| 146 |
return record_id
|
| 147 |
except requests.exceptions.HTTPError as e:
|
| 148 |
+
error_message = f"HTTP error creating {object_name} record: {e.response.status_code} - {e.response.text}"
|
| 149 |
+
logging.error(error_message)
|
| 150 |
+
return error_message
|
| 151 |
except requests.exceptions.RequestException as e:
|
| 152 |
+
error_message = f"Failed to create {object_name} record: {str(e)}"
|
| 153 |
+
logging.error(error_message)
|
| 154 |
+
return error_message
|
| 155 |
|
| 156 |
def query_records(self, query):
|
| 157 |
try:
|
|
|
|
| 245 |
"Language__c": language,
|
| 246 |
"ConsentGiven__c": consent_status
|
| 247 |
}
|
| 248 |
+
result = sf_client.create_record("Patient__c", patient_data)
|
| 249 |
+
if isinstance(result, str): # Error message returned
|
| 250 |
+
return result
|
| 251 |
+
if result:
|
| 252 |
+
logging.info(f"Registered patient: {result}")
|
| 253 |
+
return f"Patient registered successfully! ID: {result}"
|
| 254 |
return "Failed to register patient. Check Salesforce logs."
|
| 255 |
except Exception as e:
|
| 256 |
logging.error(f"Error registering patient: {str(e)}")
|
| 257 |
+
return f"Error registering patient: {str(e)}"
|
| 258 |
|
| 259 |
def submit_consent(patient_phone, consent_method):
|
| 260 |
if not patient_phone or not consent_method:
|
|
|
|
| 263 |
patients = sf_client.query_records(f"SELECT Id, Name FROM Patient__c WHERE Phone__c = '{patient_phone}'")
|
| 264 |
if patients:
|
| 265 |
consent_data = {"Method__c": consent_method, "GivenOn__c": date.today().isoformat()}
|
| 266 |
+
result = sf_client.create_record("Consent__c", consent_data)
|
| 267 |
+
if isinstance(result, str): # Error message returned
|
| 268 |
+
return result
|
| 269 |
+
if result:
|
| 270 |
logging.info(f"Consent recorded for patient: {patients[0]['Name']}")
|
| 271 |
return "Consent recorded successfully!"
|
| 272 |
return "Failed to record consent."
|
|
|
|
| 289 |
"Patient__c": patients[0]["Id"], "FollowUpDate__c": follow_up_date.isoformat(),
|
| 290 |
"MessageTemplate__c": message_template, "Status__c": "Scheduled"
|
| 291 |
}
|
| 292 |
+
result = sf_client.create_record("FollowUpPlan__c", followup_data)
|
| 293 |
+
if isinstance(result, str): # Error message returned
|
| 294 |
+
return result
|
| 295 |
+
if not result:
|
| 296 |
return "Failed to schedule follow-up."
|
| 297 |
send_result = twilio_client.send_message(patient_phone, message_template)
|
| 298 |
if send_result is True:
|
|
|
|
| 336 |
"RelatedPatient__c": patient_id, "Priority__c": "High",
|
| 337 |
"Description__c": f"High-risk response: {response_text}"
|
| 338 |
}
|
| 339 |
+
result = sf_client.create_record("Case__c", case_data)
|
| 340 |
+
if isinstance(result, str): # Error message returned
|
| 341 |
+
return result
|
| 342 |
+
if result:
|
| 343 |
logging.info(f"Case created for patient: {patient_id}")
|
| 344 |
return "Case created successfully!"
|
| 345 |
return "Failed to create case."
|
|
|
|
| 357 |
event_time = calendly_client.create_event(patient_email, event_type_uuid)
|
| 358 |
if event_time:
|
| 359 |
appointment_data = {"DateTime__c": event_time, "Status__c": "Scheduled"}
|
| 360 |
+
result = sf_client.create_record("Appointment__c", appointment_data)
|
| 361 |
+
if isinstance(result, str): # Error message returned
|
| 362 |
+
return result
|
| 363 |
+
if result:
|
| 364 |
logging.info(f"Appointment scheduled for {patient_email}")
|
| 365 |
return f"Appointment scheduled for {event_time}!"
|
| 366 |
return "Failed to create appointment record."
|
|
|
|
| 377 |
if not patients:
|
| 378 |
return "Patient not found."
|
| 379 |
survey_data = {"Question__c": question, "Answer__c": answer}
|
| 380 |
+
result = sf_client.create_record("Survey__c", survey_data)
|
| 381 |
+
if isinstance(result, str): # Error message returned
|
| 382 |
+
return result
|
| 383 |
+
if not result:
|
| 384 |
return "Failed to submit survey."
|
| 385 |
analysis = hf_client.analyze_response(answer, patients[0]["Language__c"])
|
| 386 |
if analysis:
|
|
|
|
| 389 |
"RiskScore__c": analysis["risk_level"], "Severity__c": analysis["severity_score"],
|
| 390 |
"Sentiment__c": analysis["sentiment_score"]
|
| 391 |
}
|
| 392 |
+
result = sf_client.create_record("SymptomLog__c", symptom_data)
|
| 393 |
+
if isinstance(result, str): # Error message returned
|
| 394 |
+
return result
|
| 395 |
+
if result and analysis["risk_level"] == "High":
|
| 396 |
case_data = {
|
| 397 |
"RelatedPatient__c": patients[0]["Id"], "Priority__c": "High",
|
| 398 |
"Description__c": f"High-risk survey response: {answer}"
|