🏛️ Legal Dashboard
Advanced Legal Document Management System with AI-Powered OCR
import os import tempfile import logging from pathlib import Path from typing import Optional, Tuple try: import gradio as gr GRADIO_AVAILABLE = True except ImportError: GRADIO_AVAILABLE = False logging.warning("Gradio not available") # Import our services try: from app.services.ocr_service import ocr_service from app.services.database_service import DatabaseService OCR_AVAILABLE = True except ImportError: OCR_AVAILABLE = False logging.warning("OCR service not available") logger = logging.getLogger(__name__) class LegalDashboardGradio: """ Gradio interface for Legal Dashboard """ def __init__(self): self.ocr_service = ocr_service if OCR_AVAILABLE else None self.db_service = None # Initialize database if available try: self.db_service = DatabaseService() except Exception as e: logger.warning(f"Database service not available: {e}") async def process_document(self, file) -> Tuple[str, str, str]: """ Process uploaded document and extract text """ if not file: return "❌ No file uploaded", "", "" if not self.ocr_service: return "❌ OCR service not available", "", "" try: # Get file path file_path = file.name file_extension = Path(file_path).suffix.lower() # Process based on file type if file_extension == '.pdf': result = await self.ocr_service.extract_text_from_pdf(file_path) elif file_extension in ['.jpg', '.jpeg', '.png', '.bmp', '.tiff']: result = await self.ocr_service.extract_text_from_image(file_path) else: return f"❌ Unsupported file type: {file_extension}", "", "" if result["success"]: # Process text with NLP if available processed = await self.ocr_service.process_text(result["text"]) # Create status message status = f"✅ Successfully processed using {result['method']}" # Create metadata info metadata = f""" **Processing Details:** - Method: {result['method']} - Character Count: {len(result['text'])} - Pages: {len(result.get('pages', []))} """ if processed.get('entities'): entities_info = "\n**Named Entities Found:**\n" for ent in processed['entities'][:10]: # Show first 10 entities entities_info += f"- {ent['text']} ({ent['label']})\n" metadata += entities_info return status, result["text"], metadata else: error_msg = result.get("metadata", {}).get("error", "Unknown error") return f"❌ Processing failed: {error_msg}", "", "" except Exception as e: logger.error(f"Document processing error: {e}") return f"❌ Error: {str(e)}", "", "" def search_documents(self, query: str) -> str: """ Search in processed documents """ if not query.strip(): return "Please enter a search query" if not self.db_service: return "Database service not available" try: # This would search in the database # For now, return a placeholder return f"Search results for '{query}' would appear here.\n\nDatabase integration coming soon..." except Exception as e: return f"Search error: {str(e)}" def get_system_status(self) -> str: """ Get system status information """ try: status = [] # OCR Service Status if self.ocr_service: ocr_status = self.ocr_service.get_service_status() status.append("🔍 **OCR Service:**") status.append(f" - Status: {'✅ Ready' if ocr_status['fallback_ready'] else '❌ Not Ready'}") status.append(f" - Transformers: {'✅ Available' if ocr_status['transformers_ready'] else '❌ Not Available'}") status.append(f" - spaCy: {'✅ Available' if ocr_status['spacy_ready'] else '❌ Not Available'}") status.append(f" - Models: {', '.join(ocr_status['models_loaded']) if ocr_status['models_loaded'] else 'None'}") else: status.append("🔍 **OCR Service:** ❌ Not Available") # Database Service Status if self.db_service: status.append("\n💾 **Database Service:** ✅ Available") else: status.append("\n💾 **Database Service:** ❌ Not Available") # System Info status.append(f"\n🖥️ **System Info:**") status.append(f" - Python: Available") status.append(f" - Gradio: {'✅ Available' if GRADIO_AVAILABLE else '❌ Not Available'}") return "\n".join(status) except Exception as e: return f"Error getting system status: {str(e)}" def create_gradio_interface(): """ Create and return the Gradio interface """ if not GRADIO_AVAILABLE: return None dashboard = LegalDashboardGradio() # Custom CSS css = """ .gradio-container { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; } .main-header { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; border-radius: 10px; margin-bottom: 20px; } .status-box { background: #f8f9fa; border-left: 4px solid #28a745; padding: 15px; border-radius: 5px; } """ with gr.Blocks(css=css, title="Legal Dashboard", theme=gr.themes.Soft()) as iface: # Header gr.HTML("""
Advanced Legal Document Management System with AI-Powered OCR