# the-bhuvi/mazh-ai- architecture

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

- Interactive diagram: https://gitdiagram.com/the-bhuvi/mazh-ai-
- Repository: https://github.com/the-bhuvi/mazh-ai-

## Overview

Mazh AI turns a location into rainfall-risk guidance through a React dashboard or phone service. The web flow obtains geolocation or a chosen place/PIN, requests an insight from FastAPI, and presents forecast, risk, and recommendations; chat and SMS are additional dashboard capabilities. The backend combines weather and climate context with a calibrated ML risk estimate and cached insight generation. Phone access is keypad-only: Asterisk is the documented reference deployment, with legacy Twilio voice webhooks also available. Source-index references and README statements support some subsystem links where implementation excerpts were incomplete.

Main entry points: [`backend/app/main.py`](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/main.py) (FastAPI service), [`frontend/src/App.tsx`](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/App.tsx) (Web application), [`backend/app/voice/routes.py`](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/routes.py) (Twilio IVR webhooks), [`backend/app/engine/insight.py`](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/engine/insight.py) (Insight builder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Web experience"]
  node_app["Web application<br/>[App.tsx]"]
  node_geolocate["Geolocation<br/>[useGeolocation.ts]"]
  node_location_picker["Location picker<br/>[LocationPicker.tsx]"]
  node_weather_api["Web API client<br/>[client.ts]"]
  node_dashboard["Risk dashboard<br/>[Dashboard.tsx]"]
  node_chat["Insight chat<br/>[ChatWidget.tsx]"]
  node_phone_card["Phone and SMS<br/>[PhoneCard.tsx]"]
end

subgraph group_backend["Insight service"]
  node_api["FastAPI service<br/>[main.py]"]
  node_location["Location resolution<br/>[location.py]"]
  node_insight["Insight builder<br/>[insight.py]"]
  node_cache[("Insight cache<br/>[cache.py]")]
  node_llm["Chat reply engine<br/>[client.py]"]
end

subgraph group_weather["Weather and climate"]
  node_forecast["Forecast provider<br/>[open_meteo.py]"]
  node_historical["Historical weather"]
  node_climate["Climate context<br/>[climate_service.py]"]
end

subgraph group_ml["Rainfall model"]
  node_ml_adapter["ML runtime adapter<br/>[ml_engine.py]"]
  node_inference["Risk inference<br/>[infer.py]"]
  node_features["Feature engineering<br/>[features.py]"]
  node_training_data["Training data<br/>[data.py]"]
  node_trainer["Model training<br/>[train.py]"]
end

subgraph group_voice["Phone channels"]
  node_sms["SMS adapter<br/>[twilio_adapter.py]"]
  node_voice_routes["Twilio IVR webhooks<br/>[routes.py]"]
  node_voice_phrases["Fixed voice phrases<br/>[phrases.py]"]
  node_voice_state["Signed call state<br/>[state.py]"]
  node_voice_audio["Voice audio<br/>[audio.py]"]
  node_asterisk["Asterisk AGI<br/>[mazh_weather.py]"]
end

node_person(("Web user"))
node_caller(("Phone caller"))
node_openmeteo["Open-Meteo"]
node_twilio_sms["Twilio SMS"]
node_twilio_voice["Twilio voice"]

node_person -->|"uses"| node_app
node_app -->|"gets location"| node_geolocate
node_app -->|"offers fallback"| node_location_picker
node_app -->|"requests insight"| node_weather_api
node_weather_api -->|"HTTP request"| node_api
node_api -->|"resolves location"| node_location
node_api -->|"builds insight"| node_insight
node_api -->|"fetches forecast"| node_forecast
node_api -->|"fetches archive"| node_historical
node_api -->|"uses cache"| node_cache
node_insight -->|"estimates risk"| node_ml_adapter
node_insight -.->|"adds context"| node_climate
node_forecast -->|"fetches data"| node_openmeteo
node_historical -->|"reuses provider"| node_forecast
node_ml_adapter -->|"loads and predicts"| node_inference
node_inference -->|"builds features"| node_features
node_trainer -->|"loads data"| node_training_data
node_trainer -->|"builds features"| node_features
node_app -->|"renders"| node_dashboard
node_app -->|"renders"| node_chat
node_app -->|"renders"| node_phone_card
node_chat -->|"sends chat"| node_weather_api
node_phone_card -->|"requests SMS"| node_weather_api
node_api -->|"generates reply"| node_llm
node_api -->|"sends SMS"| node_sms
node_sms -->|"delivers message"| node_twilio_sms
node_caller -->|"calls"| node_twilio_voice
node_twilio_voice -->|"posts webhooks"| node_voice_routes
node_voice_routes -->|"prefetches insight"| node_insight
node_voice_routes -->|"uses phrases"| node_voice_phrases
node_voice_routes -->|"validates state"| node_voice_state
node_voice_routes -->|"sends SMS"| node_sms
node_voice_routes -.->|"serves audio"| node_voice_audio
node_caller -->|"calls"| node_asterisk
node_asterisk -->|"calls phone API"| node_api

click node_app "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/App.tsx"
click node_geolocate "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/hooks/useGeolocation.ts"
click node_location_picker "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/LocationPicker.tsx"
click node_weather_api "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/api/client.ts"
click node_dashboard "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/Dashboard.tsx"
click node_chat "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/ChatWidget.tsx"
click node_phone_card "https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/PhoneCard.tsx"
click node_api "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/main.py"
click node_location "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/location.py"
click node_insight "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/engine/insight.py"
click node_cache "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/cache.py"
click node_forecast "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/providers/open_meteo.py"
click node_historical "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/providers/historical_weather.py"
click node_climate "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/climate_service.py"
click node_ml_adapter "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/ml_engine.py"
click node_inference "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/infer.py"
click node_features "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/features.py"
click node_training_data "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/data.py"
click node_trainer "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/train.py"
click node_llm "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/llm/client.py"
click node_sms "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/sms/twilio_adapter.py"
click node_voice_routes "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/routes.py"
click node_voice_phrases "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/phrases.py"
click node_voice_state "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/state.py"
click node_voice_audio "https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/audio.py"
click node_asterisk "https://github.com/the-bhuvi/mazh-ai-/blob/Main/asterisk/agi/mazh_weather.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_app,node_geolocate,node_location_picker,node_weather_api,node_dashboard,node_chat,node_phone_card,node_person toneBlue
class node_api,node_location,node_insight,node_cache,node_llm toneAmber
class node_forecast,node_historical,node_climate toneMint
class node_ml_adapter,node_inference,node_features,node_training_data,node_trainer toneRose
class node_sms,node_voice_routes,node_voice_phrases,node_voice_state,node_voice_audio,node_asterisk,node_caller,node_openmeteo,node_twilio_sms,node_twilio_voice toneIndigo
```

## Components

### Web experience

- [Web application](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/App.tsx): `frontend/src/App.tsx`
- [Geolocation](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/hooks/useGeolocation.ts): `frontend/src/hooks/useGeolocation.ts`
- [Location picker](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/LocationPicker.tsx): `frontend/src/components/LocationPicker.tsx`
- [Web API client](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/api/client.ts): `frontend/src/api/client.ts`
- [Risk dashboard](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/Dashboard.tsx): `frontend/src/components/Dashboard.tsx`
- [Insight chat](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/ChatWidget.tsx): `frontend/src/components/ChatWidget.tsx`
- [Phone and SMS](https://github.com/the-bhuvi/mazh-ai-/blob/Main/frontend/src/components/PhoneCard.tsx): `frontend/src/components/PhoneCard.tsx`

### Insight service

- [FastAPI service](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/main.py): `backend/app/main.py`
- [Location resolution](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/location.py): `backend/app/location.py`
- [Insight builder](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/engine/insight.py): `backend/app/engine/insight.py`
- [Insight cache](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/cache.py): `backend/app/cache.py`
- [Chat reply engine](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/llm/client.py): `backend/app/llm/client.py`

### Weather and climate

- [Forecast provider](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/providers/open_meteo.py): `backend/app/providers/open_meteo.py`
- [Historical weather](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/providers/historical_weather.py): `backend/app/providers/historical_weather.py`
- [Climate context](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/climate_service.py): `backend/app/climate_service.py`

### Rainfall model

- [ML runtime adapter](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/ml_engine.py): `backend/app/ml_engine.py`
- [Risk inference](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/infer.py): `backend/ml/infer.py`
- [Feature engineering](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/features.py): `backend/ml/features.py`
- [Training data](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/data.py): `backend/ml/data.py`
- [Model training](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/ml/train.py): `backend/ml/train.py`

### Phone channels

- [SMS adapter](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/sms/twilio_adapter.py): `backend/app/sms/twilio_adapter.py`
- [Twilio IVR webhooks](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/routes.py): `backend/app/voice/routes.py`
- [Fixed voice phrases](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/phrases.py): `backend/app/voice/phrases.py`
- [Signed call state](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/state.py): `backend/app/voice/state.py`
- [Voice audio](https://github.com/the-bhuvi/mazh-ai-/blob/Main/backend/app/voice/audio.py): `backend/app/voice/audio.py`
- [Asterisk AGI](https://github.com/the-bhuvi/mazh-ai-/blob/Main/asterisk/agi/mazh_weather.py): `asterisk/agi/mazh_weather.py`

### Other components

- **Web user**
- **Phone caller**
- **Open-Meteo**
- **Twilio SMS**
- **Twilio voice**

## Connections

- Web user → Web application: uses (evidence: `frontend/src/App.tsx`)
- Web application → Geolocation: gets location (evidence: `frontend/src/App.tsx`)
- Web application → Location picker: offers fallback (evidence: `frontend/src/App.tsx`)
- Web application → Web API client: requests insight (evidence: `frontend/src/App.tsx`)
- Web API client → FastAPI service: HTTP request (evidence: `frontend/src/api/client.ts`)
- FastAPI service → Location resolution: resolves location (evidence: `backend/app/main.py`)
- FastAPI service → Insight builder: builds insight (evidence: `backend/app/main.py`)
- FastAPI service → Forecast provider: fetches forecast (evidence: `backend/app/main.py`)
- FastAPI service → Historical weather: fetches archive (evidence: `backend/app/main.py`)
- FastAPI service → Insight cache: uses cache (evidence: `backend/app/main.py`)
- Insight builder → ML runtime adapter: estimates risk (evidence: `backend/app/engine/insight.py`)
- Insight builder → Climate context: adds context
- Forecast provider → Open-Meteo: fetches data (evidence: `backend/app/providers/open_meteo.py`)
- Historical weather → Forecast provider: reuses provider (evidence: `backend/app/providers/historical_weather.py`)
- ML runtime adapter → Risk inference: loads and predicts (evidence: `backend/app/ml_engine.py`)
- Risk inference → Feature engineering: builds features (evidence: `backend/ml/infer.py`)
- Model training → Training data: loads data (evidence: `backend/ml/train.py`)
- Model training → Feature engineering: builds features (evidence: `backend/ml/train.py`)
- Web application → Risk dashboard: renders (evidence: `frontend/src/App.tsx`)
- Web application → Insight chat: renders (evidence: `frontend/src/App.tsx`)
- Web application → Phone and SMS: renders (evidence: `frontend/src/App.tsx`)
- Insight chat → Web API client: sends chat (evidence: `frontend/src/components/ChatWidget.tsx`)
- Phone and SMS → Web API client: requests SMS (evidence: `frontend/src/components/PhoneCard.tsx`)
- FastAPI service → Chat reply engine: generates reply (evidence: `backend/app/main.py`)
- FastAPI service → SMS adapter: sends SMS (evidence: `backend/app/main.py`)
- SMS adapter → Twilio SMS: delivers message (evidence: `backend/app/sms/twilio_adapter.py`)
- Phone caller → Twilio voice: calls (evidence: `README.md`)
- Twilio voice → Twilio IVR webhooks: posts webhooks (evidence: `README.md`)
- Twilio IVR webhooks → Insight builder: prefetches insight (evidence: `backend/app/voice/routes.py`)
- Twilio IVR webhooks → Fixed voice phrases: uses phrases (evidence: `backend/app/voice/routes.py`)
- Twilio IVR webhooks → Signed call state: validates state (evidence: `backend/app/voice/routes.py`)
- Twilio IVR webhooks → SMS adapter: sends SMS (evidence: `backend/app/voice/routes.py`)
- Twilio IVR webhooks → Voice audio: serves audio
- Phone caller → Asterisk AGI: calls (evidence: `README.md`)
- Asterisk AGI → FastAPI service: calls phone API (evidence: `README.md`)
