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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
---
sdk: docker
emoji: πŸ‘€
colorFrom: yellow
colorTo: red
pinned: true
---
# πŸ“Š Legal Dashboard - داشبورد Ψ­Ω‚ΩˆΩ‚ΫŒ

[![Python](https://img.shields.io/badge/Python-3.10+-blue.svg)](https://python.org)
[![FastAPI](https://img.shields.io/badge/FastAPI-Latest-green.svg)](https://fastapi.tiangolo.com)
[![Gradio](https://img.shields.io/badge/Gradio-Latest-orange.svg)](https://gradio.app)
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

A comprehensive legal document management and analysis system built with FastAPI and Gradio, optimized for multiple deployment environments including Hugging Face Spaces.

## 🌟 Features

- **πŸ“„ Document Management**: Upload, process, and manage legal documents (PDF, DOCX, DOC, TXT)
- **πŸ€– AI-Powered Analysis**: Extract key information using advanced NLP models
- **πŸ” Secure Authentication**: JWT-based authentication with role management
- **πŸ“Š Analytics Dashboard**: Real-time analytics and document insights
- **🌐 Web Scraping**: Extract content from legal websites
- **πŸ” Smart Search**: Advanced search capabilities across documents
- **πŸ“± Multi-Interface**: Web dashboard + Gradio interface for HF Spaces
- **🌍 Multi-Language**: Persian/Farsi and English support
- **☁️ Multi-Platform**: Docker, HF Spaces, Local deployment

## πŸš€ Quick Start

### Option 1: Hugging Face Spaces (Recommended for Demo)

1. **Fork this Space** or create a new one
2. **Upload all files** to your space
3. **Set environment variables** in Space settings:
   ```bash
   JWT_SECRET_KEY=your-super-secret-key-here
   DATABASE_DIR=/tmp/legal_dashboard/data
   LOG_LEVEL=INFO
   ```
4. **Launch the space** - it will automatically start

**Demo Credentials:**
- Username: `admin`
- Password: `admin123`

### Option 2: Docker Deployment

```bash
# Clone repository
git clone <your-repo-url>
cd legal-dashboard

# Build and run
docker-compose up --build

# Or with Docker only
docker build -t legal-dashboard .
docker run -p 8000:8000 legal-dashboard
```

### Option 3: Local Development

```bash
# Install dependencies
pip install -r requirements.txt

# Setup environment
cp .env.example .env
# Edit .env with your settings

# Run application
python run.py
# Or specific interfaces:
python app.py          # Gradio interface
uvicorn app.main:app   # FastAPI only
```

## πŸ“ Project Structure

```
legal-dashboard/
β”œβ”€β”€ πŸš€ Deployment & Config
β”‚   β”œβ”€β”€ run.py                  # Universal runner (All environments)
β”‚   β”œβ”€β”€ config.py               # Configuration management
β”‚   β”œβ”€β”€ startup_hf.py           # HF Spaces startup
β”‚   β”œβ”€β”€ app.py                  # Gradio interface
β”‚   β”œβ”€β”€ Dockerfile              # Docker configuration
β”‚   β”œβ”€β”€ docker-compose.yml      # Docker Compose
β”‚   β”œβ”€β”€ requirements.txt        # Dependencies
β”‚   └── .env                    # Environment variables
β”‚
β”œβ”€β”€ πŸ—οΈ Backend (FastAPI)
β”‚   β”œβ”€β”€ app/
β”‚   β”‚   β”œβ”€β”€ main.py             # FastAPI application
β”‚   β”‚   β”œβ”€β”€ api/                # API endpoints
β”‚   β”‚   β”‚   β”œβ”€β”€ auth.py         # Authentication
β”‚   β”‚   β”‚   β”œβ”€β”€ documents.py    # Document management
β”‚   β”‚   β”‚   β”œβ”€β”€ analytics.py    # Analytics
β”‚   β”‚   β”‚   β”œβ”€β”€ scraping.py     # Web scraping
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   β”œβ”€β”€ services/           # Business logic
β”‚   β”‚   β”‚   β”œβ”€β”€ ai_service.py   # AI/ML services
β”‚   β”‚   β”‚   β”œβ”€β”€ database_service.py
β”‚   β”‚   β”‚   β”œβ”€β”€ ocr_service.py  # OCR processing
β”‚   β”‚   β”‚   └── ...
β”‚   β”‚   └── models/             # Data models
β”‚
β”œβ”€β”€ 🎨 Frontend
β”‚   β”œβ”€β”€ index.html              # Main dashboard
β”‚   β”œβ”€β”€ documents.html          # Document management
β”‚   β”œβ”€β”€ analytics.html          # Analytics page
β”‚   β”œβ”€β”€ upload.html             # File upload
β”‚   β”œβ”€β”€ js/                     # JavaScript modules
β”‚   └── ...
β”‚
└── πŸ§ͺ Testing & Docs
    β”œβ”€β”€ tests/                  # Test suites
    β”œβ”€β”€ docs/                   # Documentation
    └── README.md               # This file
```

## βš™οΈ Configuration

### Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `JWT_SECRET_KEY` | `auto-generated` | JWT signing key |
| `DATABASE_DIR` | `/app/data` | Database directory |
| `LOG_LEVEL` | `INFO` | Logging level |
| `ENVIRONMENT` | `production` | Environment type |
| `HF_HOME` | `/app/cache` | ML models cache |
| `PORT` | `8000/7860` | Server port |
| `WORKERS` | `1/4` | Worker processes |

### Multi-Environment Support

The system automatically detects and optimizes for:

- **πŸ€— Hugging Face Spaces**: Gradio interface, optimized resources
- **🐳 Docker**: Full FastAPI with all features
- **πŸ’» Local**: Development mode with hot reload

## πŸ”§ Advanced Configuration

### Custom Model Configuration

```python
# config.py - AI Configuration
ai_config = {
    "model_name": "microsoft/trocr-small-stage1",  # HF Spaces
    "device": "cpu",  # Force CPU for compatibility
    "max_workers": 1,  # Optimize for environment
    "batch_size": 1,   # Memory optimization
}
```

### Database Optimization

```python
# Automatic fallback paths for different environments
database_paths = [
    "/app/data/legal_documents.db",      # Docker
    "/tmp/legal_dashboard/data/legal.db", # HF Spaces
    "./data/legal_documents.db",         # Local
    ":memory:"                           # Final fallback
]
```

## πŸ› Troubleshooting

### Common Issues & Solutions

1. **Permission Denied Error**
   ```bash
   PermissionError: [Errno 13] Permission denied: '/app/database'
   ```
   **Solution**: System uses automatic fallback directories
   ```bash
   # Check logs for actual directory used:
   grep "πŸ“.*directory" logs/legal_dashboard.log
   ```

2. **bcrypt Version Error**
   ```bash
   (trapped) error reading bcrypt version
   ```
   **Solution**: Fixed with bcrypt==4.0.1 in requirements.txt

3. **Redis Connection Failed**
   ```bash
   Redis connection failed: Error 111 connecting to localhost:6379
   ```
   **Solution**: System automatically falls back to in-memory storage

4. **Model Loading Issues**
   ```bash
   OutOfMemoryError or CUDA errors
   ```
   **Solution**: System forces CPU mode and optimizes model selection

5. **Port Already in Use**
   ```bash
   [Errno 48] Address already in use
   ```
   **Solution**: System automatically tries alternative ports

### Debug Mode

```bash
# Enable debug logging
export LOG_LEVEL=DEBUG
python run.py

# Or check specific components
python -c "from config import config; print(config.get_summary())"
```

### Health Checks

```bash
# Check system health
curl http://localhost:8000/api/health

# Expected response:
{
  "status": "healthy",
  "services": {
    "database": "healthy",
    "ocr": "healthy",
    "ai": "healthy"
  }
}
```

## πŸ”’ Security

### Authentication Flow

1. **Registration**: Create account with email/password
2. **Login**: JWT access token (30 min) + refresh token (7 days)
3. **Authorization**: Role-based access control (admin/user)
4. **Session Management**: Secure token storage and refresh

### Security Features

- πŸ” bcrypt password hashing
- 🎫 JWT token authentication
- πŸ›‘οΈ CORS protection
- πŸ“ Audit logging
- πŸ”’ Role-based permissions
- 🚫 Rate limiting (planned)

### Default Credentials

⚠️ **Change immediately in production:**
- Username: `admin`
- Password: `admin123`

## πŸ“Š API Documentation

### Main Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/api/auth/login` | POST | User authentication |
| `/api/auth/register` | POST | User registration |
| `/api/documents` | GET/POST | Document management |
| `/api/ocr/process` | POST | OCR processing |
| `/api/analytics/overview` | GET | Analytics data |
| `/api/scraping/scrape` | POST | Web scraping |
| `/api/health` | GET | System health |

### Interactive Documentation

- **Swagger UI**: `/api/docs`
- **ReDoc**: `/api/redoc`
- **OpenAPI JSON**: `/api/openapi.json`

## πŸš€ Deployment Guide

### Hugging Face Spaces

1. **Create Space**:
   ```bash
   # Go to https://huggingface.co/spaces
   # Create new Space with Gradio SDK
   ```

2. **Upload Files**:
   ```bash
   git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE
   cp -r legal-dashboard/* YOUR_SPACE/
   cd YOUR_SPACE
   git add .
   git commit -m "Initial deployment"
   git push
   ```

3. **Configure Space**:
   - Set `JWT_SECRET_KEY` in Space settings
   - Optional: Set custom domain

### Docker Production

```bash
# Production docker-compose
version: "3.8"
services:
  legal-dashboard:
    build: .
    ports:
      - "80:8000"
    environment:
      - JWT_SECRET_KEY=${JWT_SECRET_KEY}
      - ENVIRONMENT=production
    volumes:
      - ./data:/app/data
      - ./logs:/app/logs
```

### Kubernetes (Advanced)

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: legal-dashboard
spec:
  replicas: 3
  selector:
    matchLabels:
      app: legal-dashboard
  template:
    spec:
      containers:
      - name: legal-dashboard
        image: legal-dashboard:latest
        ports:
        - containerPort: 8000
        env:
        - name: JWT_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: legal-dashboard-secrets
              key: jwt-secret
```

## 🀝 Contributing

1. **Fork the repository**
2. **Create feature branch**: `git checkout -b feature/amazing-feature`
3. **Make changes** and test thoroughly
4. **Run tests**: `python -m pytest tests/`
5. **Commit changes**: `git commit -m 'Add amazing feature'`
6. **Push to branch**: `git push origin feature/amazing-feature`
7. **Create Pull Request**

### Development Setup

```bash
# Clone and setup
git clone <repo-url>
cd legal-dashboard

# Install development dependencies
pip install -r requirements.txt
pip install pytest black isort mypy

# Setup pre-commit hooks
pre-commit install

# Run tests
python -m pytest tests/ -v

# Code formatting
black .
isort .
```

## πŸ“„ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## πŸ™ Acknowledgments

- **FastAPI**: Modern, fast web framework
- **Gradio**: Easy-to-use ML app interface  
- **Hugging Face**: Model hosting and Spaces platform
- **Transformers**: State-of-the-art NLP models
- **Chart.js**: Beautiful charts and visualizations

## πŸ“ž Support

- **Issues**: [GitHub Issues](../../issues)
- **Discussions**: [GitHub Discussions](../../discussions)
- **Email**: Contact maintainers
- **Documentation**: [Full Docs](./docs/)

---

### 🌐 Live Demo

Try the live demo: [Your HF Space URL]

**Made with ❀️ for the legal community**