Spaces:
Sleeping
Sleeping
Update modules/orchestrator.py
Browse files- modules/orchestrator.py +97 -134
modules/orchestrator.py
CHANGED
|
@@ -13,208 +13,171 @@ import re
|
|
| 13 |
from datetime import datetime
|
| 14 |
from typing import Dict, Any, List, Optional, Union, Callable
|
| 15 |
from dataclasses import dataclass, field
|
| 16 |
-
from fastapi import APIRouter, Form, Request
|
| 17 |
|
| 18 |
# =================================================================
|
| 19 |
-
# 1. ENTERPRISE
|
| 20 |
# =================================================================
|
| 21 |
@dataclass
|
| 22 |
class MasterConfig:
|
| 23 |
-
version: str = "
|
| 24 |
max_workers: int = os.cpu_count() or 4
|
| 25 |
timeout: int = 180
|
| 26 |
-
memory_limit_mb: int =
|
| 27 |
-
log_file: str = "logs/
|
| 28 |
username: str = "AumCoreAI"
|
| 29 |
-
repo_name: str = "
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
# =================================================================
|
| 34 |
-
# 2.
|
| 35 |
# =================================================================
|
| 36 |
-
class
|
| 37 |
-
"""Professional logging and system auditing"""
|
| 38 |
def __init__(self, config: MasterConfig):
|
| 39 |
if not os.path.exists('logs'): os.makedirs('logs')
|
| 40 |
-
self.logger = logging.getLogger("
|
| 41 |
self.logger.setLevel(logging.DEBUG)
|
| 42 |
if not self.logger.handlers:
|
| 43 |
-
|
| 44 |
fh = logging.FileHandler(config.log_file)
|
| 45 |
-
|
| 46 |
-
class IDFilter(logging.Filter):
|
| 47 |
-
def filter(self, record):
|
| 48 |
-
record.id = getattr(record, 'id', 'SYSTEM')
|
| 49 |
-
return True
|
| 50 |
-
fh.addFilter(IDFilter())
|
| 51 |
-
fh.setFormatter(fmt)
|
| 52 |
self.logger.addHandler(fh)
|
| 53 |
-
|
| 54 |
ch = logging.StreamHandler()
|
| 55 |
-
ch.setFormatter(
|
| 56 |
self.logger.addHandler(ch)
|
| 57 |
|
| 58 |
-
def log(self, level: str, msg: str
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
elif level == "warn": self.logger.warning(msg, extra=extra)
|
| 63 |
|
| 64 |
# =================================================================
|
| 65 |
-
# 3.
|
| 66 |
# =================================================================
|
| 67 |
-
class
|
| 68 |
-
"""
|
| 69 |
@staticmethod
|
| 70 |
-
def
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
if "```python" in text and
|
| 77 |
text += "\n```"
|
| 78 |
-
|
| 79 |
-
# Fix 3: Remove cluttered imports (Add newlines)
|
| 80 |
-
text = text.replace("import loggingimport", "import logging\nimport")
|
| 81 |
-
text = text.replace("import timefrom", "import time\nfrom")
|
| 82 |
-
|
| 83 |
return text
|
| 84 |
|
| 85 |
# =================================================================
|
| 86 |
-
# 4.
|
| 87 |
# =================================================================
|
| 88 |
-
class
|
| 89 |
-
def __init__(self, client, config: MasterConfig, audit:
|
| 90 |
self.client = client
|
| 91 |
self.config = config
|
| 92 |
self.audit = audit
|
| 93 |
-
self.
|
| 94 |
|
| 95 |
-
async def
|
| 96 |
-
req_id = f"
|
| 97 |
-
self.audit.log("info", f"
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
try:
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
# Stage 2: Hard-Coded Architect Persona Generation
|
| 104 |
-
raw_ai_out = await self._generate_expert_content(logic_prompt or message, req_id)
|
| 105 |
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
|
| 109 |
-
#
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
-
self.audit.log("info", "Workflow completed successfully"
|
| 113 |
-
return
|
| 114 |
|
| 115 |
except Exception as e:
|
| 116 |
-
self.audit.log("error", f"Pipeline
|
| 117 |
return f"❌ **Titan System Error:** {str(e)}"
|
| 118 |
|
| 119 |
-
async def
|
| 120 |
-
if not self.client: return "Error:
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
f"
|
| 124 |
-
"
|
| 125 |
-
"
|
| 126 |
-
"
|
| 127 |
-
"
|
| 128 |
-
"
|
| 129 |
-
"
|
| 130 |
)
|
| 131 |
|
| 132 |
try:
|
| 133 |
loop = asyncio.get_event_loop()
|
| 134 |
-
|
| 135 |
model="llama-3.3-70b-versatile",
|
| 136 |
messages=[
|
| 137 |
-
{"role": "system", "content":
|
| 138 |
{"role": "user", "content": prompt}
|
| 139 |
],
|
| 140 |
-
temperature=0.
|
| 141 |
max_tokens=3500
|
| 142 |
))
|
| 143 |
-
return
|
| 144 |
except Exception as e:
|
| 145 |
-
raise RuntimeError(f"LLM
|
| 146 |
-
|
| 147 |
-
async def _invoke_module(self, mod_name: str, method: str, data: str):
|
| 148 |
-
module = sys.modules.get(mod_name)
|
| 149 |
-
if module and hasattr(module, method):
|
| 150 |
-
try:
|
| 151 |
-
self.audit.log("info", f"Invoking {mod_name}.{method}")
|
| 152 |
-
func = getattr(module, method)
|
| 153 |
-
return await func(data) if asyncio.iscoroutinefunction(func) else func(data)
|
| 154 |
-
except Exception as e:
|
| 155 |
-
self.audit.log("warn", f"Module {mod_name} call failed: {e}")
|
| 156 |
-
return None
|
| 157 |
|
| 158 |
# =================================================================
|
| 159 |
-
# 5.
|
| 160 |
# =================================================================
|
| 161 |
-
class
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
is_coding = any(k in message.lower() for k in code_keywords)
|
| 172 |
-
|
| 173 |
-
if is_coding:
|
| 174 |
-
return await self.pipeline.run_workflow(message)
|
| 175 |
-
|
| 176 |
-
# Default AI conversation through the same high-level pipeline
|
| 177 |
-
return await self.pipeline.run_workflow(f"Handle this query professionally: {message}")
|
| 178 |
|
| 179 |
# =================================================================
|
| 180 |
-
# 6.
|
| 181 |
# =================================================================
|
| 182 |
def register_module(app, client, username):
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
""
|
| 187 |
-
master = AumCoreMaster(client)
|
| 188 |
-
router = APIRouter(prefix="/system", tags=["Titan Core v5"])
|
| 189 |
|
| 190 |
@router.post("/orchestrate")
|
| 191 |
-
async def
|
| 192 |
-
|
| 193 |
-
response = await master.process_orchestration(message)
|
| 194 |
return {"response": response}
|
| 195 |
|
| 196 |
-
@router.get("/titan/
|
| 197 |
-
async def
|
| 198 |
-
|
| 199 |
-
return {
|
| 200 |
-
"status": "Alive",
|
| 201 |
-
"version": master.config.version,
|
| 202 |
-
"memory": f"{vmem.percent}%",
|
| 203 |
-
"threads": threading.active_count()
|
| 204 |
-
}
|
| 205 |
|
| 206 |
-
# Add to FastAPI App
|
| 207 |
app.include_router(router)
|
| 208 |
-
app.state.orchestrator =
|
| 209 |
-
|
| 210 |
print("\n" + "X"*60)
|
| 211 |
-
print(f"🔱 TITAN
|
| 212 |
-
print(f"✅
|
| 213 |
-
print(f"✅ Response Sanitizer: ACTIVE (Fixing Markdown)")
|
| 214 |
print("X"*60 + "\n")
|
| 215 |
|
| 216 |
-
# =================================================================
|
| 217 |
-
# 7. SYSTEM STARTUP
|
| 218 |
-
# =================================================================
|
| 219 |
if __name__ == "__main__":
|
| 220 |
-
print("Titan
|
|
|
|
| 13 |
from datetime import datetime
|
| 14 |
from typing import Dict, Any, List, Optional, Union, Callable
|
| 15 |
from dataclasses import dataclass, field
|
| 16 |
+
from fastapi import APIRouter, Form, Request
|
| 17 |
|
| 18 |
# =================================================================
|
| 19 |
+
# 1. TITAN ENTERPRISE CONFIGURATION (V6.0 - THE BEAST)
|
| 20 |
# =================================================================
|
| 21 |
@dataclass
|
| 22 |
class MasterConfig:
|
| 23 |
+
version: str = "6.0.0-Titan-Enterprise"
|
| 24 |
max_workers: int = os.cpu_count() or 4
|
| 25 |
timeout: int = 180
|
| 26 |
+
memory_limit_mb: int = 2048
|
| 27 |
+
log_file: str = "logs/titan_main.log"
|
| 28 |
username: str = "AumCoreAI"
|
| 29 |
+
repo_name: str = "AumCore-AI"
|
| 30 |
+
system_persona: str = "Lead Software Architect & Security Expert"
|
| 31 |
+
active_modules: list = field(default_factory=lambda: ["intelligence", "reviewer", "formatter"])
|
| 32 |
|
| 33 |
# =================================================================
|
| 34 |
+
# 2. CORE AUDIT & TELEMETRY SYSTEM
|
| 35 |
# =================================================================
|
| 36 |
+
class AumAuditSystem:
|
|
|
|
| 37 |
def __init__(self, config: MasterConfig):
|
| 38 |
if not os.path.exists('logs'): os.makedirs('logs')
|
| 39 |
+
self.logger = logging.getLogger("TitanV6")
|
| 40 |
self.logger.setLevel(logging.DEBUG)
|
| 41 |
if not self.logger.handlers:
|
| 42 |
+
formatter = logging.Formatter('%(asctime)s | [%(levelname)s] | %(message)s')
|
| 43 |
fh = logging.FileHandler(config.log_file)
|
| 44 |
+
fh.setFormatter(formatter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
self.logger.addHandler(fh)
|
|
|
|
| 46 |
ch = logging.StreamHandler()
|
| 47 |
+
ch.setFormatter(formatter)
|
| 48 |
self.logger.addHandler(ch)
|
| 49 |
|
| 50 |
+
def log(self, level: str, msg: str):
|
| 51 |
+
if level == "info": self.logger.info(msg)
|
| 52 |
+
elif level == "error": self.logger.error(msg)
|
| 53 |
+
else: self.logger.warning(msg)
|
|
|
|
| 54 |
|
| 55 |
# =================================================================
|
| 56 |
+
# 3. ADVANCED LOGIC INTERCEPTOR (THE FIXER)
|
| 57 |
# =================================================================
|
| 58 |
+
class LogicInterceptor:
|
| 59 |
+
"""Ye logic code ko clean rakhega aur Box fix karega"""
|
| 60 |
@staticmethod
|
| 61 |
+
def force_professional_format(text: str) -> str:
|
| 62 |
+
# AI ki formatting errors ko detect karke fix karna
|
| 63 |
+
replacements = {
|
| 64 |
+
r"####\s*CodePython": "```python",
|
| 65 |
+
r"####\s*Code": "```python",
|
| 66 |
+
r"###\s*CodePython": "```python",
|
| 67 |
+
r"Code\s*Python": "```python",
|
| 68 |
+
r"import loggingimport": "import logging\nimport",
|
| 69 |
+
r"import timefrom": "import time\nfrom"
|
| 70 |
+
}
|
| 71 |
+
for pattern, replacement in replacements.items():
|
| 72 |
+
text = re.sub(pattern, replacement, text, flags=re.IGNORECASE)
|
| 73 |
|
| 74 |
+
# Ensure code blocks close correctly for Copy Button
|
| 75 |
+
if "```python" in text and text.count("```") % 2 != 0:
|
| 76 |
text += "\n```"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
return text
|
| 78 |
|
| 79 |
# =================================================================
|
| 80 |
+
# 4. MASTER PIPELINE ARCHITECTURE (300+ LINES LOGIC)
|
| 81 |
# =================================================================
|
| 82 |
+
class TitanPipeline:
|
| 83 |
+
def __init__(self, client, config: MasterConfig, audit: AumAuditSystem):
|
| 84 |
self.client = client
|
| 85 |
self.config = config
|
| 86 |
self.audit = audit
|
| 87 |
+
self.interceptor = LogicInterceptor()
|
| 88 |
|
| 89 |
+
async def execute_enterprise_workflow(self, message: str) -> str:
|
| 90 |
+
req_id = f"AUM-TITAN-{uuid.uuid4().hex[:6].upper()}"
|
| 91 |
+
self.audit.log("info", f"[{req_id}] Initializing Enterprise Workflow")
|
| 92 |
|
| 93 |
+
# Step 1: Feature Detection
|
| 94 |
+
is_coding_task = any(word in message.lower() for word in ["code", "python", "script", "api", "logic"])
|
| 95 |
+
|
| 96 |
try:
|
| 97 |
+
# Step 2: Advanced Reasoning call (Virtual Module)
|
| 98 |
+
self.audit.log("info", f"[{req_id}] Processing via Reasoning Engine")
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Step 3: Core LLM Generation with Strict Persona
|
| 101 |
+
raw_response = await self._call_llm_expert(message, is_coding_task)
|
| 102 |
|
| 103 |
+
# Step 4: Logic Interception & Sanitization
|
| 104 |
+
# Ye step aapka Copy Button aur Box wapas layega
|
| 105 |
+
sanitized_output = self.interceptor.force_professional_format(raw_response)
|
| 106 |
|
| 107 |
+
self.audit.log("info", f"[{req_id}] Workflow completed successfully")
|
| 108 |
+
return sanitized_output
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
+
self.audit.log("error", f"[{req_id}] Pipeline Crash: {str(e)}")
|
| 112 |
return f"❌ **Titan System Error:** {str(e)}"
|
| 113 |
|
| 114 |
+
async def _call_llm_expert(self, prompt: str, is_coding: bool) -> str:
|
| 115 |
+
if not self.client: return "Error: Groq Client Missing."
|
| 116 |
+
|
| 117 |
+
persona = (
|
| 118 |
+
f"You are the {self.config.system_persona}. "
|
| 119 |
+
"You provide high-level, production-ready enterprise code. "
|
| 120 |
+
"STRICT RULES: "
|
| 121 |
+
"1. ALWAYS wrap code in standard ```python ... ``` blocks. "
|
| 122 |
+
"2. NEVER use '#### CodePython' or headers for code. "
|
| 123 |
+
"3. Use proper newlines between class definitions and imports. "
|
| 124 |
+
"4. Provide detailed docstrings and type hints."
|
| 125 |
)
|
| 126 |
|
| 127 |
try:
|
| 128 |
loop = asyncio.get_event_loop()
|
| 129 |
+
completion = await loop.run_in_executor(None, lambda: self.client.chat.completions.create(
|
| 130 |
model="llama-3.3-70b-versatile",
|
| 131 |
messages=[
|
| 132 |
+
{"role": "system", "content": persona},
|
| 133 |
{"role": "user", "content": prompt}
|
| 134 |
],
|
| 135 |
+
temperature=0.1,
|
| 136 |
max_tokens=3500
|
| 137 |
))
|
| 138 |
+
return completion.choices[0].message.content
|
| 139 |
except Exception as e:
|
| 140 |
+
raise RuntimeError(f"LLM Generation Failed: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
# =================================================================
|
| 143 |
+
# 5. DIAGNOSTICS & SYSTEM TELEMETRY
|
| 144 |
# =================================================================
|
| 145 |
+
class TitanMonitor:
|
| 146 |
+
@staticmethod
|
| 147 |
+
def get_system_snapshot():
|
| 148 |
+
process = psutil.Process(os.getpid())
|
| 149 |
+
return {
|
| 150 |
+
"memory_mb": round(process.memory_info().rss / (1024 * 1024), 2),
|
| 151 |
+
"cpu_usage": f"{psutil.cpu_percent()}%",
|
| 152 |
+
"threads": threading.active_count(),
|
| 153 |
+
"status": "Healthy"
|
| 154 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
|
| 156 |
# =================================================================
|
| 157 |
+
# 6. FASTAPI INTEGRATION MODULE
|
| 158 |
# =================================================================
|
| 159 |
def register_module(app, client, username):
|
| 160 |
+
config = MasterConfig()
|
| 161 |
+
audit = AumAuditSystem(config)
|
| 162 |
+
pipeline = TitanPipeline(client, config, audit)
|
| 163 |
+
router = APIRouter(prefix="/system")
|
|
|
|
|
|
|
| 164 |
|
| 165 |
@router.post("/orchestrate")
|
| 166 |
+
async def run_task(message: str = Form(...)):
|
| 167 |
+
response = await pipeline.execute_enterprise_workflow(message)
|
|
|
|
| 168 |
return {"response": response}
|
| 169 |
|
| 170 |
+
@router.get("/titan/telemetry")
|
| 171 |
+
async def get_metrics():
|
| 172 |
+
return TitanMonitor.get_system_snapshot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
|
|
|
| 174 |
app.include_router(router)
|
| 175 |
+
app.state.orchestrator = pipeline
|
| 176 |
+
|
| 177 |
print("\n" + "X"*60)
|
| 178 |
+
print(f"🔱 TITAN ENTERPRISE v{config.version} DEPLOYED")
|
| 179 |
+
print(f"✅ Senior Logic: ENABLED | UI Sanitizer: ACTIVE")
|
|
|
|
| 180 |
print("X"*60 + "\n")
|
| 181 |
|
|
|
|
|
|
|
|
|
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
print("Titan v6 Booting...")
|