Spaces:
Paused
Paused
File size: 8,945 Bytes
922c3ba |
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 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 |
# Legal Dashboard OCR - Deployment Instructions
## π Quick Start
### 1. Local Development Setup
```bash
# Clone or navigate to the project
cd legal_dashboard_ocr
# Install dependencies
pip install -r requirements.txt
# Set environment variables
export HF_TOKEN="your_huggingface_token"
# Run the application
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
```
### 2. Access the Application
- **Web Dashboard**: http://localhost:8000
- **API Documentation**: http://localhost:8000/docs
- **Health Check**: http://localhost:8000/health
## π¦ Project Structure
```
legal_dashboard_ocr/
βββ README.md # Main documentation
βββ requirements.txt # Python dependencies
βββ test_structure.py # Structure verification
βββ DEPLOYMENT_INSTRUCTIONS.md # This file
βββ app/ # Backend application
β βββ __init__.py
β βββ main.py # FastAPI entry point
β βββ api/ # API routes
β β βββ __init__.py
β β βββ documents.py # Document CRUD
β β βββ ocr.py # OCR processing
β β βββ dashboard.py # Dashboard analytics
β βββ services/ # Business logic
β β βββ __init__.py
β β βββ ocr_service.py # OCR pipeline
β β βββ database_service.py # Database operations
β β βββ ai_service.py # AI scoring
β βββ models/ # Data models
β βββ __init__.py
β βββ document_models.py # Pydantic schemas
βββ frontend/ # Web interface
β βββ improved_legal_dashboard.html
β βββ test_integration.html
βββ tests/ # Test suite
β βββ test_api_endpoints.py
β βββ test_ocr_pipeline.py
βββ data/ # Sample documents
β βββ sample_persian.pdf
βββ huggingface_space/ # HF Space deployment
βββ app.py # Gradio interface
βββ Spacefile # Deployment config
βββ README.md # Space documentation
```
## π§ Configuration
### Environment Variables
Create a `.env` file in the project root:
```env
# Hugging Face Token (required for OCR models)
HF_TOKEN=your_huggingface_token_here
# Database configuration (optional)
DATABASE_URL=sqlite:///legal_documents.db
# Server configuration (optional)
HOST=0.0.0.0
PORT=8000
DEBUG=true
```
### Hugging Face Token
1. Go to https://huggingface.co/settings/tokens
2. Create a new token with read permissions
3. Add it to your environment variables
## π§ͺ Testing
### Run Structure Test
```bash
python test_structure.py
```
### Run API Tests
```bash
# Install test dependencies
pip install pytest pytest-asyncio
# Run tests
python -m pytest tests/
```
### Manual Testing
```bash
# Test OCR endpoint
curl -X POST "http://localhost:8000/api/ocr/process" \
-H "Content-Type: multipart/form-data" \
-F "file=@data/sample_persian.pdf"
# Test dashboard
curl "http://localhost:8000/api/dashboard/summary"
```
## π Deployment Options
### 1. Hugging Face Spaces
#### Automatic Deployment
1. Create a new Space on Hugging Face
2. Upload all files from `huggingface_space/` directory
3. Set the `HF_TOKEN` environment variable in Space settings
4. The Space will automatically build and deploy
#### Manual Deployment
```bash
# Navigate to HF Space directory
cd huggingface_space
# Install dependencies
pip install -r ../requirements.txt
# Run the Gradio app
python app.py
```
### 2. Docker Deployment
#### Create Dockerfile
```dockerfile
FROM python:3.10-slim
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y \
build-essential \
&& rm -rf /var/lib/apt/lists/*
# Copy requirements and install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy application code
COPY . .
# Expose port
EXPOSE 8000
# Run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
```
#### Build and Run
```bash
# Build Docker image
docker build -t legal-dashboard-ocr .
# Run container
docker run -p 8000:8000 \
-e HF_TOKEN=your_token \
legal-dashboard-ocr
```
### 3. Production Deployment
#### Using Gunicorn
```bash
# Install gunicorn
pip install gunicorn
# Run with multiple workers
gunicorn app.main:app \
--workers 4 \
--worker-class uvicorn.workers.UvicornWorker \
--bind 0.0.0.0:8000
```
#### Using Nginx (Reverse Proxy)
```nginx
server {
listen 80;
server_name your-domain.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
## π Troubleshooting
### Common Issues
#### 1. Import Errors
```bash
# Ensure you're in the correct directory
cd legal_dashboard_ocr
# Install dependencies
pip install -r requirements.txt
# Check Python path
python -c "import sys; print(sys.path)"
```
#### 2. OCR Model Loading Issues
```bash
# Check HF token
echo $HF_TOKEN
# Test model download
python -c "from transformers import pipeline; p = pipeline('image-to-text', 'microsoft/trocr-base-stage1')"
```
#### 3. Database Issues
```bash
# Check database file
ls -la legal_documents.db
# Reset database (if needed)
rm legal_documents.db
```
#### 4. Port Already in Use
```bash
# Find process using port 8000
lsof -i :8000
# Kill process
kill -9 <PID>
# Or use different port
uvicorn app.main:app --port 8001
```
### Performance Optimization
#### 1. Model Caching
```python
# In app/services/ocr_service.py
# Models are automatically cached by Hugging Face
# Cache location: ~/.cache/huggingface/
```
#### 2. Database Optimization
```sql
-- Add indexes for better performance
CREATE INDEX idx_documents_category ON documents(category);
CREATE INDEX idx_documents_status ON documents(status);
CREATE INDEX idx_documents_created_at ON documents(created_at);
```
#### 3. Memory Management
```python
# In app/main.py
# Configure memory limits
import gc
gc.collect() # Force garbage collection
```
## π Monitoring
### Health Check
```bash
curl http://localhost:8000/health
```
### API Documentation
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
### Logs
```bash
# View application logs
tail -f logs/app.log
# View error logs
grep ERROR logs/app.log
```
## π Security
### Production Checklist
- [ ] Set `DEBUG=false` in production
- [ ] Use HTTPS in production
- [ ] Implement rate limiting
- [ ] Add authentication/authorization
- [ ] Secure file upload validation
- [ ] Regular security updates
### Environment Security
```bash
# Secure environment variables
export HF_TOKEN="your_secure_token"
export DATABASE_URL="your_secure_db_url"
# Use .env file (don't commit to git)
echo "HF_TOKEN=your_token" > .env
echo ".env" >> .gitignore
```
## π Scaling
### Horizontal Scaling
```bash
# Run multiple instances
uvicorn app.main:app --host 0.0.0.0 --port 8000 &
uvicorn app.main:app --host 0.0.0.0 --port 8001 &
uvicorn app.main:app --host 0.0.0.0 --port 8002 &
```
### Load Balancing
```nginx
upstream legal_dashboard {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}
server {
listen 80;
location / {
proxy_pass http://legal_dashboard;
}
}
```
## π Support
### Getting Help
1. Check the logs for error messages
2. Verify environment variables are set
3. Test with the sample PDF in `data/`
4. Check the API documentation at `/docs`
### Common Commands
```bash
# Start development server
uvicorn app.main:app --reload
# Run tests
python -m pytest tests/
# Check structure
python test_structure.py
# View API docs
open http://localhost:8000/docs
```
## π― Next Steps
1. **Deploy to Hugging Face Spaces** for easy sharing
2. **Add authentication** for production use
3. **Implement user management** for multi-user support
4. **Add more OCR models** for different document types
5. **Create mobile app** for document scanning
6. **Add batch processing** for multiple documents
7. **Implement advanced analytics** and reporting
---
**Note**: This project is designed for Persian legal documents. Ensure your documents are clear and well-scanned for best OCR results. |