Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -240,13 +240,35 @@ calendly_client = CalendlyClient(CALENDLY_API_TOKEN)
|
|
| 240 |
# State to store patient phone number
|
| 241 |
patient_phone_state = gr.State(value=None)
|
| 242 |
|
| 243 |
-
def register_patient(
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
return "All fields are required.", gr.State(value=None)
|
| 246 |
try:
|
| 247 |
patient_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
"Phone__c": phone,
|
| 249 |
"Email__c": email,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
"Language__c": language,
|
| 251 |
"ConsentGiven__c": consent_status
|
| 252 |
}
|
|
@@ -445,110 +467,64 @@ def escalate_case(patient_id, response_text):
|
|
| 445 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI-Powered Patient Follow-up Agent") as demo:
|
| 446 |
gr.Markdown("# AI-Powered Patient Follow-up Agent for Clinics")
|
| 447 |
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
try:
|
| 507 |
-
patient_data = {
|
| 508 |
-
"First_Name__c": name_first,
|
| 509 |
-
"Last_Name__c": name_last,
|
| 510 |
-
"Date_of_Birth__c": dob,
|
| 511 |
-
"Sex__c": sex,
|
| 512 |
-
"Height__c": height,
|
| 513 |
-
"Weight__c": weight,
|
| 514 |
-
"Marital_Status__c": marital,
|
| 515 |
-
"Phone__c": phone,
|
| 516 |
-
"Email__c": email,
|
| 517 |
-
"Address_Street1__c": address_street1,
|
| 518 |
-
"Address_Street2__c": address_street2 or "",
|
| 519 |
-
"City__c": address_city,
|
| 520 |
-
"State__c": address_state,
|
| 521 |
-
"Postal_Code__c": postal,
|
| 522 |
-
"Medications__c": med_list if med_yes_no == "Yes" else "",
|
| 523 |
-
"Emergency_First_Name__c": emergency_first or "",
|
| 524 |
-
"Emergency_Last_Name__c": emergency_last or "",
|
| 525 |
-
"Emergency_Relation__c": emergency_relation or "",
|
| 526 |
-
"Emergency_Number__c": emergency_number or "",
|
| 527 |
-
"Language__c": language,
|
| 528 |
-
"ConsentGiven__c": consent_status
|
| 529 |
-
}
|
| 530 |
-
result = sf_client.create_record("Patient__c", patient_data)
|
| 531 |
-
if isinstance(result, str): # Error message returned
|
| 532 |
-
return result, gr.State(value=None)
|
| 533 |
-
if result:
|
| 534 |
-
logging.info(f"Registered patient: {result}")
|
| 535 |
-
message = f"Welcome! You are registered. Your patient ID is {result}."
|
| 536 |
-
if consent_status in ["Approved", "Awaiting"]:
|
| 537 |
-
twilio_client.send_message(phone, message, method=consent_status.lower().replace(" ", "")[:3] if consent_status == "Awaiting" else "sms")
|
| 538 |
-
return f"Patient registered successfully! Your patient ID is {result}. You can use this ID for future actions.", gr.State(value=phone)
|
| 539 |
-
return "Failed to register patient. Please try again or contact support.", gr.State(value=None)
|
| 540 |
-
except Exception as e:
|
| 541 |
-
logging.error(f"Error registering patient: {str(e)}")
|
| 542 |
-
return f"Error registering patient: {str(e)}. Please try again or contact support.", gr.State(value=None)
|
| 543 |
-
|
| 544 |
-
register_button.click(
|
| 545 |
-
fn=register_patient,
|
| 546 |
-
inputs=[name_first, name_last, dob_input, sex_input, height_input, weight_input, marital_input,
|
| 547 |
-
phone_input, email_input, address_street1, address_street2, address_city, address_state,
|
| 548 |
-
postal_input, med_yes_no, med_list, emergency_first, emergency_last, emergency_relation,
|
| 549 |
-
emergency_number, language_input, consent_input],
|
| 550 |
-
outputs=[register_output, patient_phone_state]
|
| 551 |
-
)
|
| 552 |
|
| 553 |
with gr.Tab("Consent Form"):
|
| 554 |
consent_phone_input = gr.Textbox(label="Patient Phone Number", value=lambda x: x, interactive=False) if patient_phone_state.value else gr.Textbox(label="Patient Phone Number", placeholder="+1234567890")
|
|
|
|
| 240 |
# State to store patient phone number
|
| 241 |
patient_phone_state = gr.State(value=None)
|
| 242 |
|
| 243 |
+
def register_patient(name_first, name_last, dob, sex, height, weight, marital, phone, email,
|
| 244 |
+
address_street1, address_street2, address_city, address_state, postal,
|
| 245 |
+
med_yes_no, med_list, emergency_first, emergency_last, emergency_relation,
|
| 246 |
+
emergency_number, language, consent_status):
|
| 247 |
+
if not all([name_first, name_last, dob, sex, height, weight, marital, phone, email,
|
| 248 |
+
address_street1, address_city, address_state, postal, language, consent_status]) or \
|
| 249 |
+
(med_yes_no == "Yes" and not med_list):
|
| 250 |
return "All fields are required.", gr.State(value=None)
|
| 251 |
try:
|
| 252 |
patient_data = {
|
| 253 |
+
"First_Name__c": name_first,
|
| 254 |
+
"Last_Name__c": name_last,
|
| 255 |
+
"Date_of_Birth__c": dob,
|
| 256 |
+
"Sex__c": sex,
|
| 257 |
+
"Height__c": height,
|
| 258 |
+
"Weight__c": weight,
|
| 259 |
+
"Marital_Status__c": marital,
|
| 260 |
"Phone__c": phone,
|
| 261 |
"Email__c": email,
|
| 262 |
+
"Address_Street1__c": address_street1,
|
| 263 |
+
"Address_Street2__c": address_street2 or "",
|
| 264 |
+
"City__c": address_city,
|
| 265 |
+
"State__c": address_state,
|
| 266 |
+
"Postal_Code__c": postal,
|
| 267 |
+
"Medications__c": med_list if med_yes_no == "Yes" else "",
|
| 268 |
+
"Emergency_First_Name__c": emergency_first or "",
|
| 269 |
+
"Emergency_Last_Name__c": emergency_last or "",
|
| 270 |
+
"Emergency_Relation__c": emergency_relation or "",
|
| 271 |
+
"Emergency_Number__c": emergency_number or "",
|
| 272 |
"Language__c": language,
|
| 273 |
"ConsentGiven__c": consent_status
|
| 274 |
}
|
|
|
|
| 467 |
with gr.Blocks(theme=gr.themes.Soft(), title="AI-Powered Patient Follow-up Agent") as demo:
|
| 468 |
gr.Markdown("# AI-Powered Patient Follow-up Agent for Clinics")
|
| 469 |
|
| 470 |
+
with gr.Tabs():
|
| 471 |
+
with gr.Tab("Patient Registration"):
|
| 472 |
+
with gr.Row():
|
| 473 |
+
with gr.Column():
|
| 474 |
+
name_first = gr.Textbox(label="First Name", placeholder="First Name")
|
| 475 |
+
name_last = gr.Textbox(label="Last Name", placeholder="Last Name")
|
| 476 |
+
with gr.Column():
|
| 477 |
+
dob_input = gr.Textbox(label="Date of Birth", placeholder="MM-DD-YYYY")
|
| 478 |
+
sex_input = gr.Dropdown(label="Sex", choices=["Please Select", "Male", "Female", "Other"], value="Please Select")
|
| 479 |
+
with gr.Row():
|
| 480 |
+
height_input = gr.Textbox(label="Height (inches)", placeholder="e.g., 65")
|
| 481 |
+
weight_input = gr.Textbox(label="Weight (pounds)", placeholder="e.g., 150")
|
| 482 |
+
marital_input = gr.Dropdown(label="Marital Status", choices=["Please Select", "Single", "Married", "Divorced", "Widowed"], value="Please Select")
|
| 483 |
+
with gr.Row():
|
| 484 |
+
phone_input = gr.Textbox(label="Contact Number", placeholder="(000) 000-0000")
|
| 485 |
+
email_input = gr.Textbox(label="Email", placeholder="patient@example.com")
|
| 486 |
+
with gr.Row():
|
| 487 |
+
with gr.Column():
|
| 488 |
+
address_street1 = gr.Textbox(label="Street Address", placeholder="Street Address")
|
| 489 |
+
address_street2 = gr.Textbox(label="Street Address Line 2", placeholder="Apt, Suite, etc.")
|
| 490 |
+
with gr.Column():
|
| 491 |
+
address_city = gr.Textbox(label="City", placeholder="City")
|
| 492 |
+
address_state = gr.Textbox(label="State/Province", placeholder="State/Province")
|
| 493 |
+
postal_input = gr.Textbox(label="Postal/Zip Code", placeholder="Postal/Zip Code")
|
| 494 |
+
with gr.Row():
|
| 495 |
+
med_yes_no = gr.Radio(label="Taking any medications, currently?", choices=["Yes", "No"], value="No")
|
| 496 |
+
med_list = gr.Textbox(label="Please list it here", placeholder="List medications here", lines=2, visible=False)
|
| 497 |
+
with gr.Accordion("In case of emergency", open=False):
|
| 498 |
+
with gr.Row():
|
| 499 |
+
with gr.Column():
|
| 500 |
+
emergency_first = gr.Textbox(label="First Name", placeholder="First Name")
|
| 501 |
+
emergency_last = gr.Textbox(label="Last Name", placeholder="Last Name")
|
| 502 |
+
with gr.Column():
|
| 503 |
+
emergency_relation = gr.Textbox(label="Relationship", placeholder="e.g., Spouse")
|
| 504 |
+
emergency_number = gr.Textbox(label="Contact Number", placeholder="(000) 000-0000")
|
| 505 |
+
language_input = gr.Dropdown(["English", "Telugu", "Hindi"], label="Language")
|
| 506 |
+
consent_input = gr.Dropdown(["SMS", "WhatsApp", "Awaiting"], label="Consent Method")
|
| 507 |
+
register_button = gr.Button("Submit")
|
| 508 |
+
register_output = gr.Textbox(label="Result")
|
| 509 |
+
|
| 510 |
+
# Update visibility of medication list based on radio selection
|
| 511 |
+
def toggle_med_list(med_choice):
|
| 512 |
+
return gr.update(visible=med_choice == "Yes")
|
| 513 |
+
|
| 514 |
+
med_yes_no.change(
|
| 515 |
+
fn=toggle_med_list,
|
| 516 |
+
inputs=med_yes_no,
|
| 517 |
+
outputs=med_list
|
| 518 |
+
)
|
| 519 |
+
|
| 520 |
+
register_button.click(
|
| 521 |
+
fn=register_patient,
|
| 522 |
+
inputs=[name_first, name_last, dob_input, sex_input, height_input, weight_input, marital_input,
|
| 523 |
+
phone_input, email_input, address_street1, address_street2, address_city, address_state,
|
| 524 |
+
postal_input, med_yes_no, med_list, emergency_first, emergency_last, emergency_relation,
|
| 525 |
+
emergency_number, language_input, consent_input],
|
| 526 |
+
outputs=[register_output, patient_phone_state]
|
| 527 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 528 |
|
| 529 |
with gr.Tab("Consent Form"):
|
| 530 |
consent_phone_input = gr.Textbox(label="Patient Phone Number", value=lambda x: x, interactive=False) if patient_phone_state.value else gr.Textbox(label="Patient Phone Number", placeholder="+1234567890")
|