How himanshuchoudhary247/farm_logging works
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: main.py Service API · personalized.py Personalized advisory · api.py Onboarding service · service.py Cache refresh
Components 22
Public Edge
Nginx routes the React portal and service paths.
- Nginx gateway · TLS termination and path-based routing.
deploy/nginx-farmer-chat.conf
User Clients
- React portal
frontend/src/App.tsx - Streamlit assistant
app.py - Onboarding UI
onboarding_app.py
Core API & Data
- Service API · Data and authentication boundary in service mode.
services/api_service/main.py - Service gateway · Makes Streamlit use service endpoints in service mode.
gateways.py - Authentication & roles
auth.py - Shared JSON storage · Locked file writes protect shared local records.
storage.py - Farm records
data/farmers.json
Advisory Services
- Personalized advisory · Combines farmer and farm context with signals.
services/advisory/personalized.py - Market prices
services/market_prices/mandi.py - Appointment supervisor
services/appointment_supervisor/service.py - Weather alerts
services/weather_alert/service.py - Emergency alerts · Scans emergency signals including Kisan Suvidha retrieval.
services/emergency_alerts/scan.py
LLM, Query & Voice
- LLM service · Service-mode completion endpoint with Bedrock integration.
services/llm_service/main.py - AWS Bedrock
- Query agent · Owns conversational query schema and database boundary.
services/query_agent/agent.py - Voice agent · Orchestrates transcription, extraction, and speech synthesis.
services/voice_agent/orchestrator.py - Onboarding service · Extracts fields, tracks missing data, and persists completed profiles.
services/farmer_onboarding_service/api.py
Refresh & Deployment
- Cache refresh · Retains valid PIN artifacts and rebuilds expired alerts.
services/cache_refresh/service.py - PIN cache artifacts
- Cron cache schedule · Runs refresh rather than an always-on scheduler.
deploy/cache-refresh.cron
Connections 25
- 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