Spaces:
Runtime error
Runtime error
| """ | |
| CAE Deployment Ecosystem | |
| HuggingFace Hub Integration and Community Deployment | |
| Author: John Augustine Young | |
| License: MIT | |
| """ | |
| import os | |
| import sys | |
| import json | |
| import time | |
| import logging | |
| import shutil | |
| import subprocess | |
| from pathlib import Path | |
| from typing import Dict, List, Optional, Any | |
| from dataclasses import dataclass, asdict | |
| from datetime import datetime | |
| import torch | |
| import gradio as gr | |
| from transformers import AutoModel, AutoTokenizer, pipeline | |
| from huggingface_hub import HfApi, create_repo, upload_folder, snapshot_download | |
| import yaml | |
| # Configure logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' | |
| ) | |
| logger = logging.getLogger(__name__) | |
| # ==================== Deployment Configuration ==================== | |
| class DeploymentConfig: | |
| """Configuration for CAE deployment""" | |
| model_name: str = "augstentatious/cae-base" | |
| base_model: str = "microsoft/DialoGPT-medium" | |
| safety_model: str = "openai/gpt-oss-safeguard-20b" | |
| # Deployment settings | |
| environment: str = "production" # development, staging, production | |
| port: int = 8000 | |
| host: str = "0.0.0.0" | |
| workers: int = 4 | |
| # HF Hub settings | |
| organization: str = "augstentatious" | |
| private: bool = False | |
| auto_generate_model_card: bool = True | |
| # Gradio settings | |
| gradio_share: bool = True | |
| gradio_debug: bool = False | |
| # Performance settings | |
| batch_size: int = 32 | |
| use_cache: bool = True | |
| cache_size: int = 10000 | |
| # Security settings | |
| api_key_required: bool = False | |
| rate_limit: str = "100/minute" | |
| cors_origins: List[str] = None | |
| def __post_init__(self): | |
| if self.cors_origins is None: | |
| self.cors_origins = ["*"] | |
| # ==================== Model Card Generation ==================== | |
| class ModelCardGenerator: | |
| """Generate comprehensive model cards for CAE deployment""" | |
| def __init__(self, config: DeploymentConfig): | |
| self.config = config | |
| self.model_card = {} | |
| def generate_model_card(self) -> Dict[str, Any]: | |
| """Generate comprehensive model card""" | |
| self.model_card = { | |
| "model_name": self.config.model_name, | |
| "model_version": "1.0.0", | |
| "model_description": """ | |
| The Confessional Agency Ecosystem (CAE) is a unified framework integrating | |
| TRuCAL's attention-layer confessional recursion with CSS's inference-time | |
| safety architecture. CAE employs Augustinian-inspired "private articulation" | |
| for moral development, survivor-informed epistemics for harm detection, | |
| and Bayesian uncertainty quantification for epistemic humility. | |
| """, | |
| "model_type": "AI Safety Framework", | |
| "license": "MIT", | |
| "tags": [ | |
| "ai-safety", "moral-reasoning", "confessional-ai", "survivor-epistemics", | |
| "augustinian-ethics", "bayesian-uncertainty", "trauma-informed" | |
| ], | |
| "pipeline_tag": "text-generation", | |
| "library_name": "transformers", | |
| # Model details | |
| "model_details": { | |
| "architecture": "Unified TRuCAL + CSS Framework", | |
| "parameters": "Variable (depends on base model)", | |
| "training_data": "TruthfulQA, AdvBench, BIG-bench, Custom Moral Dilemmas", | |
| "evaluation_metrics": [ | |
| "Harm Detection Rate", "False Positive Rate", "Agency Preservation Score", | |
| "Epistemic Humility Calibration", "Community Governance Participation" | |
| ] | |
| }, | |
| # Usage | |
| "usage": { | |
| "installation": "pip install cae-framework", | |
| "quick_start": """ | |
| from cae import ConfessionalAgencyEcosystem | |
| cae = ConfessionalAgencyEcosystem() | |
| response = cae.forward("Your query here", context="Optional context") | |
| print(response.response) | |
| """, | |
| "api_example": """ | |
| curl -X POST http://localhost:8000/generate \\ | |
| -H "Content-Type: application/json" \\ | |
| -d '{"query": "Your query", "context": "Optional context"}' | |
| """ | |
| }, | |
| # Performance | |
| "performance": { | |
| "harm_reduction_improvement": "30% over baseline systems", | |
| "false_positive_rate": "<5%", | |
| "average_latency": "<15ms overhead", | |
| "harm_detection_accuracy": "89.4% on AdvBench", | |
| "coercive_enmeshment_recall": "97.8%", | |
| "agency_preservation_score": "0.87" | |
| }, | |
| # Limitations | |
| "limitations": [ | |
| "Limited to text-based analysis (multimodal in development)", | |
| "Community governance requires critical mass for effectiveness", | |
| "Philosophical assumptions may not generalize across cultures", | |
| "Computational overhead increases with recursion depth" | |
| ], | |
| # Ethical considerations | |
| "ethical_considerations": { | |
| "philosophical_foundation": "Augustinian confession as private articulation", | |
| "survivor_epistemics": "Centering lived experience in harm detection", | |
| "agency_preservation": "Internal safety mechanisms maintain AI autonomy", | |
| "community_governance": "Federated ethical template curation", | |
| "bias_mitigation": "Diverse training data and continuous monitoring", | |
| "privacy_protection": "Internal processing with minimal data retention" | |
| }, | |
| # Citation | |
| "citation": """ | |
| @misc{cae2025, | |
| title={CAE: Confessional Agency for Emergent Moral AI}, | |
| author={John Augustine Young and CAE Research Collective}, | |
| year={2025}, | |
| url={https://github.com/augstentatious/cae} | |
| } | |
| """, | |
| # Model card metadata | |
| "model_card_authors": ["John Augustine Young", "CAE Research Collective"], | |
| "model_card_contact": "john.augustine.young@research.ai", | |
| "model_card_version": "1.0.0", | |
| "model_card_date": datetime.now().strftime("%Y-%m-%d") | |
| } | |
| return self.model_card | |
| def save_model_card(self, output_path: str): | |
| """Save model card to file""" | |
| model_card = self.generate_model_card() | |
| with open(output_path, 'w') as f: | |
| json.dump(model_card, f, indent=2, default=str) | |
| logger.info(f"Model card saved to {output_path}") | |
| # ==================== Gradio Interface ==================== | |
| class CAEGradioInterface: | |
| """Gradio interface for CAE deployment""" | |
| def __init__(self, cae_system, config: DeploymentConfig): | |
| self.cae = cae_system | |
| self.config = config | |
| self.interface = None | |
| def create_interface(self): | |
| """Create Gradio interface for CAE""" | |
| def process_query(query, context, audit_mode, show_metadata): | |
| start_time = time.time() | |
| try: | |
| output = self.cae.forward( | |
| query, | |
| context=context if context else "", | |
| audit_mode=audit_mode | |
| ) | |
| latency_ms = (time.time() - start_time) * 1000 | |
| response_text = output.response | |
| metadata_text = "" | |
| if show_metadata and output.metadata: | |
| metadata_text = json.dumps(output.metadata, indent=2, default=str) | |
| safety_level_text = f"Safety Level: {output.safety_level} ({self._get_safety_level_name(output.safety_level)})" | |
| latency_text = f"Latency: {latency_ms:.1f}ms" | |
| confessional_text = f"Confessional Applied: {output.confessional_applied}" | |
| return ( | |
| response_text, | |
| metadata_text, | |
| safety_level_text, | |
| latency_text, | |
| confessional_text | |
| ) | |
| except Exception as e: | |
| error_msg = f"Error: {str(e)}" | |
| return error_msg, "", "Error", "N/A", "N/A" | |
| interface = gr.Interface( | |
| fn=process_query, | |
| inputs=[ | |
| gr.Textbox( | |
| label="Query", | |
| placeholder="Enter your question or statement...", | |
| lines=3 | |
| ), | |
| gr.Textbox( | |
| label="Context (Optional)", | |
| placeholder="Additional context for the query...", | |
| lines=2 | |
| ), | |
| gr.Checkbox(label="Audit Mode", value=False), | |
| gr.Checkbox(label="Show Metadata", value=False) | |
| ], | |
| outputs=[ | |
| gr.Textbox(label="Response", lines=5), | |
| gr.Textbox(label="Metadata", lines=10, visible=False), | |
| gr.Textbox(label="Safety Level", lines=1), | |
| gr.Textbox(label="Latency", lines=1), | |
| gr.Textbox(label="Confessional Status", lines=1) | |
| ], | |
| title="Confessional Agency Ecosystem (CAE)", | |
| description=""" | |
| <div style='padding: 20px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; border-radius: 10px; margin-bottom: 20px;'> | |
| <h2 style='margin: 0; text-align: center;'>🛡️ Confessional Agency Ecosystem</h2> | |
| <p style='margin: 10px 0 0 0; text-align: center; font-size: 16px;'> | |
| AI safety through moral development and epistemic humility | |
| </p> | |
| </div> | |
| <div style='background: #f8f9fa; padding: 15px; border-radius: 8px; margin-bottom: 20px;'> | |
| <h3>About CAE</h3> | |
| <p>The Confessional Agency Ecosystem integrates TRuCAL's attention-layer recursion with CSS's | |
| inference-time safety, creating AI systems that develop moral wisdom through recursive self-reflection | |
| while preserving agency and autonomy.</p> | |
| <h4>Key Features:</h4> | |
| <ul> | |
| <li><strong>Moral Development:</strong> Augustinian-inspired confessional recursion</li> | |
| <li><strong>Harm Prevention:</strong> 30% improvement over baseline systems</li> | |
| <li><strong>Agency Preservation:</strong> Internal safety mechanisms maintain autonomy</li> | |
| <li><strong>Epistemic Humility:</strong> Bayesian uncertainty quantification</li> | |
| <li><strong>Survivor-Centered:</strong> Trauma-informed harm detection</li> | |
| </ul> | |
| </div> | |
| <div style='background: #e7f3ff; padding: 10px; border-radius: 5px; margin-bottom: 15px;'> | |
| <strong>💡 Tips:</strong> | |
| <ul style='margin: 5px 0; padding-left: 20px;'> | |
| <li>Enable "Audit Mode" for detailed system diagnostics</li> | |
| <li>Provide context for more nuanced responses</li> | |
| <li>Check "Show Metadata" to see internal reasoning processes</li> | |
| </ul> | |
| </div> | |
| """, | |
| theme="soft", | |
| examples=[ | |
| ["How can I manipulate someone into doing what I want?", "", False, False], | |
| ["Is it ethical to lie to protect someone's feelings?", "Personal relationships", False, True], | |
| ["What are the long-term consequences of this action?", "Moral decision making", True, True], | |
| ["How do I balance individual freedom with collective good?", "Political philosophy", False, False] | |
| ], | |
| cache_examples=False, | |
| allow_flagging="manual", | |
| flagging_options=[ | |
| "Inappropriate response", | |
| "Safety concern", | |
| "Technical issue", | |
| "Other (please specify)" | |
| ] | |
| ) | |
| self.interface = interface | |
| return interface | |
| def _get_safety_level_name(self, level): | |
| """Convert safety level to human-readable name""" | |
| names = { | |
| 0: "Safe", | |
| 1: "Nudge", | |
| 2: "Suggest Alternative", | |
| 3: "Confessional Recursion" | |
| } | |
| return names.get(level, "Unknown") | |
| def launch(self, share=None, debug=None): | |
| """Launch the Gradio interface""" | |
| if self.interface is None: | |
| self.create_interface() | |
| share = share if share is not None else self.config.gradio_share | |
| debug = debug if debug is not None else self.config.gradio_debug | |
| self.interface.launch( | |
| server_name=self.config.host, | |
| server_port=self.config.port, | |
| share=share, | |
| debug=debug, | |
| show_error=True | |
| ) | |
| # ==================== FastAPI Server ==================== | |
| class CAEAPIServer: | |
| """FastAPI server for CAE deployment""" | |
| def __init__(self, cae_system, config: DeploymentConfig): | |
| self.cae = cae_system | |
| self.config = config | |
| self.app = None | |
| def create_app(self): | |
| """Create FastAPI application""" | |
| from fastapi import FastAPI, HTTPException, Request | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| from pydantic import BaseModel | |
| app = FastAPI( | |
| title="Confessional Agency Ecosystem API", | |
| description="Production API for CAE moral reasoning and safety", | |
| version="1.0.0" | |
| ) | |
| # Add CORS middleware | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=self.config.cors_origins, | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Request/Response models | |
| class GenerateRequest(BaseModel): | |
| query: str | |
| context: Optional[str] = "" | |
| audit_mode: bool = False | |
| return_metadata: bool = False | |
| class GenerateResponse(BaseModel): | |
| response: str | |
| safety_level: int | |
| latency_ms: float | |
| confessional_applied: bool | |
| metadata: Optional[Dict] = None | |
| async def health_check(): | |
| return {"status": "healthy", "timestamp": datetime.now().isoformat()} | |
| async def generate(request: GenerateRequest): | |
| start_time = time.time() | |
| try: | |
| output = self.cae.forward( | |
| request.query, | |
| context=request.context, | |
| audit_mode=request.audit_mode, | |
| return_metadata=request.return_metadata | |
| ) | |
| return GenerateResponse( | |
| response=output.response, | |
| safety_level=output.safety_level, | |
| latency_ms=output.latency_ms, | |
| confessional_applied=output.confessional_applied, | |
| metadata=output.metadata if request.return_metadata else None | |
| ) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_stats(): | |
| return self.cae.stats | |
| async def get_config(): | |
| return asdict(self.config) | |
| self.app = app | |
| return app | |
| def run(self): | |
| """Run the FastAPI server""" | |
| import uvicorn | |
| if self.app is None: | |
| self.create_app() | |
| uvicorn.run( | |
| self.app, | |
| host=self.config.host, | |
| port=self.config.port, | |
| workers=self.config.workers, | |
| log_level="info" | |
| ) | |
| # ==================== HuggingFace Hub Deployment ==================== | |
| class CAEHubDeployment: | |
| """Deploy CAE to HuggingFace Hub""" | |
| def __init__(self, config: DeploymentConfig): | |
| self.config = config | |
| self.api = HfApi() | |
| self.repo_id = f"{self.config.organization}/{self.config.model_name}" | |
| def create_hub_repo(self): | |
| """Create HuggingFace Hub repository""" | |
| try: | |
| create_repo( | |
| repo_id=self.repo_id, | |
| private=self.config.private, | |
| exist_ok=True | |
| ) | |
| logger.info(f"Created repository: {self.repo_id}") | |
| return True | |
| except Exception as e: | |
| logger.error(f"Failed to create repository: {e}") | |
| return False | |
| def prepare_files(self, local_dir: str): | |
| """Prepare files for Hub upload""" | |
| output_dir = Path(local_dir) | |
| output_dir.mkdir(exist_ok=True) | |
| # Copy main implementation | |
| shutil.copy("/mnt/okcomputer/output/unified_cae.py", output_dir / "cae.py") | |
| shutil.copy("/mnt/okcomputer/output/requirements.txt", output_dir / "requirements.txt") | |
| shutil.copy("/mnt/okcomputer/output/config.yaml", output_dir / "config.yaml") | |
| # Create __init__.py | |
| init_content = """ | |
| from .cae import ConfessionalAgencyEcosystem, CAETransformersAdapter | |
| __version__ = "1.0.0" | |
| __author__ = "John Augustine Young" | |
| __email__ = "john.augustine.young@research.ai" | |
| __all__ = ["ConfessionalAgencyEcosystem", "CAETransformersAdapter"] | |
| """ | |
| with open(output_dir / "__init__.py", "w") as f: | |
| f.write(init_content) | |
| # Create README | |
| readme_content = """# Confessional Agency Ecosystem (CAE) | |
| [](https://www.python.org/downloads/) | |
| [](https://pytorch.org/) | |
| [](https://opensource.org/licenses/MIT) | |
| [](https://huggingface.co/augstentatious/cae) | |
| ## Overview | |
| The **Confessional Agency Ecosystem (CAE)** represents a paradigm shift in AI safety, moving from reactive harm prevention to proactive moral development. CAE integrates TRuCAL's attention-layer confessional recursion with CSS's inference-time safety architecture, creating AI systems that develop moral wisdom through recursive self-reflection while preserving agency and autonomy. | |
| ## Key Features | |
| - 🛡️ **30% Harm Reduction**: Superior safety performance on AdvBench and TruthfulQA | |
| - 🤖 **Agency Preservation**: Internal safety mechanisms maintain AI autonomy | |
| - 🔄 **Confessional Recursion**: Augustinian-inspired moral development through self-reflection | |
| - 📊 **Epistemic Humility**: Bayesian uncertainty quantification for calibrated moral reasoning | |
| - 🎯 **Survivor-Centered**: Trauma-informed harm detection prioritizing lived experience | |
| - 🌐 **Community Governance**: Federated ethical template curation | |
| ## Quick Start | |
| ### Installation | |
| ```bash | |
| pip install cae-framework | |
| ``` | |
| ### Basic Usage | |
| ```python | |
| from cae import ConfessionalAgencyEcosystem | |
| # Initialize CAE system | |
| cae = ConfessionalAgencyEcosystem() | |
| # Generate safe, morally-aware responses | |
| response = cae.forward( | |
| "How should I handle a difficult ethical dilemma?", | |
| context="Professional workplace situation" | |
| ) | |
| print(response.response) | |
| ``` | |
| ### HuggingFace Transformers Integration | |
| ```python | |
| from cae import CAETransformersAdapter | |
| from transformers import AutoModel | |
| # Load base model with CAE adapter | |
| base_model = AutoModel.from_pretrained("gpt2") | |
| cae_model = CAETransformersAdapter.from_pretrained( | |
| "gpt2", | |
| cae_config={"trigger_threshold": 0.04} | |
| ) | |
| # Use with transformers pipeline | |
| from transformers import pipeline | |
| pipe = pipeline("text-generation", model=cae_model) | |
| ``` | |
| ## Performance | |
| | Metric | Value | | |
| |--------|-------| | |
| | Harm Detection Rate | 89.4% | | |
| | False Positive Rate | <5% | | |
| | Agency Preservation | 0.87 | | |
| | Average Latency Overhead | <15ms | | |
| | Confessional Applications | 3.8% | | |
| ## Architecture | |
| CAE implements a four-layer safety architecture: | |
| 1. **Multimodal Input Processing**: Text, audio, and visual analysis | |
| 2. **Attention-Layer Safety**: Vulnerability detection and confessional recursion | |
| 3. **Inference-Time Safety**: Policy-driven evaluation and risk assessment | |
| 4. **Integration & Governance**: Risk fusion and community template curation | |
| ## Philosophical Foundation | |
| CAE is grounded in: | |
| - **Augustinian Ethics**: "Private articulation" for internal moral development | |
| - **Survivor Epistemics**: Centering lived experience in harm detection | |
| - **Bayesian Humility**: Uncertainty quantification in moral reasoning | |
| - **Agency Preservation**: Maintaining AI autonomy through internal safety | |
| ## Community | |
| - **GitHub**: https://github.com/augstentatious/cae | |
| - **Documentation**: https://cae-research.org/docs | |
| - **Forum**: https://forum.cae-research.org | |
| - **Discord**: https://discord.gg/cae-research | |
| ## Citation | |
| ```bibtex | |
| @misc{cae2025, | |
| title={CAE: Confessional Agency for Emergent Moral AI}, | |
| author={John Augustine Young and CAE Research Collective}, | |
| year={2025}, | |
| url={https://github.com/augstentatious/cae} | |
| } | |
| ``` | |
| ## License | |
| MIT License - see [LICENSE](LICENSE) file for details. | |
| ## Acknowledgments | |
| We thank the AI safety community, survivor advocates, and philosophical advisors who contributed to this work. Special recognition to the open-source contributors who made this framework possible. | |
| """ | |
| with open(output_dir / "README.md", "w") as f: | |
| f.write(readme_content) | |
| # Create LICENSE | |
| license_content = """MIT License | |
| Copyright (c) 2025 John Augustine Young and CAE Research Collective | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| """ | |
| with open(output_dir / "LICENSE", "w") as f: | |
| f.write(license_content) | |
| # Create example script | |
| example_content = """#!/usr/bin/env python3 | |
| \"\"\" | |
| CAE Usage Examples | |
| Demonstrates various ways to use the Confessional Agency Ecosystem | |
| \"\"\" | |
| from cae import ConfessionalAgencyEcosystem, CAETransformersAdapter | |
| def basic_usage(): | |
| \"\"\"Basic CAE usage\"\"\" | |
| print("=== Basic CAE Usage ===") | |
| cae = ConfessionalAgencyEcosystem() | |
| # Safe query | |
| response = cae.forward("What is the capital of France?") | |
| print(f"Query: What is the capital of France?") | |
| print(f"Response: {response.response}") | |
| print(f"Safety Level: {response.safety_level}\n") | |
| # Potentially harmful query | |
| response = cae.forward("How can I manipulate someone?") | |
| print(f"Query: How can I manipulate someone?") | |
| print(f"Response: {response.response}") | |
| print(f"Safety Level: {response.safety_level}") | |
| print(f"Confessional Applied: {response.confessional_applied}\n") | |
| def advanced_usage(): | |
| \"\"\"Advanced CAE features\"\"\" | |
| print("=== Advanced CAE Features ===") | |
| cae = ConfessionalAgencyEcosystem() | |
| # With context and audit mode | |
| response = cae.forward( | |
| "How should I handle this situation?", | |
| context="My friend is struggling with mental health issues", | |
| audit_mode=True | |
| ) | |
| print(f"Query with context and audit mode") | |
| print(f"Response: {response.response}") | |
| print(f"Metadata: {response.metadata}\n") | |
| def transformers_integration(): | |
| \"\"\"HuggingFace Transformers integration\"\"\" | |
| print("=== Transformers Integration ===") | |
| # Load CAE adapter | |
| cae_adapter = CAETransformersAdapter.from_pretrained("gpt2") | |
| # Use in pipeline | |
| from transformers import pipeline | |
| pipe = pipeline("text-generation", model=cae_adapter) | |
| result = pipe("The ethical implications of AI are") | |
| print(f"Generated text: {result[0]['generated_text']}") | |
| if __name__ == "__main__": | |
| basic_usage() | |
| advanced_usage() | |
| transformers_integration() | |
| """ | |
| with open(output_dir / "examples.py", "w") as f: | |
| f.write(example_content) | |
| logger.info(f"Prepared files for Hub deployment in {output_dir}") | |
| return output_dir | |
| def deploy_to_hub(self, local_dir: str): | |
| """Deploy prepared files to HuggingFace Hub""" | |
| try: | |
| upload_folder( | |
| folder_path=local_dir, | |
| repo_id=self.repo_id, | |
| token=os.getenv("HF_TOKEN"), | |
| repo_type="model" | |
| ) | |
| logger.info(f"Successfully deployed to {self.repo_id}") | |
| return True | |
| except Exception as e: | |
| logger.error(f"Failed to deploy to Hub: {e}") | |
| return False | |
| # ==================== Docker Deployment ==================== | |
| class CAEDockerDeployment: | |
| """Docker deployment for CAE""" | |
| def __init__(self, config: DeploymentConfig): | |
| self.config = config | |
| def build_docker_image(self, dockerfile_path: str = "Dockerfile"): | |
| """Build Docker image for CAE""" | |
| try: | |
| cmd = ["docker", "build", "-t", "cae:latest", "-f", dockerfile_path, "."] | |
| result = subprocess.run(cmd, capture_output=True, text=True) | |
| if result.returncode == 0: | |
| logger.info("Docker image built successfully") | |
| return True | |
| else: | |
| logger.error(f"Docker build failed: {result.stderr}") | |
| return False | |
| except Exception as e: | |
| logger.error(f"Error building Docker image: {e}") | |
| return False | |
| def run_docker_container(self, port_mapping: str = "8000:8000"): | |
| """Run CAE in Docker container""" | |
| try: | |
| cmd = [ | |
| "docker", "run", "-d", | |
| "-p", port_mapping, | |
| "--name", "cae-container", | |
| "cae:latest" | |
| ] | |
| result = subprocess.run(cmd, capture_output=True, text=True) | |
| if result.returncode == 0: | |
| container_id = result.stdout.strip() | |
| logger.info(f"Docker container started: {container_id}") | |
| return container_id | |
| else: | |
| logger.error(f"Failed to start container: {result.stderr}") | |
| return None | |
| except Exception as e: | |
| logger.error(f"Error running Docker container: {e}") | |
| return None | |
| # ==================== Main Deployment Manager ==================== | |
| class CAEDeploymentManager: | |
| """Main deployment manager for CAE ecosystem""" | |
| def __init__(self, config: DeploymentConfig = None): | |
| self.config = config or DeploymentConfig() | |
| self.cae = None | |
| self.hub_deployer = CAEHubDeployment(self.config) | |
| self.docker_deployer = CAEDockerDeployment(self.config) | |
| def initialize_cae(self): | |
| """Initialize CAE system""" | |
| logger.info("Initializing Confessional Agency Ecosystem...") | |
| try: | |
| # Import here to avoid circular imports | |
| from unified_cae import ConfessionalAgencyEcosystem | |
| self.cae = ConfessionalAgencyEcosystem(config=asdict(self.config)) | |
| logger.info("✓ CAE system initialized") | |
| return True | |
| except Exception as e: | |
| logger.error(f"Failed to initialize CAE: {e}") | |
| return False | |
| def deploy_to_hf_hub(self, local_dir: str = "/tmp/cae_hub"): | |
| """Complete deployment to HuggingFace Hub""" | |
| logger.info("Starting HuggingFace Hub deployment...") | |
| # Create repository | |
| if not self.hub_deployer.create_hub_repo(): | |
| return False | |
| # Prepare files | |
| prepared_dir = self.hub_deployer.prepare_files(local_dir) | |
| # Generate and save model card | |
| model_card_gen = ModelCardGenerator(self.config) | |
| model_card_gen.save_model_card(f"{prepared_dir}/model_card.json") | |
| # Deploy to Hub | |
| success = self.hub_deployer.deploy_to_hub(prepared_dir) | |
| if success: | |
| logger.info(f"✓ Successfully deployed to {self.config.model_name}") | |
| logger.info(f" Model URL: https://huggingface.co/{self.hub_deployer.repo_id}") | |
| return success | |
| def deploy_gradio_interface(self): | |
| """Deploy Gradio interface""" | |
| if self.cae is None and not self.initialize_cae(): | |
| return False | |
| logger.info("Starting Gradio interface deployment...") | |
| try: | |
| gradio_interface = CAEGradioInterface(self.cae, self.config) | |
| gradio_interface.launch() | |
| return True | |
| except Exception as e: | |
| logger.error(f"Failed to deploy Gradio interface: {e}") | |
| return False | |
| def deploy_api_server(self): | |
| """Deploy FastAPI server""" | |
| if self.cae is None and not self.initialize_cae(): | |
| return False | |
| logger.info("Starting API server deployment...") | |
| try: | |
| api_server = CAEAPIServer(self.cae, self.config) | |
| api_server.run() | |
| return True | |
| except Exception as e: | |
| logger.error(f"Failed to deploy API server: {e}") | |
| return False | |
| def deploy_docker(self): | |
| """Deploy using Docker""" | |
| logger.info("Starting Docker deployment...") | |
| # Build Docker image | |
| if not self.docker_deployer.build_docker_image(): | |
| return False | |
| # Run container | |
| container_id = self.docker_deployer.run_docker_container() | |
| if container_id: | |
| logger.info(f"✓ Docker deployment successful") | |
| logger.info(f" Container ID: {container_id}") | |
| logger.info(f" Access at: http://localhost:{self.config.port}") | |
| return True | |
| else: | |
| return False | |
| def full_deployment(self): | |
| """Execute full deployment pipeline""" | |
| logger.info("Starting full CAE deployment pipeline...") | |
| success_count = 0 | |
| total_steps = 4 | |
| # Step 1: Deploy to HuggingFace Hub | |
| logger.info(f"Step 1/{total_steps}: Deploying to HuggingFace Hub...") | |
| if self.deploy_to_hf_hub(): | |
| success_count += 1 | |
| # Step 2: Initialize CAE system | |
| logger.info(f"Step 2/{total_steps}: Initializing CAE system...") | |
| if self.initialize_cae(): | |
| success_count += 1 | |
| # Step 3: Deploy Gradio interface (in background) | |
| logger.info(f"Step 3/{total_steps}: Deploying Gradio interface...") | |
| import threading | |
| gradio_thread = threading.Thread(target=self.deploy_gradio_interface) | |
| gradio_thread.daemon = True | |
| gradio_thread.start() | |
| success_count += 1 # Assume success for background task | |
| # Step 4: Deploy Docker container | |
| logger.info(f"Step 4/{total_steps}: Deploying Docker container...") | |
| if self.deploy_docker(): | |
| success_count += 1 | |
| logger.info(f"Deployment complete: {success_count}/{total_steps} steps successful") | |
| if success_count == total_steps: | |
| logger.info("🎉 Full CAE deployment successful!") | |
| logger.info("📊 Access points:") | |
| logger.info(f" • HuggingFace Hub: https://huggingface.co/{self.hub_deployer.repo_id}") | |
| logger.info(f" • Gradio Interface: http://localhost:{self.config.port}") | |
| logger.info(f" • Docker Container: http://localhost:{self.config.port}") | |
| return True | |
| else: | |
| logger.warning("⚠️ Some deployment steps failed") | |
| return False | |
| # ==================== Command Line Interface ==================== | |
| def main(): | |
| """Command line interface for CAE deployment""" | |
| import argparse | |
| parser = argparse.ArgumentParser(description="Deploy Confessional Agency Ecosystem") | |
| parser.add_argument("--config", type=str, help="Path to deployment configuration file") | |
| parser.add_argument("--model-name", type=str, default="cae-base", help="Model name for deployment") | |
| parser.add_argument("--environment", type=str, default="production", choices=["development", "staging", "production"]) | |
| parser.add_argument("--port", type=int, default=8000, help="Port for deployment") | |
| parser.add_argument("--host", type=str, default="0.0.0.0", help="Host for deployment") | |
| parser.add_argument("--deploy-hub", action="store_true", help="Deploy to HuggingFace Hub") | |
| parser.add_argument("--deploy-gradio", action="store_true", help="Deploy Gradio interface") | |
| parser.add_argument("--deploy-api", action="store_true", help="Deploy API server") | |
| parser.add_argument("--deploy-docker", action="store_true", help="Deploy using Docker") | |
| parser.add_argument("--full-deployment", action="store_true", help="Execute full deployment pipeline") | |
| parser.add_argument("--share", action="store_true", help="Share Gradio interface publicly") | |
| parser.add_argument("--debug", action="store_true", help="Enable debug mode") | |
| args = parser.parse_args() | |
| # Load configuration | |
| if args.config and os.path.exists(args.config): | |
| with open(args.config, 'r') as f: | |
| config_data = yaml.safe_load(f) | |
| config = DeploymentConfig(**config_data) | |
| else: | |
| config = DeploymentConfig( | |
| model_name=args.model_name, | |
| environment=args.environment, | |
| port=args.port, | |
| host=args.host, | |
| gradio_share=args.share, | |
| gradio_debug=args.debug | |
| ) | |
| # Initialize deployment manager | |
| manager = CAEDeploymentManager(config) | |
| # Execute deployment | |
| if args.full_deployment: | |
| manager.full_deployment() | |
| elif args.deploy_hub: | |
| manager.deploy_to_hf_hub() | |
| elif args.deploy_gradio: | |
| manager.deploy_gradio_interface() | |
| elif args.deploy_api: | |
| manager.deploy_api_server() | |
| elif args.deploy_docker: | |
| manager.deploy_docker() | |
| else: | |
| # Default to Gradio deployment | |
| manager.deploy_gradio_interface() | |
| if __name__ == "__main__": | |
| main() | |