File size: 8,077 Bytes
068bc7f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #!/usr/bin/env python3
"""
Stack 2.9 - Full Enhanced Version with All Features
"""
import os
os.environ['HF_HUB_DISABLE_PROGRESS_BARS'] = '1'
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent / "src"))
import torch
# Enhancement modules
from enhancements.nlp import IntentDetector, EntityRecognizer
from enhancements.knowledge_graph import RAGEngine
from enhancements.emotional_intelligence import SentimentAnalyzer, EmpathyEngine
from enhancements.collaboration import ConversationStateManager
from enhancements.learning import FeedbackCollector, PerformanceMonitor
from enhancements.technical import DevOpsTools, CodeAnalyzer, DebuggingAssistant
from enhancements import get_config
# Load model
model_path = Path("/Users/walidsobhi/stack-2-9-final-model")
print("=" * 50)
print("Stack 2.9 - Enhanced Edition")
print("=" * 50)
# Initialize enhancement modules
print("\n[1/4] Loading NLP modules...")
intent_detector = IntentDetector()
entity_recognizer = EntityRecognizer()
print(" β Intent Detection")
print(" β Entity Recognition")
print("\n[2/4] Loading Knowledge Graph...")
rag_engine = RAGEngine()
rag_engine.add_document("intro", "Stack 2.9 is an AI coding assistant trained on code and technical content")
rag_engine.add_document("commands", "Commands: help, debug, analyze, devops, quit")
print(" β RAG Engine")
print("\n[3/4] Loading Emotional Intelligence...")
sentiment_analyzer = SentimentAnalyzer()
empathy_engine = EmpathyEngine()
print(" β Sentiment Analysis")
print(" β Empathy Engine")
print("\n[4/4] Loading Technical Capabilities...")
devops_tools = DevOpsTools()
code_analyzer = CodeAnalyzer()
debugging_assistant = DebuggingAssistant()
print(" β DevOps Tools")
print(" β Code Analyzer")
print(" β Debugging Assistant")
# Other systems
conversation_manager = ConversationStateManager()
feedback_collector = FeedbackCollector()
performance_monitor = PerformanceMonitor()
print("\n" + "=" * 50)
# Load model
print("\nLoading model...")
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
str(model_path),
torch_dtype=torch.float16,
device_map="cpu",
local_files_only=True
)
tokenizer = AutoTokenizer.from_pretrained(
str(model_path),
local_files_only=True
)
tokenizer.pad_token = "<|endoftext|>"
if torch.cuda.is_available():
model = model.to("cuda")
# Setup session
conversation_manager.create_session()
performance_monitor.increment_session_count()
print("β Stack 2.9 Ready!\n")
# Demo function
def demo_feature(name, func):
print(f"\n--- {name} ---")
try:
result = func()
print(result)
except Exception as e:
print(f"Error: {e}")
# Interactive chat
while True:
try:
print("\n" + "=" * 40)
print("Commands: test, debug <error>, analyze <code>, devops, quit")
user_input = input("\nYou: ").strip()
if not user_input:
continue
if user_input.lower() in ['quit', 'exit', 'q']:
break
# Handle special commands
if user_input.lower() == 'test':
print("\n=== TESTING ALL ENHANCEMENTS ===\n")
# Test Intent Detection
demo_feature("Intent Detection", lambda: intent_detector.detect_intent("Write a function to calculate fibonacci"))
# Test Entity Recognition
demo_feature("Entity Recognition", lambda: entity_recognizer.recognize_entities("My email is test@example.com"))
# Test Sentiment
demo_feature("Sentiment Analysis", lambda: sentiment_analyzer.analyze_sentiment("I'm frustrated with this bug"))
# Test RAG
demo_feature("RAG Context", lambda: rag_engine.retrieve_as_context("what can you do", 200))
# Test Code Analysis
sample_code = "def hello():\n print('hello')\n x = 1"
demo_feature("Code Analysis", lambda: code_analyzer.get_code_summary(sample_code))
# Test DevOps
demo_feature("DevOps - Docker", lambda: devops_tools.generate_dockerfile("python", "3.11"))
# Test Debugging
demo_feature("Debugging", lambda: debugging_assistant.analyze_error("NameError: name 'x' is not defined"))
print("\n=== ALL TESTS COMPLETE ===")
continue
# Debug command
if user_input.lower().startswith("debug "):
error = user_input[6:]
analysis = debugging_assistant.analyze_error(error)
print(f"\nError Type: {analysis['error_type']}")
print(f"Description: {analysis['description']}")
print("\nCommon Causes:")
for cause in analysis['common_causes']:
print(f" - {cause}")
print("\nSuggested Fixes:")
for fix in analysis['suggested_fixes']:
print(f" - {fix}")
continue
# Analyze code command
if user_input.lower().startswith("analyze "):
code = user_input[8:]
summary = code_analyzer.get_code_summary(code)
print(f"\nLanguage: {summary['language']}")
print(f"Lines of Code: {summary['complexity']['lines_of_code']}")
print(f"Complexity: {summary['complexity']['cyclomatic_complexity']}")
print(f"Maintainability: {summary['maintainability_index']:.1f}/100")
if summary['issues']:
print("Issues:")
for issue in summary['issues'][:3]:
print(f" - {issue['type']}: {issue['message']}")
continue
# DevOps command
if user_input.lower().startswith("devops"):
parts = user_input.split()
if len(parts) > 1:
template = devops_tools.generate_dockerfile(parts[1] if len(parts) > 1 else "python")
else:
template = devops_tools.generate_dockerfile()
print(f"\n{template}")
continue
# Normal chat with enhancements
# 1. Detect intent
intent = intent_detector.detect_intent(user_input)
# 2. Detect sentiment
sentiment = sentiment_analyzer.analyze_sentiment(user_input)
# 3. Get RAG context
rag_context = rag_engine.retrieve_as_context(user_input, 300)
# Build prompt with enhancements
prompt_parts = ["You are Stack 2.9, an expert AI coding assistant."]
if rag_context:
prompt_parts.append(f"Context: {rag_context}")
# Add emotional tone guidance
if sentiment['sentiment'] == 'negative':
prompt_parts.append("Be empathetic and understanding.")
elif sentiment['sentiment'] == 'positive':
prompt_parts.append("Be enthusiastic and helpful.")
prompt_parts.append(f"\n\nUser: {user_input}\nAssistant:")
full_prompt = "\n".join(prompt_parts)
# Generate
inputs = tokenizer(full_prompt, return_tensors='pt')
if torch.cuda.is_available():
inputs = inputs.to("cuda")
outputs = model.generate(
**inputs,
max_new_tokens=150,
temperature=0.4,
pad_token_id=tokenizer.eos_token_id
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
if "Assistant:" in response:
response = response.split("Assistant:")[-1].strip()
# Apply empathy if needed
if sentiment['sentiment'] == 'negative':
response = empathy_engine.generate_empathetic_response(user_input, response)
print(f"\n[Intent: {intent['intent']}] [Sentiment: {sentiment['sentiment']}]")
print(f"AI: {response}")
# Track metrics
performance_monitor.increment_message_count()
except KeyboardInterrupt:
break
# Show stats
stats = performance_monitor.get_session_stats()
print(f"\n\nSession Stats: {stats['total_messages']} messages")
print("Stack 2.9 Enhanced - Session Complete!") |