Skip to content
Β 
Β 

Latest commit

Β 

History

9 Commits

Folders and files

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

Repository files navigation

🌾 Agroculture β€” AI-Powered Agriculture Assistant

Agroculture is a context-aware, AI-powered agriculture assistant built for Indian farmers. It combines real-time market data (Agmarknet), live weather forecasting, a RAG-based knowledge engine, bilingual (Hindi/English) voice I/O, proactive smart alerts, and a clean web UI β€” all in a single deployable backend.


✨ Key Features

Feature Description
🧠 Contextual Chat Agent Multi-intent NLP pipeline routes queries to weather, market, agri-advisory, policy, logistics, and compliance handlers
πŸ“ˆ Live Market Prices Real-time mandi prices via the Agmarknet / data.gov.in API with trend analysis and comparisons
🌦️ Weather Forecasting Open-Meteo powered daily forecasts with farmer-specific summaries (rain probability, ETβ‚€, wind gusts)
πŸ”” Proactive Alerts Hourly scheduled background job generates personalised weather alerts and government scheme updates per user
πŸŽ™οΈ Bilingual Voice I/O Hindi-first ASR using OpenAI Whisper / faster-whisper; TTS via Microsoft Edge voices (hi-IN-SwaraNeural, en-IN-NeerjaNeural)
πŸ“š RAG Knowledge Base ChromaDB + sentence-transformers vector store built from agronomy PDFs, Wikipedia articles, and ICAR publications
🌱 Planting Decision Engine Go/no-go planting advisor that fuses crop type, live weather, and LLM reasoning into a structured JSON decision
πŸ‘€ Farmer Onboarding Conversational profile builder (location, land size, budget, crops) for personalised recommendations
🌐 Web UI Single-file index.html frontend β€” no build step required

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        index.html (UI)                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚  HTTP / REST
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                   FastAPI  (main.py)                        β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ Intent Routerβ”‚  β”‚ Scheduler     β”‚  β”‚  Voice Endpoints  β”‚ β”‚
β”‚  β”‚ detect_intentβ”‚  β”‚ APScheduler   β”‚  β”‚ /voice/transcribe β”‚ β”‚
β”‚  β”‚ _nlp()       β”‚  β”‚ (hourly jobs) β”‚  β”‚ /voice/ask  /tts  β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
   β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚              Core Modules                         β”‚
   β”‚  data_sources.py  ──  Agmarknet + Open-Meteo APIs β”‚
   β”‚  qna.py           ──  RAG query + Mistral LLM     β”‚
   β”‚  rag.py           ──  ChromaDB ingestion pipeline  β”‚
   β”‚  ner_utils.py     ──  spaCy location extractor    β”‚
   β”‚  translator.py    ──  Language detect + translate  β”‚
   β”‚  url_checker.py   ──  Source validation utilities  β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

Agroculture/
β”œβ”€β”€ main.py           # FastAPI application β€” all API endpoints & scheduler
β”œβ”€β”€ data_sources.py   # Agmarknet market prices + Open-Meteo weather helpers
β”œβ”€β”€ qna.py            # RAG query engine + Mistral LLM advisory generator
β”œβ”€β”€ rag.py            # One-time script to build the ChromaDB vector store
β”œβ”€β”€ ner_utils.py      # Named entity recognition (location extraction)
β”œβ”€β”€ translator.py     # Language detection, translation & transliteration
β”œβ”€β”€ url_checker.py    # URL validation and source management utilities
β”œβ”€β”€ index.html        # Frontend single-page web UI
β”œβ”€β”€ requirements.txt  # Python dependencies
└── agri_db/          # Auto-generated ChromaDB vector store (after rag.py)

πŸš€ Quick Start

1. Prerequisites

  • Python 3.10 or 3.11 (recommended)
  • pip installed
  • Internet connectivity for API calls and model downloads

2. Clone & Set Up Virtual Environment

# Windows PowerShell
python -m venv .venv
.venv\Scripts\Activate.ps1

# macOS / Linux
python -m venv .venv
source .venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

Create a .env file in the project root:

AGMARKNET_API_KEY=your_data_gov_in_api_key
MISTRAL_API_KEY=your_mistral_api_key
Variable Where to Get
AGMARKNET_API_KEY data.gov.in β€” register for a free API key
MISTRAL_API_KEY console.mistral.ai

5. Build the RAG Knowledge Base (one-time)

python rag.py

This downloads and embeds agronomy PDFs and Wikipedia articles into a local ChromaDB store at agri_db/. Takes ~5–10 minutes on first run.

6. Start the Backend

uvicorn main:app --host 127.0.0.1 --port 8000 --reload
  • πŸ“– Interactive API docs: http://127.0.0.1:8000/docs
  • πŸ”— ReDoc: http://127.0.0.1:8000/redoc

7. Open the UI

Open index.html directly in your browser, or serve it locally:

python -m http.server 5173
# Then visit: http://127.0.0.1:5173/index.html

πŸ”Œ API Reference

Onboarding & Profile

Method Endpoint Description
GET /status?user_id=<id> Check if user has completed onboarding
POST /chat?user_id=<id> Step through the conversational onboarding flow

AI Advisory

Method Endpoint Description
POST /ask Context-aware Q&A (weather, market, policy, agri, logistics)
GET /get-suggestion?user_id=<id>&category=<crop|land|budget> On-demand personalised suggestion
POST /plan/plant Go/no-go planting decision based on live weather

Alerts

Method Endpoint Description
GET /alerts?user_id=<id> Fetch latest alerts and scheme suggestions
POST /alerts/run-now?user_id=<id> Trigger alert generation immediately

Weather

Method Endpoint Description
GET /weather_summary?location=<city> Compact dashboard weather metrics

Voice (ASR + TTS)

Method Endpoint Description
POST /voice/transcribe Upload audio β†’ transcribed text (Whisper)
POST /voice/ask Upload audio β†’ answer text + base64 MP3
POST /tts Text β†’ base64 MP3 (auto en-IN / hi-IN voice)

🧠 How It Works

Intent Detection Pipeline

Each /ask query is routed through detect_intent_nlp() which classifies it into one of:

weather β†’ get_weather_forecast() + Mistral LLM
market  β†’ agmark_qna_answer()   (Agmarknet API + fuzzy matching)
agriculture β†’ get_answer_from_books() (RAG + LLM)
policy  β†’ get_answer_from_books() (RAG + LLM)
logistics / compliance / general β†’ get_answer_from_books()

RAG Pipeline (rag.py)

  1. Fetch β€” Downloads PDFs (via PyMuPDF) and Wikipedia articles (via BeautifulSoup)
  2. Chunk β€” Splits into overlapping 1000-word chunks (200-word overlap)
  3. Embed β€” Uses all-MiniLM-L6-v2 via sentence-transformers
  4. Store β€” Persists to ChromaDB with cosine similarity index

Proactive Alerts (APScheduler)

Every hour the scheduler calls check_for_personalized_alerts() for each registered user:

  1. Fetches live weather for the user's location
  2. Prompts Mistral to generate ALERT: + 3 SUGGESTION: lines
  3. Fetches applicable Indian government schemes for the farmer's profile
  4. Stores results in an in-memory list, served via GET /alerts

πŸ› οΈ Tech Stack

Layer Technology
Backend FastAPI 0.115, Uvicorn, APScheduler 3.10
LLM Mistral AI (mistralai SDK)
Vector DB ChromaDB 0.5 + sentence-transformers (all-MiniLM-L6-v2)
ASR OpenAI Whisper (fallback: faster-whisper on CPU, int8)
TTS Microsoft Edge TTS (edge-tts) β€” Indian voices
Market Data Agmarknet via data.gov.in API
Weather Open-Meteo (free, no API key needed)
Geocoding pgeocode + Open-Meteo geocoder
NER / NLP rapidfuzz fuzzy matching, regex-based intent & commodity extractor
Frontend Vanilla HTML/CSS/JS (index.html)

βš™οΈ Configuration & Tips

  • Hindi ASR: Voice input defaults to lang=hi to avoid Urdu auto-detection. Pass ?lang=en to /voice/ask for English queries.
  • TTS Voices: English uses en-IN-NeerjaNeural; Hindi/Devanagari text automatically switches to hi-IN-SwaraNeural.
  • Alerts frequency: Default is hourly. Use POST /alerts/run-now?user_id=<id> to trigger on demand during development.
  • Whisper compatibility: If Whisper fails due to NumPy/Numba version mismatches, the system automatically falls back to faster-whisper (CPU, int8).
  • RAG refresh: Re-run python rag.py anytime to add new knowledge sources by editing SOURCE_URLS in rag.py.

πŸ“‹ Requirements

See requirements.txt for pinned versions. Key dependencies:

fastapi==0.115.0
uvicorn[standard]==0.30.6
apscheduler==3.10.4
mistralai==0.4.0
chromadb==0.5.5
sentence-transformers==2.6.1
torch>=2.2.0
edge-tts==6.1.9
faster-whisper==1.0.3
openai-whisper==20231117
rapidfuzz==3.9.6
pgeocode==0.5.0

πŸ—ΊοΈ Roadmap

  • PostgreSQL / Redis persistence for user profiles and alerts
  • Multi-language translation pipeline (Tamil, Telugu, Marathi)
  • Mobile-responsive PWA frontend
  • Real-time crop price push notifications
  • Image-based pest/disease detection (vision LLM)
  • Integration with e-NAM for direct market linkage

🀝 Contributing

Contributions are welcome! Please open an issue first to discuss major changes.

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Commit your changes: git commit -m 'Add your feature'
  4. Push and open a Pull Request

πŸ“„ License

This project is licensed under the MIT License. See LICENSE for details.


Built with ❀️ for Indian farmers | Powered by Mistral AI, Open-Meteo & Agmarknet

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages