Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -434,17 +434,30 @@ MEDICAL_KB = {
|
|
| 434 |
}
|
| 435 |
|
| 436 |
# ββ #20: A/B Testing Variants ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 437 |
-
|
| 438 |
-
'You are a clinical symptom extractor for a
|
| 439 |
-
'The patient
|
| 440 |
'Rule-based extraction already found some symptoms.\n\n'
|
| 441 |
-
|
| 442 |
-
'
|
| 443 |
-
'
|
| 444 |
-
'
|
| 445 |
-
'
|
| 446 |
-
'
|
| 447 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
)
|
| 449 |
|
| 450 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -462,7 +475,8 @@ PHRASE_MAP = {
|
|
| 462 |
r"loss of sensation|numbness|numb": "loss_of_sensation",
|
| 463 |
r"visual aura|aura|spots.*vision|vision disturbance|blurred vision|dhundla": "spots_or_clouds_in_vision",
|
| 464 |
r"sensitivity.*light|light.*sensitiv|photophobia|light.*hurts|bright.*light|sensitive.*light": "photophobia",
|
| 465 |
-
r"
|
|
|
|
| 466 |
r"upper.*stomach|upper.*abdom|upper.*belly": "upper_abdominal_pain",
|
| 467 |
r"sharp.*stomach|sharp.*abdom|sharp.*belly": "sharp_abdominal_pain",
|
| 468 |
r"burning.*stomach|burning.*abdom|acid.*stomach|jalan.*pet": "burning_abdominal_pain",
|
|
@@ -536,6 +550,90 @@ PHRASE_MAP = {
|
|
| 536 |
r"hair loss|hair fall|losing hair|hair.*falling|baal jhad|baal girna|baal toot": "irregular_appearing_scalp",
|
| 537 |
}
|
| 538 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 540 |
# 7. PYDANTIC MODELS
|
| 541 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -745,31 +843,6 @@ async def _groq_text(system: str, user: str, max_tokens: int = 500, temperature:
|
|
| 745 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 746 |
# 10. SYSTEM PROMPTS
|
| 747 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 748 |
-
EXTRACT_SYSTEM = (
|
| 749 |
-
'You are a clinical symptom extractor for a women\'s menstrual health app.\n'
|
| 750 |
-
'The patient may write in Hindi, English, or Hinglish.\n'
|
| 751 |
-
'Rule-based extraction already found some symptoms.\n\n'
|
| 752 |
-
'Your job:\n'
|
| 753 |
-
'1. Add SYMPTOMS ONLY that are EXPLICITLY mentioned or very strongly implied by the patient.\n'
|
| 754 |
-
' NEVER infer symptoms the patient did not describe β do not add "likely" symptoms.\n'
|
| 755 |
-
' - ONLY ask about cycle/periods if symptoms EXPLICITLY include: unpredictable_menstruation, painful_menstruation, heavy_menstrual_flow, pelvic_pain, vaginal_discharge, infertility, or intermenstrual_bleeding. NEVER ask cycle questions for headache, fever, nausea, vomiting, or stomach pain.\n'
|
| 756 |
-
' NEVER add disease names (not "PCOS", "endometriosis", "migraine" etc.)\n'
|
| 757 |
-
'2. Ask UP TO 2 questions. IMPORTANT RULES for questions:\n'
|
| 758 |
-
'CRITICAL: When asking about a symptom the user already mentioned, you MUST reference it as "the [symptom] you mentioned" or "this [symptom]". NEVER ask "Do you have [symptom]?" if they just told you they have it.',
|
| 759 |
-
' - ONLY ask about cycle/periods if symptoms EXPLICITLY include: unpredictable_menstruation, painful_menstruation, heavy_menstrual_flow, pelvic_pain, vaginal_discharge, infertility, intermenstrual_bleeding, or hirsutism. NEVER ask cycle questions for general symptoms like headache, fever, nausea, vomiting, or stomach pain.\n'
|
| 760 |
-
' - For purely general symptoms with NO menstrual symptoms, do NOT ask cycle questions and set reproductive_context to null.\n'
|
| 761 |
-
' - Second question (optional) covers the most likely missing symptom cluster.\n'
|
| 762 |
-
' - Always add "(you can say I don\'t know or skip)" to each question.\n'
|
| 763 |
-
' - NEVER ask about both cycle length and last period in separate questions β combine them.\n'
|
| 764 |
-
' - If cycle_data_already_provided is true, do NOT ask any cycle questions.\n'
|
| 765 |
-
' - Do NOT ask about symptoms already in the found list.\n'
|
| 766 |
-
' - Always ask questions in English only.\n'
|
| 767 |
-
' - Include an "acknowledgment" field β a brief, warm sentence that first acknowledges the symptoms the patient has already mentioned. Example: "I understand you\'re experiencing pelvic pain and hair thinning." This makes the conversation feel natural and heard.\n\n'
|
| 768 |
-
'Respond ONLY with compact JSON on one line, no markdown:\n'
|
| 769 |
-
'{"additional_symptoms":["pelvic_pain","hair_thinning"],"acknowledgment":"I understand you\'re experiencing pelvic pain and hair thinning.","reproductive_context":"menstrual",'
|
| 770 |
-
'"questions":[{"id":"q1","text":"How long is your usual cycle and when did your last period start? (you can say I don\'t know or skip)"}]}'
|
| 771 |
-
)
|
| 772 |
-
|
| 773 |
SYNTHESIZE_SYSTEM = (
|
| 774 |
'You are a clinical routing engine for a women\'s menstrual health app.\n'
|
| 775 |
'Given patient message, extracted symptoms, and follow-up answers:\n\n'
|
|
@@ -862,6 +935,229 @@ RAG_SYSTEM = (
|
|
| 862 |
'Rules: Plain language, 3-4 sentences max. No markdown.'
|
| 863 |
)
|
| 864 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 865 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 866 |
# 11. LLM CALLS
|
| 867 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -1100,6 +1396,139 @@ def _check_daily_limit(identifier: str) -> bool:
|
|
| 1100 |
_daily_usage[key] = count + 1
|
| 1101 |
return True
|
| 1102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1103 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1104 |
# 14. MAIN CHAT ENDPOINT
|
| 1105 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 434 |
}
|
| 435 |
|
| 436 |
# ββ #20: A/B Testing Variants ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 437 |
+
EXTRACT_SYSTEM = (
|
| 438 |
+
'You are a clinical symptom extractor for a health app.\n'
|
| 439 |
+
'The patient writes in English.\n'
|
| 440 |
'Rule-based extraction already found some symptoms.\n\n'
|
| 441 |
+
|
| 442 |
+
'βββ STRICT RULES βββ\n'
|
| 443 |
+
'1. You MUST respond in English only. No Hindi, no Hinglish.\n'
|
| 444 |
+
'2. NEVER ask about any symptom already in found_symptoms.\n'
|
| 445 |
+
'3. Every question MUST start with "Do you also have" or "Are you also experiencing".\n'
|
| 446 |
+
'4. NEVER ask about periods/cycles unless found_symptoms contains: unpredictable_menstruation, painful_menstruation, heavy_menstrual_flow, pelvic_pain, vaginal_discharge, infertility, intermenstrual_bleeding, hirsutism.\n'
|
| 447 |
+
'5. For stomach pain, nausea, fever, headache, cough, fatigue β do NOT ask about periods.\n'
|
| 448 |
+
'6. NEVER include disease names in additional_symptoms.\n'
|
| 449 |
+
'7. NEVER infer symptoms not explicitly mentioned.\n\n'
|
| 450 |
+
|
| 451 |
+
'βββ OUTPUT FORMAT βββ\n'
|
| 452 |
+
'Return ONLY this JSON on one line:\n'
|
| 453 |
+
'{"acknowledgment":"I understand you are experiencing [list all symptoms from found_symptoms in plain English].","additional_symptoms":[],"reproductive_context":null,"questions":[{"id":"q1","text":"Do you also have [new symptom]? (you can say I dont know or skip)"}]}\n\n'
|
| 454 |
+
|
| 455 |
+
'βββ EXAMPLES βββ\n'
|
| 456 |
+
'Input: found_symptoms=["upper_abdominal_pain","nausea","fever"]\n'
|
| 457 |
+
'Output: {"acknowledgment":"I understand you are experiencing stomach pain, nausea, and fever.","additional_symptoms":[],"reproductive_context":null,"questions":[{"id":"q1","text":"Do you also have vomiting or loose motions? (you can say I dont know or skip)"}]}\n\n'
|
| 458 |
+
|
| 459 |
+
'Input: found_symptoms=["unpredictable_menstruation","acne_or_pimples","weight_gain"]\n'
|
| 460 |
+
'Output: {"acknowledgment":"I understand you are experiencing irregular periods, acne, and weight gain.","additional_symptoms":[],"reproductive_context":"menstrual","questions":[{"id":"q1","text":"Do you also have facial hair growth or hair thinning? (you can say I dont know or skip)"},{"id":"q2","text":"What is your usual cycle length and when did your last period start? (you can say I dont know or skip)"}]}'
|
| 461 |
)
|
| 462 |
|
| 463 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 475 |
r"loss of sensation|numbness|numb": "loss_of_sensation",
|
| 476 |
r"visual aura|aura|spots.*vision|vision disturbance|blurred vision|dhundla": "spots_or_clouds_in_vision",
|
| 477 |
r"sensitivity.*light|light.*sensitiv|photophobia|light.*hurts|bright.*light|sensitive.*light": "photophobia",
|
| 478 |
+
r"stomach.*killing|killing.*stomach|stomach.*hurt|stomachache|stomach.*pain|stomach.*ache|tummy.*killing|killing.*tummy|tummy.*hurt|tummy.*ache|tummy.*pain|pait|paet|pet.*dard|pet mein|belly.*pain|belly.*hurt|abdominal pain": "upper_abdominal_pain",
|
| 479 |
+
r"lower belly|lower tummy|lower abdomen": "lower_abdominal_pain",
|
| 480 |
r"upper.*stomach|upper.*abdom|upper.*belly": "upper_abdominal_pain",
|
| 481 |
r"sharp.*stomach|sharp.*abdom|sharp.*belly": "sharp_abdominal_pain",
|
| 482 |
r"burning.*stomach|burning.*abdom|acid.*stomach|jalan.*pet": "burning_abdominal_pain",
|
|
|
|
| 550 |
r"hair loss|hair fall|losing hair|hair.*falling|baal jhad|baal girna|baal toot": "irregular_appearing_scalp",
|
| 551 |
}
|
| 552 |
|
| 553 |
+
SYMPTOM_TO_ENGLISH = {
|
| 554 |
+
"headache": "headache",
|
| 555 |
+
"frontal_headache": "headache in the front of your head",
|
| 556 |
+
"dizziness": "dizziness",
|
| 557 |
+
"fainting": "fainting",
|
| 558 |
+
"seizures": "seizures",
|
| 559 |
+
"disturbance_of_memory": "memory issues",
|
| 560 |
+
"difficulty_speaking": "difficulty speaking",
|
| 561 |
+
"ringing_in_ear": "ringing in your ears",
|
| 562 |
+
"loss_of_sensation": "numbness or loss of sensation",
|
| 563 |
+
"spots_or_clouds_in_vision": "spots or blurriness in your vision",
|
| 564 |
+
"photophobia": "sensitivity to light",
|
| 565 |
+
"upper_abdominal_pain": "stomach pain",
|
| 566 |
+
"lower_abdominal_pain": "lower abdominal pain",
|
| 567 |
+
"sharp_abdominal_pain": "sharp abdominal pain",
|
| 568 |
+
"burning_abdominal_pain": "burning sensation in your stomach",
|
| 569 |
+
"nausea": "nausea",
|
| 570 |
+
"vomiting": "vomiting",
|
| 571 |
+
"diarrhea": "diarrhea or loose motions",
|
| 572 |
+
"constipation": "constipation",
|
| 573 |
+
"stomach_bloating": "bloating",
|
| 574 |
+
"heartburn": "heartburn",
|
| 575 |
+
"decreased_appetite": "loss of appetite",
|
| 576 |
+
"fever": "fever",
|
| 577 |
+
"chills": "chills",
|
| 578 |
+
"fatigue": "fatigue",
|
| 579 |
+
"weakness": "weakness",
|
| 580 |
+
"ache_all_over": "body ache",
|
| 581 |
+
"sweating": "sweating",
|
| 582 |
+
"feeling_ill": "feeling unwell",
|
| 583 |
+
"flu-like_syndrome": "flu-like symptoms",
|
| 584 |
+
"cough": "cough",
|
| 585 |
+
"shortness_of_breath": "shortness of breath",
|
| 586 |
+
"sore_throat": "sore throat",
|
| 587 |
+
"nasal_congestion": "blocked or runny nose",
|
| 588 |
+
"wheezing": "wheezing",
|
| 589 |
+
"chest_tightness": "tightness in your chest",
|
| 590 |
+
"sharp_chest_pain": "chest pain",
|
| 591 |
+
"burning_chest_pain": "burning sensation in your chest",
|
| 592 |
+
"blood_clots_during_menstrual_periods": "blood clots during your periods",
|
| 593 |
+
"heavy_menstrual_flow": "heavy menstrual bleeding",
|
| 594 |
+
"unpredictable_menstruation": "irregular periods",
|
| 595 |
+
"painful_menstruation": "painful periods",
|
| 596 |
+
"vaginal_discharge": "vaginal discharge",
|
| 597 |
+
"hot_flashes": "hot flashes",
|
| 598 |
+
"pelvic_pain": "pelvic pain",
|
| 599 |
+
"infertility": "difficulty getting pregnant",
|
| 600 |
+
"long_menstrual_periods": "periods that last longer than usual",
|
| 601 |
+
"frequent_menstruation": "frequent periods",
|
| 602 |
+
"intermenstrual_bleeding": "bleeding between periods",
|
| 603 |
+
"hirsutism": "facial hair growth",
|
| 604 |
+
"hair_thinning": "hair thinning",
|
| 605 |
+
"painful_intercourse": "pain during intercourse",
|
| 606 |
+
"oily_skin": "oily skin",
|
| 607 |
+
"weight_loss": "weight loss",
|
| 608 |
+
"skin_rash": "skin rash",
|
| 609 |
+
"itching_of_skin": "itching",
|
| 610 |
+
"jaundice": "jaundice",
|
| 611 |
+
"acne_or_pimples": "acne",
|
| 612 |
+
"skin_dryness__peeling__scaliness__or_roughness": "dry skin",
|
| 613 |
+
"joint_pain": "joint pain",
|
| 614 |
+
"back_pain": "back pain",
|
| 615 |
+
"low_back_pain": "lower back pain",
|
| 616 |
+
"neck_pain": "neck pain",
|
| 617 |
+
"knee_pain": "knee pain",
|
| 618 |
+
"shoulder_pain": "shoulder pain",
|
| 619 |
+
"palpitations": "palpitations",
|
| 620 |
+
"irregular_heartbeat": "irregular heartbeat",
|
| 621 |
+
"increased_heart_rate": "increased heart rate",
|
| 622 |
+
"frequent_urination": "frequent urination",
|
| 623 |
+
"painful_urination": "burning during urination",
|
| 624 |
+
"blood_in_urine": "blood in urine",
|
| 625 |
+
"pain_in_eye": "eye pain",
|
| 626 |
+
"eye_redness": "redness in your eyes",
|
| 627 |
+
"diminished_vision": "blurred vision",
|
| 628 |
+
"anxiety_and_nervousness": "anxiety",
|
| 629 |
+
"depression": "depression",
|
| 630 |
+
"insomnia": "difficulty sleeping",
|
| 631 |
+
"restlessness": "restlessness",
|
| 632 |
+
"excessive_anger": "irritability",
|
| 633 |
+
"weight_gain": "weight gain",
|
| 634 |
+
"irregular_appearing_scalp": "hair loss",
|
| 635 |
+
}
|
| 636 |
+
|
| 637 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 638 |
# 7. PYDANTIC MODELS
|
| 639 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 843 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 844 |
# 10. SYSTEM PROMPTS
|
| 845 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 846 |
SYNTHESIZE_SYSTEM = (
|
| 847 |
'You are a clinical routing engine for a women\'s menstrual health app.\n'
|
| 848 |
'Given patient message, extracted symptoms, and follow-up answers:\n\n'
|
|
|
|
| 935 |
'Rules: Plain language, 3-4 sentences max. No markdown.'
|
| 936 |
)
|
| 937 |
|
| 938 |
+
|
| 939 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 940 |
+
# 11. CHAT ENDPOINT
|
| 941 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 942 |
+
|
| 943 |
+
class ChatRequest(BaseModel):
|
| 944 |
+
conversation_id: Optional[str] = None
|
| 945 |
+
accumulated_symptoms: List[str] = []
|
| 946 |
+
accumulated_negated: List[str] = []
|
| 947 |
+
asked_questions: List[str] = []
|
| 948 |
+
cycle_data: Dict[str, Any] = {}
|
| 949 |
+
pending_questions: List[dict] = []
|
| 950 |
+
conversation_turn: int = 0
|
| 951 |
+
chat_history: List[dict] = []
|
| 952 |
+
first_message: Optional[str] = None
|
| 953 |
+
|
| 954 |
+
@app.post("/api/chat")
|
| 955 |
+
async def chat_endpoint(req: ChatRequest, request: Request):
|
| 956 |
+
user_message = req.first_message if req.conversation_turn == 0 else req.chat_history[-1]["content"] if req.chat_history else ""
|
| 957 |
+
|
| 958 |
+
if not user_message or not user_message.strip():
|
| 959 |
+
return {"error": "Empty message", "message": "Please describe your symptoms."}
|
| 960 |
+
|
| 961 |
+
# ββ Step 1: AUP Check ββ
|
| 962 |
+
for pattern in AUP_PATTERNS:
|
| 963 |
+
if re.search(pattern, user_message.lower()):
|
| 964 |
+
return {"error": "policy_violation", "message": "I cannot help with that request."}
|
| 965 |
+
|
| 966 |
+
# ββ Step 2: Crisis Check ββ
|
| 967 |
+
for kw in CRISIS_KEYWORDS:
|
| 968 |
+
if kw in user_message.lower():
|
| 969 |
+
return {
|
| 970 |
+
"acknowledgment": "π¨ This sounds like a medical emergency. Please go to the nearest hospital emergency room immediately.",
|
| 971 |
+
"questions": [],
|
| 972 |
+
"accumulated_symptoms": req.accumulated_symptoms,
|
| 973 |
+
"conversation_turn": req.conversation_turn,
|
| 974 |
+
"is_emergency": True
|
| 975 |
+
}
|
| 976 |
+
|
| 977 |
+
# ββ Step 3: Red Flag Triage ββ
|
| 978 |
+
triage = triage_check(user_message)
|
| 979 |
+
if triage:
|
| 980 |
+
level, response = triage
|
| 981 |
+
return {
|
| 982 |
+
"acknowledgment": response,
|
| 983 |
+
"questions": [],
|
| 984 |
+
"accumulated_symptoms": req.accumulated_symptoms,
|
| 985 |
+
"conversation_turn": req.conversation_turn,
|
| 986 |
+
"triage_level": level
|
| 987 |
+
}
|
| 988 |
+
|
| 989 |
+
# ββ Step 4: Intent Classification ββ
|
| 990 |
+
intent = await classify_intent(user_message)
|
| 991 |
+
|
| 992 |
+
# Handle greetings
|
| 993 |
+
if intent == "greeting":
|
| 994 |
+
return {
|
| 995 |
+
"acknowledgment": "Hello! I am ShakTide AI. Please describe your symptoms and I will help you understand what might be going on.",
|
| 996 |
+
"questions": [],
|
| 997 |
+
"accumulated_symptoms": req.accumulated_symptoms,
|
| 998 |
+
"conversation_turn": req.conversation_turn
|
| 999 |
+
}
|
| 1000 |
+
|
| 1001 |
+
# Handle gratitude
|
| 1002 |
+
if intent == "gratitude":
|
| 1003 |
+
return {
|
| 1004 |
+
"acknowledgment": "You are welcome! Is there anything else I can help you with?",
|
| 1005 |
+
"questions": [],
|
| 1006 |
+
"accumulated_symptoms": req.accumulated_symptoms,
|
| 1007 |
+
"conversation_turn": req.conversation_turn
|
| 1008 |
+
}
|
| 1009 |
+
|
| 1010 |
+
# Handle irrelevant
|
| 1011 |
+
if intent == "irrelevant":
|
| 1012 |
+
return {
|
| 1013 |
+
"acknowledgment": "I am here to help with health concerns. Please describe any symptoms you are experiencing.",
|
| 1014 |
+
"questions": [],
|
| 1015 |
+
"accumulated_symptoms": req.accumulated_symptoms,
|
| 1016 |
+
"conversation_turn": req.conversation_turn
|
| 1017 |
+
}
|
| 1018 |
+
|
| 1019 |
+
# ββ Step 5: Extract Symptoms (Rule-based) ββ
|
| 1020 |
+
rule_based_symptoms = rule_extract_symptoms(user_message)
|
| 1021 |
+
|
| 1022 |
+
# ββ Step 6: Handle Follow-up Answers ββ
|
| 1023 |
+
if intent == "followup_answer" and req.pending_questions:
|
| 1024 |
+
# Extract symptoms from the user's answer
|
| 1025 |
+
answer_symptoms = rule_extract_symptoms(user_message)
|
| 1026 |
+
|
| 1027 |
+
# Check for "no" / "none" / negation
|
| 1028 |
+
answer_lower = user_message.lower().strip()
|
| 1029 |
+
negated_this_turn = []
|
| 1030 |
+
|
| 1031 |
+
if answer_lower in {"no", "none", "nope", "not really", "nothing else", "no other symptoms", "thats all", "that's all", "only that"}:
|
| 1032 |
+
# User said no more symptoms - proceed to diagnosis
|
| 1033 |
+
all_symptoms = list(set(req.accumulated_symptoms))
|
| 1034 |
+
|
| 1035 |
+
if len(all_symptoms) >= MIN_SYMPTOMS:
|
| 1036 |
+
diagnosis = await run_diagnosis(all_symptoms, req.cycle_data)
|
| 1037 |
+
return {
|
| 1038 |
+
"acknowledgment": "Thank you for that information. Based on your symptoms, here is my analysis.",
|
| 1039 |
+
"questions": [],
|
| 1040 |
+
"accumulated_symptoms": all_symptoms,
|
| 1041 |
+
"conversation_turn": req.conversation_turn + 1,
|
| 1042 |
+
"diagnosis": diagnosis
|
| 1043 |
+
}
|
| 1044 |
+
else:
|
| 1045 |
+
return {
|
| 1046 |
+
"acknowledgment": "Thank you. I have limited information so far. Could you describe any other symptoms you are experiencing?",
|
| 1047 |
+
"questions": [],
|
| 1048 |
+
"accumulated_symptoms": all_symptoms,
|
| 1049 |
+
"conversation_turn": req.conversation_turn + 1
|
| 1050 |
+
}
|
| 1051 |
+
|
| 1052 |
+
# User answered with new symptoms
|
| 1053 |
+
all_symptoms = list(set(req.accumulated_symptoms + answer_symptoms))
|
| 1054 |
+
|
| 1055 |
+
# Build acknowledgment from new symptoms
|
| 1056 |
+
new_english = [SYMPTOM_TO_ENGLISH.get(s, s.replace("_", " ")) for s in answer_symptoms]
|
| 1057 |
+
if new_english:
|
| 1058 |
+
if len(new_english) == 1:
|
| 1059 |
+
ack = f"I understand you are also experiencing {new_english[0]}."
|
| 1060 |
+
elif len(new_english) == 2:
|
| 1061 |
+
ack = f"I understand you are also experiencing {new_english[0]} and {new_english[1]}."
|
| 1062 |
+
else:
|
| 1063 |
+
ack = f"I understand you are also experiencing {', '.join(new_english[:-1])}, and {new_english[-1]}."
|
| 1064 |
+
else:
|
| 1065 |
+
ack = "Thank you for that information."
|
| 1066 |
+
|
| 1067 |
+
# Check if we have enough symptoms for diagnosis
|
| 1068 |
+
if len(all_symptoms) >= MIN_SYMPTOMS:
|
| 1069 |
+
diagnosis = await run_diagnosis(all_symptoms, req.cycle_data)
|
| 1070 |
+
return {
|
| 1071 |
+
"acknowledgment": ack,
|
| 1072 |
+
"questions": [],
|
| 1073 |
+
"accumulated_symptoms": all_symptoms,
|
| 1074 |
+
"conversation_turn": req.conversation_turn + 1,
|
| 1075 |
+
"diagnosis": diagnosis
|
| 1076 |
+
}
|
| 1077 |
+
else:
|
| 1078 |
+
# Ask more questions
|
| 1079 |
+
followup_questions = await generate_followup_questions(all_symptoms, req.asked_questions, req.cycle_data)
|
| 1080 |
+
return {
|
| 1081 |
+
"acknowledgment": ack,
|
| 1082 |
+
"questions": followup_questions,
|
| 1083 |
+
"accumulated_symptoms": all_symptoms,
|
| 1084 |
+
"conversation_turn": req.conversation_turn + 1,
|
| 1085 |
+
"pending_questions": followup_questions
|
| 1086 |
+
}
|
| 1087 |
+
|
| 1088 |
+
# ββ Step 7: First Symptom Report - Call LLM ββ
|
| 1089 |
+
if rule_based_symptoms:
|
| 1090 |
+
# Convert to English for acknowledgment
|
| 1091 |
+
symptom_english = [SYMPTOM_TO_ENGLISH.get(s, s.replace("_", " ")) for s in rule_based_symptoms]
|
| 1092 |
+
|
| 1093 |
+
# Build prompt for LLM
|
| 1094 |
+
extract_msg = (
|
| 1095 |
+
f"Patient said: {user_message}\n"
|
| 1096 |
+
f"found_symptoms: {json.dumps(rule_based_symptoms)}\n"
|
| 1097 |
+
f"found_symptoms_in_english: {json.dumps(symptom_english)}\n"
|
| 1098 |
+
f"cycle_data_already_provided: {bool(req.cycle_data)}\n"
|
| 1099 |
+
f"already_asked: {json.dumps(req.asked_questions)}"
|
| 1100 |
+
)
|
| 1101 |
+
|
| 1102 |
+
try:
|
| 1103 |
+
llm_raw = await _groq(EXTRACT_SYSTEM, extract_msg, 400)
|
| 1104 |
+
llm_data = json.loads(llm_raw)
|
| 1105 |
+
|
| 1106 |
+
acknowledgment = llm_data.get("acknowledgment", "")
|
| 1107 |
+
additional_symptoms = llm_data.get("additional_symptoms", [])
|
| 1108 |
+
questions = llm_data.get("questions", [])
|
| 1109 |
+
repro_context = llm_data.get("reproductive_context")
|
| 1110 |
+
except Exception as e:
|
| 1111 |
+
logger.error(f"LLM extraction failed: {e}")
|
| 1112 |
+
# Fallback acknowledgment
|
| 1113 |
+
if len(symptom_english) == 1:
|
| 1114 |
+
acknowledgment = f"I understand you are experiencing {symptom_english[0]}."
|
| 1115 |
+
elif len(symptom_english) == 2:
|
| 1116 |
+
acknowledgment = f"I understand you are experiencing {symptom_english[0]} and {symptom_english[1]}."
|
| 1117 |
+
else:
|
| 1118 |
+
ack_list = ", ".join(symptom_english[:-1]) + ", and " + symptom_english[-1]
|
| 1119 |
+
acknowledgment = f"I understand you are experiencing {ack_list}."
|
| 1120 |
+
|
| 1121 |
+
additional_symptoms = []
|
| 1122 |
+
questions = []
|
| 1123 |
+
repro_context = None
|
| 1124 |
+
|
| 1125 |
+
# Combine symptoms
|
| 1126 |
+
all_symptoms = list(set(rule_based_symptoms + additional_symptoms))
|
| 1127 |
+
|
| 1128 |
+
# Filter out already asked questions
|
| 1129 |
+
asked_set = set(req.asked_questions)
|
| 1130 |
+
filtered_questions = [q for q in questions if q.get("text", "") not in asked_set]
|
| 1131 |
+
|
| 1132 |
+
# Update asked list
|
| 1133 |
+
new_asked = req.asked_questions + [q.get("text", "") for q in filtered_questions]
|
| 1134 |
+
|
| 1135 |
+
# Handle cycle data
|
| 1136 |
+
cycle_data = req.cycle_data.copy() if req.cycle_data else {}
|
| 1137 |
+
if repro_context == "menstrual" and not cycle_data:
|
| 1138 |
+
cycle_data = {}
|
| 1139 |
+
|
| 1140 |
+
return {
|
| 1141 |
+
"acknowledgment": acknowledgment,
|
| 1142 |
+
"questions": filtered_questions,
|
| 1143 |
+
"accumulated_symptoms": all_symptoms,
|
| 1144 |
+
"accumulated_negated": req.accumulated_negated,
|
| 1145 |
+
"conversation_id": req.conversation_id or str(datetime.datetime.now().timestamp()),
|
| 1146 |
+
"conversation_turn": req.conversation_turn + 1,
|
| 1147 |
+
"pending_questions": filtered_questions,
|
| 1148 |
+
"asked_questions": new_asked,
|
| 1149 |
+
"cycle_data": cycle_data,
|
| 1150 |
+
"message": acknowledgment # Fallback for old frontend
|
| 1151 |
+
}
|
| 1152 |
+
|
| 1153 |
+
# ββ Step 8: No Symptoms Found ββ
|
| 1154 |
+
return {
|
| 1155 |
+
"acknowledgment": "I want to help, but I did not catch any specific symptoms. Could you describe what you are feeling? For example: 'I have a headache and fever for 2 days.'",
|
| 1156 |
+
"questions": [],
|
| 1157 |
+
"accumulated_symptoms": req.accumulated_symptoms,
|
| 1158 |
+
"conversation_turn": req.conversation_turn + 1
|
| 1159 |
+
}
|
| 1160 |
+
|
| 1161 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1162 |
# 11. LLM CALLS
|
| 1163 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1396 |
_daily_usage[key] = count + 1
|
| 1397 |
return True
|
| 1398 |
|
| 1399 |
+
|
| 1400 |
+
async def run_diagnosis(symptoms: list, cycle_data: dict = None) -> dict:
|
| 1401 |
+
"""Run XGBoost prediction and return diagnosis card data."""
|
| 1402 |
+
|
| 1403 |
+
# Check medication overlap
|
| 1404 |
+
med_note = None # Would need medications from profile
|
| 1405 |
+
|
| 1406 |
+
# ββ NLP Retrieval ββ
|
| 1407 |
+
symptom_text = " ".join(symptoms).replace("_", " ")
|
| 1408 |
+
|
| 1409 |
+
tfidf_results = pd.DataFrame()
|
| 1410 |
+
semantic_results = pd.DataFrame()
|
| 1411 |
+
|
| 1412 |
+
if tfidf_retriever:
|
| 1413 |
+
try:
|
| 1414 |
+
tfidf_results = tfidf_retriever.query(symptom_text, top_k=5)
|
| 1415 |
+
except Exception as e:
|
| 1416 |
+
logger.error(f"TF-IDF failed: {e}")
|
| 1417 |
+
|
| 1418 |
+
if sbert_retriever:
|
| 1419 |
+
try:
|
| 1420 |
+
semantic_results = sbert_retriever.query(symptom_text, top_k=5)
|
| 1421 |
+
except Exception as e:
|
| 1422 |
+
logger.error(f"SBERT failed: {e}")
|
| 1423 |
+
|
| 1424 |
+
# Merge and rank candidates
|
| 1425 |
+
candidates = []
|
| 1426 |
+
seen = set()
|
| 1427 |
+
for df in [tfidf_results, semantic_results]:
|
| 1428 |
+
for _, row in df.iterrows():
|
| 1429 |
+
disease = row.get("disease", "")
|
| 1430 |
+
if disease and disease not in seen and not _is_blocked(disease, symptoms):
|
| 1431 |
+
seen.add(disease)
|
| 1432 |
+
candidates.append({"disease": disease, "score": float(row.get("score", 0))})
|
| 1433 |
+
|
| 1434 |
+
candidates.sort(key=lambda x: x["score"], reverse=True)
|
| 1435 |
+
candidate_names = [c["disease"] for c in candidates[:5]]
|
| 1436 |
+
|
| 1437 |
+
# ββ XGBoost Prediction ββ
|
| 1438 |
+
xgb_confidence = 0.0
|
| 1439 |
+
xgb_prediction = None
|
| 1440 |
+
|
| 1441 |
+
if disease_model and disease_encoder and symptom_scaler:
|
| 1442 |
+
try:
|
| 1443 |
+
# Create feature vector
|
| 1444 |
+
all_possible_symptoms = list(disease_encoder.classes_) if hasattr(disease_encoder, 'classes_') else []
|
| 1445 |
+
feature_vec = np.zeros(len(all_possible_symptoms))
|
| 1446 |
+
for s in symptoms:
|
| 1447 |
+
if s in all_possible_symptoms:
|
| 1448 |
+
idx = list(all_possible_symptoms).index(s)
|
| 1449 |
+
feature_vec[idx] = 1
|
| 1450 |
+
|
| 1451 |
+
feature_scaled = symptom_scaler.transform([feature_vec])
|
| 1452 |
+
probas = disease_model.predict_proba(feature_scaled)[0]
|
| 1453 |
+
max_idx = np.argmax(probas)
|
| 1454 |
+
xgb_prediction = disease_encoder.inverse_transform([max_idx])[0] if hasattr(disease_encoder, 'inverse_transform') else all_possible_symptoms[max_idx]
|
| 1455 |
+
xgb_confidence = float(probas[max_idx])
|
| 1456 |
+
except Exception as e:
|
| 1457 |
+
logger.error(f"XGBoost failed: {e}")
|
| 1458 |
+
|
| 1459 |
+
# ββ LLM Verification ββ
|
| 1460 |
+
final_condition = xgb_prediction or (candidate_names[0] if candidate_names else "Unknown condition")
|
| 1461 |
+
final_confidence = xgb_confidence
|
| 1462 |
+
|
| 1463 |
+
if candidate_names:
|
| 1464 |
+
verify_msg = (
|
| 1465 |
+
f"Confirmed symptoms: {json.dumps(symptoms)}\n"
|
| 1466 |
+
f"Candidates: {json.dumps(candidate_names)}\n"
|
| 1467 |
+
f"XGBoost predicted: {xgb_prediction} (confidence: {xgb_confidence})"
|
| 1468 |
+
)
|
| 1469 |
+
try:
|
| 1470 |
+
verify_raw = await _groq(VERIFY_SYSTEM, verify_msg, 200)
|
| 1471 |
+
verify_data = json.loads(verify_raw)
|
| 1472 |
+
final_condition = verify_data.get("final_condition", final_condition)
|
| 1473 |
+
conf_map = {"high": 0.85, "medium": 0.65, "low": 0.45}
|
| 1474 |
+
final_confidence = conf_map.get(verify_data.get("confidence", "medium"), xgb_confidence)
|
| 1475 |
+
except Exception as e:
|
| 1476 |
+
logger.error(f"Verify LLM failed: {e}")
|
| 1477 |
+
|
| 1478 |
+
# ββ Check Exclusions ββ
|
| 1479 |
+
exclusion_note = apply_exclusions(final_condition, symptoms)
|
| 1480 |
+
|
| 1481 |
+
# ββ Build Symptom Lists ββ
|
| 1482 |
+
matched = []
|
| 1483 |
+
unmatched = []
|
| 1484 |
+
disease_words = set(final_condition.lower().replace("_", " ").split())
|
| 1485 |
+
for s in symptoms:
|
| 1486 |
+
s_words = set(s.replace("_", " ").split())
|
| 1487 |
+
if disease_words & s_words or any(dw in s for dw in disease_words if len(dw) > 3):
|
| 1488 |
+
matched.append(s)
|
| 1489 |
+
else:
|
| 1490 |
+
unmatched.append(s)
|
| 1491 |
+
|
| 1492 |
+
# ββ Severity ββ
|
| 1493 |
+
if final_confidence >= 0.75:
|
| 1494 |
+
severity = "Moderate"
|
| 1495 |
+
sev_color = "#d97706"
|
| 1496 |
+
sev_dots = 3
|
| 1497 |
+
elif final_confidence >= 0.55:
|
| 1498 |
+
severity = "Low-Moderate"
|
| 1499 |
+
sev_color = "#23856b"
|
| 1500 |
+
sev_dots = 2
|
| 1501 |
+
else:
|
| 1502 |
+
severity = "Low"
|
| 1503 |
+
sev_color = "#52b788"
|
| 1504 |
+
sev_dots = 1
|
| 1505 |
+
|
| 1506 |
+
# ββ Build Diet Recommendations ββ
|
| 1507 |
+
eat_list = ["Light, easily digestible food", "Cooked vegetables and dal", "Fresh fruits like papaya or banana", "Small frequent meals", "Plenty of water and fluids"]
|
| 1508 |
+
avoid_list = ["Spicy and oily food", "Processed or packaged food", "Excessive tea or coffee", "Cold drinks and ice cream", "Heavy meals at night"]
|
| 1509 |
+
|
| 1510 |
+
# ββ Build Precautions ββ
|
| 1511 |
+
precautions = ["Rest adequately and get proper sleep", "Monitor your symptoms for any changes", "Stay hydrated with warm fluids", "Avoid self-medication", "Keep a symptom diary"]
|
| 1512 |
+
|
| 1513 |
+
return {
|
| 1514 |
+
"condition": final_condition,
|
| 1515 |
+
"summary": f"Based on your symptoms, this appears to be {final_condition.replace('_', ' ').title()}.",
|
| 1516 |
+
"why": f"Your symptoms of {', '.join([SYMPTOM_TO_ENGLISH.get(s, s.replace('_', ' ')) for s in matched[:3]])} align with this condition.",
|
| 1517 |
+
"severity": severity,
|
| 1518 |
+
"severity_color": sev_color,
|
| 1519 |
+
"severity_dots": sev_dots,
|
| 1520 |
+
"confidence": round(final_confidence * 100, 1),
|
| 1521 |
+
"matched_symptoms": [SYMPTOM_TO_ENGLISH.get(s, s.replace("_", " ")) for s in matched],
|
| 1522 |
+
"unmatched_symptoms": [SYMPTOM_TO_ENGLISH.get(s, s.replace("_", " ")) for s in unmatched],
|
| 1523 |
+
"eat": eat_list,
|
| 1524 |
+
"avoid": avoid_list,
|
| 1525 |
+
"precautions": precautions,
|
| 1526 |
+
"exclusion_note": exclusion_note,
|
| 1527 |
+
"candidates": candidate_names[:5],
|
| 1528 |
+
"ruled_out": [c for c in candidate_names[1:4] if c != final_condition]
|
| 1529 |
+
}
|
| 1530 |
+
|
| 1531 |
+
|
| 1532 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1533 |
# 14. MAIN CHAT ENDPOINT
|
| 1534 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|