Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,9 @@ import asyncio
|
|
| 7 |
import importlib.util
|
| 8 |
import json
|
| 9 |
from pathlib import Path
|
| 10 |
-
from fastapi import FastAPI, Form
|
| 11 |
-
from fastapi.responses import HTMLResponse
|
|
|
|
| 12 |
from groq import Groq
|
| 13 |
|
| 14 |
# ============================================
|
|
@@ -132,29 +133,70 @@ class ModuleManager:
|
|
| 132 |
}
|
| 133 |
|
| 134 |
# ============================================
|
| 135 |
-
# 3.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
# ============================================
|
| 137 |
|
| 138 |
app = FastAPI(
|
| 139 |
title="AumCore AI",
|
| 140 |
description="Advanced Modular AI Assistant System",
|
| 141 |
-
version=AumCoreConfig.VERSION
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
# Initialize Groq client
|
| 145 |
try:
|
| 146 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 147 |
GROQ_AVAILABLE = True
|
|
|
|
| 148 |
except Exception as e:
|
| 149 |
print(f"β οΈ Groq client initialization failed: {e}")
|
| 150 |
client = None
|
| 151 |
GROQ_AVAILABLE = False
|
|
|
|
| 152 |
|
| 153 |
# Initialize Module Manager
|
| 154 |
module_manager = ModuleManager(app, client)
|
|
|
|
| 155 |
|
| 156 |
# ============================================
|
| 157 |
-
#
|
| 158 |
# ============================================
|
| 159 |
|
| 160 |
HTML_UI = '''
|
|
@@ -621,7 +663,7 @@ document.addEventListener('DOMContentLoaded',()=>{const input=document.getElemen
|
|
| 621 |
'''
|
| 622 |
|
| 623 |
# ============================================
|
| 624 |
-
#
|
| 625 |
# ============================================
|
| 626 |
|
| 627 |
@app.get("/", response_class=HTMLResponse)
|
|
@@ -710,7 +752,7 @@ async def chat(message: str = Form(...)):
|
|
| 710 |
return {"response": error_msg}
|
| 711 |
|
| 712 |
# ============================================
|
| 713 |
-
#
|
| 714 |
# ============================================
|
| 715 |
|
| 716 |
@app.get("/system/health")
|
|
@@ -721,13 +763,13 @@ async def system_health():
|
|
| 721 |
"timestamp": asyncio.get_event_loop().time(),
|
| 722 |
"version": AumCoreConfig.VERSION,
|
| 723 |
"status": "OPERATIONAL",
|
| 724 |
-
"modules_loaded": len(module_manager.loaded_modules),
|
| 725 |
-
"groq_available":
|
| 726 |
"health_score": 95 # Default high score
|
| 727 |
}
|
| 728 |
|
| 729 |
# Add module-specific health if available
|
| 730 |
-
diagnostics_module = module_manager.get_module("sys_diagnostics")
|
| 731 |
if diagnostics_module and hasattr(diagnostics_module, 'get_health'):
|
| 732 |
try:
|
| 733 |
module_health = await diagnostics_module.get_health()
|
|
@@ -742,14 +784,14 @@ async def modules_status():
|
|
| 742 |
"""Get status of all loaded modules"""
|
| 743 |
return {
|
| 744 |
"success": True,
|
| 745 |
-
"total": len(module_manager.loaded_modules),
|
| 746 |
"modules": [
|
| 747 |
{
|
| 748 |
"name": name,
|
| 749 |
"status": info["status"],
|
| 750 |
"active": True
|
| 751 |
}
|
| 752 |
-
for name, info in module_manager.loaded_modules.items()
|
| 753 |
]
|
| 754 |
}
|
| 755 |
|
|
@@ -769,9 +811,9 @@ async def system_info():
|
|
| 769 |
"code_generation": True,
|
| 770 |
"hindi_english": True,
|
| 771 |
"memory_storage": True,
|
| 772 |
-
"system_monitoring": "diagnostics" in module_manager.loaded_modules,
|
| 773 |
-
"automated_testing": "testing" in module_manager.loaded_modules,
|
| 774 |
-
"task_orchestration": "orchestrator" in module_manager.loaded_modules
|
| 775 |
},
|
| 776 |
"endpoints": [
|
| 777 |
"/", "/chat", "/reset",
|
|
@@ -779,41 +821,6 @@ async def system_info():
|
|
| 779 |
]
|
| 780 |
}
|
| 781 |
|
| 782 |
-
# ============================================
|
| 783 |
-
# 7. STARTUP AND SHUTDOWN EVENTS
|
| 784 |
-
# ============================================
|
| 785 |
-
|
| 786 |
-
@app.on_event("startup")
|
| 787 |
-
async def startup_event():
|
| 788 |
-
"""Initialize system on startup"""
|
| 789 |
-
print("=" * 60)
|
| 790 |
-
print("π AUMCORE AI - ULTIMATE FINAL VERSION")
|
| 791 |
-
print("=" * 60)
|
| 792 |
-
print(f"π Version: {AumCoreConfig.VERSION}")
|
| 793 |
-
print(f"π€ Username: {AumCoreConfig.USERNAME}")
|
| 794 |
-
print(f"π Server: http://{AumCoreConfig.HOST}:{AumCoreConfig.PORT}")
|
| 795 |
-
print(f"π€ AI Model: llama-3.3-70b-versatile")
|
| 796 |
-
print(f"πΎ Database: TiDB Cloud")
|
| 797 |
-
print(f"π¨ UI Features: Code formatting + Copy button")
|
| 798 |
-
|
| 799 |
-
# Load all modules
|
| 800 |
-
module_manager.load_all_modules()
|
| 801 |
-
|
| 802 |
-
# Initial health check
|
| 803 |
-
print("\nπ Initial System Check:")
|
| 804 |
-
print(f" Groq API: {'β
Available' if GROQ_AVAILABLE else 'β Not Available'}")
|
| 805 |
-
print(f" Modules: {len(module_manager.loaded_modules)} loaded")
|
| 806 |
-
print(f" Directories: All created")
|
| 807 |
-
print("=" * 60)
|
| 808 |
-
print("β
System ready! Waiting for requests...")
|
| 809 |
-
print("=" * 60)
|
| 810 |
-
|
| 811 |
-
@app.on_event("shutdown")
|
| 812 |
-
async def shutdown_event():
|
| 813 |
-
"""Cleanup on shutdown"""
|
| 814 |
-
print("\nπ System shutting down...")
|
| 815 |
-
print("β
Cleanup completed")
|
| 816 |
-
|
| 817 |
# ============================================
|
| 818 |
# 8. MAIN EXECUTION
|
| 819 |
# ============================================
|
|
|
|
| 7 |
import importlib.util
|
| 8 |
import json
|
| 9 |
from pathlib import Path
|
| 10 |
+
from fastapi import FastAPI, Form
|
| 11 |
+
from fastapi.responses import HTMLResponse
|
| 12 |
+
from contextlib import asynccontextmanager
|
| 13 |
from groq import Groq
|
| 14 |
|
| 15 |
# ============================================
|
|
|
|
| 133 |
}
|
| 134 |
|
| 135 |
# ============================================
|
| 136 |
+
# 3. LIFESPAN MANAGEMENT (MODERN APPROACH)
|
| 137 |
+
# ============================================
|
| 138 |
+
|
| 139 |
+
@asynccontextmanager
|
| 140 |
+
async def lifespan(app: FastAPI):
|
| 141 |
+
"""Modern lifespan handler for startup/shutdown events"""
|
| 142 |
+
# Startup code
|
| 143 |
+
print("=" * 60)
|
| 144 |
+
print("π AUMCORE AI - ULTIMATE FINAL VERSION")
|
| 145 |
+
print("=" * 60)
|
| 146 |
+
print(f"π Version: {AumCoreConfig.VERSION}")
|
| 147 |
+
print(f"π€ Username: {AumCoreConfig.USERNAME}")
|
| 148 |
+
print(f"π Server: http://{AumCoreConfig.HOST}:{AumCoreConfig.PORT}")
|
| 149 |
+
print(f"π€ AI Model: llama-3.3-70b-versatile")
|
| 150 |
+
print(f"πΎ Database: TiDB Cloud")
|
| 151 |
+
print(f"π¨ UI Features: Code formatting + Copy button")
|
| 152 |
+
|
| 153 |
+
# Load all modules
|
| 154 |
+
if hasattr(app.state, 'module_manager'):
|
| 155 |
+
app.state.module_manager.load_all_modules()
|
| 156 |
+
|
| 157 |
+
# Initial health check
|
| 158 |
+
print("\nπ Initial System Check:")
|
| 159 |
+
print(f" Groq API: {'β
Available' if hasattr(app.state, 'groq_available') and app.state.groq_available else 'β Not Available'}")
|
| 160 |
+
print(f" Modules: {len(app.state.module_manager.loaded_modules) if hasattr(app.state, 'module_manager') else 0} loaded")
|
| 161 |
+
print(f" Directories: All created")
|
| 162 |
+
print("=" * 60)
|
| 163 |
+
print("β
System ready! Waiting for requests...")
|
| 164 |
+
print("=" * 60)
|
| 165 |
+
|
| 166 |
+
yield # Application runs here
|
| 167 |
+
|
| 168 |
+
# Shutdown code
|
| 169 |
+
print("\nπ System shutting down...")
|
| 170 |
+
print("β
Cleanup completed")
|
| 171 |
+
|
| 172 |
+
# ============================================
|
| 173 |
+
# 4. CORE FASTAPI APPLICATION
|
| 174 |
# ============================================
|
| 175 |
|
| 176 |
app = FastAPI(
|
| 177 |
title="AumCore AI",
|
| 178 |
description="Advanced Modular AI Assistant System",
|
| 179 |
+
version=AumCoreConfig.VERSION,
|
| 180 |
+
lifespan=lifespan
|
| 181 |
)
|
| 182 |
|
| 183 |
# Initialize Groq client
|
| 184 |
try:
|
| 185 |
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 186 |
GROQ_AVAILABLE = True
|
| 187 |
+
app.state.groq_available = True
|
| 188 |
except Exception as e:
|
| 189 |
print(f"β οΈ Groq client initialization failed: {e}")
|
| 190 |
client = None
|
| 191 |
GROQ_AVAILABLE = False
|
| 192 |
+
app.state.groq_available = False
|
| 193 |
|
| 194 |
# Initialize Module Manager
|
| 195 |
module_manager = ModuleManager(app, client)
|
| 196 |
+
app.state.module_manager = module_manager
|
| 197 |
|
| 198 |
# ============================================
|
| 199 |
+
# 5. CORE UI (NEVER CHANGES)
|
| 200 |
# ============================================
|
| 201 |
|
| 202 |
HTML_UI = '''
|
|
|
|
| 663 |
'''
|
| 664 |
|
| 665 |
# ============================================
|
| 666 |
+
# 6. CORE ENDPOINTS (NEVER CHANGES)
|
| 667 |
# ============================================
|
| 668 |
|
| 669 |
@app.get("/", response_class=HTMLResponse)
|
|
|
|
| 752 |
return {"response": error_msg}
|
| 753 |
|
| 754 |
# ============================================
|
| 755 |
+
# 7. SYSTEM MANAGEMENT ENDPOINTS
|
| 756 |
# ============================================
|
| 757 |
|
| 758 |
@app.get("/system/health")
|
|
|
|
| 763 |
"timestamp": asyncio.get_event_loop().time(),
|
| 764 |
"version": AumCoreConfig.VERSION,
|
| 765 |
"status": "OPERATIONAL",
|
| 766 |
+
"modules_loaded": len(app.state.module_manager.loaded_modules),
|
| 767 |
+
"groq_available": app.state.groq_available,
|
| 768 |
"health_score": 95 # Default high score
|
| 769 |
}
|
| 770 |
|
| 771 |
# Add module-specific health if available
|
| 772 |
+
diagnostics_module = app.state.module_manager.get_module("sys_diagnostics")
|
| 773 |
if diagnostics_module and hasattr(diagnostics_module, 'get_health'):
|
| 774 |
try:
|
| 775 |
module_health = await diagnostics_module.get_health()
|
|
|
|
| 784 |
"""Get status of all loaded modules"""
|
| 785 |
return {
|
| 786 |
"success": True,
|
| 787 |
+
"total": len(app.state.module_manager.loaded_modules),
|
| 788 |
"modules": [
|
| 789 |
{
|
| 790 |
"name": name,
|
| 791 |
"status": info["status"],
|
| 792 |
"active": True
|
| 793 |
}
|
| 794 |
+
for name, info in app.state.module_manager.loaded_modules.items()
|
| 795 |
]
|
| 796 |
}
|
| 797 |
|
|
|
|
| 811 |
"code_generation": True,
|
| 812 |
"hindi_english": True,
|
| 813 |
"memory_storage": True,
|
| 814 |
+
"system_monitoring": "diagnostics" in app.state.module_manager.loaded_modules,
|
| 815 |
+
"automated_testing": "testing" in app.state.module_manager.loaded_modules,
|
| 816 |
+
"task_orchestration": "orchestrator" in app.state.module_manager.loaded_modules
|
| 817 |
},
|
| 818 |
"endpoints": [
|
| 819 |
"/", "/chat", "/reset",
|
|
|
|
| 821 |
]
|
| 822 |
}
|
| 823 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 824 |
# ============================================
|
| 825 |
# 8. MAIN EXECUTION
|
| 826 |
# ============================================
|