Spaces:
Sleeping
Sleeping
Commit Β·
e428554
1
Parent(s): 7f3a266
docs(README): plain-language explanation under every Mermaid diagram
Browse filesAdds a 'What this diagram traces' aggregate + per-key-block bulleted
explanations after every Mermaid in Β§4.1 (user journey) and Β§4.3 A-G
(request flow, fail-loud fallback, voice, profile+recall, data+ingest,
8 upload gates, deployment). Diagrams themselves are unchanged; the
text complements them so a non-Mermaid reader (or a reader skimming)
gets the objective + meaning of every block and edge.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
README.md
CHANGED
|
@@ -144,6 +144,27 @@ flowchart TD
|
|
| 144 |
VOICE -.-> QA
|
| 145 |
```
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
### 4.2 The shape, in one paragraph
|
| 149 |
|
|
@@ -244,6 +265,45 @@ flowchart TB
|
|
| 244 |
HT -.->|"1 JSON line / turn"| LOG["logs/turns.jsonl (replay/audit)"]
|
| 245 |
```
|
| 246 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
**B Β· LLM brain + fail-loud fallback chain**
|
| 248 |
|
| 249 |
```mermaid
|
|
@@ -258,6 +318,23 @@ flowchart LR
|
|
| 258 |
GUARD --> OUT["sent to user"]
|
| 259 |
```
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
**C Β· Voice pipeline (in / out, with barge-in)**
|
| 262 |
|
| 263 |
```mermaid
|
|
@@ -274,6 +351,23 @@ flowchart LR
|
|
| 274 |
SPK -.->|"abort in-flight"| BR
|
| 275 |
```
|
| 276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
**D Β· Profile, personalisation & returning-user recall**
|
| 278 |
|
| 279 |
```mermaid
|
|
@@ -295,6 +389,29 @@ flowchart TB
|
|
| 295 |
note1["evicted/blank session + carried chat_history β<br/>STATE-RECOVERY: rebuild profile from history,<br/>never re-ask the name"] -.-> ENDT
|
| 296 |
```
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
**E Β· Data architecture & offline ingest pipeline**
|
| 299 |
|
| 300 |
```mermaid
|
|
@@ -318,6 +435,25 @@ flowchart LR
|
|
| 318 |
CUR -->|"read per request"| RUNTIME
|
| 319 |
```
|
| 320 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
**F Β· Uploaded-PDF defence β 8 sequential gates**
|
| 322 |
|
| 323 |
```mermaid
|
|
@@ -334,6 +470,30 @@ flowchart LR
|
|
| 334 |
G1 & G2 & G3 & G4 & G5 & G6 & G7 & G8 -->|"fail"| REJ["clean rejection (reason surfaced)"]
|
| 335 |
```
|
| 336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
**G Β· Deployment**
|
| 338 |
|
| 339 |
```mermaid
|
|
@@ -349,6 +509,23 @@ flowchart LR
|
|
| 349 |
LIVE --> CHK["verify reported build SHA advanced<br/>(LFS/quota push can fail silently)"]
|
| 350 |
```
|
| 351 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
---
|
| 353 |
|
| 354 |
### 4.3-text Request flow (a single turn) β plain-text fallback
|
|
|
|
| 144 |
VOICE -.-> QA
|
| 145 |
```
|
| 146 |
|
| 147 |
+
**What this diagram traces.** A first-time user opens the app and ends the
|
| 148 |
+
session having decided on a plan with confidence β and how the system loops
|
| 149 |
+
through compare / browse / Q&A / upload along the way. No backend in this
|
| 150 |
+
view; just the human path.
|
| 151 |
+
|
| 152 |
+
- **Returning-user check up front.** If the app recognises your name from
|
| 153 |
+
before, it offers to restore your saved profile (so you never re-type);
|
| 154 |
+
*no* takes you down the same path as a first-time user.
|
| 155 |
+
- **Conversational fact-find.** A short typed-or-spoken back-and-forth
|
| 156 |
+
(English or Hindi-Hinglish) captures age, family, budget, health and
|
| 157 |
+
what you care about β instead of a long form.
|
| 158 |
+
- **Personalised shortlist + a "why".** Plans are ranked for *your* fit;
|
| 159 |
+
every fact about a plan is backed by the exact clause in the real
|
| 160 |
+
policy PDF, never invented.
|
| 161 |
+
- **Branches from the shortlist.** Compare side by side, browse the full
|
| 162 |
+
marketplace, ask follow-up questions, or upload your *own* policy PDF
|
| 163 |
+
and ask about your document (kept private to your session).
|
| 164 |
+
- **Live premium.** Updates as you change the profile.
|
| 165 |
+
- **Decision.** No lead capture and no commission bias β the path ends at
|
| 166 |
+
*decide*, not at a sales handoff.
|
| 167 |
+
|
| 168 |
|
| 169 |
### 4.2 The shape, in one paragraph
|
| 170 |
|
|
|
|
| 265 |
HT -.->|"1 JSON line / turn"| LOG["logs/turns.jsonl (replay/audit)"]
|
| 266 |
```
|
| 267 |
|
| 268 |
+
**What this diagram traces.** Everything that happens between a keystroke
|
| 269 |
+
(or spoken word) in the browser and a reply (with optional voice playback)
|
| 270 |
+
coming back β drawn across every layer the request crosses.
|
| 271 |
+
|
| 272 |
+
- **Browser (Next.js / React).** The page renders chat, marketplace,
|
| 273 |
+
compare and the profile builder. For voice, Web Speech shows a live
|
| 274 |
+
*interim* transcript on screen while `MediaRecorder` captures the
|
| 275 |
+
*authoritative* audio.
|
| 276 |
+
- **FastAPI (`backend/main.py`).** Four endpoint groups: `/api/transcribe`
|
| 277 |
+
(Sarvam STT), `/api/chat` (the brain), `/api/upload-policy` (the 8
|
| 278 |
+
security gates β quarantine), and the supporting
|
| 279 |
+
`/api/coverage Β· /profile Β· /policies Β· /scorecard Β· /session`.
|
| 280 |
+
- **The brain (`single_brain.py`, Gemini).** Exactly **one** Gemini call
|
| 281 |
+
per turn with four function-calling tools: `retrieve_policies`,
|
| 282 |
+
`save_profile_field`, `get_policy_facts`, `mark_recommendation`. The
|
| 283 |
+
brain is *only* allowed to state what its tools returned.
|
| 284 |
+
- **Fit, scoring & pricing.** `scorecard.py` + `retrieval_filters.py`
|
| 285 |
+
grade each plan for the user's profile; `premium_calculator.py` +
|
| 286 |
+
`sum_insured.py` produce a live illustrative premium range.
|
| 287 |
+
- **Profile & persistence.** `session_state.py` holds the live profile;
|
| 288 |
+
`profile_store.py` / `profile_persistence.py` write a `<name>.json` at
|
| 289 |
+
end of turn so a returning user can be recognised; `profile_rag.py`
|
| 290 |
+
also embeds the profile as a session-scoped chunk so the brain can
|
| 291 |
+
ground "given my situation" references.
|
| 292 |
+
- **Retrieval & curated data.** A Chroma vector store (BGE-small,
|
| 293 |
+
local, 384-d) with the shared "policies" collection (~150 plans,
|
| 294 |
+
~7.3 k chunks, 20 insurers) and a per-session "quarantine" collection
|
| 295 |
+
for user-uploaded PDFs (24 h TTL, session-isolated); plus the
|
| 296 |
+
request-time curated facts in `40-data/policy_facts/`.
|
| 297 |
+
- **Voice out.** `voice_format.py` normalises money / Indic shorthand
|
| 298 |
+
and chunks at sentence bounds (so long answers are spoken in full);
|
| 299 |
+
Sarvam Bulbul TTS plays the reply through an in-DOM `<audio>` element.
|
| 300 |
+
- **Barge-in.** The user speaking over the bot pauses playback *and*
|
| 301 |
+
aborts the in-flight `/api/chat`.
|
| 302 |
+
- **Fallback.** On a real Gemini failure or cold-start 503, the turn
|
| 303 |
+
transparently routes to `nim_fallback.py` (NVIDIA NIM chain) β see
|
| 304 |
+
diagram B.
|
| 305 |
+
- **Audit.** Every turn appends one JSON line to `logs/turns.jsonl`.
|
| 306 |
+
|
| 307 |
**B Β· LLM brain + fail-loud fallback chain**
|
| 308 |
|
| 309 |
```mermaid
|
|
|
|
| 318 |
GUARD --> OUT["sent to user"]
|
| 319 |
```
|
| 320 |
|
| 321 |
+
**What this diagram traces.** How a chat turn is served by the primary
|
| 322 |
+
LLM, what happens when it fails, and the structural guard that prevents a
|
| 323 |
+
silently wrong answer.
|
| 324 |
+
|
| 325 |
+
- **Primary path.** Gemini (`gemini-2.5-flash-lite`). On a healthy
|
| 326 |
+
response β the reply is built *only* from what the tools returned.
|
| 327 |
+
- **Fallback path (fail-loud).** A real Gemini failure or a cold-start
|
| 328 |
+
503 routes through `backend/llm_health.py` (a background probe with
|
| 329 |
+
sticky-primary election) to the NVIDIA NIM open-model chain
|
| 330 |
+
(`nim_fallback.py`). One healthy model in that chain serves the turn.
|
| 331 |
+
- **Last resort.** If the whole chain is down, the user gets an explicit
|
| 332 |
+
*"service degraded"* message β never a silently wrong answer.
|
| 333 |
+
- **Prose-grounding guard.** Before a reply is sent, every policy / UIN
|
| 334 |
+
named in the prose is verified against the same `retrieve_policies`
|
| 335 |
+
and `get_policy_facts` results the brain saw (with an exemption for
|
| 336 |
+
genuine catalogue UINs). Faithfulness is structural, not bolt-on.
|
| 337 |
+
|
| 338 |
**C Β· Voice pipeline (in / out, with barge-in)**
|
| 339 |
|
| 340 |
```mermaid
|
|
|
|
| 351 |
SPK -.->|"abort in-flight"| BR
|
| 352 |
```
|
| 353 |
|
| 354 |
+
**What this diagram traces.** How spoken input becomes a chat turn, how
|
| 355 |
+
the reply becomes speech back, and how the user can interrupt mid-answer.
|
| 356 |
+
|
| 357 |
+
- **Capture.** Tap-to-talk (touch) or push-to-talk (desktop) starts
|
| 358 |
+
`MediaRecorder` (the authoritative audio) and Web Speech (a live
|
| 359 |
+
interim transcript shown on screen but never trusted for the turn).
|
| 360 |
+
- **STT.** The authoritative audio is sent to `/api/transcribe`
|
| 361 |
+
(Sarvam Saarika β Indian-accent + Hinglish aware).
|
| 362 |
+
- **Brain β reply.** The transcript runs through `single_brain.handle_turn`
|
| 363 |
+
exactly like a typed turn.
|
| 364 |
+
- **TTS.** `voice_format.py` normalises money / Indic shorthand and chunks
|
| 365 |
+
at sentence bounds (so long replies are spoken in full); Sarvam Bulbul
|
| 366 |
+
speaks; an in-DOM `<audio>` element plays.
|
| 367 |
+
- **Barge-in.** The user speaking over the bot pauses playback **and**
|
| 368 |
+
aborts the in-flight `/api/chat`, so the bot stops mid-thought rather
|
| 369 |
+
than over-talking.
|
| 370 |
+
|
| 371 |
**D Β· Profile, personalisation & returning-user recall**
|
| 372 |
|
| 373 |
```mermaid
|
|
|
|
| 389 |
note1["evicted/blank session + carried chat_history β<br/>STATE-RECOVERY: rebuild profile from history,<br/>never re-ask the name"] -.-> ENDT
|
| 390 |
```
|
| 391 |
|
| 392 |
+
**What this diagram traces.** How the system captures a profile during a
|
| 393 |
+
conversation, persists it for the user's next visit, recalls it *safely* on
|
| 394 |
+
return, and recovers when the server forgot it.
|
| 395 |
+
|
| 396 |
+
- **Capture.** Every answer (chat or profile-builder) is persisted via
|
| 397 |
+
`save_profile_field` into the live `session_state.profile`.
|
| 398 |
+
- **End-of-turn persist.** `auto_persist_session` writes a
|
| 399 |
+
`<name>.json` to disk **and** embeds the profile as a session-scoped
|
| 400 |
+
chunk β so a returning user can be recognised, and "given my situation"
|
| 401 |
+
references can be grounded.
|
| 402 |
+
- **Privacy-safe recall.** On a return visit the captured name is matched
|
| 403 |
+
against the stored profile; a match is **staged** on
|
| 404 |
+
`pending_profile_recall` and the bot asks *"Welcome back β are you the
|
| 405 |
+
same X?"*. Only an explicit *yes* merges the stored slots (so a name
|
| 406 |
+
collision never auto-restores a stranger's profile); *no* discards the
|
| 407 |
+
stage and the user continues fresh.
|
| 408 |
+
- **State recovery.** If the server's in-memory session was evicted /
|
| 409 |
+
restarted but the browser still carries `chat_history`, the brain
|
| 410 |
+
enters **STATE-RECOVERY MODE** β silently re-captures the profile from
|
| 411 |
+
the conversation history instead of asking the user's name again.
|
| 412 |
+
- **Drives scoring + pricing.** The same profile feeds the scorecard
|
| 413 |
+
fit-and-grade and the live premium estimate.
|
| 414 |
+
|
| 415 |
**E Β· Data architecture & offline ingest pipeline**
|
| 416 |
|
| 417 |
```mermaid
|
|
|
|
| 435 |
CUR -->|"read per request"| RUNTIME
|
| 436 |
```
|
| 437 |
|
| 438 |
+
**What this diagram traces.** Where the policy data lives, how it's
|
| 439 |
+
built *once*, offline, and how it's read at request time.
|
| 440 |
+
|
| 441 |
+
- **Three repositories, deliberately separated.** *Code* β this repo,
|
| 442 |
+
mirrored to the HF Space (`origin`) and GitHub, application source only.
|
| 443 |
+
*Data dataset* β `rohitsar567/insurance-bot-data` β the heavy binaries:
|
| 444 |
+
PDF corpus + prebuilt Chroma vectors, pulled at Docker build via
|
| 445 |
+
`huggingface_hub.snapshot_download`. *Curated facts* β `40-data/` β
|
| 446 |
+
small human-reviewed JSON, versioned with the code.
|
| 447 |
+
- **Offline ingest** (not on the request path). PDFs β `rag/ingest.py`
|
| 448 |
+
chunks β `rag/extract.py` + `schema.py` produce structured facts (every
|
| 449 |
+
value carrying a verbatim `source_quote`) β `40-data/policy_facts/*.json`;
|
| 450 |
+
the same chunks are embedded (BGE-small, local, 384-d) into the Chroma
|
| 451 |
+
vector store.
|
| 452 |
+
- **Runtime reads.** `rag/retrieve.py` queries the vector store on every
|
| 453 |
+
chat turn; the backend reads `40-data/policy_facts/` directly for
|
| 454 |
+
grading + citations. No live LLM extraction on the hot path β
|
| 455 |
+
everything heavy was done offline.
|
| 456 |
+
|
| 457 |
**F Β· Uploaded-PDF defence β 8 sequential gates**
|
| 458 |
|
| 459 |
```mermaid
|
|
|
|
| 470 |
G1 & G2 & G3 & G4 & G5 & G6 & G7 & G8 -->|"fail"| REJ["clean rejection (reason surfaced)"]
|
| 471 |
```
|
| 472 |
|
| 473 |
+
**What this diagram traces.** Every check a user-uploaded PDF passes
|
| 474 |
+
before its content is allowed to touch the vector store, and where rejected
|
| 475 |
+
files go.
|
| 476 |
+
|
| 477 |
+
- **The 8 gates, in order.** (1) **File mechanics** β `%PDF` magic, 5 KBβ25 MB
|
| 478 |
+
size band, well-formed `%%EOF`, no embedded executables / JavaScript /
|
| 479 |
+
launch actions. (2) **Content quality** β β₯1500 extractable chars,
|
| 480 |
+
β₯3 pages, at least one insurance-domain keyword. (3) **Prompt-injection
|
| 481 |
+
sweep** β "ignore previous instructions", "reveal your system prompt",
|
| 482 |
+
jailbreak patterns. (4) **Per-session rate limit.** (5) **Per-IP rate
|
| 483 |
+
limit** (catches session-ID rotation). (6) **Encrypted/locked PDF** β
|
| 484 |
+
rejected cleanly. (7) **Page-count ceiling** (>200 pages β an
|
| 485 |
+
abuse/bundle vector). (8) **Hash dedupe + reject-cache** β identical
|
| 486 |
+
re-uploads short-circuit.
|
| 487 |
+
- **Beyond identical-file dedup.** A **UIN net-new check** also runs β if
|
| 488 |
+
the PDF's IRDAI UIN already belongs to a catalogued policy, the caller
|
| 489 |
+
is pointed at the existing marketplace card instead of indexing a
|
| 490 |
+
duplicate.
|
| 491 |
+
- **On pass.** Chunks land in a per-session **quarantine** Chroma
|
| 492 |
+
collection β session-isolated, 24 h idle TTL β *never* the shared
|
| 493 |
+
`policies` corpus.
|
| 494 |
+
- **On fail.** A clean rejection naming the gate; the file is deleted;
|
| 495 |
+
nothing is embedded.
|
| 496 |
+
|
| 497 |
**G Β· Deployment**
|
| 498 |
|
| 499 |
```mermaid
|
|
|
|
| 509 |
LIVE --> CHK["verify reported build SHA advanced<br/>(LFS/quota push can fail silently)"]
|
| 510 |
```
|
| 511 |
|
| 512 |
+
**What this diagram traces.** How a `git push` becomes a live Space, end
|
| 513 |
+
to end.
|
| 514 |
+
|
| 515 |
+
- **Two remotes.** `origin` = the Hugging Face Space (a push here
|
| 516 |
+
triggers the Docker rebuild). `github` = the public mirror reviewers
|
| 517 |
+
read.
|
| 518 |
+
- **Build.** The HF Space rebuilds the image, installs the backend,
|
| 519 |
+
builds the Next.js static frontend, and runs
|
| 520 |
+
`huggingface_hub.snapshot_download` to hydrate `rag/` (PDF corpus +
|
| 521 |
+
prebuilt vectors) from the `insurance-bot-data` dataset β so the Space
|
| 522 |
+
repo itself stays code-only and small.
|
| 523 |
+
- **Start.** `entrypoint.sh` launches `uvicorn backend.main:app` on
|
| 524 |
+
`$PORT` (default 7860); FastAPI also serves the exported frontend.
|
| 525 |
+
- **Verify.** Always confirm the Space's reported build SHA actually
|
| 526 |
+
advanced before trusting that new code is live β an LFS/quota push
|
| 527 |
+
can fail without surfacing an error.
|
| 528 |
+
|
| 529 |
---
|
| 530 |
|
| 531 |
### 4.3-text Request flow (a single turn) β plain-text fallback
|