Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py - ULTIMATE FINAL VERSION -
|
| 2 |
|
| 3 |
import os
|
| 4 |
import sys
|
|
@@ -52,12 +52,18 @@ class ModuleManager:
|
|
| 52 |
"""Load module configuration from JSON"""
|
| 53 |
config_file = self.config.CONFIG_DIR / "modules.json"
|
| 54 |
default_config = {
|
| 55 |
-
"enabled_modules": ["
|
|
|
|
|
|
|
| 56 |
"auto_start": True,
|
| 57 |
"module_settings": {
|
| 58 |
-
"
|
| 59 |
"testing": {"auto_test": False, "test_on_startup": True},
|
| 60 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
|
@@ -684,28 +690,9 @@ async def reset():
|
|
| 684 |
except Exception as e:
|
| 685 |
return {"success": False, "message": f"Reset error: {str(e)}"}
|
| 686 |
|
| 687 |
-
# Check for code generation requests - ENHANCED
|
| 688 |
-
msg_lower = message.lower()
|
| 689 |
-
CODE_KEYWORDS = ["python", "code", "write", "generate", "create", "script",
|
| 690 |
-
"program", "function", "mount", "colab", "coding", "debug",
|
| 691 |
-
"algorithm", "import", "def ", "class ", "for loop", "while"]
|
| 692 |
-
|
| 693 |
-
if any(k in msg_lower for k in CODE_KEYWORDS):
|
| 694 |
-
# NEW: Try expert coding module first
|
| 695 |
-
code_module = app.state.module_manager.get_module("code_intelligence")
|
| 696 |
-
if code_module and hasattr(code_module, 'process_code_request'):
|
| 697 |
-
try:
|
| 698 |
-
expert_response = await code_module.process_code_request(message, client)
|
| 699 |
-
try:
|
| 700 |
-
tidb_memory.save_chat(message, expert_response, lang_mode)
|
| 701 |
-
except:
|
| 702 |
-
pass
|
| 703 |
-
return {"response": expert_response}
|
| 704 |
-
except Exception as e:
|
| 705 |
-
print(f"⚠️ Expert coding failed: {e}")
|
| 706 |
@app.post("/chat")
|
| 707 |
async def chat(message: str = Form(...)):
|
| 708 |
-
"""Main chat endpoint"""
|
| 709 |
if not app.state.groq_available:
|
| 710 |
return {"response": "Error: Groq API not configured."}
|
| 711 |
|
|
@@ -715,6 +702,34 @@ async def chat(message: str = Form(...)):
|
|
| 715 |
except ImportError as e:
|
| 716 |
return {"response": f"Error: {str(e)}"}
|
| 717 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
lang_mode = detect_input_language(message)
|
| 719 |
system_prompt = get_system_prompt(lang_mode, AumCoreConfig.USERNAME)
|
| 720 |
|
|
@@ -753,52 +768,6 @@ async def chat(message: str = Form(...)):
|
|
| 753 |
|
| 754 |
except Exception as e:
|
| 755 |
return {"response": f"Error: {str(e)}"}
|
| 756 |
-
|
| 757 |
-
# Fallback to basic code
|
| 758 |
-
code_response = generate_basic_code(message)
|
| 759 |
-
try:
|
| 760 |
-
tidb_memory.save_chat(message, code_response, lang_mode)
|
| 761 |
-
except Exception as e:
|
| 762 |
-
print(f"⚠️ TiDB save error: {e}")
|
| 763 |
-
return {"response": code_response}
|
| 764 |
-
|
| 765 |
-
# Get chat history
|
| 766 |
-
recent_chats = []
|
| 767 |
-
try:
|
| 768 |
-
recent_chats = tidb_memory.get_recent_chats(limit=10)
|
| 769 |
-
except Exception as e:
|
| 770 |
-
print(f"⚠️ TiDB history fetch error: {e}")
|
| 771 |
-
|
| 772 |
-
# Prepare messages for Groq
|
| 773 |
-
api_messages = [{"role": "system", "content": system_prompt}]
|
| 774 |
-
for chat_row in recent_chats:
|
| 775 |
-
user_input, ai_response, _ = chat_row
|
| 776 |
-
api_messages.append({"role": "user", "content": user_input})
|
| 777 |
-
api_messages.append({"role": "assistant", "content": ai_response})
|
| 778 |
-
api_messages.append({"role": "user", "content": message})
|
| 779 |
-
|
| 780 |
-
# Call Groq API
|
| 781 |
-
try:
|
| 782 |
-
completion = client.chat.completions.create(
|
| 783 |
-
model="llama-3.3-70b-versatile",
|
| 784 |
-
messages=api_messages,
|
| 785 |
-
temperature=0.3,
|
| 786 |
-
max_tokens=1000
|
| 787 |
-
)
|
| 788 |
-
ai_response = completion.choices[0].message.content.strip()
|
| 789 |
-
|
| 790 |
-
# Save to database
|
| 791 |
-
try:
|
| 792 |
-
tidb_memory.save_chat(message, ai_response, lang_mode)
|
| 793 |
-
except Exception as e:
|
| 794 |
-
print(f"⚠️ TiDB save error: {e}")
|
| 795 |
-
|
| 796 |
-
return {"response": ai_response}
|
| 797 |
-
|
| 798 |
-
except Exception as e:
|
| 799 |
-
error_msg = f"System Error: {str(e)}"
|
| 800 |
-
print(f"❌ API Error: {error_msg}")
|
| 801 |
-
return {"response": error_msg}
|
| 802 |
|
| 803 |
# ============================================
|
| 804 |
# 7. SYSTEM MANAGEMENT ENDPOINTS
|
|
@@ -814,7 +783,7 @@ async def system_health():
|
|
| 814 |
"status": "OPERATIONAL",
|
| 815 |
"modules_loaded": len(app.state.module_manager.loaded_modules),
|
| 816 |
"groq_available": app.state.groq_available,
|
| 817 |
-
"health_score": 95
|
| 818 |
}
|
| 819 |
|
| 820 |
# Add module-specific health if available
|
|
@@ -860,9 +829,10 @@ async def system_info():
|
|
| 860 |
"code_generation": True,
|
| 861 |
"hindi_english": True,
|
| 862 |
"memory_storage": True,
|
| 863 |
-
"system_monitoring": "
|
| 864 |
"automated_testing": "testing" in app.state.module_manager.loaded_modules,
|
| 865 |
-
"task_orchestration": "orchestrator" in app.state.module_manager.loaded_modules
|
|
|
|
| 866 |
},
|
| 867 |
"endpoints": [
|
| 868 |
"/", "/chat", "/reset",
|
|
|
|
| 1 |
+
# app.py - ULTIMATE FINAL VERSION - EXPERT CODING ENABLED
|
| 2 |
|
| 3 |
import os
|
| 4 |
import sys
|
|
|
|
| 52 |
"""Load module configuration from JSON"""
|
| 53 |
config_file = self.config.CONFIG_DIR / "modules.json"
|
| 54 |
default_config = {
|
| 55 |
+
"enabled_modules": ["orchestrator", "testing", "sys_diagnostics",
|
| 56 |
+
"code_formatter", "prompt_manager",
|
| 57 |
+
"code_intelligence", "code_reviewer"],
|
| 58 |
"auto_start": True,
|
| 59 |
"module_settings": {
|
| 60 |
+
"orchestrator": {"enabled": True, "background_tasks": True},
|
| 61 |
"testing": {"auto_test": False, "test_on_startup": True},
|
| 62 |
+
"sys_diagnostics": {"auto_run": True, "interval_minutes": 60},
|
| 63 |
+
"code_formatter": {"enabled": True, "auto_format": True},
|
| 64 |
+
"prompt_manager": {"enabled": True, "auto_optimize": True},
|
| 65 |
+
"code_intelligence": {"enabled": True, "auto_analyze": True},
|
| 66 |
+
"code_reviewer": {"enabled": True, "auto_review": False}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
|
|
|
| 690 |
except Exception as e:
|
| 691 |
return {"success": False, "message": f"Reset error: {str(e)}"}
|
| 692 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
@app.post("/chat")
|
| 694 |
async def chat(message: str = Form(...)):
|
| 695 |
+
"""Main chat endpoint WITH EXPERT CODING"""
|
| 696 |
if not app.state.groq_available:
|
| 697 |
return {"response": "Error: Groq API not configured."}
|
| 698 |
|
|
|
|
| 702 |
except ImportError as e:
|
| 703 |
return {"response": f"Error: {str(e)}"}
|
| 704 |
|
| 705 |
+
# CHECK FOR CODING QUERY FIRST
|
| 706 |
+
msg_lower = message.lower()
|
| 707 |
+
CODING_KEYWORDS = ["python", "code", "script", "function", "program",
|
| 708 |
+
"create", "write", "generate", "algorithm", "debug",
|
| 709 |
+
"class", "import", "def", "for loop", "while", "dictionary",
|
| 710 |
+
"list", "array", "json", "api", "database", "file handling"]
|
| 711 |
+
|
| 712 |
+
# If coding query, use expert modules
|
| 713 |
+
if any(keyword in msg_lower for keyword in CODING_KEYWORDS):
|
| 714 |
+
# Try code_intelligence module first
|
| 715 |
+
code_module = app.state.module_manager.get_module("code_intelligence")
|
| 716 |
+
if code_module and hasattr(code_module, 'enhance_code_response'):
|
| 717 |
+
try:
|
| 718 |
+
# Get enhanced code from expert module
|
| 719 |
+
enhanced_response = await code_module.enhance_code_response(message, client)
|
| 720 |
+
|
| 721 |
+
# Save to database
|
| 722 |
+
try:
|
| 723 |
+
tidb_memory.save_chat(message, enhanced_response, "en")
|
| 724 |
+
except:
|
| 725 |
+
pass
|
| 726 |
+
|
| 727 |
+
return {"response": enhanced_response}
|
| 728 |
+
except Exception as e:
|
| 729 |
+
print(f"⚠️ Expert coding failed: {e}")
|
| 730 |
+
# Fall through to normal flow
|
| 731 |
+
|
| 732 |
+
# NORMAL CHAT FLOW
|
| 733 |
lang_mode = detect_input_language(message)
|
| 734 |
system_prompt = get_system_prompt(lang_mode, AumCoreConfig.USERNAME)
|
| 735 |
|
|
|
|
| 768 |
|
| 769 |
except Exception as e:
|
| 770 |
return {"response": f"Error: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
|
| 772 |
# ============================================
|
| 773 |
# 7. SYSTEM MANAGEMENT ENDPOINTS
|
|
|
|
| 783 |
"status": "OPERATIONAL",
|
| 784 |
"modules_loaded": len(app.state.module_manager.loaded_modules),
|
| 785 |
"groq_available": app.state.groq_available,
|
| 786 |
+
"health_score": 95
|
| 787 |
}
|
| 788 |
|
| 789 |
# Add module-specific health if available
|
|
|
|
| 829 |
"code_generation": True,
|
| 830 |
"hindi_english": True,
|
| 831 |
"memory_storage": True,
|
| 832 |
+
"system_monitoring": "sys_diagnostics" in app.state.module_manager.loaded_modules,
|
| 833 |
"automated_testing": "testing" in app.state.module_manager.loaded_modules,
|
| 834 |
+
"task_orchestration": "orchestrator" in app.state.module_manager.loaded_modules,
|
| 835 |
+
"expert_coding": "code_intelligence" in app.state.module_manager.loaded_modules
|
| 836 |
},
|
| 837 |
"endpoints": [
|
| 838 |
"/", "/chat", "/reset",
|