MediBot / APP_FLOW.md
bnewcomer's picture
Multi-agent updates
18bfaa1
|
Raw History Blame Contribute Delete
3.56 kB

A newer version of the Gradio SDK is available: 6.32.0

Upgrade

App Flow Diagram

This diagram shows the main code path for starting the app and processing a user prompt.

Startup and Prompt Flow

flowchart TD
    A["python app.py"] --> B["MedicalChatbotApp()"]
    B --> C["load_datasets()"]
    C --> D["Create agents"]
    D --> D1["DiseaseDiagnosisAgent"]
    D --> D2["ClarificationAgent"]
    D --> D3["SymptomSeverityAgent"]
    D --> D4["DiseaseDescriptionAgent"]
    D --> D5["PrecautionAdvisorAgent"]
    D1 --> E["build_faiss_index(df)"]
    D1 --> F["build_tfidf_matrix(collapsed_df)"]
    D --> G["ReActRouter(agents, all_symptoms)"]
    G --> H["launch()"]
    H --> I["Gradio UI shown"]

    I --> J["User submits prompt"]
    J --> K["respond(msg, state)"]
    K --> L["chat(user_input, session_state)"]
    L --> M["extract_structured_context()"]
    M --> N["merge_structured_context()"]
    N --> O["router.plan(user_input, history, structured_context)"]

    O --> P{"Planner output valid?"}
    P -- "Yes" --> Q["Executable steps"]
    P -- "No" --> R["router._fallback_plan()"]
    R --> Q

    Q --> S["_execute_plan()"]
    S --> T{"Agent step type"}
    T -- "clarification_agent" --> U["ClarificationAgent.handle()"]
    T -- "symptom_severity" --> V["SymptomSeverityAgent.handle()"]
    T -- "disease_diagnosis" --> W["DiseaseDiagnosisAgent.handle()"]
    T -- "disease_description" --> X["DiseaseDescriptionAgent.handle()"]
    T -- "precaution_advisor" --> Y["PrecautionAdvisorAgent.handle()"]

    W --> W1["normalize_symptom() / map_to_known_symptoms()"]
    W1 --> W2["build_user_vector()"]
    W1 --> W3["build_user_tfidf_vector()"]
    W2 --> W4["FAISS search"]
    W3 --> W5["score_candidates()"]
    W4 --> W5
    W5 --> W6{"Top scores close?"}
    W6 -- "Yes" --> W7["LLMReranker.rerank()"]
    W6 -- "No" --> W8["Keep scored ranking"]
    W7 --> Z["diagnosis result"]
    W8 --> Z

    U --> AA["clarification result"]
    V --> AB["severity result"]
    X --> AC["description result"]
    Y --> AD["precaution result"]
    Z --> AE["Format answer"]
    AA --> AE
    AB --> AE
    AC --> AE
    AD --> AE

    AE --> AF{"Red flags or ambiguity?"}
    AF -- "Emergency" --> AG["_format_emergency_answer()"]
    AF -- "Needs clarification" --> AH["_format_clarification_answer()"]
    AF -- "Diagnosis/details" --> AI["_format_diagnosis_answer() or detail response"]

    AG --> AJ["Update session state"]
    AH --> AJ
    AI --> AJ
    AJ --> AK["_render_session_summary()"]
    AJ --> AL["_render_result_card()"]
    AK --> AM["Return history, state, summary, card_html"]
    AL --> AM

Main Files Involved

  • app.py: app startup, router, session state, prompt processing, response formatting, and Gradio UI
  • diagnosis_engine/diagnosis_agent.py: diagnosis pipeline orchestration
  • diagnosis_engine/faiss_index.py: symptom vector indexing and retrieval
  • diagnosis_engine/tfidf.py: TF-IDF symptom similarity
  • diagnosis_engine/scoring.py: candidate scoring and ranking
  • diagnosis_engine/vocabulary.py: symptom normalization and synonym mapping
  • diagnosis_engine/reranker.py: optional LLM reranking for close candidates

Practical Reading Order

If you want to trace the code manually, this is the most useful order:

  1. app.py -> MedicalChatbotApp.__init__()
  2. app.py -> launch()
  3. app.py -> chat()
  4. app.py -> ReActRouter.plan()
  5. app.py -> _execute_plan()
  6. diagnosis_engine/diagnosis_agent.py -> handle()
  7. diagnosis_engine/scoring.py -> score_candidates()
  8. app.py -> _format_*_answer() methods