Skip to content

Latest commit

Β 

History

14 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Agri-AI Advisory System 🌾

React Vite Node.js Express MongoDB TensorFlow Google Gemini Socket.io WebRTC

An intelligent agricultural advisory platform designed to bridge the gap between farmers and verified agricultural experts. The system leverages AI-powered crop recommendations, CNN-based image-to-soil detection, real-time location mapping, a multilingual generative voice chatbot, and real-time Socket.io-driven video/audio communication.


πŸ—οΈ System Architecture

The application comprises three core components:

  1. React Frontend (Vite): An interactive, multilingual, and highly responsive user interface.
  2. Node.js Express Server: The central logic handler, managing user roles (Farmer, Expert, Admin), live signaling, and community forums.
  3. Python Flask ML Service: A microservice dedicated to running TensorFlow models for crop recommendations and soil classifications.
graph TD
    classDef client fill:#e1f5fe,stroke:#039be5,stroke-width:2px;
    classDef node fill:#efebe9,stroke:#5d4037,stroke-width:2px;
    classDef python fill:#e8f5e9,stroke:#2e7d32,stroke-width:2px;
    classDef db fill:#fbe9e7,stroke:#d84315,stroke-width:2px;
    classDef external fill:#fff8e1,stroke:#f57f17,stroke-width:2px;

    %% Nodes
    React[React Frontend<br/>Vite / Socket.io Client]:::client
    Express[Express Backend<br/>Node.js - Port 5000]:::node
    Flask[ML Microservice<br/>Python Flask - Port 5001]:::python
    Mongo[(MongoDB Database)]:::db
    
    %% External API Nodes
    Gemini[Google Gemini API]:::external
    TTS[Google TTS & Translate]:::external
    OSM[OpenStreetMap Nominatim]:::external
    HF[HuggingFace API]:::external
    Reddit[Reddit API]:::external

    %% Connections
    React <-->|Socket.io / HTTP| Express
    React -->|navigator.geolocation| OSM
    Express <-->|Mongoose| Mongo
    Express <-->|Axios Proxy| Flask
    Express <-->|Google GenAI SDK| Gemini
    Express <-->|Axios Proxy| TTS
    Express <-->|Inference SDK| HF
    Express <-->|Axios| Reddit

    %% Sub-features inside Flask
    subgraph Python Flask Services
        Flask -->|Predict Crop| CropModel[crop_model.h5<br/>TensorFlow Neural Network]
        Flask -->|Classify Soil| SoilModel[soil_model_cnn.h5<br/>ResNet50 CNN]
    end

    %% Sub-features inside Node
    subgraph Express Services
        Express -->|Chat requests / signalling| SocketIO[Socket.io Signaling Server]
        SocketIO <-->|WebRTC Video Calls| React
    end
Loading

🌟 Core Features & Technical Implementations

1. Smart Crop Predictor

  • Feature: Allows farmers to input soil and climate parameters (Nitrogen, Phosphorus, Potassium, pH, humidity, temperature, rainfall) to find the most suitable crop to plant.
  • Implementation: The parameters are sent to the Python Flask microservice, scaled using a pre-saved scaler.pkl, and classified using a trained neural network (crop_model.h5). The result is mapped back using label_encoder.pkl and returned to the frontend.

2. CNN Image-to-Soil Classification

  • Feature: Farmers can upload an image of their soil to immediately classify its type (Red, Black, Alluvial, Laterite) and obtain a plausible pH level.
  • Implementation: Uses a ResNet50 Convolutional Neural Network architecture built inside train_soil_cnn.py. The Flask service accepts the image at POST /predict-soil, resizes it to $224 \times 224$, runs it through the preprocessor, and feeds it into the CNN model soil_model_cnn.h5.

3. Geolocation & Area Mapping

  • Feature: Automatically maps the farmer's location during registration/profile updates to deduce localized weather and regional soil patterns.
  • Implementation: Uses the native HTML5 Web Geolocation API (navigator.geolocation) to extract { latitude, longitude } coordinates. The frontend resolves these coordinates via the OpenStreetMap Nominatim reverse geocoding API to identify local divisions (suburb, village, city, county).

4. Multilingual Generative Voice Chatbot

  • Feature: Voice-enabled AI chatbot allowing farmers to speak queries in English, Hindi, or Telugu and receive tailored responses from distinct AI expert "personas".
  • Implementation: Utilizes the browser-based Web Speech API for live transcription. The text is sent to the Node.js backend which coordinates requests to the Google Gemini API (gemini-2.5-flash) using customized prompts tailored for Weather, Fertilizer, or Disease experts. The response is spoken back via a custom node proxy endpoint /api/tts querying Google Translate's TTS stream.

5. Open Knowledge Community Forum

  • Feature: A collaborative forum where farmers post agricultural questions and verified experts post helpful answers.
  • Implementation: Managed by Mongoose Schemas (Post and Answer). It supports asynchronous comments, chronologically ordered feeds, and user-to-expert linkages.

6. Synchronous Expert Live Conferencing & Signaling

  • Feature: Farmers can request live consultations, message chat, or launch WebRTC video/audio sessions with verified experts.
  • Implementation: Built with Socket.io over HTTP. Standard chat messages are cached to a MongoDB collection (Message) for persistence. Audio/video calls use a custom WebRTC signaling protocol: when a farmer initiates a call, Socket.io manages call routing, offering, answering, and ICE candidate exchange. An expert approval mechanism restricts excessive calls through the database collection CallRecord.

7. Farmer-to-Expert Upgrade Pipeline (Admin Panel)

  • Feature: A workflow where experienced farmers can apply to become verified experts, reviewed and approved by administrators.
  • Implementation: Farmers submit applications from their profile dashboard. Admins can view pending applications, audit credentials, and trigger a PUT request to update their role to expert, automatically instantiating their Expert details in the database.

πŸ“ Repository Structure

agri-ai-advisory-system/
β”œβ”€β”€ backend/                  # Node.js Express server
β”‚   β”œβ”€β”€ agents/               # AI Agent prompts & instructions
β”‚   β”œβ”€β”€ config/               # Database and API setups
β”‚   β”œβ”€β”€ controllers/          # Request handler functions
β”‚   β”œβ”€β”€ models/               # MongoDB Mongoose Schemas (User, Post, Answer, Expert, etc.)
β”‚   β”œβ”€β”€ routes/               # Express endpoints (auth, crops, admin, etc.)
β”‚   β”œβ”€β”€ services/             # Third-party integrations (Gemini, HuggingFace)
β”‚   β”œβ”€β”€ uploads/              # Temp storage for soil image classification
β”‚   β”œβ”€β”€ utils/                # Helper files
β”‚   β”œβ”€β”€ server.js             # Main server entrypoint
β”‚   └── .env                  # Server environment configuration
β”‚
β”œβ”€β”€ frontend/                 # React client application (Vite)
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/              # API caller functions (axios)
β”‚   β”‚   β”œβ”€β”€ assets/           # Global assets & styling sheet
β”‚   β”‚   β”œβ”€β”€ components/       # Reusable components (Navbar, Chat, etc.)
β”‚   β”‚   β”œβ”€β”€ pages/            # Application pages (Forum, Market, Advisor, etc.)
β”‚   β”‚   β”œβ”€β”€ App.jsx           # Main routing & app composition
β”‚   β”‚   └── main.jsx          # React entrypoint
β”‚   β”œβ”€β”€ index.html
β”‚   └── vite.config.js
β”‚
β”œβ”€β”€ ml-service/               # Python ML Flask microservice
β”‚   β”œβ”€β”€ Crop_recommendation.csv
β”‚   β”œβ”€β”€ app.py                # Flask entry point (Port 5001)
β”‚   β”œβ”€β”€ crop_model.h5         # Crop classification weights
β”‚   β”œβ”€β”€ soil_model_cnn.h5     # Soil ResNet50 CNN model weights
β”‚   β”œβ”€β”€ train_model.py        # Model training script for Crops
β”‚   β”œβ”€β”€ train_soil_cnn.py     # ResNet50 soil classifier trainer
β”‚   └── requirements.txt      # Python dependencies
β”‚
β”œβ”€β”€ README.md                 # Project README
└── package.json              # Root configurations

πŸš€ Setup & Installation

1. Database Setup (MongoDB)

  • Set up a MongoDB cluster (local or Atlas) and copy the URI.
  • Note: The database seeds three default administrator accounts upon initial server startup:
    • Admins: admin1@harvestmate.com, admin2@harvestmate.com, or admin3@harvestmate.com
    • Password: admin123

2. Python ML Microservice Setup

Navigate to the machine learning service directory:

cd ml-service

Create a virtual environment and activate it:

# Windows
python -m venv venv
venv\Scripts\activate

# macOS/Linux
python3 -m venv venv
source venv/bin/activate

Install requirements:

pip install -r requirements.txt

Start the ML service (runs on http://localhost:5001):

python app.py

3. Backend Express Server Setup

Open a new terminal tab, navigate to the server directory:

cd server

Install dependencies:

npm install

Create a .env file in the server directory:

MONGO_URI=your_mongodb_connection_string
GEMINI_API_KEY=your_google_gemini_api_key
HF_TOKEN=your_huggingface_token
WEATHER_API_KEY=your_openweathermap_api_key
OPENAI_API_KEY=your_openai_api_key
PORT=5000

Start the server:

npm start

The server will boot on http://localhost:5000 and establish a MongoDB connection.

4. Frontend React Client Setup

Open a new terminal tab, navigate to the frontend directory:

cd frontend

Install packages:

npm install

Start the dev server:

npm run dev

Open your browser and visit http://localhost:5173.


πŸ”Œ API Documentation (Key Endpoints)

backend Express API

Method Endpoint Description
POST /auth/register Register new Farmers or Experts
POST /auth/login Login and acquire JWT
GET /experts Retrieve all verified experts
POST /request-chat Farmer registers a private chat request with an expert
GET /api/tts Proxy to play back generated spoken audio (bypassing CORS)
POST /api/detect-soil Multipart request that proxies soil image to Python Flask API
GET /api/daily-tip/:farmerId Generate a personalized daily tip using Gemini API
GET /api/agri-news Pull live hot topics from agriculture subreddits

Python Flask ML API

Method Endpoint Description
POST /predict Accept soil statistics (N, P, K, etc.) and predict optimal crop
POST /predict-soil Accept soil image stream and perform classification

🀝 Contributing

Contributions are highly encouraged! Please make sure to test your code locally, update configurations accordingly, and submit a pull request with clear description details.

πŸ“„ License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages