Agromind-backend / README.md
gh-action-hf-auto
auto: sync backend from github@32fb9685
8a6248c
metadata
title: Agromind Backend
emoji: πŸš€
colorFrom: green
colorTo: blue
sdk: docker
app_port: 7860

AgroMind: Where Farmers Meet AI & Technology for a Greener Future! 🌾

Alt Landing Page

AgroMind is an innovative platform designed to empower farmers by connecting them with agricultural experts, AI-powered tools, and modern technology. Our goal is to make farming smarter, more efficient, and more sustainable.

πŸš€ Key Features

Core Features

  • Expert Consultations - Real-time video calls and chat with agricultural experts
  • AI-Powered Recommendations - Crop, fertilizer, and yield predictions
  • Task Management - Goal-based scheduling and tracking
  • Weather Alerts - Real-time weather updates and recommendations
  • Revenue Tracking - Income and expense management

New Features (v2.0)

Feature Description
Value Chain Marketplace Connect farmers, processors, and buyers for oilseed by-products
Hedging Platform Virtual hedging, price risk management, forward contracts
Crop Economics Comparative crop analysis, govt schemes, profitability simulation
Oil Palm Advisory Farmer profiling, ROI projections, gestation support tracking
Yield Optimization AI-driven yield predictions with intervention suggestions
Tariff Simulator Model impact of customs duty changes on prices
Millets Marketplace Specialized marketplace with traceability and offline support
CRM Machine Tracking Real-time tracking of crop residue management machines
CROPIC AI-based crop damage assessment for insurance

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        Frontend                              β”‚
β”‚                    (React.js + Vite)                        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β–Ό               β–Ό               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Backend  β”‚   β”‚ AI Backendβ”‚   β”‚ Smart Contractsβ”‚
β”‚ Node.js  β”‚   β”‚  Python  β”‚   β”‚   Solidity    β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚              β”‚
     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜
            β–Ό
      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
      β”‚ MongoDB  β”‚
      β”‚  Redis   β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“¦ Quick Start

Prerequisites

  • Node.js 20+
  • Python 3.11+
  • Docker & Docker Compose (recommended)
  • MongoDB

Local Development with Docker

# Clone repository
git clone https://github.com/Anamitra-Sarkar/AgroMind.git
cd AgroMind

# Copy environment file
cp .env.sample .env

# Start all services
docker-compose up -d

# Access applications
# Frontend: http://localhost:5173
# Backend: http://localhost:8000
# AI Backend: http://localhost:5000

Manual Setup

# Backend
cd backend && npm install && npm run dev

# AI Backend
cd ai-backend && pip install -r requirements.txt && python app.py

# Frontend
cd frontend && npm install && npm run dev

πŸ”§ Environment Variables

See .env.sample for all required environment variables.

Key Variables

Variable Description
MONGO_URL MongoDB connection string
JWT_KEY JWT signing key
FRONTEND_URL Frontend URL for CORS
AI_BACKEND_URL AI backend URL
OPENWEATHER_API_KEY OpenWeather API key
GEMINI_API_KEY Google Gemini AI key

πŸ› οΈ Technology Stack

Frontend

  • React.js 18 + Vite
  • TailwindCSS + Material-UI
  • Chart.js + Recharts
  • Socket.IO Client
  • i18next (internationalization)

Backend

  • Node.js + Express.js
  • MongoDB + Mongoose
  • Redis (caching)
  • Socket.IO
  • JWT Authentication

AI Backend

  • Python + Flask
  • PyTorch + scikit-learn
  • LightGBM (price forecasting)
  • ResNet (image classification)

Infrastructure

  • Docker + Docker Compose
  • GitHub Actions CI/CD
  • Vercel (frontend hosting)
  • Hugging Face Spaces (backend hosting)
  • Prometheus + Grafana (monitoring)

πŸ“š API Documentation

Backend API Endpoints

Endpoint Description
/api/auth/* Authentication
/api/valuechain/* Marketplace
/api/hedging/* Hedging platform
/api/crop-economics/* Crop comparison
/api/oilpalm/* Oil palm advisory
/api/crm/* Machine tracking
/api/millets/* Millets marketplace

AI Backend Endpoints

Endpoint Description
/ai/price-forecast Price predictions
/ai/yield-predict Yield predictions
/ai/tariff-simulate Tariff impact simulation
/ai/cropic/analyze Crop damage analysis
/crop_recommendation Crop recommendations
/fertilizer_prediction Fertilizer suggestions

πŸ“„ Full API documentation: docs/postman_collection.json

πŸ§ͺ Testing

# Backend tests
cd backend && npm test

# AI Backend tests
cd ai-backend && pytest tests/ -v

# Frontend tests
cd frontend && npm test

# E2E tests
cd frontend && npm run cypress:open

# Smart contract tests
cd backend/contracts && npm test

AI Backend Testing Details

The AI backend now includes comprehensive test coverage with:

Test Coverage:

  • Unit tests for model predictions (tests/test_models.py) - 15 passing, 5 skipped
  • Integration tests for all API endpoints (tests/test_api_integration.py)
  • Existing validation tests (tests/test_endpoints.py)

Test Features:

  • Mocked models for fast, deterministic tests (no HF downloads)
  • Retry logic testing with transient failures
  • Error handling and exception logging validation
  • Content-Type and JSON payload validation
  • Model loading and caching tests

Run with Coverage:

cd ai-backend
pytest tests/ -v --cov=. --cov-report=html

For detailed testing documentation and sample payloads, see docs/TESTING.md.

πŸš€ Deployment

Frontend β†’ Vercel

cd frontend
vercel --prod

Hugging Face Spaces β†’ Backend only

To push only the backend and ai-backend folders (avoid large frontend/binary files), use the helper script:

Example:

chmod +x scripts/push_to_hf.sh
./scripts/push_to_hf.sh https://huggingface.co/spaces/<username>/<repo>

This creates a temporary git repo containing only backend and ai-backend and force-pushes main to the provided remote.

GitHub Action (recommended)

You can automate the push using the provided GitHub Action. It creates a temporary repo with only backend and ai-backend and pushes it to your Hugging Face Space.

  1. Add a repository secret named HF_TOKEN containing a Hugging Face token with repo write access.
  2. Run the workflow manually from the Actions tab and provide the hf_repo input (e.g. username/Agromind-backend).

Workflow options:

  • hf_branch: target branch on the Hugging Face repo (default main).
  • force: set to true to force-push the target branch (default false). Avoid force-push unless you intentionally want to overwrite history.
  • dry_run: set to true to prepare the temporary repo and list files without pushing (default false).

Recommended safe flow:

  1. Run with dry_run=true to verify what will be pushed.
  2. Run with force=false to push to a branch without overwriting history. If you specifically need to replace the remote branch, set force=true.

The workflow file is .github/workflows/push_to_hf.yml.

Backend β†’ Hugging Face Spaces

See docs/deploy.md for detailed deployment instructions.

Required GitHub Secrets

VERCEL_TOKEN
VERCEL_ORG_ID
VERCEL_PROJECT_ID
HF_TOKEN
HF_BACKEND_SPACE_ID
HF_AI_BACKEND_SPACE_ID

πŸ“ Project Structure

AgroMind/
β”œβ”€β”€ frontend/           # React frontend
β”œβ”€β”€ backend/           # Node.js backend
β”‚   β”œβ”€β”€ routes/        # API routes
β”‚   β”œβ”€β”€ controllers/   # Route handlers
β”‚   β”œβ”€β”€ models/        # MongoDB models
β”‚   β”œβ”€β”€ middleware/    # Express middleware
β”‚   β”œβ”€β”€ socket/        # Socket.IO handlers
β”‚   └── contracts/     # Smart contracts
β”œβ”€β”€ ai-backend/        # Python AI backend
β”‚   β”œβ”€β”€ model/         # ML models
β”‚   └── tests/         # Python tests
β”œβ”€β”€ docs/              # Documentation
β”œβ”€β”€ config/            # Configuration files
β”œβ”€β”€ scripts/           # Utility scripts
└── docker-compose.yml # Local development

πŸ“– Documentation

πŸ” Security

  • JWT-based authentication
  • Rate limiting and CORS
  • Input validation and sanitization
  • Encrypted data storage
  • Regular dependency scanning

See docs/security.md for the full security checklist.

🀝 Contributing

We welcome contributions! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the ISC License.

πŸ“ž Support

For support, email support@agromind.app or join our community.


AgroMind: Empowering farmers with technology for a greener, smarter future! 🌱