# himanshuchoudhary247/farm_logging architecture

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

- Interactive diagram: https://gitdiagram.com/himanshuchoudhary247/farm_logging
- Repository: https://github.com/himanshuchoudhary247/farm_logging

## Overview

#### Purpose and Entry Points
Farmer Livestock Assistant is a Python/Streamlit livestock-support system for farmer Q&A, health and operational data, weather/emergency advisories, appointments, personalized recommendations, and conversational/voice onboarding. JSON files are the primary persistence layer.

Primary user-facing entry points are `app.py` (main assistant), `portal_app.py` (portal), `weather_streamlit_app.py` (weather UI), and `onboarding_app.py` (onboarding UI). A React portal is independently built from `frontend/src/main.tsx` and `frontend/src/App.tsx`.

#### UI and Edge Routing
Nginx is the intended public boundary: it serves the compiled React site and routes path-prefixed traffic to Streamlit and API services. Service definitions and proxy configuration are in `deploy/nginx-farmer-chat.conf`, `deploy/farmer-portal.service`, and `deploy/farmer-assistant.service`.

Legacy Streamlit experiences remain operational alongside the React migration. The frontend communicates with backend endpoints through `frontend/src/api.ts`; it should not own domain storage or LLM orchestration.

#### Core Data, Identity, and API Boundary
Shared domain structures are concentrated in `models.py`, while file-backed persistence is implemented in `storage.py`. Seed JSON datasets under `data/farmers.json`, `data/farms.json`, and related records establish the local datastore.

Authentication and roles are handled by `auth.py`. In service-split mode (`APP_MODE=services`), `services/api_service/main.py` exposes data/auth operations and `gateways.py` makes the Streamlit app a client of service endpoints rather than direct in-process logic. File locking is necessary for safe JSON writes; multi-worker deployment is intentionally constrained.

#### Advisory and Operational Intelligence
Personalized advisory composition is in `services/advisory/personalized.py`, combining farmer/farm context with cached or fetched signals. Market-price integration resides in `services/market_prices/mandi.py`; appointment workflow ownership is separated into `services/appointment_supervisor/service.py`.

Weather and hazard processing are separate service domains: `services/weather_alert/service.py` provides weather alerts, while `services/emergency_alerts/api.py` and `services/emergency_alerts/scan.py` expose and scan emergency signals. Emergency alert ingestion includes Kisan Suvidha retrieval in `services/emergency_alerts/fetch_kisansuvidha.py`.

#### Cache Refresh Pipeline
PIN-specific profiles and refresh policy form a configuration boundary in `config/pincode_profiles.yaml` and `config/cache_settings.yaml`. `scripts/refresh_caches.py` invokes refresh logic in `services/cache_refresh/service.py`, retaining valid per-service artifacts and rebuilding general alerts only when intervals expire.

Cache output is file-backed under `data/cache/<pin>/` at runtime. Scheduling is designed for cron/systemd deployment via `deploy/cache-refresh.cron`, not an always-on queue or database-backed scheduler.

#### LLM, Query, and Voice Flows
LLM provider selection, prompts, and adapters are centralized in `llm/config.py`, `llm/prompts.py`, and `llm/adapters.py`; service mode additionally exposes completion through `services/llm_service/main.py` and Bedrock-specific integration in `services/llm_service/bedrock_adapter.py`. AWS Bedrock is the material production inference dependency.

Structured conversational retrieval/query behavior is isolated in `services/query_agent/agent.py` with its schema/database boundary in `services/query_agent/schema.py` and `services/query_agent/db.py`. Voice processing is staged as audio upload/session handling, transcription, extraction/orchestration, and speech synthesis in `services/voice_agent/orchestrator.py`, `services/voice_agent/transcribe.py`, and `services/voice_agent/tts.py`; supported external services include Google Speech Recognition and AWS Transcribe/Polly alternatives.

#### Conversational Onboarding
Onboarding is a distinct bounded service: request models, field extraction, stateful flow, and API exposure live in `services/farmer_onboarding_service/models.py`, `services/farmer_onboarding_service/extraction.py`, and `services/farmer_onboarding_service/api.py`. The Streamlit onboarding client is `onboarding_app.py`, with compatibility/API entry logic in `onboarding_api.py`.

The control flow is: text or transcribed voice plus prior conversation → LLM-assisted field extraction → merge with existing farmer/farm state → identify missing fields and next question → persist completed profile through the shared storage boundary. Hindi and Kannada extraction are an explicit product requirement.

#### Runtime and Infrastructure
The target deployment is Ubuntu on AWS EC2 in `ap-south-1`, with systemd-managed services and Nginx TLS termination/path routing. AWS setup artifacts include `aws/iam-bedrock-invoke.json`, `aws/env.example`, and `aws/templates/farmer-onboarding.yaml`. Python 3.9+, Streamlit, and AWS Bedrock credentials are required; React/Vite is only required to build the new portal.

Main entry points: [`services/api_service/main.py`](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/api_service/main.py) (Service API), [`services/advisory/personalized.py`](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/advisory/personalized.py) (Personalized advisory), [`services/farmer_onboarding_service/api.py`](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/farmer_onboarding_service/api.py) (Onboarding service), [`services/cache_refresh/service.py`](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/cache_refresh/service.py) (Cache refresh)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Public Edge"]
  node_nginx{{"Nginx gateway<br/>reverse proxy"}}
end

subgraph group_clients["User Clients"]
  node_react["React portal<br/>Vite frontend<br/>[App.tsx]"]
  node_streamlit["Streamlit assistant<br/>Python UI<br/>[app.py]"]
  node_onboarding_ui["Onboarding UI<br/>Streamlit UI<br/>[onboarding_app.py]"]
end

subgraph group_core["Core API &amp; Data"]
  node_api["Service API<br/>Python API<br/>[main.py]"]
  node_gateway["Service gateway<br/>API client<br/>[gateways.py]"]
  node_auth["Authentication &amp; roles<br/>identity module<br/>[auth.py]"]
  node_storage[("Shared JSON storage<br/>persistence layer<br/>[storage.py]")]
  node_seed_data[("Farm records<br/>JSON datastore<br/>[farmers.json]")]
end

subgraph group_advisory["Advisory Services"]
  node_advisory_service["Personalized advisory<br/>advisory service<br/>[personalized.py]"]
  node_market["Market prices<br/>market integration<br/>[mandi.py]"]
  node_appointments["Appointment supervisor<br/>workflow service<br/>[service.py]"]
  node_weather["Weather alerts<br/>weather service<br/>[service.py]"]
  node_emergency["Emergency alerts<br/>hazard service<br/>[scan.py]"]
end

subgraph group_ai["LLM, Query &amp; Voice"]
  node_llm_service["LLM service<br/>inference API<br/>[main.py]"]
  node_bedrock{{"AWS Bedrock<br/>external inference"}}
  node_query_agent["Query agent<br/>structured retrieval<br/>[agent.py]"]
  node_voice["Voice agent<br/>voice pipeline<br/>[orchestrator.py]"]
  node_onboarding["Onboarding service<br/>conversation workflow<br/>[api.py]"]
end

subgraph group_operations["Refresh &amp; Deployment"]
  node_cache_refresh["Cache refresh<br/>batch refresh service<br/>[service.py]"]
  node_pin_cache[("PIN cache artifacts<br/>file cache")]
  node_scheduler["Cron cache schedule<br/>scheduled job<br/>[cache-refresh.cron]"]
end

node_nginx -->|"serves"| node_react
node_nginx -->|"routes"| node_streamlit
node_nginx -->|"routes"| node_api
node_react -->|"API requests"| node_api
node_streamlit -.->|"service mode"| node_gateway
node_gateway -->|"calls"| node_api
node_onboarding_ui -->|"conversation requests"| node_onboarding
node_api -->|"authenticates"| node_auth
node_api -->|"reads and writes"| node_storage
node_storage -->|"persists records"| node_seed_data
node_api -->|"requests advice"| node_advisory_service
node_api -->|"manages appointments"| node_appointments
node_advisory_service -->|"uses farm context"| node_storage
node_advisory_service -->|"uses prices"| node_market
node_advisory_service -->|"uses weather"| node_weather
node_advisory_service -->|"uses hazards"| node_emergency
node_api -->|"structured queries"| node_query_agent
node_onboarding -.->|"accepts transcribed voice"| node_voice
node_onboarding -->|"extracts fields"| node_llm_service
node_onboarding -->|"persists profile"| node_storage
node_llm_service -->|"invokes"| node_bedrock
node_scheduler -->|"runs"| node_cache_refresh
node_cache_refresh -->|"writes artifacts"| node_pin_cache
node_cache_refresh -->|"refreshes signals"| node_weather
node_cache_refresh -->|"refreshes signals"| node_emergency

click node_nginx "https://github.com/himanshuchoudhary247/farm_logging/blob/main/deploy/nginx-farmer-chat.conf"
click node_react "https://github.com/himanshuchoudhary247/farm_logging/blob/main/frontend/src/App.tsx"
click node_streamlit "https://github.com/himanshuchoudhary247/farm_logging/blob/main/app.py"
click node_onboarding_ui "https://github.com/himanshuchoudhary247/farm_logging/blob/main/onboarding_app.py"
click node_api "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/api_service/main.py"
click node_gateway "https://github.com/himanshuchoudhary247/farm_logging/blob/main/gateways.py"
click node_auth "https://github.com/himanshuchoudhary247/farm_logging/blob/main/auth.py"
click node_storage "https://github.com/himanshuchoudhary247/farm_logging/blob/main/storage.py"
click node_seed_data "https://github.com/himanshuchoudhary247/farm_logging/blob/main/data/farmers.json"
click node_advisory_service "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/advisory/personalized.py"
click node_market "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/market_prices/mandi.py"
click node_appointments "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/appointment_supervisor/service.py"
click node_weather "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/weather_alert/service.py"
click node_emergency "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/emergency_alerts/scan.py"
click node_llm_service "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/llm_service/main.py"
click node_query_agent "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/query_agent/agent.py"
click node_voice "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/voice_agent/orchestrator.py"
click node_onboarding "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/farmer_onboarding_service/api.py"
click node_cache_refresh "https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/cache_refresh/service.py"
click node_scheduler "https://github.com/himanshuchoudhary247/farm_logging/blob/main/deploy/cache-refresh.cron"

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_nginx toneBlue
class node_react,node_streamlit,node_onboarding_ui toneAmber
class node_api,node_gateway,node_auth,node_storage,node_seed_data toneMint
class node_advisory_service,node_market,node_appointments,node_weather,node_emergency toneRose
class node_llm_service,node_bedrock,node_query_agent,node_voice,node_onboarding toneIndigo
class node_cache_refresh,node_pin_cache,node_scheduler toneTeal
```

## Components

### Public Edge

Nginx routes the React portal and service paths.

- [Nginx gateway](https://github.com/himanshuchoudhary247/farm_logging/blob/main/deploy/nginx-farmer-chat.conf): reverse proxy · TLS termination and path-based routing. · `deploy/nginx-farmer-chat.conf`

### User Clients

- [React portal](https://github.com/himanshuchoudhary247/farm_logging/blob/main/frontend/src/App.tsx): Vite frontend · `frontend/src/App.tsx`
- [Streamlit assistant](https://github.com/himanshuchoudhary247/farm_logging/blob/main/app.py): Python UI · `app.py`
- [Onboarding UI](https://github.com/himanshuchoudhary247/farm_logging/blob/main/onboarding_app.py): Streamlit UI · `onboarding_app.py`

### Core API & Data

- [Service API](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/api_service/main.py): Python API · Data and authentication boundary in service mode. · `services/api_service/main.py`
- [Service gateway](https://github.com/himanshuchoudhary247/farm_logging/blob/main/gateways.py): API client · Makes Streamlit use service endpoints in service mode. · `gateways.py`
- [Authentication & roles](https://github.com/himanshuchoudhary247/farm_logging/blob/main/auth.py): identity module · `auth.py`
- [Shared JSON storage](https://github.com/himanshuchoudhary247/farm_logging/blob/main/storage.py): persistence layer · Locked file writes protect shared local records. · `storage.py`
- [Farm records](https://github.com/himanshuchoudhary247/farm_logging/blob/main/data/farmers.json): JSON datastore · `data/farmers.json`

### Advisory Services

- [Personalized advisory](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/advisory/personalized.py): advisory service · Combines farmer and farm context with signals. · `services/advisory/personalized.py`
- [Market prices](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/market_prices/mandi.py): market integration · `services/market_prices/mandi.py`
- [Appointment supervisor](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/appointment_supervisor/service.py): workflow service · `services/appointment_supervisor/service.py`
- [Weather alerts](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/weather_alert/service.py): weather service · `services/weather_alert/service.py`
- [Emergency alerts](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/emergency_alerts/scan.py): hazard service · Scans emergency signals including Kisan Suvidha retrieval. · `services/emergency_alerts/scan.py`

### LLM, Query & Voice

- [LLM service](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/llm_service/main.py): inference API · Service-mode completion endpoint with Bedrock integration. · `services/llm_service/main.py`
- **AWS Bedrock**: external inference
- [Query agent](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/query_agent/agent.py): structured retrieval · Owns conversational query schema and database boundary. · `services/query_agent/agent.py`
- [Voice agent](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/voice_agent/orchestrator.py): voice pipeline · Orchestrates transcription, extraction, and speech synthesis. · `services/voice_agent/orchestrator.py`
- [Onboarding service](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/farmer_onboarding_service/api.py): conversation workflow · Extracts fields, tracks missing data, and persists completed profiles. · `services/farmer_onboarding_service/api.py`

### Refresh & Deployment

- [Cache refresh](https://github.com/himanshuchoudhary247/farm_logging/blob/main/services/cache_refresh/service.py): batch refresh service · Retains valid PIN artifacts and rebuilds expired alerts. · `services/cache_refresh/service.py`
- **PIN cache artifacts**: file cache
- [Cron cache schedule](https://github.com/himanshuchoudhary247/farm_logging/blob/main/deploy/cache-refresh.cron): scheduled job · Runs refresh rather than an always-on scheduler. · `deploy/cache-refresh.cron`

## Connections

- Nginx gateway → React portal: serves
- Nginx gateway → Streamlit assistant: routes
- Nginx gateway → Service API: routes
- React portal → Service API: API requests
- Streamlit assistant → Service gateway: service mode
- Service gateway → Service API: calls
- Onboarding UI → Onboarding service: conversation requests
- Service API → Authentication & roles: authenticates
- Service API → Shared JSON storage: reads and writes
- Shared JSON storage → Farm records: persists records
- Service API → Personalized advisory: requests advice
- Service API → Appointment supervisor: manages appointments
- Personalized advisory → Shared JSON storage: uses farm context
- Personalized advisory → Market prices: uses prices
- Personalized advisory → Weather alerts: uses weather
- Personalized advisory → Emergency alerts: uses hazards
- Service API → Query agent: structured queries
- Onboarding service → Voice agent: accepts transcribed voice
- Onboarding service → LLM service: extracts fields
- Onboarding service → Shared JSON storage: persists profile
- LLM service → AWS Bedrock: invokes
- Cron cache schedule → Cache refresh: runs
- Cache refresh → PIN cache artifacts: writes artifacts
- Cache refresh → Weather alerts: refreshes signals
- Cache refresh → Emergency alerts: refreshes signals
