Skip to content

prath47/FarmSense-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

7 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌱 FarmSense AI

Your intelligent farming companion β€” powered by Gemini 2.5 Flash

FarmSense AI is a full-stack precision agriculture assistant that gives small and mid-size farmers access to expert-level crop disease diagnosis, irrigation scheduling, soil health advice, financial tracking, and proactive weather-aware alerts β€” all in a conversational interface.


πŸš€ What Makes It Different

Feature FarmSense AI Generic AI Chatbot
Location-aware by default βœ… Auto-detects GPS, injects region into every prompt ❌ User must describe location every time
Real-time weather in responses βœ… Open-Meteo API injected into irrigation prompts ❌ No live data
Proactive alerts βœ… AI scans sessions + 7-day forecast, pushes warnings ❌ Reactive only
Finance loop βœ… AI recommends β†’ "Log to Tracker" chip β†’ expense logged ❌ Advice stays in chat
Streaming responses βœ… Token-by-token SSE stream ❌ Full response wait
Multi-module memory βœ… Persistent sessions per module per user ❌ Stateless
Image diagnosis βœ… Upload crop photo for disease ID ❌ Text only

✨ Features

🌿 Crop Doctor

  • Upload a photo or describe symptoms to diagnose plant diseases
  • Confidence-rated diagnosis with severity levels (Mild / Moderate / Severe)
  • Organic treatments recommended first; chemicals only as last resort
  • Region-specific pest and disease advice based on detected location

πŸ’§ Irrigation Advisor

  • Calculates water requirements in liters per acre
  • Suggests optimal watering schedules with time-of-day recommendations
  • Real-time weather data (temperature, humidity, precipitation, wind) injected into every response
  • Flags drought risk and overwatering risk automatically

πŸͺ± Soil Health Advisor

  • Assesses soil health from description or photo
  • Recommends region-specific cover crops
  • Composting strategies with timelines
  • Designs 3-season crop rotation plans
  • Region context injected via auto-detected GPS location

πŸ“ Auto Location Detection

  • Silent background geolocation on app load
  • Reverse geocoding via Nominatim (OpenStreetMap) β€” no API key needed
  • Location pill shown in chat header (πŸ“ Thane, Maharashtra)
  • Automatically injected into all AI prompts β€” user never has to type their location

πŸ”” Proactive Alerts

  • AI scans your 3 most recent sessions against a 7-day weather forecast
  • Generates 2-3 actionable alerts per session (e.g. "Skip watering tomorrow β€” rain forecast Thursday")
  • Alerts cached for 1 hour, generated in parallel to avoid timeouts
  • Bell icon πŸ”” in top-right with unread count badge
  • Alert types: info, warning, urgent with color-coded cards
  • Dismissed alerts stored in localStorage β€” won't reappear

πŸ’° Finance Tracker ↔ AI Integration

  • After every AI response, a background Gemini call extracts any purchasing recommendations
  • "βž• Log to Tracker" chip buttons appear below AI messages
  • Click a chip β†’ pre-filled confirmation modal (description, category, amount, date)
  • Confirm β†’ POST to tracker, expense logged instantly
  • Categories: crops, fertilizers, electricity, labor, equipment, irrigation, other
  • Dashboard finance snapshot: Revenue, Expenses, Net Profit, ROI

⚑ Streaming Responses

  • Backend streams tokens via Server-Sent Events (SSE)
  • Frontend renders text word-by-word as it arrives
  • Thinking indicator (animated dots) shown until first token
  • No more mid-response truncation

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        BROWSER (Next.js 15)                     β”‚
β”‚                                                                 β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚  Auth Pages  β”‚  β”‚  Chat Modules β”‚  β”‚   Finance Tracker    β”‚ β”‚
β”‚  β”‚  /login      β”‚  β”‚  /crop-doctor β”‚  β”‚   /tracker           β”‚ β”‚
β”‚  β”‚  /register   β”‚  β”‚  /irrigation  β”‚  β”‚   + TransactionForm  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚  /soil-health β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                            β”‚
β”‚                            β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚                   Zustand Store                            β”‚ β”‚
β”‚  β”‚   token Β· user Β· location Β· sessions Β· dismissedAlerts     β”‚ β”‚
β”‚  β”‚   (persisted to localStorage via zustand/persist)          β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                            β”‚                                    β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚              useAutoLocation (background)                  β”‚ β”‚
β”‚  β”‚   navigator.geolocation β†’ Nominatim reverse geocode        β”‚ β”‚
β”‚  β”‚   β†’ setLocation({ lat, lon, city, region })                β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                               β”‚  HTTP / SSE
                               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     EXPRESS BACKEND (:4000)                     β”‚
β”‚                                                                 β”‚
β”‚  POST /api/chat/:module  ──────────────────────────────────┐   β”‚
β”‚    1. Get/create ChatSession (Prisma)                       β”‚   β”‚
β”‚    2. Persist user message                                  β”‚   β”‚
β”‚    3. Fetch weather (irrigation) or region (others)         β”‚   β”‚
β”‚    4. streamReplyFromMessages() β†’ SSE token stream ─────────┼─► β”‚ stream to browser
β”‚    5. Persist full AI reply                                 β”‚   β”‚
β”‚                                                             β”‚   β”‚
β”‚  POST /api/chat/extract-items                               β”‚   β”‚
β”‚    Gemini fast model β†’ extract purchase JSON                β”‚   β”‚
β”‚    (never persisted to DB)                                  β”‚   β”‚
β”‚                                                             β”‚   β”‚
β”‚  GET  /api/alerts?lat=&lon=                                 β”‚   β”‚
β”‚    Parallel Gemini calls per session + weather forecast     β”‚   β”‚
β”‚    1-hour cache per session                                 β”‚   β”‚
β”‚                                                             β”‚   β”‚
β”‚  GET/POST /api/tracker/transactions                         β”‚   β”‚
β”‚  GET      /api/tracker/summary                              β”‚   β”‚
β”‚  POST/GET /api/auth/*                                       β”‚   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               β”‚
       β”Œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β”‚                               β”‚
       β–Ό                               β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  PostgreSQL  β”‚              β”‚   Gemini 2.5 Flash  β”‚
β”‚  (Prisma)    β”‚              β”‚   (via LangChain)   β”‚
β”‚              β”‚              β”‚                     β”‚
β”‚  User        β”‚              β”‚  β€’ Chat replies     β”‚
β”‚  ChatSession β”‚              β”‚  β€’ Alert generation β”‚
β”‚  Message     β”‚              β”‚  β€’ Item extraction  β”‚
β”‚  Transaction β”‚              β”‚                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                        β”‚
                              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                              β”‚                    β”‚
                              β–Ό                    β–Ό
                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                     β”‚  Open-Meteo  β”‚    β”‚    Nominatim     β”‚
                     β”‚  Weather API β”‚    β”‚  Reverse Geocode β”‚
                     β”‚  (free)      β”‚    β”‚  (OpenStreetMap) β”‚
                     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ› οΈ Tech Stack

Frontend

Framework Next.js 15 (App Router) + React 19
State Zustand 4.5 with persist middleware
Styling Tailwind CSS + custom farm color palette
Charts Recharts (Pie + Bar)
Markdown react-markdown + remark-gfm
Icons lucide-react
HTTP Native fetch with SSE streaming

Backend

Runtime Node.js + Express 4
Language TypeScript (strict)
Database PostgreSQL via Prisma ORM
AI Google Gemini 2.5 Flash via @langchain/google-genai
Streaming Server-Sent Events (SSE)
Auth JWT (jsonwebtoken + bcryptjs)
Weather Open-Meteo API (free, no key needed)
Geocoding Nominatim / OpenStreetMap (free, no key needed)

External APIs (all free, no credit card)

API Used For
Google Gemini Chat replies, alert generation, item extraction
Open-Meteo Current weather + 7-day forecast
Nominatim (OSM) Reverse geocoding (lat/lon β†’ city, region)

πŸ“¦ Getting Started

Prerequisites

  • Node.js 18+
  • PostgreSQL database
  • Google Gemini API key (free tier available at aistudio.google.com)

Installation

# Clone and install all dependencies
git clone <repo>
cd FarmSense-AI
npm run install:all

Environment Setup

Create backend/.env:

DATABASE_URL=postgresql://user:password@localhost:5432/farmsense
GEMINI_API_KEY=your_gemini_api_key_here

# Optional: override AI models
GEMINI_MODEL=gemini-2.5-flash
GEMINI_FAST_MODEL=gemini-2.0-flash

Database Setup

cd backend
npx prisma migrate dev

Run

# Terminal 1 β€” backend on :4000
npm run backend

# Terminal 2 β€” frontend on :3000
npm run frontend

Open http://localhost:3000, register an account, and start farming smarter.


πŸ“± Usage Walkthrough

  1. Register / Login β†’ JWT token stored in localStorage
  2. Allow location β†’ pill appears in chat header (πŸ“ Thane, Maharashtra)
  3. Pick a module β†’ Crop Doctor, Irrigation Advisor, or Soil Health
  4. Chat β†’ AI streams responses token-by-token with region + weather context
  5. See "Log to Tracker" chips β†’ click any expense recommendation to log it
  6. Check alerts bell πŸ”” β†’ weather-aware alerts auto-generated from your sessions
  7. Finance Tracker β†’ view revenue, expenses, ROI, category breakdown

πŸ”’ Security

  • Passwords hashed with bcrypt (10 rounds)
  • All API routes protected by JWT middleware
  • Sessions scoped to authenticated user β€” no cross-user data leakage
  • Images stored as base64 in DB (never written to filesystem)

πŸ—ΊοΈ Roadmap

  • Push notifications (Web Push API)
  • Offline mode with service worker
  • Multi-language support (Hindi, Marathi, Tamil)
  • Soil test report PDF upload + OCR
  • Government scheme recommendations by region
  • WhatsApp integration for low-connectivity farmers

Built with ❀️ for farmers who deserve better tools.

About

AI Chatbot and Expence tracker for farmers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors