| |
| """ |
| NZ Legislation Loophole Analysis Streamlit App Runner |
| |
| This script runs the modern Streamlit application for analyzing New Zealand legislation |
| to identify potential loopholes, ambiguities, and unintended consequences using AI. |
| |
| Features: |
| - Advanced UI with multi-page layout |
| - Context memory cache system for improved performance |
| - Real-time progress monitoring |
| - Interactive results visualization |
| - Batch processing capabilities |
| - Comprehensive configuration management |
| |
| Usage: |
| python run_streamlit_app.py |
| |
| Requirements: |
| - All dependencies from requirements.txt must be installed |
| - Run from the project root directory |
| """ |
|
|
| import os |
| import sys |
| import subprocess |
| from pathlib import Path |
|
|
| def check_requirements(): |
| """Check if all required packages are installed""" |
| required_packages = [ |
| 'streamlit', |
| 'pandas', |
| 'plotly', |
| 'llama-cpp-python', |
| 'psutil', |
| 'numpy' |
| ] |
|
|
| missing_packages = [] |
|
|
| for package in required_packages: |
| try: |
| __import__(package.replace('-', '_')) |
| except ImportError: |
| missing_packages.append(package) |
|
|
| if missing_packages: |
| print("β Missing required packages:") |
| for package in missing_packages: |
| print(f" - {package}") |
|
|
| print("\nπ¦ Installing missing packages...") |
| try: |
| subprocess.check_call([ |
| sys.executable, '-m', 'pip', 'install', '-r', 'requirements.txt' |
| ]) |
| print("β
All packages installed successfully!") |
| except subprocess.CalledProcessError: |
| print("β Failed to install packages. Please install manually:") |
| print(" pip install -r requirements.txt") |
| return False |
|
|
| return True |
|
|
| def check_app_structure(): |
| """Check if the app structure is correct""" |
| app_dir = Path('streamlit_app') |
| required_files = [ |
| 'app.py', |
| 'core/cache_manager.py', |
| 'core/text_processor.py', |
| 'core/llm_analyzer.py', |
| 'core/dataset_builder.py', |
| 'utils/config.py', |
| 'utils/performance.py', |
| 'utils/ui_helpers.py' |
| ] |
|
|
| missing_files = [] |
|
|
| for file_path in required_files: |
| full_path = app_dir / file_path |
| if not full_path.exists(): |
| missing_files.append(str(full_path)) |
|
|
| if missing_files: |
| print("β Missing app files:") |
| for file_path in missing_files: |
| print(f" - {file_path}") |
| return False |
|
|
| print("β
App structure is complete!") |
| return True |
|
|
| def create_directories(): |
| """Create necessary directories""" |
| directories = [ |
| 'streamlit_app/cache', |
| 'streamlit_app/config', |
| 'streamlit_app/datasets', |
| 'streamlit_app/logs' |
| ] |
|
|
| for dir_path in directories: |
| Path(dir_path).mkdir(parents=True, exist_ok=True) |
| print(f"π Created directory: {dir_path}") |
|
|
| def setup_environment(): |
| """Setup environment variables and configuration""" |
| |
| current_dir = os.path.dirname(os.path.abspath(__file__)) |
| if current_dir not in sys.path: |
| sys.path.insert(0, current_dir) |
|
|
| |
| os.environ.setdefault('STREAMLIT_SERVER_HEADLESS', 'true') |
| os.environ.setdefault('STREAMLIT_BROWSER_GATHER_USAGE_STATS', 'false') |
|
|
| print("π§ Environment setup complete!") |
|
|
| def run_app(): |
| """Run the Streamlit application""" |
| print("\nπ Starting NZ Legislation Loophole Analyzer...") |
| print("=" * 60) |
| print("π± Access the app at: http://localhost:8501") |
| print("π Press Ctrl+C to stop the application") |
| print("=" * 60) |
|
|
| try: |
| |
| os.chdir('streamlit_app') |
|
|
| |
| subprocess.run([ |
| sys.executable, '-m', 'streamlit', 'run', 'app.py', |
| '--server.port', '8501', |
| '--server.address', '0.0.0.0', |
| '--theme.base', 'light' |
| ]) |
|
|
| except KeyboardInterrupt: |
| print("\n\nπ Application stopped by user") |
| except Exception as e: |
| print(f"\nβ Error running application: {e}") |
| return False |
|
|
| return True |
|
|
| def main(): |
| """Main function""" |
| print("ποΈ NZ Legislation Loophole Analysis Streamlit App") |
| print("=" * 60) |
|
|
| |
| if not check_requirements(): |
| return 1 |
|
|
| |
| if not check_app_structure(): |
| return 1 |
|
|
| |
| create_directories() |
|
|
| |
| setup_environment() |
|
|
| |
| if not run_app(): |
| return 1 |
|
|
| return 0 |
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|