Note
This project was developed as part of EPICS (Engineering Projects in Community Service), an initiative to apply engineering skills for social good and community empowerment.
Every day, millions of common people across India face situations requiring timely legal adviceโranging from landlord disputes and consumer rights to family law and labor issues. However:
Legal aid clinics and lawyers are often inaccessible due to cost, geography, or language barriers. In rural Madhya Pradesh, the lawyer-to-population ratio is approximately 1:10,000 compared to the national average of 1:1,200, leaving vast populations without legal representation.
National and state laws are voluminous, jargonโladen, and constantly updated, making manual search slow and errorโprone. The Bharatiya Nyaya Sanhita (BNS) 2023 alone contains 358 sections, while consumer protection, labor, and family laws add thousands more provisions.
A generic search may surface outdated or nonโapplicable statutes. What matters most are:
- Local regulations (state-specific amendments)
- Recent amendments (BNS 2023, BNSS 2023)
- Userโspecific factors (state, language, socioโeconomic profile)
Even when AI systems retrieve relevant sections, users can't trace "why" a particular clause applies or how it led to a recommended action plan. This blackโbox approach erodes trust among vulnerable populations.
- 65% of Madhya Pradesh's population lives in rural areas
- 70.6% literacy rate (vs. 77.7% national average)
- Limited smartphone penetration and poor internet connectivity in tribal districts like Alirajpur, Barwani, and Jhabua
Legal Sahayak is an AI-powered, explainable legal assistance platform that democratizes access to legal information through an integrated system combining intelligent search, personalized explanations, and a comprehensive digital legal library.
- FAISS Vector Store: Semantic search across 6+ domains of Indian law
- Hybrid Retrieval: Combines dense embeddings (HuggingFace Transformers) with keyword matching
- Contextual Chunking: Markdown-based hierarchical splitting preserves legal structure (Act โ Chapter โ Section)
- Cross-references: Automatically links related laws and provisions
- Metadata Preservation: Tracks source documents and specific legal sections
Adapts explanations based on user profile:
- Age & Education โ Simplifies legal language for users with lower literacy
- Location โ Prioritizes MP-specific laws and local court procedures
- Occupation โ Provides job-relevant examples (e.g., labor laws for factory workers)
- Gender โ Ensures culturally sensitive responses for sensitive issues
Every response includes structured information:
- Plain-language Answer: Legal explanation in simple terms
- Confidence Score: 0.9-1.0 (clearly defined), 0.6-0.8 (requires interpretation), <0.6 (recommends lawyer)
- Legal References: Direct citations to applicable laws and sections
- Action Plans: Step-by-step guidance with timelines, relevant authorities, required documents, and cost estimates
- Full application interface in Hindi and English
- Responsive language toggle for seamless switching
- Culturally relevant explanations adapted to local context
- Supports semi-literate users with simplified language options
- Interactive, searchable database of Indian laws
- Covers 6+ legal domains: Consumer Protection, Fundamental Rights, Marriage Laws (Hindu & Muslim), Labor Laws, Rental Laws
- Real-time document browsing with hierarchical navigation
- Quick reference guides for common legal scenarios
- No storage of personal legal queries
- Demographic data used only for response generation
- GDPR-compliant data handling
sequenceDiagram
participant U as User
participant F as Frontend
participant API as FastAPI Backend
participant RAG as RAG Service
participant FAISS as Vector Store
participant LLM as Groq LLM
U->>F: Enter legal question + demographics
F->>API: POST /api/v1/qa/ask
Note over F,API: {question, demographics}
API->>RAG: Process QA Request
RAG->>FAISS: Semantic Search (k=5)
Note over RAG,FAISS: Convert query to embeddings
FAISS-->>RAG: Top 5 relevant documents
RAG->>RAG: Format context + demographics
RAG->>LLM: Invoke with structured prompt
Note over RAG,LLM: JSON Schema: QAResponseModel
LLM-->>RAG: JSON Response
Note over LLM,RAG: {answer, confidence,<br/>legal_references,<br/>action_plan}
RAG->>RAG: Validate & Parse Response
RAG-->>API: QAResponseModel
API-->>F: HTTP 200 + JSON
F-->>U: Display formatted answer
Note over F,U: (1) Explanation<br/>(2) Confidence Score<br/>(3) Sources<br/>(4) Action Plan
- Framework: FastAPI 0.115+ (async, high-performance)
- LLM: Groq API (llama-3.3-70b-versatile or gpt-oss-120b)
- Embeddings: HuggingFace
sentence-transformers/all-MiniLM-L6-v2 - Vector DB: FAISS (Facebook AI Similarity Search)
- RAG Framework: LangChain 0.3+
- Schema Validation: Pydantic v2
- Framework: Next.js 15 (App Router)
- UI Library: shadcn/ui + Radix UI
- Styling: Tailwind CSS
- State Management: React Hooks
- Markdown Rendering: react-markdown + remark-gfm
- Document Format: Markdown (.md)
- Text Splitting: LangChain MarkdownHeaderTextSplitter
- Indexing: Python script (
build_vectorstore.py)
- Python: 3.11+
- Node.js: 18+
- pnpm: 8+ (or npm/yarn)
- Git: For cloning the repository
git clone https://github.com/sujeetgund/legalsahayak.git
cd legalsahayakcd backend
python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activatepip install -r requirements.txtCreate a .env file in the backend/ directory:
# API Keys
GROQ_API_KEY=your_groq_api_key_here
GROQ_MODEL_NAME=llama-3.3-70b-versatile
# Embedding Model
EMBEDDING_MODEL_NAME=sentence-transformers/all-MiniLM-L6-v2
# Security
BEARER_TOKEN=your_secure_bearer_token
# Paths
DATA_DIR=data
FAISS_INDEX_DIR=faiss_indexpython build_vectorstore.pyThis will:
- Load documents from
data/directory - Split into hierarchical chunks
- Generate embeddings
- Create FAISS index in
faiss_index/
uvicorn main:app --reload --host 0.0.0.0 --port 8000Backend will be available at: http://localhost:8000
API docs: http://localhost:8000/docs
cd ../frontend
pnpm installCreate a .env.local file in the frontend/ directory:
BACKEND_SERVER_URL=http://localhost:8000/api/v1pnpm devFrontend will be available at: http://localhost:3000
# From root directory
docker-compose up --buildServices:
- Frontend:
http://localhost:3000 - Backend:
http://localhost:8000
-
Navigate to Chat Interface:
http://localhost:3000 -
Fill User Profile (one-time):
- Age: 25-35
- Gender: Male/Female/Other
- Location: Bhopal, Madhya Pradesh
- Education: Graduate
- Occupation: Private Employee
-
Ask Your Question:
Example: "My landlord is asking me to vacate without notice. What are my rights?" -
Review Response:
- Explanation: Plain-language answer
- Confidence Score: 85%
- Legal References: MP Rent Control Act, 1961 - Section 12
- Action Plan:
- Check your rent agreement
- Send legal notice with 1-month period
- File complaint if harassment continues
Request:
{
"question": "What is the minimum wage in Madhya Pradesh for unskilled workers?",
"demographics": {
"age": 30,
"gender": "Male",
"location": "Indore, Madhya Pradesh",
"education_level": "High School",
"job_title": "Factory Worker"
}
}Response:
{
"answer": "As of 2024, the minimum wage for unskilled workers in Madhya Pradesh is โน320 per day (โน9,600 per month for 30 days). This applies to industries covered under the Minimum Wages Act, 1948...",
"confidence": 0.92,
"legal_references": [
"Minimum Wages Act, 1948 - Section 3",
"MP Minimum Wages Notification 2024"
],
"action_plan": [
{
"title": "Verify Your Wage Slip",
"description": "Check if your employer is paying at least โน320/day. If not, collect 3 months of wage slips as evidence."
},
{
"title": "File Complaint with Labour Commissioner",
"description": "Visit the nearest Labour Office (Office of the Commissioner, Labour Department, Indore). No fee required."
},
{
"title": "Contact MP Labour Helpline",
"description": "Call 155214 for immediate assistance and guidance on next steps."
}
]
}curl -X POST http://localhost:8000/api/v1/qa/ask \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_bearer_token" \
-d '{
"question": "How do I file a consumer complaint?",
"demographics": {
"age": 28,
"gender": "Female",
"location": "Bhopal, MP",
"education_level": "Graduate",
"job_title": "Teacher"
}
}'This project is licensed under the MIT License.