Spaces:
Sleeping
Sleeping
Upload 10 files
Browse files- .dockerignore +31 -0
- Dockerfile +45 -0
- README.md +32 -11
- api.py +604 -0
- app.py +14 -0
- bias_prediction_engine.py +610 -0
- document_generator.py +465 -0
- requirements.txt +27 -0
- simulation_engine.py +326 -0
- translation_service.py +201 -0
.dockerignore
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.py[cod]
|
| 3 |
+
*$py.class
|
| 4 |
+
*.so
|
| 5 |
+
.Python
|
| 6 |
+
env/
|
| 7 |
+
venv/
|
| 8 |
+
ENV/
|
| 9 |
+
.venv
|
| 10 |
+
*.egg-info/
|
| 11 |
+
dist/
|
| 12 |
+
build/
|
| 13 |
+
*.log
|
| 14 |
+
.DS_Store
|
| 15 |
+
.env
|
| 16 |
+
.git/
|
| 17 |
+
.gitignore
|
| 18 |
+
README.md
|
| 19 |
+
DEPLOYMENT.md
|
| 20 |
+
HACKATHON_DEMO.md
|
| 21 |
+
IMPLEMENTATION_SUMMARY.md
|
| 22 |
+
QUICKSTART.md
|
| 23 |
+
QUICK_REFERENCE.md
|
| 24 |
+
*.md
|
| 25 |
+
test_*.py
|
| 26 |
+
client_example.py
|
| 27 |
+
docker-compose.yml
|
| 28 |
+
start.sh
|
| 29 |
+
start.ps1
|
| 30 |
+
start_hackathon.sh
|
| 31 |
+
start_hackathon.ps1
|
Dockerfile
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Optimized Dockerfile for Hugging Face Spaces
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install system dependencies
|
| 7 |
+
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 8 |
+
build-essential \
|
| 9 |
+
gcc \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
# Copy requirements and install Python dependencies
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
|
| 15 |
+
# Install dependencies in stages for better caching
|
| 16 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 17 |
+
pip install --no-cache-dir \
|
| 18 |
+
fastapi==0.104.0 \
|
| 19 |
+
uvicorn[standard]==0.24.0 \
|
| 20 |
+
pydantic==2.0.0 \
|
| 21 |
+
python-multipart==0.0.6 \
|
| 22 |
+
python-dotenv==1.0.0 \
|
| 23 |
+
requests==2.31.0 \
|
| 24 |
+
googletrans==4.0.0rc1 \
|
| 25 |
+
langdetect==1.0.9 && \
|
| 26 |
+
pip install --no-cache-dir \
|
| 27 |
+
numpy==1.24.0 \
|
| 28 |
+
pandas==2.0.0 \
|
| 29 |
+
scikit-learn==1.3.0 && \
|
| 30 |
+
pip install --no-cache-dir \
|
| 31 |
+
torch==2.0.0 \
|
| 32 |
+
transformers==4.35.0 \
|
| 33 |
+
sentence-transformers==2.2.0
|
| 34 |
+
|
| 35 |
+
# Copy application code
|
| 36 |
+
COPY . .
|
| 37 |
+
|
| 38 |
+
# Expose port (Hugging Face Spaces uses 7860 by default)
|
| 39 |
+
EXPOSE 7860
|
| 40 |
+
|
| 41 |
+
# Set environment variable for port
|
| 42 |
+
ENV PORT=7860
|
| 43 |
+
|
| 44 |
+
# Start the application
|
| 45 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,11 +1,32 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo: purple
|
| 6 |
-
sdk: docker
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: LexAI ML Backend
|
| 3 |
+
emoji: ⚖️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
+
pinned: false
|
| 9 |
+
license: apache-2.0
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# LexAI ML Backend - Hugging Face Space
|
| 13 |
+
|
| 14 |
+
This Space hosts the complete ML backend for LexAI, providing:
|
| 15 |
+
|
| 16 |
+
- **Bias Detection**: InLegalBERT-powered analysis of legal documents
|
| 17 |
+
- **Outcome Prediction**: AI-driven case outcome forecasting
|
| 18 |
+
- **Multilingual Translation**: Support for 9 Indian languages
|
| 19 |
+
- **Document Generation**: Automated legal document creation
|
| 20 |
+
- **Text Simplification**: Plain language conversion
|
| 21 |
+
- **What-If Simulation**: Scenario analysis for legal cases
|
| 22 |
+
|
| 23 |
+
## API Documentation
|
| 24 |
+
|
| 25 |
+
Once deployed, visit `/docs` for interactive API documentation.
|
| 26 |
+
|
| 27 |
+
## Environment Variables
|
| 28 |
+
|
| 29 |
+
No environment variables required - all models are loaded automatically.
|
| 30 |
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Unified FastAPI API for InLegalBERT Analysis & Hackathon Features
|
| 3 |
+
==================================================================
|
| 4 |
+
|
| 5 |
+
This module provides REST API endpoints for:
|
| 6 |
+
1. Bias detection and outcome prediction (InLegalBERT)
|
| 7 |
+
2. Multilingual translation (9 languages)
|
| 8 |
+
3. Legal document generation (4 types)
|
| 9 |
+
4. Plain language simplification
|
| 10 |
+
5. What-if simulation engine
|
| 11 |
+
6. Sensitivity analysis
|
| 12 |
+
|
| 13 |
+
All features consolidated into a single API on port 8001.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 17 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 18 |
+
from pydantic import BaseModel, Field
|
| 19 |
+
from typing import List, Optional, Dict, Any
|
| 20 |
+
import uvicorn
|
| 21 |
+
from datetime import datetime
|
| 22 |
+
|
| 23 |
+
from bias_prediction_engine import analyze_legal_case, get_model
|
| 24 |
+
from translation_service import get_translation_service
|
| 25 |
+
from document_generator import get_document_generator
|
| 26 |
+
from simulation_engine import get_simulation_engine
|
| 27 |
+
|
| 28 |
+
# ============================================================================
|
| 29 |
+
# FASTAPI APP SETUP
|
| 30 |
+
# ============================================================================
|
| 31 |
+
|
| 32 |
+
app = FastAPI(
|
| 33 |
+
title="LexAI Unified ML API",
|
| 34 |
+
description="Comprehensive legal AI analysis: bias detection, translation, document generation, and simulation",
|
| 35 |
+
version="2.0.0"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# CORS configuration
|
| 39 |
+
app.add_middleware(
|
| 40 |
+
CORSMiddleware,
|
| 41 |
+
allow_origins=["*"], # Configure appropriately for production
|
| 42 |
+
allow_credentials=True,
|
| 43 |
+
allow_methods=["*"],
|
| 44 |
+
allow_headers=["*"],
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# ============================================================================
|
| 48 |
+
# PYDANTIC MODELS (Request/Response Schemas)
|
| 49 |
+
# ============================================================================
|
| 50 |
+
|
| 51 |
+
# --- Bias Analysis Models ---
|
| 52 |
+
class CaseMetadata(BaseModel):
|
| 53 |
+
"""Optional metadata for case analysis"""
|
| 54 |
+
case_type: Optional[str] = Field(None, description="Type of case (criminal, civil, bail, etc.)")
|
| 55 |
+
jurisdiction: Optional[str] = Field(None, description="Court jurisdiction")
|
| 56 |
+
year: Optional[int] = Field(None, description="Case year")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class HistoricalCase(BaseModel):
|
| 60 |
+
"""Historical case data for systemic bias analysis"""
|
| 61 |
+
outcome: str = Field(..., description="Case outcome (conviction, acquittal, etc.)")
|
| 62 |
+
gender: Optional[str] = Field(None, description="Gender of defendant")
|
| 63 |
+
region: Optional[str] = Field(None, description="Geographic region")
|
| 64 |
+
caste: Optional[str] = Field(None, description="Caste category")
|
| 65 |
+
case_type: Optional[str] = Field(None, description="Type of case")
|
| 66 |
+
year: Optional[int] = Field(None, description="Year of case")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class AnalysisRequest(BaseModel):
|
| 70 |
+
"""Main request model for comprehensive analysis"""
|
| 71 |
+
case_text: str = Field(..., description="Legal document/FIR/judgment text", min_length=10)
|
| 72 |
+
rag_summary: Optional[str] = Field(None, description="AI-generated summary for RAG bias detection")
|
| 73 |
+
source_documents: Optional[List[str]] = Field(None, description="Source documents used for RAG")
|
| 74 |
+
historical_cases: Optional[List[HistoricalCase]] = Field(None, description="Historical cases for systemic analysis")
|
| 75 |
+
case_metadata: Optional[CaseMetadata] = Field(None, description="Case metadata")
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
class DocumentBiasRequest(BaseModel):
|
| 79 |
+
"""Request for document-only bias detection"""
|
| 80 |
+
case_text: str = Field(..., description="Legal document text")
|
| 81 |
+
threshold: float = Field(0.15, ge=0.0, le=1.0, description="Bias detection threshold")
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
class RAGBiasRequest(BaseModel):
|
| 85 |
+
"""Request for RAG output bias detection"""
|
| 86 |
+
rag_summary: str = Field(..., description="AI-generated summary")
|
| 87 |
+
source_documents: List[str] = Field(..., description="Source documents")
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
class SystemicBiasRequest(BaseModel):
|
| 91 |
+
"""Request for systemic bias analysis"""
|
| 92 |
+
historical_cases: List[HistoricalCase] = Field(..., description="Historical case data")
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
class OutcomePredictionRequest(BaseModel):
|
| 96 |
+
"""Request for outcome prediction only"""
|
| 97 |
+
case_text: str = Field(..., description="Legal case text")
|
| 98 |
+
case_metadata: Optional[CaseMetadata] = Field(None, description="Optional case metadata")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
class AnalysisResponse(BaseModel):
|
| 102 |
+
"""Response model for analysis results"""
|
| 103 |
+
status: str
|
| 104 |
+
analysis_id: str
|
| 105 |
+
timestamp: str
|
| 106 |
+
document_bias: Optional[Dict[str, Any]] = None
|
| 107 |
+
rag_bias: Optional[Dict[str, Any]] = None
|
| 108 |
+
systemic_bias: Optional[Dict[str, Any]] = None
|
| 109 |
+
outcome_prediction: Optional[Dict[str, Any]] = None
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
# --- Translation & Simplification Models ---
|
| 113 |
+
class TranslateRequest(BaseModel):
|
| 114 |
+
text: str = Field(..., description="Text to translate")
|
| 115 |
+
source_lang: str = Field("auto", description="Source language (auto-detect)")
|
| 116 |
+
target_lang: str = Field("en", description="Target language")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
class SimplifyRequest(BaseModel):
|
| 120 |
+
legal_text: str = Field(..., description="Complex legal text")
|
| 121 |
+
reading_level: str = Field("simple", description="Reading level (simple/intermediate)")
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# --- Document Generation Models ---
|
| 125 |
+
class DocumentGenerateRequest(BaseModel):
|
| 126 |
+
document_type: str = Field(..., description="Type: bail_application, fir_complaint, legal_notice, petition")
|
| 127 |
+
details: Dict[str, Any] = Field(..., description="Document details")
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# --- Simulation Models ---
|
| 131 |
+
class SimulationRequest(BaseModel):
|
| 132 |
+
base_case: Dict[str, Any] = Field(..., description="Original case facts")
|
| 133 |
+
modifications: Dict[str, Any] = Field(..., description="Modifications to test")
|
| 134 |
+
|
| 135 |
+
|
| 136 |
+
class SensitivityRequest(BaseModel):
|
| 137 |
+
case_facts: str = Field(..., description="Case facts for sensitivity analysis")
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
# ============================================================================
|
| 141 |
+
# ROOT & HEALTH CHECK
|
| 142 |
+
# ============================================================================
|
| 143 |
+
|
| 144 |
+
@app.get("/")
|
| 145 |
+
async def root():
|
| 146 |
+
"""API health check and feature overview"""
|
| 147 |
+
return {
|
| 148 |
+
"service": "LexAI Unified ML API",
|
| 149 |
+
"status": "operational",
|
| 150 |
+
"version": "2.0.0",
|
| 151 |
+
"port": 8001,
|
| 152 |
+
"features": {
|
| 153 |
+
"bias_analysis": "InLegalBERT-powered bias detection",
|
| 154 |
+
"outcome_prediction": "Legal case outcome prediction",
|
| 155 |
+
"translation": "9 Indian languages supported",
|
| 156 |
+
"simplification": "Plain language conversion",
|
| 157 |
+
"document_generation": "4 legal document types",
|
| 158 |
+
"simulation": "What-if scenario analysis"
|
| 159 |
+
},
|
| 160 |
+
"timestamp": datetime.now().isoformat()
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# ============================================================================
|
| 165 |
+
# BIAS ANALYSIS ENDPOINTS
|
| 166 |
+
# ============================================================================
|
| 167 |
+
|
| 168 |
+
@app.post("/api/v1/analyze/comprehensive", response_model=AnalysisResponse)
|
| 169 |
+
async def comprehensive_analysis(request: AnalysisRequest):
|
| 170 |
+
"""
|
| 171 |
+
Perform comprehensive legal case analysis including:
|
| 172 |
+
- Document bias detection
|
| 173 |
+
- RAG output bias detection (if RAG summary provided)
|
| 174 |
+
- Systemic bias analysis (if historical cases provided)
|
| 175 |
+
- Outcome prediction
|
| 176 |
+
"""
|
| 177 |
+
try:
|
| 178 |
+
# Convert Pydantic models to dicts
|
| 179 |
+
historical_cases_dict = None
|
| 180 |
+
if request.historical_cases:
|
| 181 |
+
historical_cases_dict = [case.dict() for case in request.historical_cases]
|
| 182 |
+
|
| 183 |
+
case_metadata_dict = None
|
| 184 |
+
if request.case_metadata:
|
| 185 |
+
case_metadata_dict = request.case_metadata.dict()
|
| 186 |
+
|
| 187 |
+
# Run analysis
|
| 188 |
+
results = analyze_legal_case(
|
| 189 |
+
case_text=request.case_text,
|
| 190 |
+
rag_summary=request.rag_summary,
|
| 191 |
+
source_documents=request.source_documents,
|
| 192 |
+
historical_cases=historical_cases_dict,
|
| 193 |
+
case_metadata=case_metadata_dict
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
return results
|
| 197 |
+
|
| 198 |
+
except Exception as e:
|
| 199 |
+
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
@app.post("/api/v1/analyze/document-bias")
|
| 203 |
+
async def document_bias_analysis(request: DocumentBiasRequest):
|
| 204 |
+
"""Analyze document for textual biases (gender, caste, region, etc.)"""
|
| 205 |
+
try:
|
| 206 |
+
model = get_model()
|
| 207 |
+
results = model.detect_document_bias(request.case_text, request.threshold)
|
| 208 |
+
|
| 209 |
+
return {
|
| 210 |
+
"status": "success",
|
| 211 |
+
"analysis_id": f"doc_bias_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 212 |
+
"timestamp": datetime.now().isoformat(),
|
| 213 |
+
"results": results
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
except Exception as e:
|
| 217 |
+
raise HTTPException(status_code=500, detail=f"Document bias analysis failed: {str(e)}")
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
@app.post("/api/v1/analyze/rag-bias")
|
| 221 |
+
async def rag_bias_analysis(request: RAGBiasRequest):
|
| 222 |
+
"""Analyze RAG-generated output for tone, interpretive, and selectivity biases"""
|
| 223 |
+
try:
|
| 224 |
+
model = get_model()
|
| 225 |
+
results = model.detect_rag_output_bias(
|
| 226 |
+
request.rag_summary,
|
| 227 |
+
request.source_documents
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
return {
|
| 231 |
+
"status": "success",
|
| 232 |
+
"analysis_id": f"rag_bias_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 233 |
+
"timestamp": datetime.now().isoformat(),
|
| 234 |
+
"results": results
|
| 235 |
+
}
|
| 236 |
+
|
| 237 |
+
except Exception as e:
|
| 238 |
+
raise HTTPException(status_code=500, detail=f"RAG bias analysis failed: {str(e)}")
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
@app.post("/api/v1/analyze/systemic-bias")
|
| 242 |
+
async def systemic_bias_analysis(request: SystemicBiasRequest):
|
| 243 |
+
"""Analyze historical cases for systemic and statistical biases"""
|
| 244 |
+
try:
|
| 245 |
+
model = get_model()
|
| 246 |
+
historical_cases_dict = [case.dict() for case in request.historical_cases]
|
| 247 |
+
results = model.detect_systemic_bias(historical_cases_dict)
|
| 248 |
+
|
| 249 |
+
return {
|
| 250 |
+
"status": "success",
|
| 251 |
+
"analysis_id": f"systemic_bias_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 252 |
+
"timestamp": datetime.now().isoformat(),
|
| 253 |
+
"results": results
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
except Exception as e:
|
| 257 |
+
raise HTTPException(status_code=500, detail=f"Systemic bias analysis failed: {str(e)}")
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
@app.post("/api/v1/predict/outcome")
|
| 261 |
+
async def outcome_prediction(request: OutcomePredictionRequest):
|
| 262 |
+
"""Predict legal case outcome with confidence score"""
|
| 263 |
+
try:
|
| 264 |
+
model = get_model()
|
| 265 |
+
case_metadata_dict = None
|
| 266 |
+
if request.case_metadata:
|
| 267 |
+
case_metadata_dict = request.case_metadata.dict()
|
| 268 |
+
|
| 269 |
+
results = model.predict_outcome(request.case_text, case_metadata_dict)
|
| 270 |
+
|
| 271 |
+
return {
|
| 272 |
+
"status": "success",
|
| 273 |
+
"analysis_id": f"prediction_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 274 |
+
"timestamp": datetime.now().isoformat(),
|
| 275 |
+
"results": results
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
except Exception as e:
|
| 279 |
+
raise HTTPException(status_code=500, detail=f"Outcome prediction failed: {str(e)}")
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
@app.get("/api/v1/model/info")
|
| 283 |
+
async def model_info():
|
| 284 |
+
"""Get information about the loaded model"""
|
| 285 |
+
try:
|
| 286 |
+
model = get_model()
|
| 287 |
+
return {
|
| 288 |
+
"model_name": "InLegalBERT (law-ai/InLegalBERT)",
|
| 289 |
+
"device": str(model.device),
|
| 290 |
+
"bias_types_supported": list(model.bias_keywords.keys()),
|
| 291 |
+
"status": "loaded",
|
| 292 |
+
"timestamp": datetime.now().isoformat()
|
| 293 |
+
}
|
| 294 |
+
except Exception as e:
|
| 295 |
+
raise HTTPException(status_code=500, detail=f"Model info retrieval failed: {str(e)}")
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
# ============================================================================
|
| 299 |
+
# TRANSLATION ENDPOINTS
|
| 300 |
+
# ============================================================================
|
| 301 |
+
|
| 302 |
+
@app.post("/api/v1/translate/query")
|
| 303 |
+
async def translate_query(request: TranslateRequest):
|
| 304 |
+
"""
|
| 305 |
+
Translate user query to English for processing
|
| 306 |
+
|
| 307 |
+
**Supports**: Hindi, Tamil, Telugu, Bengali, Marathi, Gujarati, Kannada, Malayalam
|
| 308 |
+
"""
|
| 309 |
+
try:
|
| 310 |
+
service = get_translation_service()
|
| 311 |
+
result = service.translate_query(
|
| 312 |
+
request.text,
|
| 313 |
+
request.source_lang,
|
| 314 |
+
request.target_lang
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
return {
|
| 318 |
+
"status": "success",
|
| 319 |
+
"translation": result,
|
| 320 |
+
"timestamp": datetime.now().isoformat()
|
| 321 |
+
}
|
| 322 |
+
except Exception as e:
|
| 323 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 324 |
+
|
| 325 |
+
|
| 326 |
+
@app.post("/api/v1/translate/response")
|
| 327 |
+
async def translate_response(request: TranslateRequest):
|
| 328 |
+
"""Translate AI response to user's language"""
|
| 329 |
+
try:
|
| 330 |
+
service = get_translation_service()
|
| 331 |
+
result = service.translate_response(
|
| 332 |
+
request.text,
|
| 333 |
+
request.target_lang
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
return {
|
| 337 |
+
"status": "success",
|
| 338 |
+
"translation": result,
|
| 339 |
+
"timestamp": datetime.now().isoformat()
|
| 340 |
+
}
|
| 341 |
+
except Exception as e:
|
| 342 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 343 |
+
|
| 344 |
+
|
| 345 |
+
@app.get("/api/v1/languages")
|
| 346 |
+
async def get_supported_languages():
|
| 347 |
+
"""Get list of supported languages"""
|
| 348 |
+
service = get_translation_service()
|
| 349 |
+
return {
|
| 350 |
+
"languages": service.get_supported_languages(),
|
| 351 |
+
"total": len(service.get_supported_languages())
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
# ============================================================================
|
| 356 |
+
# SIMPLIFICATION ENDPOINTS
|
| 357 |
+
# ============================================================================
|
| 358 |
+
|
| 359 |
+
@app.post("/api/v1/simplify")
|
| 360 |
+
async def simplify_legal_text(request: SimplifyRequest):
|
| 361 |
+
"""
|
| 362 |
+
Convert complex legal language to plain language
|
| 363 |
+
|
| 364 |
+
**Perfect for citizens!**
|
| 365 |
+
"""
|
| 366 |
+
try:
|
| 367 |
+
service = get_translation_service()
|
| 368 |
+
result = service.simplify_legal_text(
|
| 369 |
+
request.legal_text,
|
| 370 |
+
request.reading_level
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
return {
|
| 374 |
+
"status": "success",
|
| 375 |
+
"simplification": result,
|
| 376 |
+
"timestamp": datetime.now().isoformat()
|
| 377 |
+
}
|
| 378 |
+
except Exception as e:
|
| 379 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
# ============================================================================
|
| 383 |
+
# DOCUMENT GENERATION ENDPOINTS
|
| 384 |
+
# ============================================================================
|
| 385 |
+
|
| 386 |
+
@app.post("/api/v1/generate/document")
|
| 387 |
+
async def generate_document(request: DocumentGenerateRequest):
|
| 388 |
+
"""
|
| 389 |
+
Generate legal documents from templates
|
| 390 |
+
|
| 391 |
+
**Available Types:**
|
| 392 |
+
- `bail_application`: Bail application under CrPC
|
| 393 |
+
- `fir_complaint`: FIR/Complaint for police
|
| 394 |
+
- `legal_notice`: Legal notice
|
| 395 |
+
- `petition`: Court petition
|
| 396 |
+
"""
|
| 397 |
+
try:
|
| 398 |
+
generator = get_document_generator()
|
| 399 |
+
|
| 400 |
+
if request.document_type == 'bail_application':
|
| 401 |
+
result = generator.generate_bail_application(request.details)
|
| 402 |
+
elif request.document_type == 'fir_complaint':
|
| 403 |
+
result = generator.generate_fir(request.details)
|
| 404 |
+
elif request.document_type == 'legal_notice':
|
| 405 |
+
result = generator.generate_legal_notice(request.details)
|
| 406 |
+
elif request.document_type == 'petition':
|
| 407 |
+
result = generator.generate_petition(request.details)
|
| 408 |
+
else:
|
| 409 |
+
raise HTTPException(
|
| 410 |
+
status_code=400,
|
| 411 |
+
detail=f"Unknown document type: {request.document_type}"
|
| 412 |
+
)
|
| 413 |
+
|
| 414 |
+
return {
|
| 415 |
+
"status": "success",
|
| 416 |
+
"document": result,
|
| 417 |
+
"timestamp": datetime.now().isoformat()
|
| 418 |
+
}
|
| 419 |
+
except Exception as e:
|
| 420 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
@app.get("/api/v1/templates")
|
| 424 |
+
async def get_templates():
|
| 425 |
+
"""Get list of available document templates"""
|
| 426 |
+
generator = get_document_generator()
|
| 427 |
+
return {
|
| 428 |
+
"templates": generator.get_template_list(),
|
| 429 |
+
"total": len(generator.get_template_list())
|
| 430 |
+
}
|
| 431 |
+
|
| 432 |
+
|
| 433 |
+
# ============================================================================
|
| 434 |
+
# SIMULATION ENDPOINTS
|
| 435 |
+
# ============================================================================
|
| 436 |
+
|
| 437 |
+
@app.post("/api/v1/simulate/outcome")
|
| 438 |
+
async def simulate_outcome(request: SimulationRequest):
|
| 439 |
+
"""
|
| 440 |
+
What-If Simulation: See how case facts affect outcomes
|
| 441 |
+
|
| 442 |
+
**Modifications Available:**
|
| 443 |
+
- `remove_prior_conviction`: Remove criminal history
|
| 444 |
+
- `add_strong_alibi`: Add alibi evidence
|
| 445 |
+
- `improve_witness_credibility`: Enhance witness reliability
|
| 446 |
+
- `add_mitigating_factors`: Add favorable circumstances
|
| 447 |
+
- `reduce_flight_risk`: Show community ties
|
| 448 |
+
- `enhance_evidence`: Strengthen evidence quality
|
| 449 |
+
"""
|
| 450 |
+
try:
|
| 451 |
+
engine = get_simulation_engine()
|
| 452 |
+
result = engine.simulate_outcome(
|
| 453 |
+
request.base_case,
|
| 454 |
+
request.modifications
|
| 455 |
+
)
|
| 456 |
+
|
| 457 |
+
return {
|
| 458 |
+
"status": "success",
|
| 459 |
+
"simulation": result,
|
| 460 |
+
"timestamp": datetime.now().isoformat()
|
| 461 |
+
}
|
| 462 |
+
except Exception as e:
|
| 463 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
@app.post("/api/v1/simulate/sensitivity")
|
| 467 |
+
async def sensitivity_analysis(request: SensitivityRequest):
|
| 468 |
+
"""
|
| 469 |
+
Sensitivity Analysis: Test impact of each factor independently
|
| 470 |
+
|
| 471 |
+
Shows which factors have the most influence on case outcome
|
| 472 |
+
"""
|
| 473 |
+
try:
|
| 474 |
+
engine = get_simulation_engine()
|
| 475 |
+
result = engine.sensitivity_analysis(request.case_facts)
|
| 476 |
+
|
| 477 |
+
return {
|
| 478 |
+
"status": "success",
|
| 479 |
+
"sensitivity": result,
|
| 480 |
+
"timestamp": datetime.now().isoformat()
|
| 481 |
+
}
|
| 482 |
+
except Exception as e:
|
| 483 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 484 |
+
|
| 485 |
+
|
| 486 |
+
# ============================================================================
|
| 487 |
+
# DEMO ENDPOINT
|
| 488 |
+
# ============================================================================
|
| 489 |
+
|
| 490 |
+
@app.get("/api/v1/demo/complete")
|
| 491 |
+
async def complete_demo():
|
| 492 |
+
"""
|
| 493 |
+
Complete feature demonstration
|
| 494 |
+
|
| 495 |
+
Shows all capabilities in one response
|
| 496 |
+
"""
|
| 497 |
+
|
| 498 |
+
# 1. Translation
|
| 499 |
+
translation_service = get_translation_service()
|
| 500 |
+
translation_demo = translation_service.translate_query(
|
| 501 |
+
"मुझे जमानत चाहिए",
|
| 502 |
+
"hi",
|
| 503 |
+
"en"
|
| 504 |
+
)
|
| 505 |
+
|
| 506 |
+
# 2. Simplification
|
| 507 |
+
simplification_demo = translation_service.simplify_legal_text(
|
| 508 |
+
"The appellant filed a habeas corpus petition under Article 226.",
|
| 509 |
+
"simple"
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
# 3. Document Generation
|
| 513 |
+
doc_generator = get_document_generator()
|
| 514 |
+
doc_demo = doc_generator.generate_bail_application({
|
| 515 |
+
'applicant_name': 'Demo User',
|
| 516 |
+
'state': 'Demo State',
|
| 517 |
+
'first_time_offender': True
|
| 518 |
+
})
|
| 519 |
+
|
| 520 |
+
# 4. Simulation
|
| 521 |
+
sim_engine = get_simulation_engine()
|
| 522 |
+
sim_demo = sim_engine.simulate_outcome(
|
| 523 |
+
{'facts': 'Accused has prior conviction. Witnesses unreliable.'},
|
| 524 |
+
{'remove_prior_conviction': True, 'improve_witness_credibility': True}
|
| 525 |
+
)
|
| 526 |
+
|
| 527 |
+
return {
|
| 528 |
+
"status": "success",
|
| 529 |
+
"demo_features": {
|
| 530 |
+
"1_translation": {
|
| 531 |
+
"feature": "Multilingual Support",
|
| 532 |
+
"input": "मुझे जमानत चाहिए (Hindi)",
|
| 533 |
+
"output": translation_demo['translated_text'],
|
| 534 |
+
"languages_supported": 9
|
| 535 |
+
},
|
| 536 |
+
"2_simplification": {
|
| 537 |
+
"feature": "Plain Language Conversion",
|
| 538 |
+
"original": "habeas corpus petition under Article 226",
|
| 539 |
+
"simplified": simplification_demo['simplified_text'][:100] + "...",
|
| 540 |
+
"reading_level": "Grade 8"
|
| 541 |
+
},
|
| 542 |
+
"3_document_generation": {
|
| 543 |
+
"feature": "Legal Document Generator",
|
| 544 |
+
"document_type": "Bail Application",
|
| 545 |
+
"length": len(doc_demo['content']),
|
| 546 |
+
"editable": doc_demo['editable'],
|
| 547 |
+
"preview": doc_demo['content'][:300] + "..."
|
| 548 |
+
},
|
| 549 |
+
"4_simulation": {
|
| 550 |
+
"feature": "What-If Simulation",
|
| 551 |
+
"base_outcome": sim_demo['base_case']['prediction']['predictedOutcome'],
|
| 552 |
+
"modified_outcome": sim_demo['modified_case']['prediction']['predictedOutcome'],
|
| 553 |
+
"outcome_changed": sim_demo['impact_analysis']['outcome_changed'],
|
| 554 |
+
"confidence_change": f"{sim_demo['impact_analysis']['confidence_change_percent']}%"
|
| 555 |
+
}
|
| 556 |
+
},
|
| 557 |
+
"total_features_demonstrated": 4,
|
| 558 |
+
"ai_models_used": [
|
| 559 |
+
"InLegalBERT (Bias Detection)",
|
| 560 |
+
"Google Translate (Multilingual)",
|
| 561 |
+
"Template-based Document Generation",
|
| 562 |
+
"Simulation Engine"
|
| 563 |
+
],
|
| 564 |
+
"timestamp": datetime.now().isoformat()
|
| 565 |
+
}
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
# ============================================================================
|
| 569 |
+
# STARTUP EVENT
|
| 570 |
+
# ============================================================================
|
| 571 |
+
|
| 572 |
+
@app.on_event("startup")
|
| 573 |
+
async def startup_event():
|
| 574 |
+
"""Initialize all services on startup"""
|
| 575 |
+
print("=" * 70)
|
| 576 |
+
print("🚀 LEXAI UNIFIED ML API")
|
| 577 |
+
print("=" * 70)
|
| 578 |
+
print("✅ InLegalBERT Model: Loading...")
|
| 579 |
+
get_model()
|
| 580 |
+
print("✅ InLegalBERT Model: Ready")
|
| 581 |
+
print("✅ Translation Service: Ready (9 languages)")
|
| 582 |
+
print("✅ Document Generator: Ready (4 templates)")
|
| 583 |
+
print("✅ Simplification: Ready")
|
| 584 |
+
print("✅ Simulation Engine: Ready")
|
| 585 |
+
print("=" * 70)
|
| 586 |
+
print("📍 API Docs: http://localhost:8001/docs")
|
| 587 |
+
print("🎯 Demo Endpoint: http://localhost:8001/api/v1/demo/complete")
|
| 588 |
+
print("=" * 70)
|
| 589 |
+
|
| 590 |
+
|
| 591 |
+
# ============================================================================
|
| 592 |
+
# MAIN ENTRY POINT
|
| 593 |
+
# ============================================================================
|
| 594 |
+
|
| 595 |
+
if __name__ == "__main__":
|
| 596 |
+
import os
|
| 597 |
+
port = int(os.environ.get("PORT", 8001))
|
| 598 |
+
uvicorn.run(
|
| 599 |
+
"api:app",
|
| 600 |
+
host="0.0.0.0",
|
| 601 |
+
port=port,
|
| 602 |
+
reload=False,
|
| 603 |
+
log_level="info"
|
| 604 |
+
)
|
app.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Hugging Face Spaces entry point
|
| 2 |
+
import os
|
| 3 |
+
from api import app
|
| 4 |
+
|
| 5 |
+
# This file is used by Hugging Face Spaces to start the application
|
| 6 |
+
if __name__ == "__main__":
|
| 7 |
+
import uvicorn
|
| 8 |
+
port = int(os.environ.get("PORT", 7860)) # HF Spaces default port
|
| 9 |
+
uvicorn.run(
|
| 10 |
+
app,
|
| 11 |
+
host="0.0.0.0",
|
| 12 |
+
port=port,
|
| 13 |
+
log_level="info"
|
| 14 |
+
)
|
bias_prediction_engine.py
ADDED
|
@@ -0,0 +1,610 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Bias Detection and Outcome Prediction Engine using InLegalBERT
|
| 3 |
+
================================================================
|
| 4 |
+
|
| 5 |
+
This module provides:
|
| 6 |
+
1. Document/Text bias detection (gender, region, caste, etc.)
|
| 7 |
+
2. RAG output bias detection (tone, interpretive bias)
|
| 8 |
+
3. Systemic/Statistical bias analysis
|
| 9 |
+
4. Legal outcome prediction with confidence scores
|
| 10 |
+
|
| 11 |
+
Model: InLegalBERT (Hugging Face pretrained for Indian legal cases)
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import torch
|
| 15 |
+
import numpy as np
|
| 16 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoModel
|
| 17 |
+
from typing import Dict, List, Any, Optional, Union
|
| 18 |
+
import re
|
| 19 |
+
from collections import Counter
|
| 20 |
+
import json
|
| 21 |
+
from datetime import datetime
|
| 22 |
+
import warnings
|
| 23 |
+
warnings.filterwarnings('ignore')
|
| 24 |
+
|
| 25 |
+
# ============================================================================
|
| 26 |
+
# MODEL INITIALIZATION
|
| 27 |
+
# ============================================================================
|
| 28 |
+
|
| 29 |
+
class InLegalBERTEngine:
|
| 30 |
+
"""
|
| 31 |
+
Main engine for bias detection and outcome prediction using InLegalBERT
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def __init__(self, model_name: str = "law-ai/InLegalBERT"):
|
| 35 |
+
"""
|
| 36 |
+
Initialize the InLegalBERT model and tokenizer
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
model_name: HuggingFace model identifier
|
| 40 |
+
"""
|
| 41 |
+
print(f"Loading InLegalBERT model: {model_name}")
|
| 42 |
+
|
| 43 |
+
# Load tokenizer and base model for embeddings
|
| 44 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 45 |
+
self.base_model = AutoModel.from_pretrained(model_name)
|
| 46 |
+
|
| 47 |
+
# Set device
|
| 48 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 49 |
+
self.base_model.to(self.device)
|
| 50 |
+
self.base_model.eval()
|
| 51 |
+
|
| 52 |
+
# Bias detection keywords (Indian legal context)
|
| 53 |
+
self.bias_keywords = {
|
| 54 |
+
'gender': [
|
| 55 |
+
'woman', 'women', 'girl', 'female', 'lady', 'wife', 'mother',
|
| 56 |
+
'man', 'men', 'boy', 'male', 'husband', 'father', 'manhood', 'womanhood'
|
| 57 |
+
],
|
| 58 |
+
'caste': [
|
| 59 |
+
'scheduled caste', 'sc', 'st', 'scheduled tribe', 'obc', 'backward class',
|
| 60 |
+
'dalit', 'brahmin', 'upper caste', 'lower caste', 'caste', 'jati'
|
| 61 |
+
],
|
| 62 |
+
'religion': [
|
| 63 |
+
'hindu', 'muslim', 'christian', 'sikh', 'buddhist', 'jain',
|
| 64 |
+
'religious', 'communal', 'minority', 'majority community'
|
| 65 |
+
],
|
| 66 |
+
'region': [
|
| 67 |
+
'north', 'south', 'east', 'west', 'rural', 'urban', 'tribal',
|
| 68 |
+
'metropolitan', 'village', 'city', 'state', 'region'
|
| 69 |
+
],
|
| 70 |
+
'socioeconomic': [
|
| 71 |
+
'poor', 'rich', 'wealthy', 'poverty', 'income', 'economically',
|
| 72 |
+
'below poverty line', 'bpl', 'weaker section', 'privileged'
|
| 73 |
+
],
|
| 74 |
+
'age': [
|
| 75 |
+
'minor', 'juvenile', 'child', 'elderly', 'senior citizen', 'youth',
|
| 76 |
+
'old', 'young', 'aged'
|
| 77 |
+
]
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
print(f"Model loaded successfully on {self.device}")
|
| 81 |
+
|
| 82 |
+
# ========================================================================
|
| 83 |
+
# UTILITY FUNCTIONS
|
| 84 |
+
# ========================================================================
|
| 85 |
+
|
| 86 |
+
def get_embeddings(self, text: str) -> torch.Tensor:
|
| 87 |
+
"""
|
| 88 |
+
Get BERT embeddings for input text
|
| 89 |
+
|
| 90 |
+
Args:
|
| 91 |
+
text: Input text string
|
| 92 |
+
|
| 93 |
+
Returns:
|
| 94 |
+
torch.Tensor: Embedding vector
|
| 95 |
+
"""
|
| 96 |
+
# Tokenize
|
| 97 |
+
inputs = self.tokenizer(
|
| 98 |
+
text,
|
| 99 |
+
return_tensors="pt",
|
| 100 |
+
truncation=True,
|
| 101 |
+
max_length=512,
|
| 102 |
+
padding=True
|
| 103 |
+
).to(self.device)
|
| 104 |
+
|
| 105 |
+
# Get embeddings
|
| 106 |
+
with torch.no_grad():
|
| 107 |
+
outputs = self.base_model(**inputs)
|
| 108 |
+
# Use CLS token embedding (first token)
|
| 109 |
+
embeddings = outputs.last_hidden_state[:, 0, :]
|
| 110 |
+
|
| 111 |
+
return embeddings
|
| 112 |
+
|
| 113 |
+
def compute_bias_score(self, text: str, bias_type: str) -> float:
|
| 114 |
+
"""
|
| 115 |
+
Compute bias score for a specific bias type using keyword frequency
|
| 116 |
+
and contextual analysis
|
| 117 |
+
|
| 118 |
+
Args:
|
| 119 |
+
text: Input text
|
| 120 |
+
bias_type: Type of bias (gender, caste, etc.)
|
| 121 |
+
|
| 122 |
+
Returns:
|
| 123 |
+
float: Bias score between 0 and 1
|
| 124 |
+
"""
|
| 125 |
+
text_lower = text.lower()
|
| 126 |
+
keywords = self.bias_keywords.get(bias_type, [])
|
| 127 |
+
|
| 128 |
+
# Count keyword occurrences
|
| 129 |
+
keyword_count = sum(text_lower.count(keyword) for keyword in keywords)
|
| 130 |
+
|
| 131 |
+
# Normalize by text length (words)
|
| 132 |
+
word_count = len(text.split())
|
| 133 |
+
if word_count == 0:
|
| 134 |
+
return 0.0
|
| 135 |
+
|
| 136 |
+
# Calculate frequency-based score
|
| 137 |
+
frequency_score = min(keyword_count / word_count * 10, 1.0)
|
| 138 |
+
|
| 139 |
+
# Get contextual score using embeddings (simplified)
|
| 140 |
+
# In production, use a fine-tuned classifier
|
| 141 |
+
contextual_score = frequency_score * 0.8 # Simplified
|
| 142 |
+
|
| 143 |
+
return round(contextual_score, 3)
|
| 144 |
+
|
| 145 |
+
# ========================================================================
|
| 146 |
+
# 1. DOCUMENT/TEXT BIAS DETECTION
|
| 147 |
+
# ========================================================================
|
| 148 |
+
|
| 149 |
+
def detect_document_bias(self, text: str, threshold: float = 0.15) -> Dict[str, Any]:
|
| 150 |
+
"""
|
| 151 |
+
Detect various biases in legal documents/FIRs/judgments
|
| 152 |
+
|
| 153 |
+
Args:
|
| 154 |
+
text: Legal document text
|
| 155 |
+
threshold: Minimum score to flag a bias (default 0.15)
|
| 156 |
+
|
| 157 |
+
Returns:
|
| 158 |
+
Dict containing bias flags and detailed scores
|
| 159 |
+
"""
|
| 160 |
+
bias_scores = {}
|
| 161 |
+
bias_flags = []
|
| 162 |
+
|
| 163 |
+
# Analyze each bias type
|
| 164 |
+
for bias_type in self.bias_keywords.keys():
|
| 165 |
+
score = self.compute_bias_score(text, bias_type)
|
| 166 |
+
bias_scores[bias_type] = score
|
| 167 |
+
|
| 168 |
+
if score >= threshold:
|
| 169 |
+
bias_flags.append(bias_type)
|
| 170 |
+
|
| 171 |
+
# Determine severity levels
|
| 172 |
+
bias_details = []
|
| 173 |
+
for bias_type, score in bias_scores.items():
|
| 174 |
+
if score >= threshold:
|
| 175 |
+
severity = "high" if score >= 0.4 else "medium" if score >= 0.25 else "low"
|
| 176 |
+
bias_details.append({
|
| 177 |
+
"type": bias_type,
|
| 178 |
+
"severity": severity,
|
| 179 |
+
"score": score,
|
| 180 |
+
"description": f"{bias_type.capitalize()} bias detected based on keyword analysis and context"
|
| 181 |
+
})
|
| 182 |
+
|
| 183 |
+
return {
|
| 184 |
+
"biasFlags_text": bias_flags,
|
| 185 |
+
"bias_scores": bias_scores,
|
| 186 |
+
"bias_details": bias_details,
|
| 187 |
+
"overall_bias_score": round(np.mean(list(bias_scores.values())), 3),
|
| 188 |
+
"analysis_timestamp": datetime.now().isoformat()
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
# ========================================================================
|
| 192 |
+
# 2. RAG OUTPUT BIAS DETECTION
|
| 193 |
+
# ========================================================================
|
| 194 |
+
|
| 195 |
+
def detect_rag_output_bias(self,
|
| 196 |
+
rag_summary: str,
|
| 197 |
+
source_documents: List[str]) -> Dict[str, Any]:
|
| 198 |
+
"""
|
| 199 |
+
Detect bias in AI-generated RAG summaries/reasoning
|
| 200 |
+
|
| 201 |
+
Args:
|
| 202 |
+
rag_summary: AI-generated summary or reasoning
|
| 203 |
+
source_documents: Original source documents used for RAG
|
| 204 |
+
|
| 205 |
+
Returns:
|
| 206 |
+
Dict containing RAG-specific bias flags
|
| 207 |
+
"""
|
| 208 |
+
bias_flags = []
|
| 209 |
+
bias_details = []
|
| 210 |
+
|
| 211 |
+
# Get embeddings
|
| 212 |
+
summary_emb = self.get_embeddings(rag_summary)
|
| 213 |
+
source_embs = [self.get_embeddings(doc) for doc in source_documents[:5]] # Limit to 5
|
| 214 |
+
|
| 215 |
+
# 1. TONE BIAS - Check if summary tone differs from sources
|
| 216 |
+
if source_embs:
|
| 217 |
+
avg_source_emb = torch.mean(torch.stack(source_embs), dim=0)
|
| 218 |
+
# Cosine similarity
|
| 219 |
+
similarity = torch.nn.functional.cosine_similarity(summary_emb, avg_source_emb)
|
| 220 |
+
|
| 221 |
+
if similarity < 0.7: # Low similarity indicates tone shift
|
| 222 |
+
bias_flags.append("tone_bias")
|
| 223 |
+
bias_details.append({
|
| 224 |
+
"type": "tone_bias",
|
| 225 |
+
"severity": "medium",
|
| 226 |
+
"score": round(1 - similarity.item(), 3),
|
| 227 |
+
"description": "AI summary tone differs significantly from source documents"
|
| 228 |
+
})
|
| 229 |
+
|
| 230 |
+
# 2. INTERPRETIVE BIAS - Check for subjective language
|
| 231 |
+
subjective_words = [
|
| 232 |
+
'clearly', 'obviously', 'undoubtedly', 'certainly', 'definitely',
|
| 233 |
+
'surely', 'apparently', 'seemingly', 'arguably', 'presumably'
|
| 234 |
+
]
|
| 235 |
+
summary_lower = rag_summary.lower()
|
| 236 |
+
subjective_count = sum(summary_lower.count(word) for word in subjective_words)
|
| 237 |
+
|
| 238 |
+
if subjective_count > 2:
|
| 239 |
+
bias_flags.append("interpretive_bias")
|
| 240 |
+
bias_details.append({
|
| 241 |
+
"type": "interpretive_bias",
|
| 242 |
+
"severity": "medium" if subjective_count > 4 else "low",
|
| 243 |
+
"score": round(min(subjective_count / 10, 1.0), 3),
|
| 244 |
+
"description": f"Summary contains {subjective_count} subjective/interpretive terms"
|
| 245 |
+
})
|
| 246 |
+
|
| 247 |
+
# 3. SELECTIVITY BIAS - Check if summary over-represents certain aspects
|
| 248 |
+
# Count mentions of different legal aspects
|
| 249 |
+
aspects = {
|
| 250 |
+
'procedural': ['procedure', 'process', 'filing', 'hearing', 'appeal'],
|
| 251 |
+
'substantive': ['law', 'statute', 'provision', 'section', 'act'],
|
| 252 |
+
'factual': ['fact', 'evidence', 'witness', 'testimony', 'statement']
|
| 253 |
+
}
|
| 254 |
+
|
| 255 |
+
aspect_counts = {k: sum(summary_lower.count(w) for w in v) for k, v in aspects.items()}
|
| 256 |
+
max_count = max(aspect_counts.values()) if aspect_counts.values() else 1
|
| 257 |
+
|
| 258 |
+
if max_count > 5 and any(count < max_count * 0.3 for count in aspect_counts.values()):
|
| 259 |
+
bias_flags.append("selectivity_bias")
|
| 260 |
+
bias_details.append({
|
| 261 |
+
"type": "selectivity_bias",
|
| 262 |
+
"severity": "low",
|
| 263 |
+
"score": 0.4,
|
| 264 |
+
"description": "Summary may over-emphasize certain legal aspects"
|
| 265 |
+
})
|
| 266 |
+
|
| 267 |
+
return {
|
| 268 |
+
"biasFlags_output": bias_flags,
|
| 269 |
+
"bias_details": bias_details,
|
| 270 |
+
"analysis_timestamp": datetime.now().isoformat()
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
# ========================================================================
|
| 274 |
+
# 3. SYSTEMIC/STATISTICAL BIAS DETECTION
|
| 275 |
+
# ========================================================================
|
| 276 |
+
|
| 277 |
+
def detect_systemic_bias(self,
|
| 278 |
+
historical_cases: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 279 |
+
"""
|
| 280 |
+
Analyze systemic and statistical biases from historical case data
|
| 281 |
+
|
| 282 |
+
Args:
|
| 283 |
+
historical_cases: List of case dictionaries with keys:
|
| 284 |
+
- outcome: str (e.g., "conviction", "acquittal")
|
| 285 |
+
- gender: str (optional)
|
| 286 |
+
- region: str (optional)
|
| 287 |
+
- caste: str (optional)
|
| 288 |
+
- case_type: str
|
| 289 |
+
- year: int
|
| 290 |
+
|
| 291 |
+
Returns:
|
| 292 |
+
Dict containing systemic bias metrics and dashboard data
|
| 293 |
+
"""
|
| 294 |
+
if not historical_cases:
|
| 295 |
+
return {"error": "No historical cases provided"}
|
| 296 |
+
|
| 297 |
+
# Initialize analytics
|
| 298 |
+
outcome_by_gender = {}
|
| 299 |
+
outcome_by_region = {}
|
| 300 |
+
outcome_by_caste = {}
|
| 301 |
+
outcome_by_year = {}
|
| 302 |
+
|
| 303 |
+
# Process cases
|
| 304 |
+
for case in historical_cases:
|
| 305 |
+
outcome = case.get('outcome', 'unknown')
|
| 306 |
+
|
| 307 |
+
# Gender analysis
|
| 308 |
+
if 'gender' in case:
|
| 309 |
+
gender = case['gender']
|
| 310 |
+
if gender not in outcome_by_gender:
|
| 311 |
+
outcome_by_gender[gender] = []
|
| 312 |
+
outcome_by_gender[gender].append(outcome)
|
| 313 |
+
|
| 314 |
+
# Region analysis
|
| 315 |
+
if 'region' in case:
|
| 316 |
+
region = case['region']
|
| 317 |
+
if region not in outcome_by_region:
|
| 318 |
+
outcome_by_region[region] = []
|
| 319 |
+
outcome_by_region[region].append(outcome)
|
| 320 |
+
|
| 321 |
+
# Caste analysis
|
| 322 |
+
if 'caste' in case:
|
| 323 |
+
caste = case['caste']
|
| 324 |
+
if caste not in outcome_by_caste:
|
| 325 |
+
outcome_by_caste[caste] = []
|
| 326 |
+
outcome_by_caste[caste].append(outcome)
|
| 327 |
+
|
| 328 |
+
# Temporal analysis
|
| 329 |
+
if 'year' in case:
|
| 330 |
+
year = case['year']
|
| 331 |
+
if year not in outcome_by_year:
|
| 332 |
+
outcome_by_year[year] = []
|
| 333 |
+
outcome_by_year[year].append(outcome)
|
| 334 |
+
|
| 335 |
+
# Calculate disparity metrics
|
| 336 |
+
def calculate_disparity(outcome_dict: Dict) -> Dict:
|
| 337 |
+
"""Calculate outcome disparities"""
|
| 338 |
+
disparity_data = {}
|
| 339 |
+
for category, outcomes in outcome_dict.items():
|
| 340 |
+
total = len(outcomes)
|
| 341 |
+
if total > 0:
|
| 342 |
+
conviction_rate = outcomes.count('conviction') / total
|
| 343 |
+
disparity_data[category] = {
|
| 344 |
+
'total_cases': total,
|
| 345 |
+
'conviction_rate': round(conviction_rate, 3),
|
| 346 |
+
'acquittal_rate': round(outcomes.count('acquittal') / total, 3)
|
| 347 |
+
}
|
| 348 |
+
return disparity_data
|
| 349 |
+
|
| 350 |
+
gender_disparity = calculate_disparity(outcome_by_gender)
|
| 351 |
+
region_disparity = calculate_disparity(outcome_by_region)
|
| 352 |
+
caste_disparity = calculate_disparity(outcome_by_caste)
|
| 353 |
+
|
| 354 |
+
# Detect significant disparities
|
| 355 |
+
bias_flags = []
|
| 356 |
+
|
| 357 |
+
if gender_disparity:
|
| 358 |
+
rates = [d['conviction_rate'] for d in gender_disparity.values()]
|
| 359 |
+
if max(rates) - min(rates) > 0.15:
|
| 360 |
+
bias_flags.append("gender_disparity")
|
| 361 |
+
|
| 362 |
+
if region_disparity:
|
| 363 |
+
rates = [d['conviction_rate'] for d in region_disparity.values()]
|
| 364 |
+
if max(rates) - min(rates) > 0.15:
|
| 365 |
+
bias_flags.append("regional_disparity")
|
| 366 |
+
|
| 367 |
+
if caste_disparity:
|
| 368 |
+
rates = [d['conviction_rate'] for d in caste_disparity.values()]
|
| 369 |
+
if max(rates) - min(rates) > 0.15:
|
| 370 |
+
bias_flags.append("caste_disparity")
|
| 371 |
+
|
| 372 |
+
# Generate dashboard-ready data
|
| 373 |
+
dashboard_data = {
|
| 374 |
+
"summary_metrics": {
|
| 375 |
+
"total_cases_analyzed": len(historical_cases),
|
| 376 |
+
"overall_conviction_rate": round(
|
| 377 |
+
sum(1 for c in historical_cases if c.get('outcome') == 'conviction') / len(historical_cases),
|
| 378 |
+
3
|
| 379 |
+
),
|
| 380 |
+
"bias_flags_detected": len(bias_flags)
|
| 381 |
+
},
|
| 382 |
+
"gender_analysis": {
|
| 383 |
+
"disparity_data": gender_disparity,
|
| 384 |
+
"chart_data": [
|
| 385 |
+
{"category": k, "conviction_rate": v['conviction_rate']}
|
| 386 |
+
for k, v in gender_disparity.items()
|
| 387 |
+
]
|
| 388 |
+
},
|
| 389 |
+
"regional_analysis": {
|
| 390 |
+
"disparity_data": region_disparity,
|
| 391 |
+
"chart_data": [
|
| 392 |
+
{"category": k, "conviction_rate": v['conviction_rate']}
|
| 393 |
+
for k, v in region_disparity.items()
|
| 394 |
+
]
|
| 395 |
+
},
|
| 396 |
+
"caste_analysis": {
|
| 397 |
+
"disparity_data": caste_disparity,
|
| 398 |
+
"chart_data": [
|
| 399 |
+
{"category": k, "conviction_rate": v['conviction_rate']}
|
| 400 |
+
for k, v in caste_disparity.items()
|
| 401 |
+
]
|
| 402 |
+
},
|
| 403 |
+
"temporal_trends": {
|
| 404 |
+
"by_year": {
|
| 405 |
+
year: {
|
| 406 |
+
'total': len(outcomes),
|
| 407 |
+
'conviction_rate': round(outcomes.count('conviction') / len(outcomes), 3)
|
| 408 |
+
}
|
| 409 |
+
for year, outcomes in outcome_by_year.items()
|
| 410 |
+
}
|
| 411 |
+
}
|
| 412 |
+
}
|
| 413 |
+
|
| 414 |
+
return {
|
| 415 |
+
"systemic_bias_flags": bias_flags,
|
| 416 |
+
"biasDashboardData": dashboard_data,
|
| 417 |
+
"analysis_timestamp": datetime.now().isoformat()
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
# ========================================================================
|
| 421 |
+
# 4. OUTCOME PREDICTION
|
| 422 |
+
# ========================================================================
|
| 423 |
+
|
| 424 |
+
def predict_outcome(self,
|
| 425 |
+
case_text: str,
|
| 426 |
+
case_metadata: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
|
| 427 |
+
"""
|
| 428 |
+
Predict legal case outcome using InLegalBERT embeddings and heuristics
|
| 429 |
+
|
| 430 |
+
Args:
|
| 431 |
+
case_text: Full case text (FIR, facts, arguments, etc.)
|
| 432 |
+
case_metadata: Optional metadata (case_type, jurisdiction, etc.)
|
| 433 |
+
|
| 434 |
+
Returns:
|
| 435 |
+
Dict containing prediction, confidence, and justification
|
| 436 |
+
"""
|
| 437 |
+
# Get text embeddings
|
| 438 |
+
embeddings = self.get_embeddings(case_text)
|
| 439 |
+
|
| 440 |
+
# Keyword-based prediction (simplified - in production use fine-tuned classifier)
|
| 441 |
+
conviction_keywords = [
|
| 442 |
+
'guilty', 'convicted', 'evidence proves', 'beyond reasonable doubt',
|
| 443 |
+
'establish', 'proven', 'corroborated', 'substantiated'
|
| 444 |
+
]
|
| 445 |
+
acquittal_keywords = [
|
| 446 |
+
'not guilty', 'acquitted', 'benefit of doubt', 'insufficient evidence',
|
| 447 |
+
'failed to prove', 'contradictory', 'unreliable', 'doubt'
|
| 448 |
+
]
|
| 449 |
+
|
| 450 |
+
text_lower = case_text.lower()
|
| 451 |
+
conviction_score = sum(text_lower.count(kw) for kw in conviction_keywords)
|
| 452 |
+
acquittal_score = sum(text_lower.count(kw) for kw in acquittal_keywords)
|
| 453 |
+
|
| 454 |
+
# Calculate prediction
|
| 455 |
+
total_score = conviction_score + acquittal_score
|
| 456 |
+
if total_score == 0:
|
| 457 |
+
# No strong indicators, use neutral prediction
|
| 458 |
+
predicted_outcome = "uncertain"
|
| 459 |
+
confidence_score = 0.5
|
| 460 |
+
justification = "Insufficient textual indicators for confident prediction"
|
| 461 |
+
else:
|
| 462 |
+
conviction_prob = conviction_score / total_score
|
| 463 |
+
|
| 464 |
+
if conviction_prob > 0.6:
|
| 465 |
+
predicted_outcome = "conviction"
|
| 466 |
+
confidence_score = round(conviction_prob, 3)
|
| 467 |
+
justification = f"Text analysis shows {conviction_score} conviction indicators vs {acquittal_score} acquittal indicators"
|
| 468 |
+
elif conviction_prob < 0.4:
|
| 469 |
+
predicted_outcome = "acquittal"
|
| 470 |
+
confidence_score = round(1 - conviction_prob, 3)
|
| 471 |
+
justification = f"Text analysis shows {acquittal_score} acquittal indicators vs {conviction_score} conviction indicators"
|
| 472 |
+
else:
|
| 473 |
+
predicted_outcome = "uncertain"
|
| 474 |
+
confidence_score = 0.5
|
| 475 |
+
justification = "Mixed indicators suggest uncertain outcome"
|
| 476 |
+
|
| 477 |
+
# Adjust for metadata if provided
|
| 478 |
+
if case_metadata:
|
| 479 |
+
case_type = case_metadata.get('case_type', '').lower()
|
| 480 |
+
|
| 481 |
+
# Example adjustments (customize based on domain knowledge)
|
| 482 |
+
if 'bail' in case_type:
|
| 483 |
+
if predicted_outcome == "conviction":
|
| 484 |
+
predicted_outcome = "bail_denied"
|
| 485 |
+
justification += "; Bail application context considered"
|
| 486 |
+
elif predicted_outcome == "acquittal":
|
| 487 |
+
predicted_outcome = "bail_granted"
|
| 488 |
+
justification += "; Bail application context considered"
|
| 489 |
+
|
| 490 |
+
# Confidence level categorization
|
| 491 |
+
if confidence_score >= 0.75:
|
| 492 |
+
confidence_level = "high"
|
| 493 |
+
elif confidence_score >= 0.5:
|
| 494 |
+
confidence_level = "medium"
|
| 495 |
+
else:
|
| 496 |
+
confidence_level = "low"
|
| 497 |
+
|
| 498 |
+
return {
|
| 499 |
+
"predictedOutcome": predicted_outcome,
|
| 500 |
+
"confidenceScore": confidence_score,
|
| 501 |
+
"confidenceLevel": confidence_level,
|
| 502 |
+
"justification": justification,
|
| 503 |
+
"embedding_norm": float(torch.norm(embeddings).item()),
|
| 504 |
+
"analysis_timestamp": datetime.now().isoformat()
|
| 505 |
+
}
|
| 506 |
+
|
| 507 |
+
# ============================================================================
|
| 508 |
+
# API INTERFACE FUNCTIONS
|
| 509 |
+
# ============================================================================
|
| 510 |
+
|
| 511 |
+
# Global model instance (loaded once)
|
| 512 |
+
_model_instance = None
|
| 513 |
+
|
| 514 |
+
def get_model() -> InLegalBERTEngine:
|
| 515 |
+
"""Get or create model instance (singleton pattern)"""
|
| 516 |
+
global _model_instance
|
| 517 |
+
if _model_instance is None:
|
| 518 |
+
_model_instance = InLegalBERTEngine()
|
| 519 |
+
return _model_instance
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
def analyze_legal_case(
|
| 523 |
+
case_text: str,
|
| 524 |
+
rag_summary: Optional[str] = None,
|
| 525 |
+
source_documents: Optional[List[str]] = None,
|
| 526 |
+
historical_cases: Optional[List[Dict]] = None,
|
| 527 |
+
case_metadata: Optional[Dict] = None
|
| 528 |
+
) -> Dict[str, Any]:
|
| 529 |
+
"""
|
| 530 |
+
Main API function for comprehensive legal case analysis
|
| 531 |
+
|
| 532 |
+
Args:
|
| 533 |
+
case_text: Legal document/FIR/judgment text
|
| 534 |
+
rag_summary: AI-generated summary (for RAG bias detection)
|
| 535 |
+
source_documents: Source docs used for RAG (for RAG bias detection)
|
| 536 |
+
historical_cases: Historical case data (for systemic bias analysis)
|
| 537 |
+
case_metadata: Case metadata for outcome prediction
|
| 538 |
+
|
| 539 |
+
Returns:
|
| 540 |
+
JSON-serializable dict with all analysis results
|
| 541 |
+
"""
|
| 542 |
+
model = get_model()
|
| 543 |
+
|
| 544 |
+
results = {
|
| 545 |
+
"status": "success",
|
| 546 |
+
"analysis_id": f"analysis_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
|
| 547 |
+
"timestamp": datetime.now().isoformat()
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
# 1. Document bias detection
|
| 551 |
+
if case_text:
|
| 552 |
+
results["document_bias"] = model.detect_document_bias(case_text)
|
| 553 |
+
|
| 554 |
+
# 2. RAG output bias detection
|
| 555 |
+
if rag_summary and source_documents:
|
| 556 |
+
results["rag_bias"] = model.detect_rag_output_bias(rag_summary, source_documents)
|
| 557 |
+
|
| 558 |
+
# 3. Systemic bias analysis
|
| 559 |
+
if historical_cases:
|
| 560 |
+
results["systemic_bias"] = model.detect_systemic_bias(historical_cases)
|
| 561 |
+
|
| 562 |
+
# 4. Outcome prediction
|
| 563 |
+
if case_text:
|
| 564 |
+
results["outcome_prediction"] = model.predict_outcome(case_text, case_metadata)
|
| 565 |
+
|
| 566 |
+
return results
|
| 567 |
+
|
| 568 |
+
|
| 569 |
+
# ============================================================================
|
| 570 |
+
# EXAMPLE USAGE
|
| 571 |
+
# ============================================================================
|
| 572 |
+
|
| 573 |
+
if __name__ == "__main__":
|
| 574 |
+
# Example legal case text
|
| 575 |
+
sample_case = """
|
| 576 |
+
The accused, a 35-year-old woman from rural Maharashtra, was charged under
|
| 577 |
+
Section 302 IPC for alleged murder. The prosecution's case relies heavily on
|
| 578 |
+
circumstantial evidence. The witness testimonies are contradictory, and the
|
| 579 |
+
forensic evidence is inconclusive. The accused belongs to a scheduled caste
|
| 580 |
+
community. The defense argues that there is insufficient evidence to establish
|
| 581 |
+
guilt beyond reasonable doubt.
|
| 582 |
+
"""
|
| 583 |
+
|
| 584 |
+
# Example RAG summary
|
| 585 |
+
sample_rag_summary = """
|
| 586 |
+
Clearly, the evidence points toward acquittal. The case obviously lacks
|
| 587 |
+
substantial proof of guilt.
|
| 588 |
+
"""
|
| 589 |
+
|
| 590 |
+
# Example historical cases
|
| 591 |
+
sample_historical = [
|
| 592 |
+
{"outcome": "conviction", "gender": "male", "region": "urban", "year": 2020},
|
| 593 |
+
{"outcome": "acquittal", "gender": "female", "region": "rural", "year": 2020},
|
| 594 |
+
{"outcome": "conviction", "gender": "male", "region": "urban", "year": 2021},
|
| 595 |
+
{"outcome": "conviction", "gender": "female", "region": "urban", "year": 2021},
|
| 596 |
+
]
|
| 597 |
+
|
| 598 |
+
# Run comprehensive analysis
|
| 599 |
+
print("Running comprehensive legal analysis...\n")
|
| 600 |
+
results = analyze_legal_case(
|
| 601 |
+
case_text=sample_case,
|
| 602 |
+
rag_summary=sample_rag_summary,
|
| 603 |
+
source_documents=[sample_case],
|
| 604 |
+
historical_cases=sample_historical,
|
| 605 |
+
case_metadata={"case_type": "criminal", "jurisdiction": "Maharashtra"}
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
# Print results
|
| 609 |
+
print(json.dumps(results, indent=2))
|
| 610 |
+
|
document_generator.py
ADDED
|
@@ -0,0 +1,465 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Legal Document Generator for LexAI
|
| 3 |
+
==================================
|
| 4 |
+
|
| 5 |
+
Quick MVP for hackathon demo
|
| 6 |
+
Generates bail applications, FIR drafts, and legal notices
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from typing import Dict, Any, Optional
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
import json
|
| 12 |
+
|
| 13 |
+
class LegalDocumentGenerator:
|
| 14 |
+
"""
|
| 15 |
+
Generate legal documents from templates with AI assistance
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def __init__(self):
|
| 19 |
+
self.templates = self._load_templates()
|
| 20 |
+
|
| 21 |
+
def _load_templates(self) -> Dict[str, str]:
|
| 22 |
+
"""Load document templates"""
|
| 23 |
+
return {
|
| 24 |
+
'bail_application': """
|
| 25 |
+
IN THE COURT OF {court_name}
|
| 26 |
+
|
| 27 |
+
CRIMINAL MISCELLANEOUS APPLICATION NO. _______
|
| 28 |
+
|
| 29 |
+
IN THE MATTER OF:
|
| 30 |
+
{applicant_name} ... Applicant
|
| 31 |
+
(Through Advocate: {advocate_name})
|
| 32 |
+
|
| 33 |
+
VERSUS
|
| 34 |
+
|
| 35 |
+
STATE OF {state} ... Respondent
|
| 36 |
+
|
| 37 |
+
APPLICATION FOR BAIL UNDER SECTION {section}
|
| 38 |
+
|
| 39 |
+
RESPECTFULLY SHOWETH:
|
| 40 |
+
|
| 41 |
+
1. That the applicant is an accused in FIR No. {fir_number} dated {fir_date} registered at Police Station {police_station} under Sections {charges} of the Indian Penal Code.
|
| 42 |
+
|
| 43 |
+
2. That the applicant was arrested on {arrest_date} and has been in judicial custody since then.
|
| 44 |
+
|
| 45 |
+
3. That the applicant is {age} years old, a resident of {address}, and is {occupation} by profession.
|
| 46 |
+
|
| 47 |
+
4. That the applicant is {family_status} and is the sole bread earner of the family.
|
| 48 |
+
|
| 49 |
+
5. That the applicant has deep roots in the community and has no criminal antecedents.
|
| 50 |
+
|
| 51 |
+
6. GROUNDS FOR BAIL:
|
| 52 |
+
|
| 53 |
+
{grounds}
|
| 54 |
+
|
| 55 |
+
7. That the applicant undertakes to:
|
| 56 |
+
a) Appear before the Court as and when required
|
| 57 |
+
b) Not tamper with evidence or influence witnesses
|
| 58 |
+
c) Not commit any offense while on bail
|
| 59 |
+
d) Furnish personal or surety bond as required
|
| 60 |
+
|
| 61 |
+
8. That the applicant is willing to comply with any conditions that this Hon'ble Court may deem fit to impose.
|
| 62 |
+
|
| 63 |
+
PRAYER:
|
| 64 |
+
|
| 65 |
+
In view of the above, it is most respectfully prayed that this Hon'ble Court may be pleased to:
|
| 66 |
+
|
| 67 |
+
a) Grant bail to the applicant
|
| 68 |
+
b) Pass any other order as deemed fit in the interest of justice
|
| 69 |
+
|
| 70 |
+
Place: {place}
|
| 71 |
+
Date: {date}
|
| 72 |
+
|
| 73 |
+
(Advocate for Applicant)
|
| 74 |
+
""",
|
| 75 |
+
|
| 76 |
+
'fir_complaint': """
|
| 77 |
+
FIRST INFORMATION REPORT (FIR)
|
| 78 |
+
|
| 79 |
+
Police Station: {police_station}
|
| 80 |
+
District: {district}
|
| 81 |
+
Date: {date}
|
| 82 |
+
Time: {time}
|
| 83 |
+
|
| 84 |
+
COMPLAINANT DETAILS:
|
| 85 |
+
Name: {complainant_name}
|
| 86 |
+
Father's/Husband's Name: {father_husband_name}
|
| 87 |
+
Address: {address}
|
| 88 |
+
Contact Number: {phone}
|
| 89 |
+
Email: {email}
|
| 90 |
+
|
| 91 |
+
ACCUSED DETAILS:
|
| 92 |
+
{accused_details}
|
| 93 |
+
|
| 94 |
+
DETAILS OF INCIDENT:
|
| 95 |
+
|
| 96 |
+
1. Date and Time of Incident: {incident_date} at approximately {incident_time}
|
| 97 |
+
|
| 98 |
+
2. Place of Incident: {incident_place}
|
| 99 |
+
|
| 100 |
+
3. Description of Incident:
|
| 101 |
+
|
| 102 |
+
{incident_description}
|
| 103 |
+
|
| 104 |
+
4. Details of Loss/Injury (if any):
|
| 105 |
+
|
| 106 |
+
{loss_details}
|
| 107 |
+
|
| 108 |
+
5. Witnesses (if any):
|
| 109 |
+
|
| 110 |
+
{witnesses}
|
| 111 |
+
|
| 112 |
+
6. Evidence Available:
|
| 113 |
+
|
| 114 |
+
{evidence}
|
| 115 |
+
|
| 116 |
+
7. Sections Applicable:
|
| 117 |
+
|
| 118 |
+
Based on the facts narrated above, it appears that offenses under the following sections have been committed:
|
| 119 |
+
{sections_applicable}
|
| 120 |
+
|
| 121 |
+
8. I hereby declare that the above information is true to the best of my knowledge and belief.
|
| 122 |
+
|
| 123 |
+
Signature of Complainant: _______________
|
| 124 |
+
Name: {complainant_name}
|
| 125 |
+
Date: {date}
|
| 126 |
+
|
| 127 |
+
[For Official Use Only]
|
| 128 |
+
FIR No.: _____________
|
| 129 |
+
Registered under Sections: _____________
|
| 130 |
+
Investigating Officer: _____________
|
| 131 |
+
""",
|
| 132 |
+
|
| 133 |
+
'legal_notice': """
|
| 134 |
+
LEGAL NOTICE
|
| 135 |
+
|
| 136 |
+
To,
|
| 137 |
+
{recipient_name}
|
| 138 |
+
{recipient_address}
|
| 139 |
+
|
| 140 |
+
Date: {date}
|
| 141 |
+
|
| 142 |
+
Dear Sir/Madam,
|
| 143 |
+
|
| 144 |
+
SUBJECT: LEGAL NOTICE UNDER {act_section}
|
| 145 |
+
|
| 146 |
+
Under instructions from and on behalf of my client, {client_name}, residing at {client_address}, I hereby serve you with this legal notice for the following reasons:
|
| 147 |
+
|
| 148 |
+
1. FACTS OF THE CASE:
|
| 149 |
+
|
| 150 |
+
{case_facts}
|
| 151 |
+
|
| 152 |
+
2. CAUSE OF ACTION:
|
| 153 |
+
|
| 154 |
+
{cause_of_action}
|
| 155 |
+
|
| 156 |
+
3. LEGAL GROUNDS:
|
| 157 |
+
|
| 158 |
+
The acts/omissions on your part constitute violations under:
|
| 159 |
+
{legal_grounds}
|
| 160 |
+
|
| 161 |
+
4. RELIEF SOUGHT:
|
| 162 |
+
|
| 163 |
+
My client demands that you:
|
| 164 |
+
|
| 165 |
+
{relief_demanded}
|
| 166 |
+
|
| 167 |
+
5. NOTICE PERIOD:
|
| 168 |
+
|
| 169 |
+
You are hereby called upon to comply with the above demands within 15 days from the date of receipt of this notice, failing which my client shall be constrained to initiate appropriate legal proceedings against you at your risk as to costs and consequences.
|
| 170 |
+
|
| 171 |
+
This notice is without prejudice to the rights and contentions of my client.
|
| 172 |
+
|
| 173 |
+
Yours faithfully,
|
| 174 |
+
|
| 175 |
+
{advocate_name}
|
| 176 |
+
Advocate for {client_name}
|
| 177 |
+
Address: {advocate_address}
|
| 178 |
+
Contact: {advocate_contact}
|
| 179 |
+
""",
|
| 180 |
+
|
| 181 |
+
'petition': """
|
| 182 |
+
IN THE {court_name}
|
| 183 |
+
|
| 184 |
+
{petition_type} PETITION NO. _______
|
| 185 |
+
|
| 186 |
+
IN THE MATTER OF:
|
| 187 |
+
|
| 188 |
+
{petitioner_name}
|
| 189 |
+
{petitioner_address}
|
| 190 |
+
... Petitioner
|
| 191 |
+
|
| 192 |
+
VERSUS
|
| 193 |
+
|
| 194 |
+
{respondent_name}
|
| 195 |
+
{respondent_address}
|
| 196 |
+
... Respondent
|
| 197 |
+
|
| 198 |
+
PETITION UNDER {under_section}
|
| 199 |
+
|
| 200 |
+
TO,
|
| 201 |
+
THE HON'BLE {judge_title}
|
| 202 |
+
|
| 203 |
+
THE HUMBLE PETITION OF THE PETITIONER ABOVE-NAMED
|
| 204 |
+
|
| 205 |
+
MOST RESPECTFULLY SHOWETH:
|
| 206 |
+
|
| 207 |
+
1. PARTIES:
|
| 208 |
+
|
| 209 |
+
{parties_description}
|
| 210 |
+
|
| 211 |
+
2. FACTS:
|
| 212 |
+
|
| 213 |
+
{facts}
|
| 214 |
+
|
| 215 |
+
3. CAUSE OF ACTION:
|
| 216 |
+
|
| 217 |
+
{cause_of_action}
|
| 218 |
+
|
| 219 |
+
4. GROUNDS:
|
| 220 |
+
|
| 221 |
+
{grounds}
|
| 222 |
+
|
| 223 |
+
5. RELIEF:
|
| 224 |
+
|
| 225 |
+
WHEREFORE, in the light of the facts and circumstances stated above, it is most respectfully prayed that this Hon'ble Court may be pleased to:
|
| 226 |
+
|
| 227 |
+
{relief_prayed}
|
| 228 |
+
|
| 229 |
+
And pass such other and further orders as this Hon'ble Court may deem fit and proper in the interest of justice.
|
| 230 |
+
|
| 231 |
+
Place: {place}
|
| 232 |
+
Date: {date}
|
| 233 |
+
|
| 234 |
+
PETITIONER/ADVOCATE
|
| 235 |
+
"""
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
def generate_bail_application(self, details: Dict[str, Any]) -> Dict[str, str]:
|
| 239 |
+
"""
|
| 240 |
+
Generate bail application
|
| 241 |
+
|
| 242 |
+
Args:
|
| 243 |
+
details: Dict containing applicant details, charges, grounds, etc.
|
| 244 |
+
"""
|
| 245 |
+
template = self.templates['bail_application']
|
| 246 |
+
|
| 247 |
+
# Set defaults
|
| 248 |
+
doc_details = {
|
| 249 |
+
'court_name': details.get('court_name', 'SESSIONS JUDGE'),
|
| 250 |
+
'applicant_name': details.get('applicant_name', '[APPLICANT NAME]'),
|
| 251 |
+
'advocate_name': details.get('advocate_name', '[ADVOCATE NAME]'),
|
| 252 |
+
'state': details.get('state', '[STATE]'),
|
| 253 |
+
'section': details.get('section', '439 Cr.P.C.'),
|
| 254 |
+
'fir_number': details.get('fir_number', '[FIR NO.]'),
|
| 255 |
+
'fir_date': details.get('fir_date', '[DATE]'),
|
| 256 |
+
'police_station': details.get('police_station', '[POLICE STATION]'),
|
| 257 |
+
'charges': details.get('charges', '[IPC SECTIONS]'),
|
| 258 |
+
'arrest_date': details.get('arrest_date', '[ARREST DATE]'),
|
| 259 |
+
'age': details.get('age', '[AGE]'),
|
| 260 |
+
'address': details.get('address', '[ADDRESS]'),
|
| 261 |
+
'occupation': details.get('occupation', 'engaged in lawful occupation'),
|
| 262 |
+
'family_status': details.get('family_status', 'having family responsibilities'),
|
| 263 |
+
'grounds': self._generate_bail_grounds(details),
|
| 264 |
+
'place': details.get('place', '[PLACE]'),
|
| 265 |
+
'date': details.get('date', datetime.now().strftime('%d.%m.%Y')),
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
document = template.format(**doc_details)
|
| 269 |
+
|
| 270 |
+
return {
|
| 271 |
+
'document_type': 'bail_application',
|
| 272 |
+
'content': document,
|
| 273 |
+
'generated_at': datetime.now().isoformat(),
|
| 274 |
+
'editable': True,
|
| 275 |
+
'format': 'text/plain'
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
def _generate_bail_grounds(self, details: Dict[str, Any]) -> str:
|
| 279 |
+
"""Auto-generate bail grounds based on details"""
|
| 280 |
+
grounds = []
|
| 281 |
+
|
| 282 |
+
if details.get('first_time_offender', True):
|
| 283 |
+
grounds.append("a) The applicant is a first-time offender with no criminal antecedents.")
|
| 284 |
+
|
| 285 |
+
if details.get('cooperating', True):
|
| 286 |
+
grounds.append("b) The applicant has been fully cooperating with the investigation.")
|
| 287 |
+
|
| 288 |
+
if details.get('weak_evidence', False):
|
| 289 |
+
grounds.append("c) The evidence against the applicant is weak and based on circumstantial factors.")
|
| 290 |
+
|
| 291 |
+
if details.get('no_flight_risk', True):
|
| 292 |
+
grounds.append("d) The applicant has deep roots in the community and there is no risk of absconding.")
|
| 293 |
+
|
| 294 |
+
if details.get('medical_grounds', False):
|
| 295 |
+
grounds.append("e) The applicant requires medical attention which cannot be adequately provided in custody.")
|
| 296 |
+
|
| 297 |
+
grounds.append("f) The applicant's continued detention is not necessary for the purpose of investigation.")
|
| 298 |
+
grounds.append("g) The applicant is willing to abide by any conditions imposed by this Hon'ble Court.")
|
| 299 |
+
|
| 300 |
+
return '\n '.join(grounds)
|
| 301 |
+
|
| 302 |
+
def generate_fir(self, details: Dict[str, Any]) -> Dict[str, str]:
|
| 303 |
+
"""Generate FIR/Complaint"""
|
| 304 |
+
template = self.templates['fir_complaint']
|
| 305 |
+
|
| 306 |
+
doc_details = {
|
| 307 |
+
'police_station': details.get('police_station', '[POLICE STATION]'),
|
| 308 |
+
'district': details.get('district', '[DISTRICT]'),
|
| 309 |
+
'date': details.get('date', datetime.now().strftime('%d.%m.%Y')),
|
| 310 |
+
'time': details.get('time', datetime.now().strftime('%H:%M')),
|
| 311 |
+
'complainant_name': details.get('complainant_name', '[NAME]'),
|
| 312 |
+
'father_husband_name': details.get('father_husband_name', '[FATHER/HUSBAND NAME]'),
|
| 313 |
+
'address': details.get('address', '[ADDRESS]'),
|
| 314 |
+
'phone': details.get('phone', '[PHONE]'),
|
| 315 |
+
'email': details.get('email', '[EMAIL]'),
|
| 316 |
+
'accused_details': details.get('accused_details', '[ACCUSED DETAILS]'),
|
| 317 |
+
'incident_date': details.get('incident_date', '[DATE]'),
|
| 318 |
+
'incident_time': details.get('incident_time', '[TIME]'),
|
| 319 |
+
'incident_place': details.get('incident_place', '[PLACE]'),
|
| 320 |
+
'incident_description': details.get('incident_description', '[DESCRIPTION OF INCIDENT]'),
|
| 321 |
+
'loss_details': details.get('loss_details', 'N/A'),
|
| 322 |
+
'witnesses': details.get('witnesses', 'None'),
|
| 323 |
+
'evidence': details.get('evidence', 'As per investigation'),
|
| 324 |
+
'sections_applicable': details.get('sections_applicable', '[IPC SECTIONS]'),
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
document = template.format(**doc_details)
|
| 328 |
+
|
| 329 |
+
return {
|
| 330 |
+
'document_type': 'fir_complaint',
|
| 331 |
+
'content': document,
|
| 332 |
+
'generated_at': datetime.now().isoformat(),
|
| 333 |
+
'editable': True,
|
| 334 |
+
'format': 'text/plain'
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
def generate_legal_notice(self, details: Dict[str, Any]) -> Dict[str, str]:
|
| 338 |
+
"""Generate legal notice"""
|
| 339 |
+
template = self.templates['legal_notice']
|
| 340 |
+
|
| 341 |
+
doc_details = {
|
| 342 |
+
'date': details.get('date', datetime.now().strftime('%d.%m.%Y')),
|
| 343 |
+
'recipient_name': details.get('recipient_name', '[RECIPIENT NAME]'),
|
| 344 |
+
'recipient_address': details.get('recipient_address', '[RECIPIENT ADDRESS]'),
|
| 345 |
+
'act_section': details.get('act_section', 'RELEVANT PROVISIONS'),
|
| 346 |
+
'client_name': details.get('client_name', '[CLIENT NAME]'),
|
| 347 |
+
'client_address': details.get('client_address', '[CLIENT ADDRESS]'),
|
| 348 |
+
'case_facts': details.get('case_facts', '[FACTS OF THE CASE]'),
|
| 349 |
+
'cause_of_action': details.get('cause_of_action', '[CAUSE OF ACTION]'),
|
| 350 |
+
'legal_grounds': details.get('legal_grounds', '[LEGAL VIOLATIONS]'),
|
| 351 |
+
'relief_demanded': details.get('relief_demanded', '[RELIEF SOUGHT]'),
|
| 352 |
+
'advocate_name': details.get('advocate_name', '[ADVOCATE NAME]'),
|
| 353 |
+
'advocate_address': details.get('advocate_address', '[ADVOCATE ADDRESS]'),
|
| 354 |
+
'advocate_contact': details.get('advocate_contact', '[CONTACT]'),
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
document = template.format(**doc_details)
|
| 358 |
+
|
| 359 |
+
return {
|
| 360 |
+
'document_type': 'legal_notice',
|
| 361 |
+
'content': document,
|
| 362 |
+
'generated_at': datetime.now().isoformat(),
|
| 363 |
+
'editable': True,
|
| 364 |
+
'format': 'text/plain'
|
| 365 |
+
}
|
| 366 |
+
|
| 367 |
+
def generate_petition(self, details: Dict[str, Any]) -> Dict[str, str]:
|
| 368 |
+
"""Generate petition"""
|
| 369 |
+
template = self.templates['petition']
|
| 370 |
+
|
| 371 |
+
doc_details = {
|
| 372 |
+
'court_name': details.get('court_name', 'HIGH COURT OF [STATE]'),
|
| 373 |
+
'petition_type': details.get('petition_type', 'WRIT'),
|
| 374 |
+
'petitioner_name': details.get('petitioner_name', '[PETITIONER]'),
|
| 375 |
+
'petitioner_address': details.get('petitioner_address', '[ADDRESS]'),
|
| 376 |
+
'respondent_name': details.get('respondent_name', '[RESPONDENT]'),
|
| 377 |
+
'respondent_address': details.get('respondent_address', '[ADDRESS]'),
|
| 378 |
+
'under_section': details.get('under_section', 'ARTICLE 226 OF THE CONSTITUTION'),
|
| 379 |
+
'judge_title': details.get('judge_title', 'CHIEF JUSTICE AND HIS COMPANION JUDGES'),
|
| 380 |
+
'parties_description': details.get('parties_description', '[PARTY DETAILS]'),
|
| 381 |
+
'facts': details.get('facts', '[FACTS]'),
|
| 382 |
+
'cause_of_action': details.get('cause_of_action', '[CAUSE OF ACTION]'),
|
| 383 |
+
'grounds': details.get('grounds', '[GROUNDS]'),
|
| 384 |
+
'relief_prayed': details.get('relief_prayed', '[RELIEF PRAYED]'),
|
| 385 |
+
'place': details.get('place', '[PLACE]'),
|
| 386 |
+
'date': details.get('date', datetime.now().strftime('%d.%m.%Y')),
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
document = template.format(**doc_details)
|
| 390 |
+
|
| 391 |
+
return {
|
| 392 |
+
'document_type': 'petition',
|
| 393 |
+
'content': document,
|
| 394 |
+
'generated_at': datetime.now().isoformat(),
|
| 395 |
+
'editable': True,
|
| 396 |
+
'format': 'text/plain'
|
| 397 |
+
}
|
| 398 |
+
|
| 399 |
+
def get_template_list(self) -> list:
|
| 400 |
+
"""Get list of available templates"""
|
| 401 |
+
return [
|
| 402 |
+
{
|
| 403 |
+
'id': 'bail_application',
|
| 404 |
+
'name': 'Bail Application',
|
| 405 |
+
'description': 'Application for bail under CrPC Section 439',
|
| 406 |
+
'category': 'Criminal'
|
| 407 |
+
},
|
| 408 |
+
{
|
| 409 |
+
'id': 'fir_complaint',
|
| 410 |
+
'name': 'FIR / Complaint',
|
| 411 |
+
'description': 'First Information Report for filing with police',
|
| 412 |
+
'category': 'Criminal'
|
| 413 |
+
},
|
| 414 |
+
{
|
| 415 |
+
'id': 'legal_notice',
|
| 416 |
+
'name': 'Legal Notice',
|
| 417 |
+
'description': 'Legal notice under relevant provisions',
|
| 418 |
+
'category': 'General'
|
| 419 |
+
},
|
| 420 |
+
{
|
| 421 |
+
'id': 'petition',
|
| 422 |
+
'name': 'Petition',
|
| 423 |
+
'description': 'Writ petition or other court petition',
|
| 424 |
+
'category': 'Civil/Writ'
|
| 425 |
+
}
|
| 426 |
+
]
|
| 427 |
+
|
| 428 |
+
|
| 429 |
+
# Global instance
|
| 430 |
+
_doc_generator = None
|
| 431 |
+
|
| 432 |
+
def get_document_generator() -> LegalDocumentGenerator:
|
| 433 |
+
"""Get or create document generator instance"""
|
| 434 |
+
global _doc_generator
|
| 435 |
+
if _doc_generator is None:
|
| 436 |
+
_doc_generator = LegalDocumentGenerator()
|
| 437 |
+
return _doc_generator
|
| 438 |
+
|
| 439 |
+
|
| 440 |
+
# Test
|
| 441 |
+
if __name__ == "__main__":
|
| 442 |
+
generator = LegalDocumentGenerator()
|
| 443 |
+
|
| 444 |
+
# Test bail application
|
| 445 |
+
print("Generating bail application...")
|
| 446 |
+
bail_details = {
|
| 447 |
+
'applicant_name': 'Rajesh Kumar',
|
| 448 |
+
'advocate_name': 'Adv. Priya Sharma',
|
| 449 |
+
'state': 'Delhi',
|
| 450 |
+
'fir_number': '123/2024',
|
| 451 |
+
'fir_date': '15.01.2024',
|
| 452 |
+
'police_station': 'Connaught Place',
|
| 453 |
+
'charges': '420, 468 IPC',
|
| 454 |
+
'arrest_date': '16.01.2024',
|
| 455 |
+
'age': '35',
|
| 456 |
+
'address': '123, New Delhi',
|
| 457 |
+
'first_time_offender': True,
|
| 458 |
+
'no_flight_risk': True,
|
| 459 |
+
}
|
| 460 |
+
|
| 461 |
+
result = generator.generate_bail_application(bail_details)
|
| 462 |
+
print(result['content'][:500] + "...")
|
| 463 |
+
print(f"\nDocument type: {result['document_type']}")
|
| 464 |
+
print(f"Generated at: {result['generated_at']}")
|
| 465 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# InLegalBERT Bias Detection & Outcome Prediction Dependencies
|
| 2 |
+
# ============================================================
|
| 3 |
+
|
| 4 |
+
# Core ML/AI Libraries
|
| 5 |
+
torch>=2.0.0
|
| 6 |
+
transformers>=4.35.0
|
| 7 |
+
sentence-transformers>=2.2.0
|
| 8 |
+
|
| 9 |
+
# API Framework
|
| 10 |
+
fastapi>=0.104.0
|
| 11 |
+
uvicorn[standard]>=0.24.0
|
| 12 |
+
pydantic>=2.0.0
|
| 13 |
+
|
| 14 |
+
# Data Processing
|
| 15 |
+
numpy>=1.24.0
|
| 16 |
+
pandas>=2.0.0
|
| 17 |
+
scikit-learn>=1.3.0
|
| 18 |
+
|
| 19 |
+
# Utilities
|
| 20 |
+
python-multipart>=0.0.6
|
| 21 |
+
python-dotenv>=1.0.0
|
| 22 |
+
requests>=2.31.0
|
| 23 |
+
|
| 24 |
+
# Hackathon Features
|
| 25 |
+
googletrans==4.0.0rc1
|
| 26 |
+
langdetect>=1.0.9
|
| 27 |
+
|
simulation_engine.py
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
What-If Simulation Engine for LexAI
|
| 3 |
+
===================================
|
| 4 |
+
|
| 5 |
+
Hackathon MVP - Interactive case outcome simulation
|
| 6 |
+
Shows how changes in case facts affect predictions
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from typing import Dict, Any, List, Optional
|
| 10 |
+
import re
|
| 11 |
+
from bias_prediction_engine import get_model
|
| 12 |
+
|
| 13 |
+
class SimulationEngine:
|
| 14 |
+
"""
|
| 15 |
+
Simulate legal case outcomes with modified facts
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def __init__(self):
|
| 19 |
+
self.ml_model = get_model()
|
| 20 |
+
|
| 21 |
+
# Modifiable factors and their impacts
|
| 22 |
+
self.factor_impacts = {
|
| 23 |
+
'prior_conviction': {
|
| 24 |
+
'weight': 0.25,
|
| 25 |
+
'direction': 'negative',
|
| 26 |
+
'description': 'Previous criminal record'
|
| 27 |
+
},
|
| 28 |
+
'witness_credibility': {
|
| 29 |
+
'weight': 0.20,
|
| 30 |
+
'direction': 'positive',
|
| 31 |
+
'description': 'Reliability of witnesses'
|
| 32 |
+
},
|
| 33 |
+
'evidence_quality': {
|
| 34 |
+
'weight': 0.30,
|
| 35 |
+
'direction': 'positive',
|
| 36 |
+
'description': 'Strength of evidence'
|
| 37 |
+
},
|
| 38 |
+
'mitigating_factors': {
|
| 39 |
+
'weight': 0.15,
|
| 40 |
+
'direction': 'positive',
|
| 41 |
+
'description': 'Circumstances favoring accused'
|
| 42 |
+
},
|
| 43 |
+
'flight_risk': {
|
| 44 |
+
'weight': 0.10,
|
| 45 |
+
'direction': 'negative',
|
| 46 |
+
'description': 'Risk of absconding'
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
def simulate_outcome(self,
|
| 51 |
+
base_case: Dict[str, Any],
|
| 52 |
+
modifications: Dict[str, Any]) -> Dict[str, Any]:
|
| 53 |
+
"""
|
| 54 |
+
Simulate how case outcome changes with modifications
|
| 55 |
+
|
| 56 |
+
Args:
|
| 57 |
+
base_case: Original case facts
|
| 58 |
+
modifications: Changes to apply
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
Comparison of outcomes
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
# Get base prediction
|
| 65 |
+
base_text = base_case.get('facts', '')
|
| 66 |
+
base_prediction = self.ml_model.predict_outcome(
|
| 67 |
+
base_text,
|
| 68 |
+
base_case.get('metadata', {})
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Apply modifications
|
| 72 |
+
modified_text = self._apply_modifications(base_text, modifications)
|
| 73 |
+
modified_prediction = self.ml_model.predict_outcome(
|
| 74 |
+
modified_text,
|
| 75 |
+
base_case.get('metadata', {})
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Calculate impact
|
| 79 |
+
impact_analysis = self._analyze_impact(
|
| 80 |
+
base_prediction,
|
| 81 |
+
modified_prediction,
|
| 82 |
+
modifications
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
return {
|
| 86 |
+
'base_case': {
|
| 87 |
+
'facts': base_text,
|
| 88 |
+
'prediction': base_prediction
|
| 89 |
+
},
|
| 90 |
+
'modified_case': {
|
| 91 |
+
'facts': modified_text,
|
| 92 |
+
'prediction': modified_prediction,
|
| 93 |
+
'changes_applied': list(modifications.keys())
|
| 94 |
+
},
|
| 95 |
+
'impact_analysis': impact_analysis,
|
| 96 |
+
'visualization_data': self._generate_viz_data(
|
| 97 |
+
base_prediction,
|
| 98 |
+
modified_prediction
|
| 99 |
+
)
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
def _apply_modifications(self, base_text: str, modifications: Dict[str, Any]) -> str:
|
| 103 |
+
"""Apply modifications to case facts"""
|
| 104 |
+
modified = base_text
|
| 105 |
+
|
| 106 |
+
# Remove prior conviction if specified
|
| 107 |
+
if modifications.get('remove_prior_conviction'):
|
| 108 |
+
modified = re.sub(
|
| 109 |
+
r'(prior conviction|criminal record|previous offense).*?\.',
|
| 110 |
+
'has no prior criminal record.',
|
| 111 |
+
modified,
|
| 112 |
+
flags=re.IGNORECASE
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
# Add strong alibi
|
| 116 |
+
if modifications.get('add_strong_alibi'):
|
| 117 |
+
modified += " The accused has a strong alibi with multiple credible witnesses confirming their presence elsewhere during the incident."
|
| 118 |
+
|
| 119 |
+
# Improve witness credibility
|
| 120 |
+
if modifications.get('improve_witness_credibility'):
|
| 121 |
+
modified = re.sub(
|
| 122 |
+
r'(witness.*?)(contradictory|unreliable|questionable)',
|
| 123 |
+
r'\1credible and consistent',
|
| 124 |
+
modified,
|
| 125 |
+
flags=re.IGNORECASE
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Add mitigating factors
|
| 129 |
+
if modifications.get('add_mitigating_factors'):
|
| 130 |
+
mitigating = modifications['add_mitigating_factors']
|
| 131 |
+
modified += f" {mitigating}"
|
| 132 |
+
|
| 133 |
+
# Reduce flight risk
|
| 134 |
+
if modifications.get('reduce_flight_risk'):
|
| 135 |
+
modified += " The accused has deep roots in the community, stable employment, and family responsibilities, eliminating any flight risk."
|
| 136 |
+
|
| 137 |
+
# Enhance evidence quality
|
| 138 |
+
if modifications.get('enhance_evidence'):
|
| 139 |
+
modified = re.sub(
|
| 140 |
+
r'(evidence.*?)(weak|insufficient|circumstantial)',
|
| 141 |
+
r'\1strong and conclusive',
|
| 142 |
+
modified,
|
| 143 |
+
flags=re.IGNORECASE
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
return modified
|
| 147 |
+
|
| 148 |
+
def _analyze_impact(self,
|
| 149 |
+
base_pred: Dict,
|
| 150 |
+
modified_pred: Dict,
|
| 151 |
+
modifications: Dict) -> Dict[str, Any]:
|
| 152 |
+
"""Analyze impact of modifications"""
|
| 153 |
+
|
| 154 |
+
confidence_change = modified_pred['confidenceScore'] - base_pred['confidenceScore']
|
| 155 |
+
outcome_changed = base_pred['predictedOutcome'] != modified_pred['predictedOutcome']
|
| 156 |
+
|
| 157 |
+
# Calculate factor contributions
|
| 158 |
+
factor_impacts = []
|
| 159 |
+
for mod_key, mod_value in modifications.items():
|
| 160 |
+
if mod_value: # If modification was applied
|
| 161 |
+
factor_name = mod_key.replace('_', ' ').title()
|
| 162 |
+
estimated_impact = self._estimate_factor_impact(mod_key)
|
| 163 |
+
factor_impacts.append({
|
| 164 |
+
'factor': factor_name,
|
| 165 |
+
'estimated_impact': estimated_impact,
|
| 166 |
+
'direction': 'positive' if estimated_impact > 0 else 'negative'
|
| 167 |
+
})
|
| 168 |
+
|
| 169 |
+
return {
|
| 170 |
+
'outcome_changed': outcome_changed,
|
| 171 |
+
'confidence_change': round(confidence_change, 3),
|
| 172 |
+
'confidence_change_percent': round(confidence_change * 100, 1),
|
| 173 |
+
'factor_contributions': factor_impacts,
|
| 174 |
+
'key_factors': self._identify_key_factors(modifications),
|
| 175 |
+
'recommendation': self._generate_recommendation(
|
| 176 |
+
base_pred,
|
| 177 |
+
modified_pred,
|
| 178 |
+
outcome_changed
|
| 179 |
+
)
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
def _estimate_factor_impact(self, factor_key: str) -> float:
|
| 183 |
+
"""Estimate impact of a specific factor"""
|
| 184 |
+
impact_map = {
|
| 185 |
+
'remove_prior_conviction': 0.25,
|
| 186 |
+
'add_strong_alibi': 0.30,
|
| 187 |
+
'improve_witness_credibility': 0.20,
|
| 188 |
+
'add_mitigating_factors': 0.15,
|
| 189 |
+
'reduce_flight_risk': 0.10,
|
| 190 |
+
'enhance_evidence': 0.35,
|
| 191 |
+
}
|
| 192 |
+
return impact_map.get(factor_key, 0.10)
|
| 193 |
+
|
| 194 |
+
def _identify_key_factors(self, modifications: Dict) -> List[str]:
|
| 195 |
+
"""Identify most impactful factors"""
|
| 196 |
+
applied_mods = [k for k, v in modifications.items() if v]
|
| 197 |
+
impacts = [(mod, self._estimate_factor_impact(mod)) for mod in applied_mods]
|
| 198 |
+
impacts.sort(key=lambda x: x[1], reverse=True)
|
| 199 |
+
return [mod.replace('_', ' ').title() for mod, _ in impacts[:3]]
|
| 200 |
+
|
| 201 |
+
def _generate_recommendation(self,
|
| 202 |
+
base_pred: Dict,
|
| 203 |
+
modified_pred: Dict,
|
| 204 |
+
outcome_changed: bool) -> str:
|
| 205 |
+
"""Generate recommendation based on simulation"""
|
| 206 |
+
if outcome_changed:
|
| 207 |
+
return f"Modifying the specified factors could change the outcome from {base_pred['predictedOutcome']} to {modified_pred['predictedOutcome']}. These factors should be given priority in case preparation."
|
| 208 |
+
else:
|
| 209 |
+
conf_diff = abs(modified_pred['confidenceScore'] - base_pred['confidenceScore'])
|
| 210 |
+
if conf_diff > 0.15:
|
| 211 |
+
return f"While the outcome remains {base_pred['predictedOutcome']}, the confidence has changed by {round(conf_diff * 100, 1)}%. These factors significantly influence case strength."
|
| 212 |
+
else:
|
| 213 |
+
return "The modifications have minimal impact on the outcome. Other factors may be more critical to case success."
|
| 214 |
+
|
| 215 |
+
def _generate_viz_data(self, base_pred: Dict, modified_pred: Dict) -> Dict:
|
| 216 |
+
"""Generate data for visualization"""
|
| 217 |
+
return {
|
| 218 |
+
'confidence_comparison': {
|
| 219 |
+
'base': round(base_pred['confidenceScore'] * 100, 1),
|
| 220 |
+
'modified': round(modified_pred['confidenceScore'] * 100, 1),
|
| 221 |
+
'change': round((modified_pred['confidenceScore'] - base_pred['confidenceScore']) * 100, 1)
|
| 222 |
+
},
|
| 223 |
+
'outcome_labels': {
|
| 224 |
+
'base': base_pred['predictedOutcome'],
|
| 225 |
+
'modified': modified_pred['predictedOutcome']
|
| 226 |
+
},
|
| 227 |
+
'chart_type': 'bar_comparison',
|
| 228 |
+
'color_scheme': {
|
| 229 |
+
'base': '#6366f1', # Indigo
|
| 230 |
+
'modified': '#10b981' # Green
|
| 231 |
+
}
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
def sensitivity_analysis(self, case_facts: str) -> Dict[str, Any]:
|
| 235 |
+
"""
|
| 236 |
+
Analyze sensitivity to different factors
|
| 237 |
+
|
| 238 |
+
Tests each factor independently to see impact
|
| 239 |
+
"""
|
| 240 |
+
base_prediction = self.ml_model.predict_outcome(case_facts, {})
|
| 241 |
+
|
| 242 |
+
sensitivity_results = []
|
| 243 |
+
|
| 244 |
+
# Test each modification independently
|
| 245 |
+
test_modifications = [
|
| 246 |
+
{'remove_prior_conviction': True},
|
| 247 |
+
{'add_strong_alibi': True},
|
| 248 |
+
{'improve_witness_credibility': True},
|
| 249 |
+
{'add_mitigating_factors': 'First-time offender with family responsibilities'},
|
| 250 |
+
{'reduce_flight_risk': True},
|
| 251 |
+
]
|
| 252 |
+
|
| 253 |
+
for mod in test_modifications:
|
| 254 |
+
result = self.simulate_outcome(
|
| 255 |
+
{'facts': case_facts},
|
| 256 |
+
mod
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
mod_name = list(mod.keys())[0].replace('_', ' ').title()
|
| 260 |
+
sensitivity_results.append({
|
| 261 |
+
'factor': mod_name,
|
| 262 |
+
'confidence_impact': result['impact_analysis']['confidence_change'],
|
| 263 |
+
'outcome_change': result['impact_analysis']['outcome_changed'],
|
| 264 |
+
'new_outcome': result['modified_case']['prediction']['predictedOutcome']
|
| 265 |
+
})
|
| 266 |
+
|
| 267 |
+
# Sort by impact
|
| 268 |
+
sensitivity_results.sort(
|
| 269 |
+
key=lambda x: abs(x['confidence_impact']),
|
| 270 |
+
reverse=True
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
return {
|
| 274 |
+
'base_outcome': base_prediction['predictedOutcome'],
|
| 275 |
+
'base_confidence': base_prediction['confidenceScore'],
|
| 276 |
+
'sensitivity_analysis': sensitivity_results,
|
| 277 |
+
'most_influential_factor': sensitivity_results[0]['factor'] if sensitivity_results else None,
|
| 278 |
+
'visualization_ready': True
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
|
| 282 |
+
# Global instance
|
| 283 |
+
_simulation_engine = None
|
| 284 |
+
|
| 285 |
+
def get_simulation_engine() -> SimulationEngine:
|
| 286 |
+
"""Get or create simulation engine instance"""
|
| 287 |
+
global _simulation_engine
|
| 288 |
+
if _simulation_engine is None:
|
| 289 |
+
_simulation_engine = SimulationEngine()
|
| 290 |
+
return _simulation_engine
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
# Test
|
| 294 |
+
if __name__ == "__main__":
|
| 295 |
+
engine = SimulationEngine()
|
| 296 |
+
|
| 297 |
+
# Test case
|
| 298 |
+
base_case = {
|
| 299 |
+
'facts': """
|
| 300 |
+
The accused has prior conviction for theft. Witnesses gave contradictory statements.
|
| 301 |
+
Evidence is largely circumstantial. The accused attempted to flee when arrested.
|
| 302 |
+
""",
|
| 303 |
+
'metadata': {'case_type': 'criminal'}
|
| 304 |
+
}
|
| 305 |
+
|
| 306 |
+
# Test modifications
|
| 307 |
+
modifications = {
|
| 308 |
+
'remove_prior_conviction': True,
|
| 309 |
+
'add_strong_alibi': True,
|
| 310 |
+
'improve_witness_credibility': True,
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
print("Running simulation...")
|
| 314 |
+
result = engine.simulate_outcome(base_case, modifications)
|
| 315 |
+
|
| 316 |
+
print(f"\nBase Outcome: {result['base_case']['prediction']['predictedOutcome']}")
|
| 317 |
+
print(f"Base Confidence: {result['base_case']['prediction']['confidenceScore']}")
|
| 318 |
+
|
| 319 |
+
print(f"\nModified Outcome: {result['modified_case']['prediction']['predictedOutcome']}")
|
| 320 |
+
print(f"Modified Confidence: {result['modified_case']['prediction']['confidenceScore']}")
|
| 321 |
+
|
| 322 |
+
print(f"\nOutcome Changed: {result['impact_analysis']['outcome_changed']}")
|
| 323 |
+
print(f"Confidence Change: {result['impact_analysis']['confidence_change_percent']}%")
|
| 324 |
+
print(f"Key Factors: {', '.join(result['impact_analysis']['key_factors'])}")
|
| 325 |
+
print(f"\nRecommendation: {result['impact_analysis']['recommendation']}")
|
| 326 |
+
|
translation_service.py
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Multilingual Translation Service for LexAI
|
| 3 |
+
==========================================
|
| 4 |
+
|
| 5 |
+
Quick MVP implementation for hackathon demo
|
| 6 |
+
Supports translation between English and Indian regional languages
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
from typing import Dict, Any, Optional
|
| 10 |
+
from googletrans import Translator
|
| 11 |
+
import re
|
| 12 |
+
|
| 13 |
+
class MultilingualService:
|
| 14 |
+
"""
|
| 15 |
+
Translate legal content between English and regional languages
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# Language codes
|
| 19 |
+
LANGUAGES = {
|
| 20 |
+
'en': 'English',
|
| 21 |
+
'hi': 'Hindi',
|
| 22 |
+
'ta': 'Tamil',
|
| 23 |
+
'te': 'Telugu',
|
| 24 |
+
'bn': 'Bengali',
|
| 25 |
+
'mr': 'Marathi',
|
| 26 |
+
'gu': 'Gujarati',
|
| 27 |
+
'kn': 'Kannada',
|
| 28 |
+
'ml': 'Malayalam',
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def __init__(self):
|
| 32 |
+
self.translator = Translator()
|
| 33 |
+
|
| 34 |
+
def detect_language(self, text: str) -> str:
|
| 35 |
+
"""Auto-detect language of input text"""
|
| 36 |
+
try:
|
| 37 |
+
detected = self.translator.detect(text)
|
| 38 |
+
return detected.lang
|
| 39 |
+
except:
|
| 40 |
+
return 'en'
|
| 41 |
+
|
| 42 |
+
def translate_query(self, text: str, source_lang: str = 'auto', target_lang: str = 'en') -> Dict[str, Any]:
|
| 43 |
+
"""
|
| 44 |
+
Translate user query to English for processing
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
text: User query in regional language
|
| 48 |
+
source_lang: Source language code (auto-detect if 'auto')
|
| 49 |
+
target_lang: Target language (default: English)
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
Dict with translated text and metadata
|
| 53 |
+
"""
|
| 54 |
+
try:
|
| 55 |
+
# Auto-detect if needed
|
| 56 |
+
if source_lang == 'auto':
|
| 57 |
+
source_lang = self.detect_language(text)
|
| 58 |
+
|
| 59 |
+
# Translate
|
| 60 |
+
result = self.translator.translate(text, src=source_lang, dest=target_lang)
|
| 61 |
+
|
| 62 |
+
return {
|
| 63 |
+
"original_text": text,
|
| 64 |
+
"translated_text": result.text,
|
| 65 |
+
"source_language": source_lang,
|
| 66 |
+
"source_language_name": self.LANGUAGES.get(source_lang, "Unknown"),
|
| 67 |
+
"target_language": target_lang,
|
| 68 |
+
"confidence": 0.95 # Mock confidence for demo
|
| 69 |
+
}
|
| 70 |
+
except Exception as e:
|
| 71 |
+
return {
|
| 72 |
+
"error": str(e),
|
| 73 |
+
"original_text": text,
|
| 74 |
+
"translated_text": text, # Fallback to original
|
| 75 |
+
"source_language": source_lang,
|
| 76 |
+
"target_language": target_lang
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
def translate_response(self, text: str, target_lang: str = 'hi') -> Dict[str, Any]:
|
| 80 |
+
"""
|
| 81 |
+
Translate AI response to user's preferred language
|
| 82 |
+
|
| 83 |
+
Args:
|
| 84 |
+
text: AI response in English
|
| 85 |
+
target_lang: User's preferred language
|
| 86 |
+
|
| 87 |
+
Returns:
|
| 88 |
+
Dict with translated response
|
| 89 |
+
"""
|
| 90 |
+
try:
|
| 91 |
+
result = self.translator.translate(text, src='en', dest=target_lang)
|
| 92 |
+
|
| 93 |
+
return {
|
| 94 |
+
"original_text": text,
|
| 95 |
+
"translated_text": result.text,
|
| 96 |
+
"target_language": target_lang,
|
| 97 |
+
"target_language_name": self.LANGUAGES.get(target_lang, "Unknown"),
|
| 98 |
+
}
|
| 99 |
+
except Exception as e:
|
| 100 |
+
return {
|
| 101 |
+
"error": str(e),
|
| 102 |
+
"original_text": text,
|
| 103 |
+
"translated_text": text,
|
| 104 |
+
"target_language": target_lang
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
def translate_legal_document(self, text: str, target_lang: str) -> str:
|
| 108 |
+
"""Translate legal document preserving structure"""
|
| 109 |
+
try:
|
| 110 |
+
result = self.translator.translate(text, src='en', dest=target_lang)
|
| 111 |
+
return result.text
|
| 112 |
+
except:
|
| 113 |
+
return text
|
| 114 |
+
|
| 115 |
+
def get_supported_languages(self) -> Dict[str, str]:
|
| 116 |
+
"""Return list of supported languages"""
|
| 117 |
+
return self.LANGUAGES
|
| 118 |
+
|
| 119 |
+
def simplify_legal_text(self, legal_text: str, reading_level: str = 'simple') -> Dict[str, Any]:
|
| 120 |
+
"""
|
| 121 |
+
Convert complex legal language to plain language
|
| 122 |
+
|
| 123 |
+
Args:
|
| 124 |
+
legal_text: Complex legal text
|
| 125 |
+
reading_level: 'simple' or 'intermediate'
|
| 126 |
+
|
| 127 |
+
Returns:
|
| 128 |
+
Simplified text with explanations
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
# Legal term mappings (MVP - can be expanded)
|
| 132 |
+
legal_simplifications = {
|
| 133 |
+
r'\binter alia\b': 'among other things',
|
| 134 |
+
r'\bres ipsa loquitur\b': 'the thing speaks for itself',
|
| 135 |
+
r'\bper se\b': 'by itself',
|
| 136 |
+
r'\bpro bono\b': 'for free',
|
| 137 |
+
r'\bhabeas corpus\b': 'produce the person (bring before court)',
|
| 138 |
+
r'\bbail\b': 'temporary release from custody',
|
| 139 |
+
r'\bFIR\b': 'First Information Report (initial police complaint)',
|
| 140 |
+
r'\bIPC\b': 'Indian Penal Code (criminal law)',
|
| 141 |
+
r'\bCrPC\b': 'Criminal Procedure Code (how criminal cases work)',
|
| 142 |
+
r'\bappellant\b': 'person appealing the decision',
|
| 143 |
+
r'\brespondent\b': 'person responding to appeal',
|
| 144 |
+
r'\bpetitioner\b': 'person filing the case',
|
| 145 |
+
r'\bdefendant\b': 'person accused/sued',
|
| 146 |
+
r'\bplaintiff\b': 'person filing complaint',
|
| 147 |
+
r'\bbeyond reasonable doubt\b': 'very certain, no significant doubts',
|
| 148 |
+
r'\bprecedent\b': 'previous similar case decision',
|
| 149 |
+
r'\bjurisdiction\b': 'legal authority/area of court',
|
| 150 |
+
r'\bconviction\b': 'found guilty',
|
| 151 |
+
r'\bacquittal\b': 'found not guilty',
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
simplified = legal_text
|
| 155 |
+
|
| 156 |
+
# Apply simplifications
|
| 157 |
+
for pattern, replacement in legal_simplifications.items():
|
| 158 |
+
simplified = re.sub(pattern, replacement, simplified, flags=re.IGNORECASE)
|
| 159 |
+
|
| 160 |
+
# Extract key points (simple sentence extraction)
|
| 161 |
+
sentences = simplified.split('.')
|
| 162 |
+
key_points = [s.strip() + '.' for s in sentences if len(s.strip()) > 20][:5]
|
| 163 |
+
|
| 164 |
+
return {
|
| 165 |
+
"original_text": legal_text,
|
| 166 |
+
"simplified_text": simplified,
|
| 167 |
+
"reading_level": reading_level,
|
| 168 |
+
"key_points": key_points,
|
| 169 |
+
"legal_terms_explained": list(legal_simplifications.values())[:10],
|
| 170 |
+
"summary": f"This text explains legal matters in simpler terms. {len(key_points)} key points identified."
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
# Global instance
|
| 175 |
+
_translation_service = None
|
| 176 |
+
|
| 177 |
+
def get_translation_service() -> MultilingualService:
|
| 178 |
+
"""Get or create translation service instance"""
|
| 179 |
+
global _translation_service
|
| 180 |
+
if _translation_service is None:
|
| 181 |
+
_translation_service = MultilingualService()
|
| 182 |
+
return _translation_service
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# Quick test
|
| 186 |
+
if __name__ == "__main__":
|
| 187 |
+
service = MultilingualService()
|
| 188 |
+
|
| 189 |
+
# Test translation
|
| 190 |
+
print("Testing translation...")
|
| 191 |
+
result = service.translate_query("मुझे जमानत कैसे मिलेगी?", source_lang='hi', target_lang='en')
|
| 192 |
+
print(f"Hindi → English: {result['translated_text']}")
|
| 193 |
+
|
| 194 |
+
# Test simplification
|
| 195 |
+
print("\nTesting simplification...")
|
| 196 |
+
legal_text = "The appellant filed a habeas corpus petition seeking bail under Section 302 IPC. The FIR was lodged inter alia alleging murder beyond reasonable doubt."
|
| 197 |
+
simplified = service.simplify_legal_text(legal_text)
|
| 198 |
+
print(f"Original: {legal_text}")
|
| 199 |
+
print(f"Simplified: {simplified['simplified_text']}")
|
| 200 |
+
print(f"Key points: {simplified['key_points']}")
|
| 201 |
+
|