File size: 5,348 Bytes
c601417
 
 
 
 
 
 
 
 
 
 
9844436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
---
title: LegalLens API
emoji: πŸ›οΈ
colorFrom: blue
colorTo: indigo
sdk: docker
app_file: Dockerfile
pinned: false
---


# Legal RAG Analysis API

A FastAPI backend for legal case analysis using Retrieval-Augmented Generation (RAG) system with LegalBERT predictions and Gemini AI evaluation.

## Overview

This API provides comprehensive legal case analysis by combining:
- LegalBERT model for initial verdict predictions
- RAG system with FAISS indexes for retrieving relevant legal documents
- Gemini AI for final evaluation and detailed explanations

## Features

- **Case Analysis**: Analyze legal cases and predict verdicts
- **RAG Integration**: Retrieve relevant legal documents from multiple sources
- **AI Evaluation**: Get detailed legal explanations from Gemini AI
- **Health Monitoring**: Check system status across all components
- **Model Status**: Monitor loading status of ML models and indexes

## API Endpoints

### Core Endpoints

#### `POST /api/v1/analyze-case`
Analyze a legal case and provide verdict prediction with detailed explanation.

**Request Body:**
```json
{
  "caseText": "The accused was found in possession of stolen property...",
  "useQueryGeneration": true
}
```

**Response:**
```json
{
  "initialVerdict": "guilty",
  "initialConfidence": 0.85,
  "finalVerdict": "guilty", 
  "verdictChanged": false,
  "searchQuery": "stolen property, IPC section 411, criminal breach of trust",
  "geminiExplanation": "Based on the legal analysis...",
  "supportingSources": {...},
  "analysisLogs": {...}
}
```

#### `GET /api/v1/health`
Check the health status of all system components.

**Response:**
```json
{
  "status": "healthy",
  "services": {
    "legal_bert": true,
    "rag": true,
    "gemini": true
  },
  "error": null
}
```

#### `GET /api/v1/models/status`
Get detailed status of all models and indexes.

**Response:**
```json
{
  "legalBert": {
    "loaded": false,
    "device": "cpu"
  },
  "ragIndexes": {
    "loaded": false,
    "indexCount": 0
  },
  "gemini": {
    "configured": true
  }
}
```

## Setup Instructions

### Prerequisites

1. **Gemini API Key**: Required for AI analysis
   - Get from [Google AI Studio](https://aistudio.google.com/)
   - Add as `GEMINI_API_KEY` environment variable

2. **Model Files** (Optional for development):
   - LegalBERT model files in `./models/legalbert_model/`
   - FAISS indexes in `./faiss_indexes/`

### Installation

1. **Install Dependencies:**
   ```bash
   pip install fastapi uvicorn pydantic pydantic-settings google-genai
   ```

2. **For Full Functionality (ML Models):**
   ```bash
   pip install torch transformers sentence-transformers faiss-cpu numpy
   ```

3. **Run the Server:**
   ```bash
   python -m uvicorn main:app --host 0.0.0.0 --port 5000 --reload
   ```

## Project Structure

```
β”œβ”€β”€ main.py                 # FastAPI application entry point
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── routes.py       # API route definitions
β”‚   β”œβ”€β”€ core/
β”‚   β”‚   └── config.py       # Configuration settings
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   └── schemas.py      # Pydantic models
β”‚   └── services/
β”‚       β”œβ”€β”€ legal_bert.py   # LegalBERT service
β”‚       β”œβ”€β”€ rag_service.py  # RAG retrieval service
β”‚       └── gemini_service.py # Gemini AI service
β”œβ”€β”€ models/                 # LegalBERT model files (to be added)
└── faiss_indexes/          # FAISS indexes (to be added)
```

## Development Mode

The API works in development mode without ML dependencies:
- Uses placeholder predictions for LegalBERT
- Provides mock RAG retrieval
- Full Gemini AI integration for analysis

## Adding Model Files

To enable full functionality:

1. **LegalBERT Model:**
   - Place model files in `./models/legalbert_model/`
   - Install torch and transformers

2. **FAISS Indexes:**
   - Add indexes to `./faiss_indexes/`
   - Install faiss-cpu and sentence-transformers

## Configuration

Key settings in `app/core/config.py`:
- Model paths
- FAISS index locations  
- API configuration
- RAG parameters

## Environment Variables

- `GEMINI_API_KEY`: Required for Gemini AI integration
- `LEGAL_BERT_MODEL_PATH`: Path to LegalBERT model
- `FAISS_INDEXES_PATH`: Base path for FAISS indexes

## Usage Examples

### Basic Case Analysis
```python
import requests

response = requests.post('http://localhost:5000/api/v1/analyze-case', json={
    'caseText': 'The accused was caught stealing from a shop.',
    'useQueryGeneration': True
})

result = response.json()
print(f"Verdict: {result['finalVerdict']}")
print(f"Explanation: {result['geminiExplanation']}")
```

### Health Check
```python
import requests

health = requests.get('http://localhost:5000/api/v1/health')
print(health.json())
```

## API Documentation

Once running, visit:
- **Interactive API Docs**: http://localhost:5000/docs
- **OpenAPI Schema**: http://localhost:5000/openapi.json

## Legal Document Sources

The RAG system retrieves from:
- Indian Constitution articles
- IPC sections
- Case law precedents  
- Legal statutes
- Q&A legal content

## Notes

- The system is designed for Indian criminal law cases
- Placeholder implementations allow development without full ML setup
- All services include health monitoring for production deployment
- CORS is configured for frontend integration