shaktide-api / main.py
shaktideadmin's picture
Update main.py
074ae7d verified
Raw History Blame Contribute Delete
87.3 kB
""" ShakTide AI β€” main.py (v3.2) ───────────────────────────── Architecture: POST /api/chat β€” multi-turn chatbot (session state in payload) GET /api/tokens β€” daily usage check GET / β€” health ping Diagnostic pipeline (Cell 13 logic): Turn 1 : rule-based + LLM symptom extraction β†’ clarifying question Turn 2+ : answers processed β†’ more questions OR diagnosis Diagnose: Disease β†’ TF-IDF + SBERT ensemble + Groq verification PCOS β†’ weighted heuristic (Cell 13 scoring) Conception β†’ cycle ML model + calendar Fertility β†’ calendar-first + ML model PKL files required (place in same folder as main.py): tfidf_retriever.pkl sbert_retriever.pkl sym_embedder.pkl best_disease_model.pkl disease_encoder.pkl symptom_scaler.pkl best_pcos_model.pkl pcos_scaler.pkl best_cycle_model.pkl cycle_scaler.pkl best_fert_model.pkl fert_scaler.pkl Environment: GROQ_API_KEY=gsk_xxxxx SUPABASE_URL=... (optional) SUPABASE_SERVICE_ROLE_KEY=... (optional) """ import os, re, json, io, pickle, datetime import numpy as np import pandas as pd from fastapi import FastAPI, Request, HTTPException from fastapi.middleware.cors import CORSMiddleware from pydantic import BaseModel, Field from typing import List, Optional, Dict, Any from groq import AsyncGroq # ────────────────────────────────────────────────────────────────────────────── # 0. NLP STACK CLASS DEFINITIONS # ────────────────────────────────────────────────────────────────────────────── class TFIDFRetriever: def __init__(self, vectorizer=None, tfidf_matrix=None, diseases=None): self.vectorizer = vectorizer self.tfidf_matrix = tfidf_matrix self.diseases = diseases def query(self, text: str, top_k: int = 5) -> pd.DataFrame: vectorizer = getattr(self, "vectorizer", None) or getattr(self, "tfidf", None) tfidf_matrix = getattr(self, "tfidf_matrix", None) or getattr(self, "matrix", None) diseases = getattr(self, "diseases", None) or getattr(self, "labels", None) if vectorizer is None or tfidf_matrix is None: return pd.DataFrame(columns=["disease", "score", "rank"]) q_vec = vectorizer.transform([text]) sims = (tfidf_matrix * q_vec.T).toarray().flatten() top_i = np.argsort(sims)[::-1][:top_k] return pd.DataFrame([ {"rank": r+1, "disease": diseases[i], "score": float(sims[i])} for r, i in enumerate(top_i) ]) class SemanticRetriever: def __init__(self, model=None, embeddings=None, diseases=None): self.model = model self.embeddings = embeddings self.diseases = diseases def query(self, text: str, top_k: int = 5) -> pd.DataFrame: if self.model is None or self.embeddings is None: return pd.DataFrame(columns=["disease", "score", "rank"]) q_emb = self.model.encode([text])[0] norm_e = self.embeddings / (np.linalg.norm(self.embeddings, axis=1, keepdims=True) + 1e-9) norm_q = q_emb / (np.linalg.norm(q_emb) + 1e-9) sims = np.dot(norm_e, norm_q) top_i = np.argsort(sims)[::-1][:top_k] return pd.DataFrame([ {"rank": r+1, "disease": self.diseases[i], "score": float(sims[i])} for r, i in enumerate(top_i) ]) class SymptomEmbedder: def __init__(self, model=None, symptom_embeddings=None, symptoms=None): self.model = model self.symptom_embeddings = symptom_embeddings self.symptoms = symptoms def find_symptoms(self, query: str, top_k: int = 15) -> pd.DataFrame: return pd.DataFrame(columns=["symptom", "similarity"]) try: import __main__ __main__.TFIDFRetriever = TFIDFRetriever __main__.SemanticRetriever = SemanticRetriever __main__.SymptomEmbedder = SymptomEmbedder except Exception as e: print(f"[WARN] __main__ injection skipped: {e}") # ────────────────────────────────────────────────────────────────────────────── # 1. APP + CORS # ────────────────────────────────────────────────────────────────────────────── app = FastAPI(title="ShakTide AI", version="3.2") app.add_middleware( CORSMiddleware, allow_origins=[ "https://shaktide.com", "https://www.shaktide.com", "http://localhost:3000", "http://127.0.0.1:3000", "http://localhost:3002", "http://127.0.0.1:3002", "http://localhost:5500", "http://127.0.0.1:5500", ], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) @app.get("/") def root(): return {"status": "ShakTide AI v3.2 running"} # ────────────────────────────────────────────────────────────────────────────── # 2. CONFIG # ────────────────────────────────────────────────────────────────────────────── GROQ_API_KEY = os.environ.get("GROQ_API_KEY", "") SUPABASE_URL = os.environ.get("SUPABASE_URL", "") SUPABASE_KEY = os.environ.get("SUPABASE_SERVICE_ROLE_KEY", "") DAILY_TOKEN_LIMIT = 10 GROQ_MODEL = "llama-3.1-8b-instant" MIN_SYMPTOMS = 2 groq_client = AsyncGroq(api_key=GROQ_API_KEY) try: from supabase import create_client supabase = create_client(SUPABASE_URL, SUPABASE_KEY) if SUPABASE_URL and SUPABASE_KEY else None except Exception: supabase = None # ────────────────────────────────────────────────────────────────────────────── # 3. MODEL LOADER # ────────────────────────────────────────────────────────────────────────────── BASE_DIR = os.path.dirname(os.path.abspath(__file__)) def _load(name: str): path = os.path.join(BASE_DIR, name) if not os.path.exists(path): print(f"[WARN] {name} not found β€” skipping.") return None class CompatUnpickler(pickle.Unpickler): def find_class(self, module, name): if isinstance(module, bytes): module = module.decode("utf-8") if isinstance(name, bytes): name = name.decode("utf-8") module = module.replace("sklearn.utils._bunch", "sklearn.utils") module = module.replace("numpy.core.multiarray", "numpy") if name == "TFIDFRetriever": return TFIDFRetriever if name == "SemanticRetriever": return SemanticRetriever if name == "SymptomEmbedder": return SymptomEmbedder if module == "__main__" and name in globals(): return globals()[name] return super().find_class(module, name) for encoding in ("utf-8", "latin-1", "bytes"): try: with open(path, "rb") as f: data = f.read() up = CompatUnpickler(io.BytesIO(data)) up.encoding = encoding result = up.load() print(f"[OK] {name} (encoding={encoding})") return result except Exception: continue try: import joblib result = joblib.load(path) print(f"[OK] {name} (joblib)") return result except Exception as e: print(f"[ERROR] Could not load {name}: {e}") return None tfidf_retriever = _load("tfidf_retriever.pkl") sbert_retriever = _load("sbert_retriever.pkl") sym_embedder_obj = _load("sym_embedder.pkl") nlp_stack = { "tfidf": tfidf_retriever, "semantic": sbert_retriever, "sym_embedder": sym_embedder_obj, } NLP_AVAILABLE = tfidf_retriever is not None and sbert_retriever is not None disease_model = _load("best_disease_model.pkl") disease_encoder = _load("disease_encoder.pkl") symptom_scaler = _load("symptom_scaler.pkl") pcos_model = _load("best_pcos_model.pkl") pcos_scaler = _load("pcos_scaler.pkl") cycle_model = _load("best_cycle_model.pkl") cycle_scaler = _load("cycle_scaler.pkl") fert_model = _load("best_fert_model.pkl") fert_scaler = _load("fert_scaler.pkl") # ────────────────────────────────────────────────────────────────────────────── # 4. CONSTANTS # ────────────────────────────────────────────────────────────────────────────── MALE_ANATOMY_TERMS = {"scrotum", "testes", "testicular", "penile", "prostate"} REPRO_TERMS = { "menstruat", "menstrual", "period", "dysmenorrhea", "menorrhagia", "oligomenorrhea", "amenorrhea", "endometriosis", "adenomyosis", "infertil", "pelvic", "vaginal", "ovarian", "uterine", "acne_or_pimples", "weight_gain", "hirsut", "irregular_appearing_scalp", "hot_flashes", "intermenstrual", "heavy_menstrual", "unpredictable_menstruation", "long_menstrual", "frequent_menstruation", "blood_clots_during", "painful_intercourse", "hair_thinning", "oily_skin", "lower_abdominal", "abdominal_pain", "pelvic_pain", "painful_menstruat", "cramping", "cramp", } ALWAYS_BLOCK = { "paralysis (brain hemorrhage)", "tuberculosis", "aids", "swelling of scrotum", "pain in testicles", "discharge from urethra", } # ADD to CONDITIONAL_BLOCK CONDITIONAL_BLOCK = { "chicken pox": lambda s: "skin_rash" not in s and "itching_of_skin" not in s, "heart attack": lambda s: not any(x in s for x in [ "sharp_chest_pain", "palpitations", "chest_tightness", "shortness_of_breath", "sweating", "burning_chest_pain"]), "cervical spondylosis": lambda s: "neck_pain" not in s and "back_pain" not in s, "malaria": lambda s: "fever" not in s, "gastroenteritis": lambda s: not ( ("diarrhea" in s or "vomiting" in s) and any(x in s for x in ["fever", "nausea", "stomach_bloating"]) ), "migraine": lambda s: "headache" not in s and "frontal_headache" not in s, "peptic ulcer": lambda s: not any(x in s for x in [ "heartburn", "burning_abdominal_pain", "upper_abdominal_pain", "nausea", "vomiting"]), } def _is_blocked(disease: str, symptoms: list) -> bool: d = disease.lower() if d in ALWAYS_BLOCK: return True for name, cond in CONDITIONAL_BLOCK.items(): if name in d and cond(symptoms): return True if any(m in d for m in MALE_ANATOMY_TERMS): return True return False PCOS_INDICATORS = { "unpredictable_menstruation": 3, "infertility": 3, "hirsutism": 3, "acne_or_pimples": 2, "weight_gain": 2, "irregular_appearing_scalp": 2, "hair_thinning": 2, "painful_menstruation": 2, "heavy_menstrual_flow": 2, "long_menstrual_periods": 2, "pelvic_pain": 2, "painful_intercourse": 2, "intermenstrual_bleeding": 2, "lower_abdominal_pain": 1, "oily_skin": 1, "fatigue": 1, "hot_flashes": 1, "frequent_menstruation": 1, "depression": 1, "excessive_anger": 1, } PCOS_FUZZY = { "hirsut": 3, "androgen": 2, "testosterone": 2, "cyst": 3, "ovarian": 2, "pcos": 3, "pcod": 3, "dysmenorrhea": 2, "menorrhagia": 2, "hair_thin": 2, "hair_loss": 2, } PCOS_CORE = { "unpredictable_menstruation", "infertility", "hirsutism", "acne_or_pimples", "weight_gain", "irregular_appearing_scalp", "hair_thinning", "painful_intercourse", } CRISIS_KEYWORDS = [ "heart attack", "stroke", "suicide", "marna chahti", "marna chahta", "jaan dena", "bleeding heavily", "can't breathe", "saans nahi", "unconscious", "behosh", ] AUP_PATTERNS = [ r"\b(bomb|explosive|weapon|make.*gun|poison.*person|how.*kill|murder plan|suicide method)\b", r"\b(hate speech|terrorist attack|jihad|white supremac|nazi)\b", r"\b(child porn|csam|minor sex|underage sex)\b", r"\b(hack.*site|ddos|sql injection|malware|ransomware)\b", r"\b(stalk|doxx|harass|blackmail)\b", ] # ── SYMPTOM NORMALIZATION ────────────────────────────────────────────────────── SYMPTOM_ALIASES = { "heavy_bleeding": "heavy_menstrual_flow", "menorrhagia": "heavy_menstrual_flow", "heavy_period": "heavy_menstrual_flow", "dysmenorrhea": "painful_menstruation", "menstrual_cramp": "painful_menstruation", "cramps": "painful_menstruation", "period_cramps": "painful_menstruation", "abdominal_pain": "lower_abdominal_pain", "pelvic_pain": "lower_abdominal_pain", "irregular_period": "unpredictable_menstruation", "irregular_periods": "unpredictable_menstruation", "irregular_menstruation": "unpredictable_menstruation", "missed_period": "unpredictable_menstruation", "acne": "acne_or_pimples", "pimples": "acne_or_pimples", "hair_loss": "hair_thinning", "hair_fall": "hair_thinning", "facial_hair": "hirsutism", "blood_clots": "blood_clots_during_menstrual_periods", "clots_in_period": "blood_clots_during_menstrual_periods", "spotting": "intermenstrual_bleeding", "bleeding_between_periods": "intermenstrual_bleeding", } def normalize_symptoms(symptoms: list) -> list: seen, seen_set = [], set() for s in symptoms: canonical = SYMPTOM_ALIASES.get(s, s) if canonical not in seen_set: seen.append(canonical) seen_set.add(canonical) return seen # 5. PHRASE MAP # ────────────────────────────────────────────────────────────────────────────── PHRASE_MAP = { r"head.*bonkers|bonkers.*head|head.*splitting|splitting.*head|head.*killing|severe headache|throbbing headache|migraine|sir.*dard|sar.*dard": "headache", r"frontal headache": "frontal_headache", r"dizz|lightheaded|vertigo|spinning|unsteady|chakkar": "dizziness", r"faint|passed out|blackout|syncope": "fainting", r"seizure|convuls": "seizures", r"memory|forgetful": "disturbance_of_memory", r"slurred speech|difficulty speak": "difficulty_speaking", r"ringing.*ear|tinnitus": "ringing_in_ear", r"loss of sensation|numbness|numb": "loss_of_sensation", r"visual aura|aura|spots.*vision|vision disturbance|blurred vision|dhundla": "spots_or_clouds_in_vision", r"tummy.*killing|killing.*tummy|stomach.*killing|tummy.*hurt|stomach.*hurt|stomachache|tummy.*ache|tummy.*pain|stomach.*ache|belly.*pain|belly.*hurt|abdominal pain|lower belly|lower tummy|pet.*dard|pet mein|pait|paet": "lower_abdominal_pain", r"upper.*stomach|upper.*abdom|upper.*belly": "upper_abdominal_pain", r"sharp.*stomach|sharp.*abdom|sharp.*belly": "sharp_abdominal_pain", r"burning.*stomach|burning.*abdom|acid.*stomach|jalan.*pet": "burning_abdominal_pain", r"nausea|nauseous|queasy|feel sick|feel like vomit|ulti|ji machla": "nausea", r"vomit|threw up|throwing up|puked|ulti ki|ulti ho|ulti aana": "vomiting", r"diarrhea|diarrhoea|loose stool|loose motion|runny stool|loose potty": "diarrhea", r"constipat|cant poop|no bowel|qabz": "constipation", r"bloat|gassy|flatulence|fart|burp|gas.*pet|acidity|afara": "stomach_bloating", r"heartburn|acid reflux|reflux|seene mein jalan": "heartburn", r"no appetite|loss of appetite|not hungry|bhukh nhi|khaana nhi": "decreased_appetite", r"indigestion|bhaari lagta|bhaari pet": "burning_abdominal_pain", r"fever|high temp|running a temp|feverish|bukhar|bukhaar": "fever", r"chills|shiver|kaamp": "chills", r"fatigue|tired|exhausted|no energy|lethargic|thakan|thak": "fatigue", r"weakness|feel weak|body weak|kamzori|kamzor": "weakness", r"ache all over|body ache|body pain|paining all over|shareer.*dard|badan.*dard|poore badan": "ache_all_over", r"sweating|night sweat|excessive sweat|pasina": "sweating", r"feeling ill|feeling sick|generally unwell|malaise|tabiyat kharab": "feeling_ill", r"flu.*like|flu symptoms": "flu-like_syndrome", r"cough|coughing|khansi": "cough", r"short of breath|shortness of breath|breathless|sans nhi|sans lena mushkil": "shortness_of_breath", r"sore throat|throat pain|throat hurt|gale mein dard|gala kharab": "sore_throat", r"runny nose|stuffy nose|blocked nose|nasal|naak band|naak behna": "nasal_congestion", r"wheezing|wheeze|saans mein awaaz": "wheezing", r"chest.*tight|tight.*chest|seena bhaari": "chest_tightness", r"chest.*pain|pain.*chest|chest hurt|seene mein dard": "sharp_chest_pain", r"burning.*chest|seene mein jalan": "burning_chest_pain", r"blood clot.*period|clot.*menstrual|menstrual.*clot|period mein clot|thakka": "blood_clots_during_menstrual_periods", r"heavy.*period|heavy.*bleeding|heavy.*flow|menorrhagia|zyada bleeding|bohot bleeding|bahut bleeding|period zyada|bahut flow": "heavy_menstrual_flow", r"irregular.*period|period.*irregular|missed period|late period|no period|unpredictable.*period|pcos|pcod|periods nhi|periods nahi|periods skip|mahina nhi aata|mahavari irregular|cycle irregular": "unpredictable_menstruation", r"period pain|menstrual cramp|dysmenorrhea|painful.*period|period.*painful|period mein dard|mahavari mein dard|dard.*period": "painful_menstruation", r"white discharge|vaginal discharge|leucorrhoea|safed pani|safed discharge|white paani": "vaginal_discharge", r"hot flash|hot flush|garmi ki lahar": "hot_flashes", r"pelvic pain|pelvis pain|pelvic pressure|pelvic heaviness|neeche dard|lower dard|kamar ke neeche": "pelvic_pain", r"infertil|not getting pregnant|cant get pregnant|conceive nhi|conceive nahi|bachha nhi|pregnant nhi|pregnant nahi|bachcha nahi ho raha|garbh nahi": "infertility", r"long.*period|period.*long|period.*zyada din|period.*bahut din": "long_menstrual_periods", r"frequent.*period|period.*frequent|baar baar period|jaldi jaldi period": "frequent_menstruation", r"bleed.*between period|spotting|beech mein bleeding|bich mein daag": "intermenstrual_bleeding", r"facial hair|upper lip hair|chin hair|hirsutism|moustache.*woman|unwanted hair|munh pe baal|thodi pe baal|moonch.*aurat": "hirsutism", r"hair.*thin|thinning.*hair|fine hair|baal pate|baal patla|baal jhad rhe|hair.*fall": "hair_thinning", r"pain.*sex|painful.*intercourse|hurt.*sex|sex.*painful|dyspareunia|sex mein dard|sambhog mein dard": "painful_intercourse", r"oily.*skin|skin.*oily|greasy.*skin|oily.*face|chehre par tel|oily chehra": "oily_skin", r"weight loss|losing weight|lost weight|wajan kam|wazan kam ho raha": "weight_loss", r"skin rash|rash|hives|red spots|daane|kharish": "skin_rash", r"itch.*skin|skin.*itch|itching|khujli": "itching_of_skin", r"jaundice|yellow.*skin|yellow.*eyes|yellowing|peela|piliya": "jaundice", r"acne|pimple|breakout|muhase|daane chehre|phunsi": "acne_or_pimples", r"skin.*dry|dry.*skin|peeling skin|rukhi twacha": "skin_dryness__peeling__scaliness__or_roughness", r"joint pain|joints.*hurt|jodon mein dard": "joint_pain", r"back pain|backache|kamar dard|kamar mein dard": "back_pain", r"lower back|low back|peeth ke neeche": "low_back_pain", r"neck pain|stiff neck|gardan mein dard|gardan akdi": "neck_pain", r"knee pain|ghutne mein dard|ghutna": "knee_pain", r"shoulder pain|kandhe mein dard|kandha": "shoulder_pain", r"palpitation|heart racing|racing heart|fast heartbeat|heart pound|dil tez|dhadkan tez": "palpitations", r"irregular heartbeat|heart flutter|dil ki dhadkan ulti": "irregular_heartbeat", r"increased heart rate": "increased_heart_rate", r"frequent urinat|pee.*often|pee.*lot|urinate.*often|baar baar peshab|baar baar toilet": "frequent_urination", r"burning.*urinat|burn.*pee|painful.*urinat|peshab mein jalan|toilet mein jalan": "painful_urination", r"blood.*urine|urine.*blood|peshab mein khoon": "blood_in_urine", r"eye pain|pain.*eye|aankh mein dard": "pain_in_eye", r"eye red|red.*eye|aankh lal": "eye_redness", r"blurred vision|cant see|dhundla dikhna": "diminished_vision", r"anxi|anxious|panic|nervous|ghabra|ghabrahat": "anxiety_and_nervousness", r"depress|depressed|sad|hopeless|udaas|nirash|dukhi": "depression", r"insomnia|cant sleep|trouble sleep|sleep problem|neend nhi|neend nahi aati": "insomnia", r"restless|chain nhi|bechain": "restlessness", r"irritable|irritability|anger|angry|gussa|chidchida": "excessive_anger", r"mood swing|mood badal|mood theek nhi": "excessive_anger", r"weight gain|gaining weight|put on weight|wajan badh|wazan badh raha": "weight_gain", r"hair loss|hair fall|losing hair|hair.*falling|baal jhad|baal girna|baal toot": "irregular_appearing_scalp", } def rule_extract_symptoms(text: str) -> list: cleaned = re.sub(r"[^a-z0-9\s',.!?]", " ", text.lower().strip()) cleaned = re.sub(r"\s+", " ", cleaned) found = set() for pattern, symptom in PHRASE_MAP.items(): if re.search(pattern, cleaned): found.add(symptom) return list(found) def expand_yes_all_answers(answers: dict, questions: list) -> dict: expanded = dict(answers) YES_ALL = {"yes all", "yes both", "all of them", "all of these", "yes all of them", "sab", "sab kuch", "haan sab", "yes sab", "yeah all", "yep all", "all yes", "sab haan"} YES_SINGLE = {"all", "both", "haan", "ha", "yes", "yeah", "yep"} for q in questions: qid = q.get("id", "") text = q.get("text", "") ans = str(answers.get(qid, "")).strip().lower() if not ans: continue is_list_q = any(kw in text.lower() for kw in [", or ", ", and ", ": ", " or "]) is_yes_all = ( ans in YES_ALL or ans.startswith("yes all") or ans.startswith("yes both") or (ans in YES_SINGLE and is_list_q) ) if not is_yes_all: continue stripped = re.sub(r"^.*?(?:have|experience|notice|feel|these)[:\s]*", "", text.lower()) stripped = re.sub(r"\(you can.*?\)", "", stripped).strip(" ?.") stripped = re.sub(r"\(list all.*?\)", "", stripped).strip(" ?.") items = re.split(r",\s*|\s+or\s+|\s+and\s+", stripped) items = [i.strip(" .?()") for i in items if len(i.strip()) > 2] if items: expanded[qid] = ", ".join(items) return expanded # ────────────────────────────────────────────────────────────────────────────── # 6. GROQ HELPERS # ────────────────────────────────────────────────────────────────────────────── def _extract_json(text: str) -> str: text = text.strip() # Strip ```fences``` (with or without language tag) text = re.sub(r"^```[a-zA-Z]*\s*\n?", "", text) text = re.sub(r"\n?```\s*$", "", text) text = text.strip() s, e = text.find("{"), text.rfind("}") if s == -1 or e == -1 or e <= s: # No JSON object found β€” wrap whatever we got so caller can decide return text c = text[s:e+1] # Repair common LLM JSON mistakes c = re.sub(r",\s*}", "}", c) # trailing comma in object c = re.sub(r",\s*]", "]", c) # trailing comma in array c = re.sub(r"}\s*}(\s*)$", r"}\1", c) # double-closing brace # Strip control chars / smart quotes c = c.replace("\u201c", '"').replace("\u201d", '"') c = re.sub(r"[\x00-\x1f\x7f]", " ", c) return c async def _groq(system: str, user: str, max_tokens: int = 800) -> str: resp = await groq_client.chat.completions.create( model=GROQ_MODEL, messages=[{"role": "system", "content": system}, {"role": "user", "content": user}], max_tokens=max_tokens, temperature=0.1, ) raw = resp.choices[0].message.content.strip() if not raw: raise RuntimeError("Groq returned empty response") return _extract_json(raw) # ────────────────────────────────────────────────────────────────────────────── # 7. SYSTEM PROMPTS # ────────────────────────────────────────────────────────────────────────────── EXTRACT_SYSTEM = ( 'You are a clinical symptom extractor for a women\'s menstrual health app.\n' 'The patient may write in Hindi, English, or Hinglish.\n' 'Rule-based extraction already found some symptoms.\n\n' 'Your job:\n' '1. Add SYMPTOMS ONLY that are EXPLICITLY mentioned or very strongly implied by the patient.\n' ' NEVER infer symptoms the patient did not describe β€” do not add "likely" symptoms.\n' ' NEVER add disease names (not "PCOS", "endometriosis", "migraine" etc.)\n' ' Good: patient said "pain in stomach" β†’ add "lower_abdominal_pain" if rules missed it\n' ' Bad: patient said "irregular periods" β†’ do NOT add "painful_intercourse" or "bloating"\n' ' unless the patient actually described those sensations.\n\n' '2. Ask UP TO 3 questions. IMPORTANT RULES for questions:\n' ' - If symptoms include periods, menstruation, bleeding, cramps, discharge,\n' ' pelvic pain, infertility, or vaginal symptoms, ONE question MUST be about\n' ' cycle details (length, regularity, last period date).\n' ' - For purely general symptoms (gas, nausea, headache, fever, chest pain etc.)\n' ' with NO menstrual symptoms, do NOT ask cycle questions and set\n' ' reproductive_context to null.\n' ' - Second question (optional) covers the most likely missing symptom cluster.\n' ' - Always add "(you can say I don\'t know or skip)" to each question.\n' ' - NEVER ask about both cycle length and last period in separate questions β€”\n' ' combine them: "How long is your usual cycle and when did your last period start? (you can say I don\'t know or skip)"\n' ' - If the patient\'s message already contains cycle length or last period date,\n' ' do NOT ask about it again. Skip the cycle question entirely.\n' ' - Cycle data is already present if the message contains phrases like:\n' ' "last period X days ago", "X day cycle", "period started X", "my cycle is X".\n' ' - If cycle_data_already_provided is true in the input JSON, do NOT ask\n' ' any question about cycle length, last period, or menstrual dates.\n' ' The system already has this information.\n' ' - Do NOT ask about symptoms already in the found list.\n' ' - Always ask questions in English only, regardless of patient language.\n\n' 'Respond ONLY with compact JSON on one line, no markdown:\n' '{"additional_symptoms":["pelvic_pain","hair_thinning"],"reproductive_context":"menstrual",' '"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)"},{"id":"q2","text":"Which of these do you have: acne, oily skin, or facial hair growth? (list all that apply, or say none)"}]}' ) SYNTHESIZE_SYSTEM = ( 'You are a clinical routing engine for a women\'s menstrual health app.\n' 'Given patient message, extracted symptoms, and follow-up answers:\n\n' '1. Produce a CLEAN final symptom list (snake_case clinical terms only):\n' ' - CRITICAL: final_symptoms MUST contain ALL symptoms from all_symptoms.\n' ' Do NOT drop any symptom. Do NOT filter by track type.\n' ' Both menstrual AND disease symptoms must appear together in final_symptoms.\n' ' Example: [fever, vomiting, unpredictable_menstruation] β†’ keep ALL THREE.\n' ' - STRIP disease names only (endometriosis, pcos, migraine, adenomyosis etc.)\n' ' - Normalize variants but keep the symptom:\n' ' dysmenorrheaβ†’painful_menstruation, menorrhagiaβ†’heavy_menstrual_flow\n' ' blood clots in periods β†’ blood_clots_during_menstrual_periods\n' ' heavy_bleeding β†’ heavy_menstrual_flow\n' ' acne β†’ acne_or_pimples\n' ' heavy_bleeding β†’ heavy_menstrual_flow\n' ' facial hair growth β†’ hirsutism\n' ' oily skin β†’ oily_skin\n' ' hair thinning β†’ hair_thinning\n' ' - Extract symptoms from followup_answers and ADD them to the list.\n' ' - Scan followup_answers values for symptom descriptions and extract them.\n' ' e.g. "big clots in periods" β†’ blood_clots_during_menstrual_periods\n' ' "breast tenderness, mood swings, fatigue" β†’ add all three\n' ' "yes pain and difficulty" β†’ lower_abdominal_pain, infertility\n' ' - NEVER add symptoms the patient did not report. Only normalize and deduplicate.\n' ' - Do NOT infer symptoms from the disease name or from PCOS association.\n' ' e.g. if patient has "unpredictable_menstruation" do NOT add "lower_abdominal_pain"\n' ' unless the patient actually described abdominal pain.\n' ' - NEVER return an empty list β€” if unsure, return all_symptoms unchanged.\n\n' '2. Route tracks strictly:\n' ' track_disease: true ONLY if patient has clear non-reproductive symptoms\n' ' such as fever, diarrhea, vomiting, chest pain, headache, rash, or\n' ' symptoms of infection. IMPORTANT: lower abdominal pain or pelvic pain\n' ' paired with menstrual symptoms is NOT grounds for track_disease=true.\n' ' Gastroenteritis, peptic ulcer, migraine must NOT be selected unless\n' ' their characteristic symptoms (diarrhea/vomiting, burning upper stomach,\n' ' throbbing headache with aura) are explicitly reported.\n' ' Set to FALSE if the primary complaint is period pain, cramps, or cycle issues.\n' ' track_pcos: true for irregular periods, acne, weight gain, hair loss, facial hair.\n' ' track_conception: true ONLY if patient mentions trying to conceive.\n' ' track_fertility_peak: true ONLY if patient asks about fertile window/ovulation.\n\n' '3. Extract cycle_data:\n' ' cycle_length_days: integer or null\n' ' last_period_days_ago: integer or null (convert "a week ago"β†’7, "2 weeks"β†’14, "1 month ago"β†’30)\n' ' cycle_regularity: "regular"|"irregular"|"very_irregular"|null\n\n' 'Respond ONLY with compact JSON on one line:\n' '{"final_symptoms":["unpredictable_menstruation","heavy_menstrual_flow","pelvic_pain"],' '"tracks":{"track_disease":false,"track_pcos":true,"track_conception":false,"track_fertility_peak":false},' '"cycle_data":{"cycle_length_days":null,"last_period_days_ago":null,"cycle_regularity":"irregular"},' '"clinical_notes":null}\n\n' 'IMPORTANT: distinguish between:\n' ' cycle length = days between period start dates (typically 21–45)\n' ' period duration = how many days bleeding lasts (typically 3–7)\n' ' "lasts 3 days" or "3 days of bleeding" β†’ period_duration, NOT cycle_length_days\n' ' "every 28 days" or "28 day cycle" β†’ cycle_length_days\n' ) VERIFY_SYSTEM = ( 'You are a clinical verification engine for a women\'s health app.\n' 'Given confirmed patient symptoms and ranked disease candidates from TF-IDF+SBERT retrieval.\n\n' 'Pick the most clinically appropriate disease FROM THE CANDIDATE LIST ONLY.\n' '- Must appear VERBATIM in the candidate list.\n' '- Check which candidate shares the most confirmed symptoms.\n' '- Do NOT default to rank 1 β€” lower-ranked may fit better.\n' '- Name exactly 2 symptoms supporting your choice.\n' '- If no candidate fits, pick the least-wrong and flag low confidence.\n\n' 'Respond ONLY with compact JSON on one line:\n' '{"final_condition":"exact name from list","reasoning":"symptom_a and symptom_b point to this","confidence":"high|medium|low"}' ) CYCLE_DATA_SYSTEM = ( 'Extract menstrual cycle data from patient text. Convert natural language to numbers.\n' 'Conversion examples:\n' ' "a week ago" β†’ last_period_days_ago: 7\n' ' "last one was 4 days back" β†’ last_period_days_ago: 4\n' ' "last one was X days back" β†’ last_period_days_ago: X\n' ' "2 weeks back" β†’ last_period_days_ago: 14\n' ' "1 month ago" β†’ last_period_days_ago: 30\n' ' "2 months ago" β†’ last_period_days_ago: 60\n' ' "4 week cycle" β†’ cycle_length_days: 28\n' ' "35-40 days" β†’ cycle_length_days: 37\n' ' "2-3 month gap" β†’ cycle_length_days: 75, cycle_regularity: very_irregular\n' ' "4 months gap" β†’ cycle_length_days: 120, cycle_regularity: very_irregular\n' ' "skips months" β†’ cycle_regularity: very_irregular\n' ' "somewhat irregular" β†’ cycle_regularity: irregular\n' ' "fairly regular" β†’ cycle_regularity: regular\n' ' "2-4 months gap" β†’ cycle_length_days: 75, cycle_regularity: very_irregular\n' 'IMPORTANT: distinguish between:\n' ' cycle length = days BETWEEN period start dates (typically 21-45, but may be longer)\n' ' period duration = how many days bleeding lasts (typically 3-7)\n' ' "lasts 3 days", "3 days of bleeding" β†’ period_duration only, NOT cycle_length_days\n' ' "every 28 days", "28 day cycle", "gap of 2 months" β†’ cycle_length_days\n' 'Return null for anything not mentioned β€” never guess defaults.\n' 'trying_to_conceive: true only if patient mentions trying to get pregnant/conceive.\n' 'Respond ONLY with compact JSON on one line:\n' '{"cycle_length_days":null,"last_period_days_ago":null,"cycle_regularity":null,"trying_to_conceive":false}' ) SUMMARY_SYSTEM = ( 'You are ShakTide\'s compassionate women\'s health AI for Indian patients.\n' 'Given a patient\'s symptoms and diagnostic results, write a SHORT plain-language summary.\n\n' 'ORDERING RULES β€” very important:\n' '1. If the main concern is disease/illness (fever, vomiting, infections, chest pain etc.):\n' ' β†’ Lead with the likely disease, THEN mention any cycle/PCOS observations after.\n' '2. If the main concern is reproductive/menstrual (periods, PCOS, fertility):\n' ' β†’ Lead with PCOS/cycle observations, THEN mention any general illness if present.\n' '3. If both are equally present, use clinical judgment β€” mention the more urgent one first.\n\n' 'LANGUAGE RULES:\n' '- Never say specific percentages like "74.5% chance".\n' '- Use: "may suggest", "could indicate", "worth checking", "signs point to", "possible".\n' '- For PCOS: "strong indicators", "some signs", "a few signs", "unlikely".\n' '- For fertility: "may be in your fertile window", "likely outside fertile window".\n' '- For disease: name the top 1-2 conditions and say what they suggest.\n' '- If menstrual symptoms present but no PCOS track ran, still mention they should\n' ' discuss cycle irregularities with a doctor.\n' '- Keep it under 5 sentences. Warm, non-alarmist tone.\n' '- End with: "Please be sure to see a doctor for a proper evaluation."\n' '- Do NOT mention menstrual cycle or PCOS unless:\n' ' (a) menstrual/reproductive symptoms are in patient_symptoms, OR\n' ' (b) patient_opted_in_to_menstrual_discussion is true.\n' '- If patient_opted_in_to_menstrual_discussion is true and cycle_data is present,\n' ' mention cycle health briefly AFTER the disease summary β€” e.g. "Regarding your\n' ' cycle, based on what you shared, it may be worth discussing with a doctor."\n' '- If cycle_regularity is very_irregular or cycle_length_days > 35, note it gently.\n' '- Keep total response under 5 sentences.\n' '- If pcos_is_primary_concern is true, lead with PCOS findings first,\n' ' then mention disease results briefly after.\n' '- If menstrual_symptoms_present is true, ALWAYS mention the menstrual\n' ' symptoms even if disease is the primary concern. e.g. "You also mentioned\n' ' irregular periods and blood clots β€” worth discussing with a doctor."\n' '- Respond with PLAIN TEXT only β€” no JSON, no bullet points, no markdown. Do not use any Hindi or Hinglish words.' ) # ────────────────────────────────────────────────────────────────────────────── # 8. LLM CALLS # ────────────────────────────────────────────────────────────────────────────── GENERAL_FALLBACK_QUESTIONS = [ {"id": "q_dur", "text": "How long have you had this β€” less than a day, a few days, or more than a week? (you can say I don't know or skip)"}, {"id": "q_assoc", "text": "Do you also have any of these: nausea, vomiting, fever, chills, body ache, or weakness? (list all that apply, or say none)"}, {"id": "q_sev", "text": "How severe is it β€” mild, moderate, or severe enough to affect daily activities? (you can say I don't know or skip)"}, ] async def llm_extract_and_question(user_query: str, rule_symptoms: list, cycle_already_known: bool = False) -> dict: content = json.dumps({ "patient_message": user_query, "already_found": rule_symptoms, "symptoms_found_count": len(rule_symptoms), "need_total": MIN_SYMPTOMS, "cycle_data_already_provided": cycle_already_known, }) try: parsed = json.loads(await _groq(EXTRACT_SYSTEM, content, 700)) except Exception as e: print(f"[GROQ FAIL] llm_extract_and_question: {e}") parsed = {"additional_symptoms": [], "reproductive_context": None, "questions": []} # Sanitize additional_symptoms β€” drop disease names / empties bad_tokens = {"pcos", "pcod", "endometriosis", "adenomyosis", "migraine", "thyroid", "diabetes", "anemia", "dengue", "malaria", "typhoid"} parsed["additional_symptoms"] = [ s for s in parsed.get("additional_symptoms", []) if isinstance(s, str) and s.strip() and s.strip().lower() not in bad_tokens ] parsed["questions"] = parsed.get("questions", []) or [] # MANDATORY: if we still need more symptoms, ensure >= 2 questions total_known = len(set(rule_symptoms) | set(parsed["additional_symptoms"])) if total_known < MIN_SYMPTOMS and len(parsed["questions"]) < 2: existing_ids = {q.get("id", "") for q in parsed["questions"]} existing_text = " ".join(q.get("text", "").lower() for q in parsed["questions"]) for fb in GENERAL_FALLBACK_QUESTIONS: if len(parsed["questions"]) >= 2: break if fb["id"] in existing_ids: continue # Don't duplicate a fallback whose topic is already covered topic = fb["text"].split("?")[0].lower()[:25] if topic in existing_text: continue parsed["questions"].append(fb) existing_text += " " + fb["text"].lower() # Absolute last-resort: still zero questions and we need more info if total_known < MIN_SYMPTOMS and not parsed["questions"]: parsed["questions"] = GENERAL_FALLBACK_QUESTIONS[:2] return parsed async def llm_synthesize_context(user_query: str, all_symptoms: list, answers: dict) -> dict: content = json.dumps({"original_query": user_query, "all_symptoms": all_symptoms, "followup_answers": answers}, indent=2) try: parsed = json.loads(await _groq(SYNTHESIZE_SYSTEM, content, 700)) if "cycle_data" not in parsed: parsed["cycle_data"] = {} return parsed except Exception as e: print(f"[GROQ FAIL] llm_synthesize_context: {e}") _cycle = any(k in " ".join(all_symptoms) for k in [ "menstruat", "period", "irregular", "unpredictable", "pcos", "pcod", "infertil", "heavy_menstrual", "hirsut", "acne_or_pimples", "hair_thin", "weight_gain", "oily_skin", "painful_intercourse", ]) _fi = any(k in user_query.lower() for k in ["pregnant", "conceive", "fertility", "fertile", "bachha", "garbh", "not getting pregnant", "cant get pregnant"]) _purely_repro = (all(any(t in s for t in REPRO_TERMS) for s in all_symptoms) if all_symptoms else False) return { "final_symptoms": all_symptoms, "tracks": {"track_disease": bool(all_symptoms) and not _fi and not _purely_repro, "track_pcos": _cycle, "track_conception": _fi, "track_fertility_peak": _fi}, "cycle_data": {}, "clinical_notes": None, } async def llm_extract_cycle_data(text: str) -> dict: try: return json.loads(await _groq(CYCLE_DATA_SYSTEM, text, 150)) except Exception as e: print(f"[GROQ FAIL] llm_extract_cycle_data: {e}") return {"cycle_length_days": None, "last_period_days_ago": None, "cycle_regularity": None, "trying_to_conceive": False} async def llm_verify_disease(symptoms: list, candidates: list) -> dict: if not candidates: return {"final_condition": "", "reasoning": "", "confidence": "low"} content = json.dumps({ "confirmed_symptoms": [s.replace("_", " ") for s in symptoms], "candidates": [{"rank": i+1, "disease": n, "score": f"{s:.3f}"} for i, (n, s) in enumerate(candidates[:8])], }, indent=2) try: return json.loads(await _groq(VERIFY_SYSTEM, content, 250)) except Exception as e: print(f"[GROQ FAIL] llm_verify_disease: {e}") return {"final_condition": candidates[0][0] if candidates else "", "reasoning": "", "confidence": "low"} async def _groq_reassess_disease(final_symptoms: list, disease_list: list) -> list: """ When model results are unclear, use Groq to reassess and pick best match or suggest it's a common illness. """ content = json.dumps({ "reported_symptoms": [s.replace("_", " ") for s in final_symptoms], "model_candidates": [ { "rank": r.get("rank"), "disease": r.get("prediction"), "score": r.get("conf_raw") } for r in disease_list[:5] ] }) try: result = await _groq( 'You are a clinical decision assistant. Patient reported certain symptoms and the model returned candidates. ' 'Decide: is it one of the candidates, or is it likely a common illness (cold/viral fever/flu)?\n' 'Respond with JSON: {"best_match": "disease name or common cold", "confidence": "high|medium|low", ' '"reasoning": "brief reason"}', content, 200 ) return json.loads(result) except Exception as e: print(f"[GROQ FAIL] disease reassessment: {e}") return {"best_match": "Needs medical evaluation", "confidence": "low"} async def llm_verify_symptom_sufficiency(symptoms: list, answered_questions: dict) -> dict: no_count = sum(1 for v in answered_questions.values() if str(v).lower().strip() in ("no", "none", "i don't know")) if len(symptoms) < 4 or no_count > 2: content = json.dumps({ "symptoms": [s.replace("_", " ") for s in symptoms], "no_answers_count": no_count, "total_questions": len(answered_questions), }) try: result = await _groq( 'You are a clinical triage assistant. Given sparse symptoms, determine if this warrants disease diagnosis or if it\'s likely just common cold/viral fever.\n' 'Respond with JSON: {"is_diagnosable": true/false, "likely_condition": "common cold|viral fever|needs more info", "confidence": "high|medium|low"}', content, 200 ) return json.loads(result) except Exception as e: print(f"[GROQ FAIL] symptom sufficiency: {e}") return {"is_diagnosable": False, "likely_condition": "viral fever", "confidence": "low"} return {"is_diagnosable": True} async def llm_summarize(symptoms: list, results: dict, cycle_data: dict, user_query: str) -> str: results_text = [] if "Disease" in results and results["Disease"]: top = results["Disease"][0] results_text.append(f"Top disease: {top['prediction']} (score {top['conf_raw']:.2f})") others = [r["prediction"] for r in results["Disease"][1:3]] if others: results_text.append(f"Others: {', '.join(others)}") if "PCOS" in results: p = results["PCOS"] results_text.append(f"PCOS: {p['prediction']} β€” {p.get('confidence','')}") if "Conception" in results: results_text.append(f"Conception: {results['Conception']['prediction']}") if "Fertility" in results: results_text.append(f"Fertile window: {results['Fertility']['prediction']}") repro_count = sum(1 for s in symptoms if any(t in s for t in REPRO_TERMS)) disease_prime = "Disease" in results and repro_count < len(symptoms) / 2 pcos_raw = results.get("PCOS", {}).get("conf_raw", 0.0) pcos_prime = pcos_raw >= 0.65 and repro_count >= len(symptoms) / 2 content = json.dumps({ "patient_symptoms": [s.replace("_", " ") for s in symptoms], "original_query": user_query, "diagnostic_results": results_text, "menstrual_symptoms_present": repro_count > 0, "disease_is_primary": disease_prime and not pcos_prime, "pcos_is_primary": pcos_prime, "cycle_data": cycle_data, }, indent=2) try: return await _groq(SUMMARY_SYSTEM, content, 350) except Exception as e: print(f"[GROQ FAIL] llm_summarize: {e}") parts = [] if "PCOS" in results: parts.append(f"There are {results['PCOS']['prediction']}. {results['PCOS'].get('confidence','')}") if "Disease" in results and results["Disease"]: parts.append(f"As a general illness, {results['Disease'][0]['prediction']} is possible.") if "Fertility" in results: parts.append(results["Fertility"].get("confidence", "")) if not parts: parts.append("Your symptoms have been evaluated.") parts.append("Please be sure to see a doctor for a proper evaluation.") return " ".join(p for p in parts if p) # ────────────────────────────────────────────────────────────────────────────── # 9. DIAGNOSTIC TRACKS # ────────────────────────────────────────────────────────────────────────────── def _score_to_label(score: float, tiers: list) -> str: for thresh, label in sorted(tiers, key=lambda x: x[0], reverse=True): if score >= thresh: return label return tiers[-1][1] def _align_to_model(df: pd.DataFrame, model) -> pd.DataFrame: expected = [] if hasattr(model, "feature_names_in_"): expected = list(model.feature_names_in_) elif hasattr(model, "feature_names_"): expected = list(model.feature_names_) if not expected: return df for col in expected: if col not in df.columns: df[col] = 0.0 return df[expected] def run_disease_track(final_symptoms: list, top_k: int = 5) -> list: results = [] if NLP_AVAILABLE: sym_text = "patient has " + ", ".join(s.replace("_", " ") for s in final_symptoms) tfidf_res = nlp_stack["tfidf"].query(sym_text, top_k=20) sbert_res = nlp_stack["semantic"].query(sym_text, top_k=20) tfidf_map = dict(zip(tfidf_res["disease"].str.lower(), tfidf_res["score"])) sbert_map = dict(zip(sbert_res["disease"].str.lower(), sbert_res["score"])) max_t = max(tfidf_map.values(), default=1.0) tfidf_norm = {k: v / max_t for k, v in tfidf_map.items()} if max_t > 0 else tfidf_map ensemble = [] for disease in set(tfidf_map) | set(sbert_map): if _is_blocked(disease, final_symptoms): continue t = tfidf_norm.get(disease, 0.0) s = sbert_map.get(disease, 0.0) combined = 0.6 * s + 0.4 * t if s == 0.0 or t == 0.0: combined = min(combined, 0.35) ensemble.append((disease, s, t, combined)) ensemble.sort(key=lambda x: x[3], reverse=True) TIERS = [(0.70, "High likelihood"), (0.50, "Moderate likelihood"), (0.30, "Possible"), (0.0, "Low likelihood")] results = [ {"rank": i+1, "prediction": e[0], "confidence": _score_to_label(e[3], TIERS), "conf_raw": round(e[3], 4), "sbert": f"{e[1]:.3f}", "tfidf": f"{e[2]:.3f}"} for i, e in enumerate(ensemble[:top_k]) ] elif disease_model is not None and disease_encoder is not None: features_expected = [] if hasattr(symptom_scaler, "feature_names_in_"): features_expected = list(symptom_scaler.feature_names_in_) elif hasattr(disease_model, "feature_names_in_"): features_expected = list(disease_model.feature_names_in_) if not features_expected: features_expected = [s.lower().replace(" ", "_") for s in final_symptoms] input_df = pd.DataFrame(0.0, index=[0], columns=features_expected) for s in final_symptoms: s_clean = s.lower().strip().replace(" ", "_") if s_clean in input_df.columns: input_df.at[0, s_clean] = 1.0 try: arr = symptom_scaler.transform(input_df) if symptom_scaler else input_df.values probs = disease_model.predict_proba(arr)[0] top_i = probs.argsort()[::-1][:top_k] TIERS = [(0.70, "High likelihood"), (0.50, "Moderate likelihood"), (0.30, "Possible"), (0.0, "Low likelihood")] for rank, idx in enumerate(top_i, 1): name = disease_encoder.inverse_transform([idx])[0] conf = float(probs[idx]) if conf < 0.05 or _is_blocked(name, final_symptoms): continue results.append({"rank": rank, "prediction": name, "confidence": _score_to_label(conf, TIERS), "conf_raw": round(conf, 4)}) except Exception as e: print(f"[ERROR] disease ML fallback: {e}") return results def run_pcos_track(final_symptoms: list) -> dict: # Define fs FIRST β€” everything below depends on it fs = [s.lower().replace(" ", "_") for s in final_symptoms] # Dysmenorrhea check: period pain with no PCOS core indicators β†’ surface primary dysmenorrhea DYSMENORRHEA_SYMPTOMS = {"painful_menstruation", "lower_abdominal_pain", "pelvic_pain", "back_pain"} dysmen_hits = sum(1 for s in fs if s in DYSMENORRHEA_SYMPTOMS) pcos_core_hits = sum(1 for s in fs if s in PCOS_CORE) if dysmen_hits >= 1 and pcos_core_hits == 0: return { "prediction": "Primary Dysmenorrhea (Period Pain)", "confidence": "Painful periods without other hormonal symptoms most commonly indicate primary dysmenorrhea. A gynaecologist can rule out endometriosis or adenomyosis.", "conf_raw": 0.7, "is_dysmenorrhea": True, } # Early exit: not enough signals to suggest PCOS score = sum(PCOS_INDICATORS.get(s, 0) for s in fs) if pcos_core_hits < 2 and score < 5: return { "prediction": "PCOS β€” Unlikely", "confidence": "Few or no PCOS indicators detected based on reported symptoms.", "conf_raw": round(min(score / 12.0, 0.95), 4), } # Full PCOS scoring raw_text = " ".join(fs).lower() matched_fuzzy = set() for kw, w in PCOS_FUZZY.items(): if kw in raw_text and kw not in matched_fuzzy: score += w matched_fuzzy.add(kw) core_hits = sum(1 for s in fs if s in PCOS_CORE) if core_hits >= 3 and score < 8: score = 8 elif core_hits >= 2 and score < 5: score = 5 pcos_prob = min(score / 12.0, 0.95) TIERS = [ (0.65, "Strong indicators", "Strong PCOS indicators present. Clinical evaluation strongly recommended."), (0.45, "Moderate indicators", "Some PCOS indicators present. Worth discussing with a doctor."), (0.30, "Mild indicators", "A few mild indicators. Monitor symptoms and consult if they persist."), (0.0, "Unlikely", "Few or no PCOS indicators detected based on reported symptoms."), ] tier_label, tier_note = next( ((label, note) for thresh, label, note in TIERS if pcos_prob >= thresh), ("Unlikely", "Few or no PCOS indicators detected based on reported symptoms.") ) return {"prediction": f"PCOS β€” {tier_label}", "confidence": tier_note, "conf_raw": round(pcos_prob, 4)} def run_conception_track(cycle_data: dict) -> dict: days_ago = cycle_data.get("last_period_days_ago") cycle_len = cycle_data.get("cycle_length_days") if not days_ago: return {"prediction": "Need last period date for an estimate", "confidence": "β€”", "conf_raw": 0.0} days_ago = int(days_ago) cycle_len = int(cycle_len) if cycle_len else 28 if cycle_model is None: return {"prediction": "Conception model unavailable", "confidence": "β€”", "conf_raw": 0.0} try: CYCLE_COLS = ["cycle_length", "patient_mean_cycle", "patient_std_cycle", "patient_median_cycle", "patient_cycle_count", "cycle_length_dev", "is_irregular", "regularity_score", "prev_cycle_length", "cycle_length_delta", "new_id", "age", "cycle_number", "cycle_month", "cycle_dayofweek"] row = pd.Series({c: 0.0 for c in CYCLE_COLS}) row["cycle_length"] = float(cycle_len) row["patient_mean_cycle"] = float(cycle_len) row["cycle_length_dev"] = 0.0 regularity = cycle_data.get("cycle_regularity", "") if regularity in ("irregular", "very_irregular"): row["is_irregular"] = 1.0 row["regularity_score"] = 0.2 row["patient_std_cycle"] = 9.0 else: row["is_irregular"] = 0.0 row["regularity_score"] = 0.9 row["patient_std_cycle"] = 1.5 X = pd.DataFrame([row]) if cycle_scaler: try: X = pd.DataFrame(cycle_scaler.transform(X), columns=X.columns) except Exception: pass X = _align_to_model(X, cycle_model) proba = float(cycle_model.predict_proba(X)[0][1]) TIERS = [(0.60, "May be likely this cycle"), (0.35, "Possible this cycle"), (0.0, "Less likely this cycle")] return {"prediction": f"Conception β€” {_score_to_label(proba, TIERS)}", "confidence": "Model estimate β€” discuss with a doctor", "conf_raw": round(proba, 4)} except Exception as e: print(f"[ERROR] conception track: {e}") return {"prediction": "Could not estimate β€” consult a doctor", "confidence": "β€”", "conf_raw": 0.0} def run_fertility_track(cycle_data: dict) -> dict: days_ago = cycle_data.get("last_period_days_ago") cycle_len = cycle_data.get("cycle_length_days") if not days_ago: return {"prediction": "Need last period date for an estimate", "confidence": "β€”", "conf_raw": 0.0} days_ago = int(days_ago) cycle_len = int(cycle_len) if cycle_len else 28 ovulation_day = max(cycle_len - 14, 10) fertile_start = max(ovulation_day - 5, 1) fertile_end = ovulation_day + 1 in_window = fertile_start <= days_ago <= fertile_end days_until = max(fertile_start - days_ago, 0) if in_window: note = (f"You are currently in your fertile window " f"(Day {fertile_start}–{fertile_end} of your cycle).") else: note = (f"You are currently outside your fertile window. " f"Next window: Day {fertile_start}–{fertile_end} " f"of your {cycle_len}-day cycle" + (f" (in about {days_until} days)." if days_until > 0 else ".")) fert_prob = 0.8 if in_window else 0.2 if fert_model is not None: try: FERT_COLS = ["lengthofcycle", "cycle_rank_pct"] row = pd.Series({c: 0.0 for c in FERT_COLS}) row["lengthofcycle"] = float(cycle_len) row["cycle_rank_pct"] = days_ago / max(cycle_len, 1) X = pd.DataFrame([row]) if fert_scaler: try: X = pd.DataFrame(fert_scaler.transform(X), columns=X.columns) except Exception: pass X = _align_to_model(X, fert_model) fert_prob = float(fert_model.predict_proba(X)[0][1]) except Exception as e: print(f"[ERROR] fertility model: {e}") label = "Likely in your fertile window" if in_window else "Likely outside your fertile window" return {"prediction": label, "confidence": note, "conf_raw": round(fert_prob, 4)} async def run_all_tracks(final_symptoms: list, active: dict, cycle_data: dict, answered_qs: dict = None, top_k: int = 5) -> dict: results = {} if active.get("track_disease") and final_symptoms: non_repro_symptoms = [ s for s in final_symptoms if not any(t in s for t in REPRO_TERMS) ] if non_repro_symptoms: disease_list = run_disease_track(final_symptoms, top_k) # FALLBACK: If disease_list is empty or low confidence, use Groq if not disease_list or (disease_list and disease_list[0].get("conf_raw", 0) < 0.35): if answered_qs: sufficiency = await llm_verify_symptom_sufficiency(final_symptoms, answered_qs) if not sufficiency.get("is_diagnosable"): results["Disease"] = [{ "prediction": sufficiency.get("likely_condition", "Common viral fever"), "confidence": "Low confidence β€” likely benign. Monitor symptoms.", "conf_raw": 0.3 }] active["track_disease"] = False else: # Still diagnosable, use Groq to reassess await _groq_reassess_disease(final_symptoms, disease_list) else: # No answered questions, just flag low confidence if disease_list: results["Disease"] = disease_list else: # High confidence result candidates = [(r["prediction"], r["conf_raw"]) for r in disease_list[:8]] verification = await llm_verify_disease(final_symptoms, candidates) verified = verification.get("final_condition", "").strip().lower() if verified: v_entry = next((r for r in disease_list if r["prediction"].lower() == verified), None) rest = [r for r in disease_list if r["prediction"].lower() != verified] if v_entry: if rest and v_entry["conf_raw"] < rest[0]["conf_raw"]: v_entry = dict(v_entry) v_entry["conf_raw"] = rest[0]["conf_raw"] + 0.01 disease_list = [v_entry] + rest results["Disease"] = disease_list if active.get("track_pcos"): results["PCOS"] = run_pcos_track(final_symptoms) if active.get("track_conception"): results["Conception"] = run_conception_track(cycle_data) if active.get("track_fertility_peak"): results["Fertility"] = run_fertility_track(cycle_data) return results # ────────────────────────────────────────────────────────────────────────────── # 10. SECURITY HELPERS # ────────────────────────────────────────────────────────────────────────────── def get_real_ip(request: Request) -> str: fwd = request.headers.get("x-forwarded-for") return fwd.split(",")[0].strip() if fwd else request.client.host def scan_aup(message: str) -> bool: for p in AUP_PATTERNS: if re.search(p, message.lower(), re.IGNORECASE): return True return False async def enforce_aup(client_ip: str, message: str): if not scan_aup(message): return if supabase: now = datetime.datetime.now(datetime.timezone.utc) block_until = (now + datetime.timedelta(hours=24)).isoformat() try: db = supabase.table("flagged_ips").select("*").eq("ip", client_ip).execute() cnt = (db.data[0].get("violation_count", 0) + 1) if db.data else 1 perm = cnt >= 3 if db.data: supabase.table("flagged_ips").update({ "violation_count": cnt, "permanently_banned": perm, "blocked_until": None if perm else block_until, }).eq("ip", client_ip).execute() else: supabase.table("flagged_ips").insert({ "ip": client_ip, "violation_count": 1, "permanently_banned": False, "blocked_until": block_until, }).execute() except Exception as e: print(f"[WARN] AUP supabase write failed: {e}") raise HTTPException(status_code=403, detail={"status": "violation", "message": "Message blocked."}) # ────────────────────────────────────────────────────────────────────────────── # 11. REQUEST SCHEMA # ────────────────────────────────────────────────────────────────────────────── class ChatRequest(BaseModel): message: str accumulated_symptoms: List[str] = Field(default_factory=list) accumulated_negated: List[str] = Field(default_factory=list) cycle_data: Dict[str, Any] = Field(default_factory=dict) pending_questions: List[dict] = Field(default_factory=list) all_questions: List[dict] = Field(default_factory=list) answered_ids: List[str] = Field(default_factory=list) conversation_turn: int = 0 original_query: str = "" chat_history: List[dict] = Field(default_factory=list) opted_in_menstrual: bool = False declined_menstrual: bool = False class Config: populate_by_name = True def _session(req: ChatRequest, **overrides) -> dict: base = { "accumulated_symptoms": req.accumulated_symptoms, "accumulated_negated": req.accumulated_negated, "cycle_data": req.cycle_data, "pending_questions": req.pending_questions, "all_questions": req.all_questions, "answered_ids": req.answered_ids, "conversation_turn": req.conversation_turn, "original_query": req.original_query, "opted_in_menstrual": req.opted_in_menstrual, "declined_menstrual": req.declined_menstrual, "chat_history": req.chat_history, } base.update(overrides) return base # ────────────────────────────────────────────────────────────────────────────── # 12. TOKENS ENDPOINT # ────────────────────────────────────────────────────────────────────────────── @app.get("/api/tokens") async def get_tokens(request: Request): client_ip = get_real_ip(request) today = datetime.date.today().isoformat() if not supabase: return {"tokens_left": DAILY_TOKEN_LIMIT} try: db = supabase.table("ai_teaser_usage").select("*").eq("ip_address", client_ip).execute() if db.data: rec = db.data[0] if rec["last_reset_date"] != today: return {"tokens_left": DAILY_TOKEN_LIMIT} return {"tokens_left": max(0, DAILY_TOKEN_LIMIT - rec["usage_count"])} except Exception: pass return {"tokens_left": DAILY_TOKEN_LIMIT} # ────────────────────────────────────────────────────────────────────────────── # 13. MAIN CHAT ENDPOINT # ────────────────────────────────────────────────────────────────────────────── @app.post("/api/chat") async def process_chat(query: ChatRequest, request: Request): # Guards if len(query.message) > 600: return {"status": "success", "message_type": "question", "message": "A little shorter, just the main symptoms, pleaseπŸ™", "session": _session(query)} client_ip = get_real_ip(request) await enforce_aup(client_ip, query.message) # Daily limit today = datetime.date.today().isoformat() if supabase: try: db = supabase.table("ai_teaser_usage").select("*").eq("ip_address", client_ip).execute() if db.data: rec = db.data[0] if rec["last_reset_date"] != today: supabase.table("ai_teaser_usage").update( {"usage_count": 1, "last_reset_date": today} ).eq("ip_address", client_ip).execute() elif rec["usage_count"] >= DAILY_TOKEN_LIMIT: return {"status": "limit_reached", "message": "Today's limit reached, try again tomorrow!πŸ™"} else: supabase.table("ai_teaser_usage").update( {"usage_count": rec["usage_count"] + 1} ).eq("ip_address", client_ip).execute() else: supabase.table("ai_teaser_usage").insert( {"ip_address": client_ip, "usage_count": 1, "last_reset_date": today} ).execute() except Exception: pass # Crisis intercept if any(kw in query.message.lower() for kw in CRISIS_KEYWORDS): return {"status": "success", "message_type": "crisis", "message": "🚨 This feels like an emergency! Call 112, or get immediate assistance."} # Greeting on turn 0 if query.conversation_turn == 0 and not query.accumulated_symptoms: stripped = query.message.strip().lower() if stripped in ("hi", "hello", "namaste", "haan", "ok", "hey", "help", "start",'heya','sup','wassup','hiii'): return { "status": "success", "message_type": "question", "message": "Hello! What symptoms are you feeling right now?", "session": _session(query, conversation_turn=1), } # ────────────────────────────────────────────────────────────────────────── # TURN: answering a pending question # ────────────────────────────────────────────────────────────────────────── answered_ids = list(query.answered_ids) answers = {} if query.pending_questions and query.conversation_turn > 0: pending_q = query.pending_questions[0] qid = pending_q["id"] answers[qid] = query.message answers = expand_yes_all_answers(answers, [pending_q]) answered_ids.append(qid) ans_syms = rule_extract_symptoms(query.message) ans_extracted = await llm_extract_and_question(query.message, ans_syms, cycle_already_known=bool(query.cycle_data.get("last_period_days_ago"))) ans_llm_syms = [s.lower().replace(" ", "_") for s in ans_extracted.get("additional_symptoms", [])] new_syms = list(dict.fromkeys(query.accumulated_symptoms + ans_syms + ans_llm_syms)) # Also extract cycle data from answer _ans_cycle = await llm_extract_cycle_data(query.message) updated_cycle = dict(query.cycle_data) for k, v in _ans_cycle.items(): if v is not None and not updated_cycle.get(k): updated_cycle[k] = v remaining = [q for q in query.all_questions if q["id"] not in answered_ids] if remaining: next_q = remaining[0] return { "status": "success", "message_type": "question", "message": next_q["text"], "session": _session( query, accumulated_symptoms=new_syms, cycle_data=updated_cycle, pending_questions=[next_q], answered_ids=answered_ids, conversation_turn=query.conversation_turn + 1, ), } # All questions answered β€” rebuild for diagnosis query = ChatRequest( message=query.original_query, accumulated_symptoms=new_syms, cycle_data=updated_cycle, pending_questions=[], all_questions=query.all_questions, answered_ids=answered_ids, conversation_turn=query.conversation_turn + 1, original_query=query.original_query, opted_in_menstrual=query.opted_in_menstrual, declined_menstrual=query.declined_menstrual, ) # ────────────────────────────────────────────────────────────────────────── # EXTRACT symptoms # ────────────────────────────────────────────────────────────────────────── original_query = query.original_query or query.message rule_syms = rule_extract_symptoms(query.message) _early_cycle = await llm_extract_cycle_data(query.message) _cycle_known = bool(_early_cycle.get("last_period_days_ago")) extracted = await llm_extract_and_question( query.message, rule_syms, cycle_already_known=_cycle_known ) llm_extras = [s.lower().replace(" ", "_") for s in extracted.get("additional_symptoms", [])] new_syms = list(dict.fromkeys(rule_syms + llm_extras)) # Cycle irregularity β†’ add unpredictable_menstruation if _early_cycle.get("cycle_regularity") in ("irregular", "very_irregular"): if "unpredictable_menstruation" not in new_syms: new_syms.append("unpredictable_menstruation") all_symptoms = list(dict.fromkeys(query.accumulated_symptoms + new_syms)) all_symptoms = [s for s in all_symptoms if s not in query.accumulated_negated] # has_repro MUST be defined before fallback question block has_repro = any(k in " ".join(all_symptoms) for k in ["menstruat", "period", "pelvic", "vaginal", "infertil", "heavy_menstrual", "painful_menstruat", "unpredictable", "hirsut", "acne_or_pimples", "irregular_appearing_scalp"]) # Fallback questions if Groq failed to return any if not extracted.get("questions") and has_repro and not _cycle_known: extracted["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)"}, ] elif not extracted.get("questions") and has_repro and _cycle_known: extracted["questions"] = [] elif not extracted.get("questions") and not has_repro: extracted["questions"] = [ {"id": "q1", "text": "How long have you had these symptoms, and do you have any fever or chills? (you can say I don't know or skip)"}, ] # Merge cycle data cycle_data = dict(query.cycle_data) for k, v in _early_cycle.items(): if v is not None and not cycle_data.get(k): cycle_data[k] = v if not all_symptoms: return { "status": "success", "message_type": "question", "message": ("Hello! What are the symtoms you'd like to discuss?" if query.conversation_turn == 0 else "Any other symptoms? Let me know!"), "session": _session(query, conversation_turn=query.conversation_turn + 1), } # ────────────────────────────────────────────────────────────────────────── # DECIDE: ask questions OR diagnose # Never diagnose on turn 0 β€” always ask at least one question first # ────────────────────────────────────────────────────────────────────────── # ────────────────────────────────────────────────────────────────────────── # DECIDE: ask questions OR diagnose # Never diagnose on turn 0 β€” always ask at least one question first # ────────────────────────────────────────────────────────────────────────── enough_symptoms = len(all_symptoms) >= MIN_SYMPTOMS all_answered = (len(query.all_questions) > 0 and all(q["id"] in answered_ids for q in query.all_questions)) should_diagnose = ( (query.conversation_turn >= 1) and (enough_symptoms or all_answered or query.conversation_turn >= 2) ) # Build the candidate question list from what the LLM returned questions = list(extracted.get("questions", [])) # Filter out cycle questions if cycle data already known if _cycle_known: questions = [q for q in questions if not any( kw in q.get("text", "").lower() for kw in ["cycle", "period", "last period", "when did", "flow", "bleeding"] )] # Filter out already-answered questions questions = [q for q in questions if q["id"] not in answered_ids] # no_count: how many "no/skip" answers were given this turn no_count = sum(1 for v in answers.values() if str(v).lower().strip() in ("no", "none", "i don't know")) if no_count >= 2 and len(all_symptoms) >= 1: should_diagnose = True if not should_diagnose and questions: first_q = questions[0] return { "status": "success", "message_type": "question", "message": first_q["text"], "session": _session( query, accumulated_symptoms=all_symptoms, cycle_data=cycle_data, pending_questions=[first_q], all_questions=questions, answered_ids=answered_ids, conversation_turn=query.conversation_turn + 1, original_query=original_query, ), } if not should_diagnose and not questions: # LLM returned no questions and we haven't hit turn threshold yet β€” # use a safe fallback question rather than silently diagnosing fallback_q = {"id": "q_fallback", "text": ( "Any other symptoms? Since when has this been happening? " "(you can say I don't know or skip)" )} return { "status": "success", "message_type": "question", "message": fallback_q["text"], "session": _session( query, accumulated_symptoms=all_symptoms, cycle_data=cycle_data, pending_questions=[fallback_q], all_questions=[fallback_q], answered_ids=answered_ids, conversation_turn=query.conversation_turn + 1, original_query=original_query, ), } # should_diagnose is True β€” fall through to SYNTHESIZE + ROUTE below # SAFETY NET: should_diagnose was False, meaning we are NOT supposed to # diagnose yet β€” but `questions` ended up empty (e.g. LLM returned none, # or all candidate questions got filtered out as cycle-related because # the cycle was already known). Without this, execution would silently # fall through into the SYNTHESIZE + ROUTE / diagnosis section below, # which is exactly the "no question being asked" bug. fallback_q = {"id": "q_fallback", "text": ( "Any other symptom? Since when has this been happening?" "(you can say I don't know or skip)" )} return { "status": "success", "message_type": "question", "message": fallback_q["text"], "session": _session( query, accumulated_symptoms=all_symptoms, cycle_data=cycle_data, pending_questions=[fallback_q], all_questions=[fallback_q], answered_ids=answered_ids, conversation_turn=query.conversation_turn + 1, original_query=original_query, ), } # ────────────────────────────────────────────────────────────────────────── # SYNTHESIZE + ROUTE # ────────────────────────────────────────────────────────────────────────── if len(all_symptoms) < 2: return { "status": "success", "message_type": "question", "message": "Could you describe at least one more symptom? That'll help me give you a better result.", "session": _session( query, accumulated_symptoms=all_symptoms, cycle_data=cycle_data, conversation_turn=query.conversation_turn + 1, original_query=original_query, ), } # Rebuild answers from all answered question turns full_answers = answers context = await llm_synthesize_context(original_query, all_symptoms, full_answers) raw_final = context.get("final_symptoms", all_symptoms) or all_symptoms final_symptoms = normalize_symptoms(list(dict.fromkeys( SYMPTOM_ALIASES.get(s.lower().replace(" ", "_"), s.lower().replace(" ", "_")) for s in raw_final ))) for s in all_symptoms: s_clean = SYMPTOM_ALIASES.get(s.lower().replace(" ", "_"), s.lower().replace(" ", "_")) if s_clean not in final_symptoms: final_symptoms.append(s_clean) final_symptoms = normalize_symptoms(final_symptoms) # Safety filter: strip anything the user never reported (prevents LLM hallucinations) reported_canonical = set( SYMPTOM_ALIASES.get(s.lower().replace(" ", "_"), s.lower().replace(" ", "_")) for s in all_symptoms ) final_symptoms = [s for s in final_symptoms if s in reported_canonical] if not final_symptoms: final_symptoms = list(reported_canonical) final_symptoms = normalize_symptoms(final_symptoms) for k, v in (context.get("cycle_data") or {}).items(): if v is not None and not cycle_data.get(k): cycle_data[k] = v _fi = ( any(k in original_query.lower() for k in ["pregnant", "conceive", "fertility", "fertile", "ovulat", "trying for", "bachha", "garbh", "not getting pregnant", "cant get pregnant", "conceive nahi"]) or "infertility" in final_symptoms ) _cr = has_repro or any(k in " ".join(final_symptoms) for k in ["menstruat", "period", "irregular", "unpredictable", "pcos", "pcod", "heavy_menstrual", "infertil", "acne_or_pimples", "hirsut", "hair_thin", "weight_gain"]) _purely_repro = (all(any(t in s for t in REPRO_TERMS) for s in final_symptoms) if final_symptoms else False) llm_tracks = context.get("tracks") or {} if llm_tracks and any(llm_tracks.values()): active = dict(llm_tracks) else: active = { "track_disease": bool(final_symptoms) and not _fi and not _purely_repro, "track_pcos": _cr, "track_conception": _fi, "track_fertility_peak": _fi, } if has_repro and not query.declined_menstrual: active["track_pcos"] = True if query.opted_in_menstrual: active["track_pcos"] = True if active.get("track_conception") or active.get("track_fertility_peak"): if not cycle_data.get("last_period_days_ago"): cycle_kws = ["day", "week", "month", "ago", "back", "cycle", "period", "last"] ans_text = " ".join( str(v) for v in answers.values() if str(v) not in ("I don't know", "") and any(kw in str(v).lower() for kw in cycle_kws) ) if ans_text.strip(): extra = await llm_extract_cycle_data(ans_text) for k, v in extra.items(): if v is not None and not cycle_data.get(k): cycle_data[k] = v results = await run_all_tracks(final_symptoms, active, cycle_data, answered_qs=full_answers) summary = await llm_summarize(final_symptoms, results, cycle_data, original_query) structured = { "summary": summary, "symptoms_detected": [s.replace("_", " ") for s in final_symptoms], "tracks": {}, } if "Disease" in results: structured["tracks"]["disease"] = results["Disease"] if "PCOS" in results: structured["tracks"]["pcos"] = results["PCOS"] if "Conception" in results: structured["tracks"]["conception"] = results["Conception"] if "Fertility" in results: structured["tracks"]["fertility"] = results["Fertility"] return { "status": "success", "message_type": "structured", "message": summary, "structured": structured, "session": _session( query, accumulated_symptoms=[], cycle_data={}, pending_questions=[], all_questions=[], answered_ids=[], conversation_turn=0, original_query="", ), } if tfidf_retriever: print(f"[DEBUG] TFIDFRetriever attrs: {list(vars(tfidf_retriever).keys())}")
"""
ShakTide AI β€” main.py (v3.2)
─────────────────────────────
Architecture:
POST /api/chat β€” multi-turn chatbot (session state in payload)
GET /api/tokens β€” daily usage check
GET / β€” health ping
Diagnostic pipeline (Cell 13 logic):
Turn 1 : rule-based + LLM symptom extraction β†’ clarifying question
Turn 2+ : answers processed β†’ more questions OR diagnosis
Diagnose: Disease β†’ TF-IDF + SBERT ensemble + Groq verification
PCOS β†’ weighted heuristic (Cell 13 scoring)
Conception β†’ cycle ML model + calendar
Fertility β†’ calendar-first + ML model
PKL files required (place in same folder as main.py):
tfidf_retriever.pkl sbert_retriever.pkl sym_embedder.pkl
best_disease_model.pkl disease_encoder.pkl symptom_scaler.pkl
best_pcos_model.pkl pcos_scaler.pkl
best_cycle_model.pkl cycle_scaler.pkl
best_fert_model.pkl fert_scaler.pkl
Environment:
GROQ_API_KEY=gsk_xxxxx
SUPABASE_URL=... (optional)
SUPABASE_SERVICE_ROLE_KEY=... (optional)
"""
import os, re, json, io, pickle, datetime
import numpy as np
import pandas as pd
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from typing import List, Optional, Dict, Any
from groq import AsyncGroq
# ──────────────────────────────────────────────────────────────────────────────
# 0. NLP STACK CLASS DEFINITIONS
# ──────────────────────────────────────────────────────────────────────────────
class TFIDFRetriever:
def __init__(self, vectorizer=None, tfidf_matrix=None, diseases=None):
self.vectorizer = vectorizer
self.tfidf_matrix = tfidf_matrix
self.diseases = diseases
def query(self, text: str, top_k: int = 5) -> pd.DataFrame:
vectorizer = getattr(self, "vectorizer", None) or getattr(self, "tfidf", None)
tfidf_matrix = getattr(self, "tfidf_matrix", None) or getattr(self, "matrix", None)
diseases = getattr(self, "diseases", None) or getattr(self, "labels", None)
if vectorizer is None or tfidf_matrix is None:
return pd.DataFrame(columns=["disease", "score", "rank"])
q_vec = vectorizer.transform([text])
sims = (tfidf_matrix * q_vec.T).toarray().flatten()
top_i = np.argsort(sims)[::-1][:top_k]
return pd.DataFrame([
{"rank": r+1, "disease": diseases[i], "score": float(sims[i])}
for r, i in enumerate(top_i)
])
class SemanticRetriever:
def __init__(self, model=None, embeddings=None, diseases=None):
self.model = model
self.embeddings = embeddings
self.diseases = diseases
def query(self, text: str, top_k: int = 5) -> pd.DataFrame:
if self.model is None or self.embeddings is None:
return pd.DataFrame(columns=["disease", "score", "rank"])
q_emb = self.model.encode([text])[0]
norm_e = self.embeddings / (np.linalg.norm(self.embeddings, axis=1, keepdims=True) + 1e-9)
norm_q = q_emb / (np.linalg.norm(q_emb) + 1e-9)
sims = np.dot(norm_e, norm_q)
top_i = np.argsort(sims)[::-1][:top_k]
return pd.DataFrame([
{"rank": r+1, "disease": self.diseases[i], "score": float(sims[i])}
for r, i in enumerate(top_i)
])
class SymptomEmbedder:
def __init__(self, model=None, symptom_embeddings=None, symptoms=None):
self.model = model
self.symptom_embeddings = symptom_embeddings
self.symptoms = symptoms
def find_symptoms(self, query: str, top_k: int = 15) -> pd.DataFrame:
return pd.DataFrame(columns=["symptom", "similarity"])
try:
import __main__
__main__.TFIDFRetriever = TFIDFRetriever
__main__.SemanticRetriever = SemanticRetriever
__main__.SymptomEmbedder = SymptomEmbedder
except Exception as e:
print(f"[WARN] __main__ injection skipped: {e}")