# vishal88736/sanjeevani architecture

> Architecture diagram of the GitHub repository vishal88736/sanjeevani, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vishal88736/sanjeevani
- Repository: https://github.com/vishal88736/sanjeevani

## Overview

#### Purpose and Entry Points
Sanjeevani is a local FastAPI-served health-question assistant for major Indian languages. Users submit typed text or browser-recorded audio; the system returns a native-language transcript, English translation, and explicitly draft health guidance. The runtime entry point is `website/backend/main.py` (`uvicorn website.backend.main:app`); the browser client is static content rooted at `website/frontend/index.html`.

#### Web/API Boundary
The FastAPI backend serves the frontend and exposes request handling around typed and multipart audio inputs. Request/response contracts are centralized in `website/backend/schemas.py`, separating browser payloads from model-stage outputs. The frontend records audio with `MediaRecorder`, submits it to the backend, and renders pipeline results via `website/frontend/assets/app.js`.

#### Orchestration Pipeline
`model/pipeline.py` is the core application workflow coordinator. It selects the speech or text path, resolves the source language, translates to English when needed, retrieves clinical grounding context, then asks the LLM to draft the response. This is the main boundary between HTTP concerns and ML/provider integrations.

#### Speech Recognition and Language Resolution
`model/asr/indic_asr.py` wraps AI4Bharat IndicConformer for audio-to-native-language transcription. Explicit language selection supplies the decoder language; audio auto-detection instead transcribes against a short candidate set and uses plausibility heuristics to select a result. Typed text language detection is script-based and belongs to the pipeline/config-driven language model.

#### Translation Stage
`model/translation/indic_translator.py` wraps AI4Bharat IndicTrans2 to convert supported Indian-language text to English. English input bypasses translation. Language metadata and model-specific code mappings are defined centrally in `model/config.py`, which is the extension point for supported languages.

#### Answer Generation and Grounding Boundary
`model/llm/groq_client.py` is the sole adapter to Groq’s OpenAI-compatible LLM API. It receives English questions plus optional retrieved context and applies safety-oriented prompting: no diagnosis, encourage qualified health-worker consultation, and surface emergencies including India’s 108 service. `model/llm/knowledge_base.py` is intentionally a non-grounding placeholder; it currently returns no clinical context, so all answers remain drafts. Replacing its retrieval implementation is the primary production-clinical-data integration point.

#### Runtime, Models, and External Dependencies
The Python 3.11 service uses FastAPI/Uvicorn, with browser assets served locally. AI4Bharat checkpoints—IndicConformer and IndicTrans2—are downloaded from Hugging Face on first model use and cached locally. Groq requires `GROQ_API_KEY` from the process environment, read through `model/config.py`. Audio decoding relies on `torchaudio`; WebM/Opus recordings generally require system `ffmpeg` on PATH.

Main entry points: [`model/pipeline.py`](https://github.com/vishal88736/sanjeevani/blob/main/model/pipeline.py) (Question pipeline), [`website/backend/main.py`](https://github.com/vishal88736/sanjeevani/blob/main/website/backend/main.py) (FastAPI service), [`model/asr/indic_asr.py`](https://github.com/vishal88736/sanjeevani/blob/main/model/asr/indic_asr.py) (Indic ASR), [`model/llm/groq_client.py`](https://github.com/vishal88736/sanjeevani/blob/main/model/llm/groq_client.py) (Draft answer client)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Browser &amp; API"]
  node_frontend["Browser client<br/>static frontend<br/>[index.html]"]
  node_frontend_app["Client behavior<br/>browser JavaScript<br/>[app.js]"]
  node_api["FastAPI service<br/>HTTP API<br/>[main.py]"]
  node_schemas["Request contracts<br/>API schemas<br/>[schemas.py]"]
end

subgraph group_pipeline["ML Pipeline"]
  node_pipeline_core["Question pipeline<br/>workflow coordinator<br/>[pipeline.py]"]
  node_language_config["Language configuration<br/>language registry<br/>[config.py]"]
  node_asr["Indic ASR<br/>speech adapter<br/>[indic_asr.py]"]
  node_translator["Indic translation<br/>translation adapter"]
  node_knowledge_base["Clinical context<br/>grounding seam<br/>[knowledge_base.py]"]
  node_groq_client["Draft answer client<br/>LLM adapter<br/>[groq_client.py]"]
end

subgraph group_providers["Models &amp; Providers"]
  node_indic_conformer[("IndicConformer checkpoint<br/>Hugging Face model")]
  node_indictrans2[("IndicTrans2 checkpoint<br/>Hugging Face model")]
  node_groq{{"Groq API<br/>external LLM service"}}
  node_api_key["GROQ_API_KEY<br/>process environment"]
  node_ffmpeg["ffmpeg<br/>system decoder"]
end

node_frontend -->|"loads"| node_frontend_app
node_frontend_app -->|"text or audio request"| node_api
node_api -->|"validates"| node_schemas
node_api -->|"invokes"| node_pipeline_core
node_pipeline_core -->|"language metadata"| node_language_config
node_pipeline_core -->|"audio path"| node_asr
node_pipeline_core -->|"non-English text"| node_translator
node_pipeline_core -->|"retrieves context"| node_knowledge_base
node_pipeline_core -->|"English question + context"| node_groq_client
node_asr -->|"transcribes with"| node_indic_conformer
node_asr -.->|"decodes audio via"| node_ffmpeg
node_translator -->|"translates with"| node_indictrans2
node_groq_client -->|"OpenAI-compatible API"| node_groq
node_language_config -.->|"reads"| node_api_key
node_groq_client -.->|"authenticates with"| node_api_key
node_pipeline_core -->|"transcript, translation, draft"| node_api

click node_frontend "https://github.com/vishal88736/sanjeevani/blob/main/website/frontend/index.html"
click node_frontend_app "https://github.com/vishal88736/sanjeevani/blob/main/website/frontend/assets/app.js"
click node_api "https://github.com/vishal88736/sanjeevani/blob/main/website/backend/main.py"
click node_schemas "https://github.com/vishal88736/sanjeevani/blob/main/website/backend/schemas.py"
click node_pipeline_core "https://github.com/vishal88736/sanjeevani/blob/main/model/pipeline.py"
click node_language_config "https://github.com/vishal88736/sanjeevani/blob/main/model/config.py"
click node_asr "https://github.com/vishal88736/sanjeevani/blob/main/model/asr/indic_asr.py"
click node_translator "https://github.com/vishal88736/sanjeevani/blob/main/model/translation/indic_translator.py"
click node_knowledge_base "https://github.com/vishal88736/sanjeevani/blob/main/model/llm/knowledge_base.py"
click node_groq_client "https://github.com/vishal88736/sanjeevani/blob/main/model/llm/groq_client.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend,node_frontend_app,node_api,node_schemas toneBlue
class node_pipeline_core,node_language_config,node_asr,node_translator,node_knowledge_base,node_groq_client toneAmber
class node_indic_conformer,node_indictrans2,node_groq,node_api_key,node_ffmpeg toneMint
```

## Components

### Browser & API

- [Browser client](https://github.com/vishal88736/sanjeevani/blob/main/website/frontend/index.html): static frontend · Records audio, submits questions, and renders results. · `website/frontend/index.html`
- [Client behavior](https://github.com/vishal88736/sanjeevani/blob/main/website/frontend/assets/app.js): browser JavaScript · Uses MediaRecorder and calls backend endpoints. · `website/frontend/assets/app.js`
- [FastAPI service](https://github.com/vishal88736/sanjeevani/blob/main/website/backend/main.py): HTTP API · Serves static assets and accepts text or multipart audio. · `website/backend/main.py`
- [Request contracts](https://github.com/vishal88736/sanjeevani/blob/main/website/backend/schemas.py): API schemas · Separates browser payloads from pipeline outputs. · `website/backend/schemas.py`

### ML Pipeline

- [Question pipeline](https://github.com/vishal88736/sanjeevani/blob/main/model/pipeline.py): workflow coordinator · Routes speech or text through language, translation, grounding, and drafting. · `model/pipeline.py`
- [Language configuration](https://github.com/vishal88736/sanjeevani/blob/main/model/config.py): language registry · Defines supported languages, code mappings, and runtime settings. · `model/config.py`
- [Indic ASR](https://github.com/vishal88736/sanjeevani/blob/main/model/asr/indic_asr.py): speech adapter · Transcribes audio with explicit selection or candidate-based detection. · `model/asr/indic_asr.py`
- [Indic translation](https://github.com/vishal88736/sanjeevani/blob/main/model/translation/indic_translator.py): translation adapter · Converts supported native-language text to English. · `model/translation/indic_translator.py`
- [Clinical context](https://github.com/vishal88736/sanjeevani/blob/main/model/llm/knowledge_base.py): grounding seam · Currently returns no context; intended retrieval integration point. · `model/llm/knowledge_base.py`
- [Draft answer client](https://github.com/vishal88736/sanjeevani/blob/main/model/llm/groq_client.py): LLM adapter · Applies safety prompting and produces draft guidance. · `model/llm/groq_client.py`

### Models & Providers

- **IndicConformer checkpoint**: Hugging Face model · Downloaded on first ASR use and cached locally.
- **IndicTrans2 checkpoint**: Hugging Face model · Downloaded on first translation use and cached locally.
- **Groq API**: external LLM service
- **GROQ_API_KEY**: process environment
- **ffmpeg**: system decoder · Decodes typical WebM/Opus browser recordings.

## Connections

- Browser client → Client behavior: loads
- Client behavior → FastAPI service: text or audio request
- FastAPI service → Request contracts: validates
- FastAPI service → Question pipeline: invokes
- Question pipeline → Language configuration: language metadata
- Question pipeline → Indic ASR: audio path
- Question pipeline → Indic translation: non-English text
- Question pipeline → Clinical context: retrieves context
- Question pipeline → Draft answer client: English question + context
- Indic ASR → IndicConformer checkpoint: transcribes with
- Indic ASR → ffmpeg: decodes audio via
- Indic translation → IndicTrans2 checkpoint: translates with
- Draft answer client → Groq API: OpenAI-compatible API
- Language configuration → GROQ_API_KEY: reads
- Draft answer client → GROQ_API_KEY: authenticates with
- Question pipeline → FastAPI service: transcript, translation, draft
