CoolFace
Apppublic

ashhal/Power_Systems_Mini-Consultant

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
deploy.py357 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3Deployment Helper Script for Power Systems Mini-Consultant4Automates the setup and deployment process for Hugging Face Spaces5"""6 7import os8import json9import shutil10import subprocess11import sys12from pathlib import Path13from typing import Dict, List, Optional14 15class PowerSystemsDeployer:16    """17    Automated deployment helper for Power Systems Mini-Consultant18    """19    20    def __init__(self):21        self.project_root = Path.cwd()22        self.required_files = [23            'app.py',24            'requirements.txt',25            'data/knowledge_base.json',26            'utils/__init__.py',27            'utils/rag_system.py',28            'utils/diagram_generator.py'29        ]30        self.optional_files = [31            'README.md',32            '.gitignore',33            '.env.example'34        ]35        36    def check_requirements(self) -> bool:37        """Check if all required files exist"""38        print("๐Ÿ” Checking project requirements...")39        40        missing_files = []41        for file_path in self.required_files:42            if not (self.project_root / file_path).exists():43                missing_files.append(file_path)44        45        if missing_files:46            print("โŒ Missing required files:")47            for file in missing_files:48                print(f"   - {file}")49            return False50        51        print("โœ… All required files found!")52        return True53    54    def validate_knowledge_base(self) -> bool:55        """Validate the knowledge base JSON structure"""56        print("๐Ÿ” Validating knowledge base...")57        58        try:59            kb_path = self.project_root / 'data' / 'knowledge_base.json'60            with open(kb_path, 'r', encoding='utf-8') as f:61                kb_data = json.load(f)62            63            # Check for essential sections64            required_sections = ['faults', 'protection', 'standards', 'formulas']65            missing_sections = [s for s in required_sections if s not in kb_data]66            67            if missing_sections:68                print(f"โš ๏ธ  Missing knowledge base sections: {missing_sections}")69                print("   The app will still work but with limited knowledge.")70            else:71                print("โœ… Knowledge base structure validated!")72            73            return True74            75        except json.JSONDecodeError as e:76            print(f"โŒ Invalid JSON in knowledge base: {e}")77            return False78        except Exception as e:79            print(f"โŒ Error validating knowledge base: {e}")80            return False81    82    def setup_environment(self) -> bool:83        """Set up the development environment"""84        print("๐Ÿ”ง Setting up environment...")85        86        try:87            # Create virtual environment if it doesn't exist88            venv_path = self.project_root / 'venv'89            if not venv_path.exists():90                print("Creating virtual environment...")91                subprocess.run([sys.executable, '-m', 'venv', 'venv'], check=True)92            93            # Determine the correct pip path94            if os.name == 'nt':  # Windows95                pip_path = venv_path / 'Scripts' / 'pip'96            else:  # Unix/Linux/MacOS97                pip_path = venv_path / 'bin' / 'pip'98            99            # Install requirements100            print("Installing requirements...")101            subprocess.run([str(pip_path), 'install', '-r', 'requirements.txt'], check=True)102            103            print("โœ… Environment setup complete!")104            return True105            106        except subprocess.CalledProcessError as e:107            print(f"โŒ Error setting up environment: {e}")108            return False109        except Exception as e:110            print(f"โŒ Unexpected error: {e}")111            return False112    113    def test_local_deployment(self) -> bool:114        """Test the application locally"""115        print("๐Ÿงช Testing local deployment...")116        117        try:118            # Check if .env file exists119            env_path = self.project_root / '.env'120            if not env_path.exists():121                print("โš ๏ธ  No .env file found. Creating template...")122                self.create_env_template()123                print("   Please add your GROQ_API_KEY to .env file and run again.")124                return False125            126            # Try importing the main application127            import importlib.util128            spec = importlib.util.spec_from_file_location("app", self.project_root / "app.py")129            app_module = importlib.util.module_from_spec(spec)130            131            # This will test if the imports work132            spec.loader.exec_module(app_module)133            134            print("โœ… Local deployment test passed!")135            return True136            137        except ImportError as e:138            print(f"โŒ Import error: {e}")139            print("   Please check your dependencies in requirements.txt")140            return False141        except Exception as e:142            print(f"โŒ Error testing deployment: {e}")143            return False144    145    def create_env_template(self):146        """Create .env template file"""147        env_template = """# Environment Variables for Power Systems Mini-Consultant148 149# Required: Groq API Key (Get from https://console.groq.com)150GROQ_API_KEY=your_groq_api_key_here151 152# Optional: Debug settings153DEBUG=False154LOG_LEVEL=INFO155 156# Optional: Model settings157MODEL_NAME=mixtral-8x7b-32768158MAX_TOKENS=2000159TEMPERATURE=0.7160"""161        162        with open(self.project_root / '.env', 'w') as f:163            f.write(env_template)164    165    def prepare_huggingface_deployment(self) -> bool:166        """Prepare files for Hugging Face Spaces deployment"""167        print("๐Ÿš€ Preparing Hugging Face deployment...")168        169        try:170            # Create deployment directory171            deploy_dir = self.project_root / 'hf_deployment'172            if deploy_dir.exists():173                shutil.rmtree(deploy_dir)174            deploy_dir.mkdir()175            176            # Copy required files177            for file_path in self.required_files:178                src = self.project_root / file_path179                dst = deploy_dir / file_path180                181                # Create parent directories if needed182                dst.parent.mkdir(parents=True, exist_ok=True)183                shutil.copy2(src, dst)184            185            # Copy optional files if they exist186            for file_path in self.optional_files:187                src = self.project_root / file_path188                if src.exists():189                    dst = deploy_dir / file_path190                    shutil.copy2(src, dst)191            192            # Create HuggingFace README193            self.create_hf_readme(deploy_dir)194            195            print(f"โœ… Deployment files prepared in: {deploy_dir}")196            print("\n๐Ÿ“‹ Next steps for Hugging Face deployment:")197            print("   1. Create a new Space on Hugging Face")198            print("   2. Choose 'Gradio' as the SDK")199            print("   3. Upload all files from the hf_deployment folder")200            print("   4. Add GROQ_API_KEY as a secret in Space settings")201            print("   5. Your Space will automatically build and deploy!")202            203            return True204            205        except Exception as e:206            print(f"โŒ Error preparing deployment: {e}")207            return False208    209    def create_hf_readme(self, deploy_dir: Path):210        """Create Hugging Face specific README"""211        hf_readme = """---212title: Power Systems Mini-Consultant213emoji: โšก214colorFrom: blue215colorTo: cyan216sdk: gradio217sdk_version: 4.44.0218app_file: app.py219pinned: false220license: mit221short_description: AI-powered assistant for power systems engineering222tags:223- power-systems224- electrical-engineering225- fault-analysis226- protection-systems227- education228- RAG229- groq230---231 232# โšก Power Systems Mini-Consultant233 234An advanced AI-powered chatbot for Power Systems engineering education and professional support.235 236## Setup Instructions237 2381. **Configure API Key**: Add your GROQ_API_KEY as a secret in the Space settings2392. **Launch**: The app will automatically start once configured240 241## Features242 243- ๐Ÿ’ฌ **AI Consultant Chat**: Technical assistance for power systems244- ๐Ÿ“š **Practice Pack Generator**: Custom exam preparation materials245- ๐Ÿ“‹ **Standards Explorer**: IEEE and IEC standards guidance246- ๐Ÿ“– **Study Resources**: Formulas and reference materials247 248## Usage249 250Simply start chatting with the AI consultant about power systems topics, or use the specialized tools for practice questions and standards exploration.251 252---253 254*For complete documentation, visit the project repository.*255"""256        257        with open(deploy_dir / 'README.md', 'w', encoding='utf-8') as f:258            f.write(hf_readme)259    260    def generate_deployment_report(self) -> Dict:261        """Generate a deployment report"""262        report = {263            "timestamp": str(Path.cwd()),264            "files_checked": len(self.required_files + self.optional_files),265            "status": "ready" if self.check_requirements() else "incomplete",266            "recommendations": []267        }268        269        # Check file sizes270        large_files = []271        for file_path in self.required_files:272            file_full_path = self.project_root / file_path273            if file_full_path.exists():274                size_mb = file_full_path.stat().st_size / (1024 * 1024)275                if size_mb > 10:  # Files larger than 10MB276                    large_files.append(f"{file_path} ({size_mb:.1f}MB)")277        278        if large_files:279            report["recommendations"].append(f"Large files detected: {large_files}")280        281        # Check for sensitive information282        env_file = self.project_root / '.env'283        if env_file.exists():284            report["recommendations"].append("Ensure .env file is in .gitignore")285        286        return report287    288    def run_deployment_checklist(self):289        """Run complete deployment checklist"""290        print("๐Ÿš€ Power Systems Mini-Consultant Deployment Checklist")291        print("=" * 60)292        293        steps = [294            ("Check requirements", self.check_requirements),295            ("Validate knowledge base", self.validate_knowledge_base),296            ("Setup environment", self.setup_environment),297            ("Test local deployment", self.test_local_deployment),298            ("Prepare HF deployment", self.prepare_huggingface_deployment)299        ]300        301        passed_steps = 0302        total_steps = len(steps)303        304        for i, (step_name, step_func) in enumerate(steps, 1):305            print(f"\n[{i}/{total_steps}] {step_name}...")306            if step_func():307                passed_steps += 1308            else:309                print(f"โŒ Step {i} failed. Please fix the issues and try again.")310                if i < 4:  # Don't break on HF deployment prep failure311                    break312        313        print(f"\n๐ŸŽฏ Deployment Summary: {passed_steps}/{total_steps} steps completed")314        315        if passed_steps == total_steps:316            print("๐ŸŽ‰ All checks passed! Your app is ready for deployment.")317        elif passed_steps >= 3:318            print("โš ๏ธ  Basic requirements met. Review warnings and deploy when ready.")319        else:320            print("โŒ Critical issues found. Please fix them before deploying.")321        322        # Generate report323        report = self.generate_deployment_report()324        report_path = self.project_root / 'deployment_report.json'325        with open(report_path, 'w') as f:326            json.dump(report, f, indent=2)327        print(f"\n๐Ÿ“„ Detailed report saved to: {report_path}")328 329def main():330    """Main deployment script"""331    print("๐Ÿ”ง Power Systems Mini-Consultant Deployment Helper")332    print("=" * 50)333    334    deployer = PowerSystemsDeployer()335    336    if len(sys.argv) > 1:337        command = sys.argv[1].lower()338        339        if command == 'check':340            deployer.check_requirements()341        elif command == 'setup':342            deployer.setup_environment()343        elif command == 'test':344            deployer.test_local_deployment()345        elif command == 'prepare':346            deployer.prepare_huggingface_deployment()347        elif command == 'full':348            deployer.run_deployment_checklist()349        else:350            print(f"Unknown command: {command}")351            print("Available commands: check, setup, test, prepare, full")352    else:353        # Run full checklist by default354        deployer.run_deployment_checklist()355 356if __name__ == "__main__":357    main()