Skip to content

Repository files navigation

Python Next.js FastAPI IBM Granite

πŸ’§ GroundWater Assistant

AI-powered groundwater risk advisory system for Tamil Nadu farmers

Leveraging RAG and ReAct agents to provide data-driven borewell drilling recommendations


πŸ“‹ Overview

GroundWater Assistant is an intelligent advisory system that helps farmers and officials assess groundwater availability before drilling borewells. It combines Retrieval-Augmented Generation (RAG) with a ReAct agent framework to provide accurate, citation-backed recommendations from government reports.

Key Capabilities

Feature Description
πŸ€– Agentic AI ReAct loop with tool use for intelligent multi-step reasoning
πŸ“„ Document RAG Semantic search over CGWB/PWD government reports
πŸ—ΊοΈ Interactive Map District-wise risk visualization with click-to-query
πŸ“Š Risk Classification Clear categorization (Safe β†’ Semi-Critical β†’ Critical β†’ Over-Exploited)
πŸ“ PDF Reports Downloadable feasibility assessments
🌐 Multi-lingual Tamil, Hindi, and English support

πŸ› οΈ Technology Stack

Layer Technologies
Frontend Next.js 15, TypeScript, Tailwind CSS, Leaflet Maps
Backend API FastAPI, Uvicorn, Pydantic
LLM Engine IBM Granite 3.0 8B Instruct (via HuggingFace Transformers)
Agent Framework Custom ReAct implementation with tool integration
Embeddings sentence-transformers/all-MiniLM-L6-v2
Vector Store FAISS (Facebook AI Similarity Search)
PDF Parsing IBM Docling (structure-aware extraction)
Translation mtranslate + langdetect (no API key required)
PDF Generation ReportLab
Hardware NVIDIA GPU with CUDA (optional, recommended)

πŸš€ Quick Start

Prerequisites

  • Python 3.10+
  • Node.js 18+
  • 8GB RAM minimum (16GB recommended)
  • ~5GB disk space for models
  • NVIDIA GPU with CUDA (optional)

Installation

# Clone and navigate to project
cd "GroundWater Assistant"

# Create and activate virtual environment
python -m venv .venv
.venv\Scripts\activate  # Windows
# source .venv/bin/activate  # Linux/Mac

# Install Python dependencies
pip install -r requirements.txt

# Install as editable package (enables imports from any directory)
pip install -e .

# Install frontend dependencies
cd web && npm install && cd ..

First Run

# 1. Ingest PDF reports (place files in data/pdfs/ first)
python ingest.py

# 2. Start the backend API
uvicorn api.main:app --reload --port 8000

# 3. Start the frontend (new terminal)
cd web && npm run dev

Access the application:


πŸ“ Project Structure

GroundWater Assistant/
β”œβ”€β”€ api/                        # FastAPI backend
β”‚   β”œβ”€β”€ main.py                 # Application entry point
β”‚   └── routes/                 # API endpoints
β”‚       β”œβ”€β”€ chat.py             # /api/chat - Agent queries
β”‚       β”œβ”€β”€ report.py           # /api/report - PDF generation
β”‚       └── map.py              # /api/districts - Map data
β”‚
β”œβ”€β”€ backend/                    # Core business logic
β”‚   β”œβ”€β”€ agent.py                # ReAct agent implementation
β”‚   β”œβ”€β”€ llm.py                  # LLM integration (Granite)
β”‚   β”œβ”€β”€ retriever.py            # FAISS vector search
β”‚   β”œβ”€β”€ ingest.py               # Document processing
β”‚   β”œβ”€β”€ prompts.py              # System prompts
β”‚   β”œβ”€β”€ tools.py                # Agent tools
β”‚   β”œβ”€β”€ translator.py           # Multi-lingual support
β”‚   └── report_generator.py     # PDF report creation
β”‚
β”œβ”€β”€ web/                        # Next.js frontend
β”‚   β”œβ”€β”€ app/                    # App router pages
β”‚   └── components/             # React components
β”‚
β”œβ”€β”€ data/pdfs/                  # Input: Government reports
β”œβ”€β”€ vectorstore/                # Output: FAISS index
β”œβ”€β”€ models/                     # Cached model files
β”‚
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ pyproject.toml              # Package configuration
└── generated_map_data.json     # District risk data

πŸ”Œ API Reference

Chat Endpoint

POST /api/chat
Content-Type: application/json

{
  "query": "What is the groundwater status in Chennai?",
  "language": "en"  // Optional: en, ta, hi
}

Response:

{
  "response": "Based on the Chennai District Report...",
  "risk_level": "Over-Exploited",
  "sources": ["Chennai_Report_2023.pdf"]
}

Report Generation

POST /api/report
Content-Type: application/json

{
  "query": "Borewell feasibility assessment",
  "location": "Chennai",
  "risk_level": "Critical",
  "full_response": "..."
}

District Data

GET /api/districts

Returns GeoJSON-compatible district risk data for map visualization.


πŸ€– Agent Architecture

The system uses a ReAct (Reasoning + Acting) agent that:

  1. Thinks - Analyzes the user query
  2. Acts - Calls appropriate tools
  3. Observes - Processes tool results
  4. Repeats - Until final answer is ready

Available Tools

Tool Purpose
search_knowledge_base(query) RAG search over government reports
estimate_borewell_cost(depth_ft) Calculate drilling cost estimates
check_crop_feasibility(depth, crop) Assess crop viability at water depth

Response Format

I need to give you important advice about Chennai's groundwater situation...

According to the Chennai District Report 2023 (Page 12), the extraction 
is at 85.3% with water levels at 12.4 meters below ground level...

[Natural language recommendations]

---
πŸ“Š **Retrieved Data from Reports:**

**Source:** Chennai_District_Report_2023.pdf, Page 12
- Groundwater Extraction: 85.3%
- Water Level Depth: 12.4m below ground level
- Classification: Semi-Critical

πŸ—ΊοΈ District Coverage

The system covers 36 districts of Tamil Nadu with risk classifications:

Risk Level Districts Percentage
πŸ”΄ Over-Exploited 16 44%
🟠 Critical 2 6%
🟑 Semi-Critical 6 17%
🟒 Safe 9 25%
βšͺ Unknown 4 11%

πŸ§ͺ Testing

# Run all tests
python -m pytest tests/

# Individual test modules
python tests/test_llm.py           # LLM functionality
python tests/test_rag_usage.py     # RAG pipeline
python tests/verify_agent.py       # Agent behavior
python tests/test_pdf_generation.py # Report generation

πŸ“š Data Sources


⚠️ Responsible AI

Principle Implementation
Transparency All responses include source citations with page numbers
No Hallucination "Data not found" returned for unknown locations
Data Freshness Report dates explicitly stated in responses
Auditability Multi-step reasoning logged for review
Verification Users can cross-check cited documents

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


Built with ❀️ for Tamil Nadu farmers

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages