Spaces:
Paused
Paused
| sdk: docker | |
| emoji: π | |
| colorFrom: yellow | |
| colorTo: red | |
| pinned: true | |
| # π Legal Dashboard - Ψ―Ψ§Ψ΄Ψ¨ΩΨ±Ψ― ΨΩΩΩΫ | |
| [](https://python.org) | |
| [](https://fastapi.tiangolo.com) | |
| [](https://gradio.app) | |
| [](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** |