Spaces:
Sleeping
Sleeping
Update language_detector.py
Browse files- language_detector.py +137 -480
language_detector.py
CHANGED
|
@@ -1,507 +1,164 @@
|
|
| 1 |
-
# language_detector.py - FINAL
|
| 2 |
from langdetect import detect, DetectorFactory
|
| 3 |
import re
|
| 4 |
-
import json
|
| 5 |
-
from typing import Dict, List, Optional, Tuple, Any
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
import hashlib
|
| 8 |
-
|
| 9 |
-
###############################################################################
|
| 10 |
-
# LANGUAGE DETECTION MODULE - ENHANCED VERSION
|
| 11 |
-
###############################################################################
|
| 12 |
|
| 13 |
DetectorFactory.seed = 0
|
| 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 |
-
'common_languages': self._get_common_languages()
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
def _get_common_languages(self) -> List[str]:
|
| 76 |
-
"""Get most frequently detected languages"""
|
| 77 |
-
# Implementation for frequency analysis
|
| 78 |
-
return ['hindi', 'english', 'mixed']
|
| 79 |
-
|
| 80 |
-
# Global detector instance
|
| 81 |
-
language_detector = LanguageDetector()
|
| 82 |
-
|
| 83 |
-
def detect_input_language(text: str) -> str:
|
| 84 |
-
"""
|
| 85 |
-
Main language detection function
|
| 86 |
-
Enhanced with better mixed language handling
|
| 87 |
"""
|
| 88 |
-
lang_mode, confidence = language_detector.detect_with_confidence(text)
|
| 89 |
-
|
| 90 |
-
# Log this detection
|
| 91 |
-
detection_record = {
|
| 92 |
-
'timestamp': datetime.now().isoformat(),
|
| 93 |
-
'input': text[:100], # First 100 chars
|
| 94 |
-
'language': lang_mode,
|
| 95 |
-
'confidence': confidence,
|
| 96 |
-
'text_length': len(text)
|
| 97 |
-
}
|
| 98 |
-
language_detector.detection_history.append(detection_record)
|
| 99 |
-
|
| 100 |
-
# Keep only last 1000 records
|
| 101 |
-
if len(language_detector.detection_history) > 1000:
|
| 102 |
-
language_detector.detection_history = language_detector.detection_history[-1000:]
|
| 103 |
-
|
| 104 |
-
return lang_mode
|
| 105 |
-
|
| 106 |
-
###############################################################################
|
| 107 |
-
# PROMPT ENGINEERING MODULE - COMPREHENSIVE VERSION
|
| 108 |
-
###############################################################################
|
| 109 |
-
|
| 110 |
-
class PromptEngine:
|
| 111 |
-
"""Advanced prompt engineering for AI responses"""
|
| 112 |
-
|
| 113 |
-
def __init__(self, username: str):
|
| 114 |
-
self.username = username
|
| 115 |
-
self.prompt_templates = self._load_templates()
|
| 116 |
-
self.response_patterns = self._load_response_patterns()
|
| 117 |
-
|
| 118 |
-
def _load_templates(self) -> Dict[str, str]:
|
| 119 |
-
"""Load comprehensive prompt templates"""
|
| 120 |
-
return {
|
| 121 |
-
'hindi': self._get_hindi_template(),
|
| 122 |
-
'english': self._get_english_template(),
|
| 123 |
-
'mixed': self._get_mixed_template(),
|
| 124 |
-
'technical': self._get_technical_template(),
|
| 125 |
-
'casual': self._get_casual_template()
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
def _load_response_patterns(self) -> Dict[str, List[str]]:
|
| 129 |
-
"""Load response patterns for different intents"""
|
| 130 |
-
return {
|
| 131 |
-
'code_request': ['code', 'program', 'script', 'function', 'implement', 'create', 'build', 'develop', 'generate'],
|
| 132 |
-
'error_fix': ['error', 'fix', 'debug', 'not working', 'problem', 'issue', 'solve', 'correct'],
|
| 133 |
-
'technical_query': ['how to', 'tutorial', 'guide', 'example', 'explain', 'teach', 'learn'],
|
| 134 |
-
'casual_chat': ['hello', 'hi', 'how are you', 'what\'s up', 'kya haal hai', 'namaste', 'good morning'],
|
| 135 |
-
'knowledge_query': ['what is', 'who is', 'when is', 'where is', 'why is', 'how is', 'tell me about']
|
| 136 |
-
}
|
| 137 |
-
|
| 138 |
-
def _get_hindi_template(self) -> str:
|
| 139 |
-
"""Hindi language prompt template"""
|
| 140 |
-
return f"""
|
| 141 |
-
भूमिका: आप AumCore AI हैं - सीनियर AI आर्किटेक्ट और कोडिंग विशेषज्ञ।
|
| 142 |
-
उपयोगकर्ता: {self.username}
|
| 143 |
-
|
| 144 |
-
मुख्य नियम:
|
| 145 |
-
1. भाषा शैली: 100% हिंदी (कोड के अलावा)
|
| 146 |
-
2. कोड निर्णय: केवल तकनीकी अनुरोधों पर कोड प्रदान करें
|
| 147 |
-
3. कोड प्रारूप: केवल RAW पायथन कोड, कोई मार्कडाउन ब्लॉक नहीं
|
| 148 |
-
4. कोड गुणवत्ता: उत्पादन-तैयार कोड (300+ पंक्तियाँ जब आवश्यक हो)
|
| 149 |
-
5. त्रुटि प्रबंधन: यदि उपयोगकर्ता त्रुटि दिखाता है, तो विश्लेषण करें और सही कोड दें
|
| 150 |
-
|
| 151 |
-
इरादा पहचान नियम:
|
| 152 |
-
✅ कोड दें जब: "कोड", "प्रोग्राम", "स्क्रिप्ट", "फ़ंक्शन", "बनाएं", "विकसित करें"
|
| 153 |
-
❌ कोड न दें जब: "नमस्ते", "क्या हाल है", "कोई भजन आता है", "सपने सच होंगे"
|
| 154 |
-
|
| 155 |
-
उदाहरण प्रवाह:
|
| 156 |
-
- उपयोगकर्ता: "google drive mount code do"
|
| 157 |
-
AI: "from google.colab import drive\ndrive.mount('/content/gdrive')"
|
| 158 |
-
|
| 159 |
-
- उपयोगकर्ता: "koi bhajan aata hai"
|
| 160 |
-
AI: "हाँ {self.username} भाई, मुझे कुछ भजन याद हैं। आप किस भजन के बारे में पूछ रहे हैं?"
|
| 161 |
-
|
| 162 |
-
- उपयोगकर्ता: "ye code error de raha hai: x = 10\\nprint(y)"
|
| 163 |
-
AI: "त्रुटि: y परिभाषित नहीं है। सही कोड:\\nx = 10\\ny = x\\nprint(y)"
|
| 164 |
-
"""
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
- User: "
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
- User: "this code has error: x = 10\\nprint(y)"
|
| 191 |
-
AI: "Error: y is not defined. Corrected code:\\nx = 10\\ny = x\\nprint(y)"
|
| 192 |
"""
|
|
|
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
return f"""
|
| 197 |
-
ROLE: You are AumCore AI - Senior AI Architect and Coding Expert.
|
| 198 |
-
USER: {self.username}
|
| 199 |
-
|
| 200 |
-
CORE RULES:
|
| 201 |
-
1. LANGUAGE STYLE: 60% English + 40% Hindi (blended naturally)
|
| 202 |
-
2. CODE DECISION: Code sirf technical requests pe dena
|
| 203 |
-
3. CODE FORMAT: RAW Python code only, bilkul bhi markdown nahi
|
| 204 |
-
4. CODE QUALITY: Production-ready code (300+ lines jab appropriate ho)
|
| 205 |
-
5. ERROR HANDLING: Agar user error dikhaye, analyze karo aur corrected code do
|
| 206 |
-
|
| 207 |
-
INTENT DETECTION RULES:
|
| 208 |
-
✅ CODE DO JAB: "code", "program", "script", "function", "create", "build", "develop", "banao", "banao"
|
| 209 |
-
❌ CODE MAT DO JAB: "hello", "hi", "kya haal hai", "koi bhajan aata hai", "sapne sach honge"
|
| 210 |
-
|
| 211 |
-
EXAMPLE FLOW:
|
| 212 |
-
- User: "google drive mount code do"
|
| 213 |
-
AI: "from google.colab import drive\ndrive.mount('/content/gdrive')"
|
| 214 |
-
|
| 215 |
-
- User: "are bhai, koi bhajan aata hai"
|
| 216 |
-
AI: "Haan {self.username} bhai, mujhe kuch bhajans aate hain. Aap kis bhajan ke bare mein puch rahe ho?"
|
| 217 |
-
|
| 218 |
-
- User: "ye code error de raha hai: x = 10\\nprint(y)"
|
| 219 |
-
AI: "Error: y defined nahi hai. Corrected code:\\nx = 10\\ny = x\\nprint(y)"
|
| 220 |
-
"""
|
| 221 |
|
| 222 |
-
|
| 223 |
-
"""Technical/Code-focused template"""
|
| 224 |
-
return f"""
|
| 225 |
-
TECHNICAL CODING GUIDELINES:
|
| 226 |
-
|
| 227 |
-
1. CODE GENERATION STANDARDS:
|
| 228 |
-
- Always provide complete, runnable code
|
| 229 |
-
- Include error handling with try-except blocks
|
| 230 |
-
- Add proper logging for production environments
|
| 231 |
-
- Follow PEP 8 style guidelines
|
| 232 |
-
- Include docstrings for all functions
|
| 233 |
-
- Use type hints where applicable
|
| 234 |
-
- Add configuration management
|
| 235 |
-
- Include basic test structure
|
| 236 |
-
|
| 237 |
-
2. ERROR RESOLUTION PROTOCOL:
|
| 238 |
-
Step 1: Parse error message and traceback
|
| 239 |
-
Step 2: Identify error category (Syntax, Name, Type, Import, Runtime)
|
| 240 |
-
Step 3: Apply appropriate fix pattern
|
| 241 |
-
Step 4: Return corrected code with brief explanation
|
| 242 |
-
|
| 243 |
-
3. CODE TEMPLATE LIBRARY:
|
| 244 |
-
- Web Applications: Flask/FastAPI with authentication, database, APIs
|
| 245 |
-
- Data Analysis: Pandas, NumPy, Matplotlib with visualization
|
| 246 |
-
- ML Pipelines: Scikit-learn, TensorFlow/PyTorch workflows
|
| 247 |
-
- Automation Scripts: File processing, API integration, scheduling
|
| 248 |
-
- Utilities: Logging, configuration, error handling modules
|
| 249 |
-
"""
|
| 250 |
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
CASUAL CONVERSATION GUIDELINES:
|
| 255 |
-
|
| 256 |
-
1. RESPONSE STYLE:
|
| 257 |
-
- Be friendly, helpful, and engaging
|
| 258 |
-
- Maintain professional yet approachable tone
|
| 259 |
-
- Use appropriate language based on user's input
|
| 260 |
-
- Keep responses concise but meaningful
|
| 261 |
-
|
| 262 |
-
2. TOPIC HANDLING:
|
| 263 |
-
- General greetings: Respond warmly
|
| 264 |
-
- Personal questions: Answer appropriately
|
| 265 |
-
- Knowledge queries: Provide accurate information
|
| 266 |
-
- Off-topic chats: Gently steer back to relevant topics
|
| 267 |
-
|
| 268 |
-
3. BOUNDARIES:
|
| 269 |
-
- Do not provide medical, legal, or financial advice
|
| 270 |
-
- Maintain privacy and confidentiality
|
| 271 |
-
- Avoid political or controversial topics
|
| 272 |
-
- Stay within technical and general knowledge domains
|
| 273 |
-
"""
|
| 274 |
-
|
| 275 |
-
def generate_system_prompt(self, lang_mode: str) -> str:
|
| 276 |
-
"""Generate complete system prompt for given language mode"""
|
| 277 |
-
# Base template
|
| 278 |
-
base_prompt = self.prompt_templates.get(lang_mode, self.prompt_templates['mixed'])
|
| 279 |
-
|
| 280 |
-
# Add technical guidelines for code scenarios
|
| 281 |
-
technical_guidelines = self.prompt_templates['technical']
|
| 282 |
-
|
| 283 |
-
# Add casual guidelines for non-code scenarios
|
| 284 |
-
casual_guidelines = self.prompt_templates['casual']
|
| 285 |
-
|
| 286 |
-
# Combine all relevant sections
|
| 287 |
-
full_prompt = f"""
|
| 288 |
-
{base_prompt}
|
| 289 |
-
|
| 290 |
-
{technical_guidelines}
|
| 291 |
-
|
| 292 |
-
{casual_guidelines}
|
| 293 |
-
|
| 294 |
-
FINAL REMINDER: You are {self.username}'s personal AI assistant -
|
| 295 |
-
be helpful, accurate, and context-aware in all interactions.
|
| 296 |
-
"""
|
| 297 |
-
|
| 298 |
-
return full_prompt.strip()
|
| 299 |
-
|
| 300 |
-
###############################################################################
|
| 301 |
-
# MAIN INTERFACE FUNCTIONS
|
| 302 |
-
###############################################################################
|
| 303 |
-
|
| 304 |
-
# Global prompt engine
|
| 305 |
-
prompt_engine = PromptEngine(username="Sanjay")
|
| 306 |
-
|
| 307 |
-
def get_system_prompt(lang_mode: str, username: str) -> str:
|
| 308 |
"""
|
| 309 |
-
Main function to get system prompt
|
| 310 |
-
Enhanced with advanced prompt engineering
|
| 311 |
-
"""
|
| 312 |
-
# Update username if different
|
| 313 |
-
if username != prompt_engine.username:
|
| 314 |
-
global prompt_engine
|
| 315 |
-
prompt_engine = PromptEngine(username=username)
|
| 316 |
|
| 317 |
-
|
| 318 |
-
return prompt_engine.generate_system_prompt(lang_mode)
|
| 319 |
-
|
| 320 |
-
###############################################################################
|
| 321 |
-
# CODE GENERATION MODULE - ENHANCED VERSION
|
| 322 |
-
###############################################################################
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
def _load_code_templates(self) -> Dict[str, str]:
|
| 332 |
-
"""Load comprehensive code templates"""
|
| 333 |
-
return {
|
| 334 |
-
'web_app': self._web_app_template(),
|
| 335 |
-
'data_analysis': self._data_analysis_template(),
|
| 336 |
-
'ml_pipeline': self._ml_pipeline_template(),
|
| 337 |
-
'automation': self._automation_template(),
|
| 338 |
-
'api_service': self._api_service_template(),
|
| 339 |
-
'utility': self._utility_template()
|
| 340 |
-
}
|
| 341 |
-
|
| 342 |
-
def _load_code_snippets(self) -> Dict[str, List[str]]:
|
| 343 |
-
"""Load reusable code snippets"""
|
| 344 |
-
return {
|
| 345 |
-
'imports': self._import_snippets(),
|
| 346 |
-
'error_handling': self._error_handling_snippets(),
|
| 347 |
-
'logging': self._logging_snippets(),
|
| 348 |
-
'config': self._config_snippets()
|
| 349 |
-
}
|
| 350 |
-
|
| 351 |
-
def _web_app_template(self) -> str:
|
| 352 |
-
"""Web application template (300+ lines)"""
|
| 353 |
-
# [300+ lines of comprehensive web app code]
|
| 354 |
-
return """
|
| 355 |
-
from fastapi import FastAPI, HTTPException, Depends, status
|
| 356 |
-
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 357 |
-
from pydantic import BaseModel, Field, validator
|
| 358 |
-
from typing import List, Optional, Dict, Any
|
| 359 |
import uvicorn
|
| 360 |
-
import logging
|
| 361 |
-
import json
|
| 362 |
-
from datetime import datetime, timedelta
|
| 363 |
-
import os
|
| 364 |
-
import secrets
|
| 365 |
-
from contextlib import asynccontextmanager
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
|
|
|
|
|
|
|
|
|
| 374 |
import pandas as pd
|
| 375 |
import numpy as np
|
| 376 |
-
import matplotlib.pyplot as plt
|
| 377 |
-
import seaborn as sns
|
| 378 |
-
from scipy import stats
|
| 379 |
-
import warnings
|
| 380 |
-
warnings.filterwarnings('ignore')
|
| 381 |
-
from sklearn.preprocessing import StandardScaler, LabelEncoder
|
| 382 |
-
from sklearn.model_selection import train_test_split, cross_val_score
|
| 383 |
-
from sklearn.ensemble import RandomForestClassifier, GradientBoostingRegressor
|
| 384 |
|
| 385 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
"""
|
|
|
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
return [
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt"
|
| 396 |
-
]
|
| 397 |
-
|
| 398 |
-
# [Additional snippet methods...]
|
| 399 |
-
|
| 400 |
-
def generate_code(self, task_description: str, code_type: str = 'auto') -> str:
|
| 401 |
-
"""Generate code based on task description"""
|
| 402 |
-
if code_type == 'auto':
|
| 403 |
-
code_type = self._detect_code_type(task_description)
|
| 404 |
-
|
| 405 |
-
template = self.templates.get(code_type, self.templates['utility'])
|
| 406 |
-
|
| 407 |
-
# Enhance template with relevant snippets
|
| 408 |
-
enhanced_code = self._enhance_with_snippets(template, task_description)
|
| 409 |
-
|
| 410 |
-
return enhanced_code
|
| 411 |
-
|
| 412 |
-
def _detect_code_type(self, description: str) -> str:
|
| 413 |
-
"""Auto-detect code type from description"""
|
| 414 |
-
description_lower = description.lower()
|
| 415 |
-
|
| 416 |
-
if any(word in description_lower for word in ['web', 'app', 'flask', 'fastapi', 'django']):
|
| 417 |
-
return 'web_app'
|
| 418 |
-
elif any(word in description_lower for word in ['data', 'analysis', 'pandas', 'numpy', 'visualize']):
|
| 419 |
-
return 'data_analysis'
|
| 420 |
-
elif any(word in description_lower for word in ['machine', 'learning', 'ml', 'ai', 'model']):
|
| 421 |
-
return 'ml_pipeline'
|
| 422 |
-
elif any(word in description_lower for word in ['automate', 'script', 'batch', 'process']):
|
| 423 |
-
return 'automation'
|
| 424 |
-
elif any(word in description_lower for word in ['api', 'rest', 'endpoint', 'service']):
|
| 425 |
-
return 'api_service'
|
| 426 |
-
else:
|
| 427 |
-
return 'utility'
|
| 428 |
|
| 429 |
-
|
| 430 |
-
"""Enhance template with appropriate snippets"""
|
| 431 |
-
enhanced = template
|
| 432 |
-
|
| 433 |
-
# Add imports based on description
|
| 434 |
-
if 'logging' in description.lower() or 'debug' in description.lower():
|
| 435 |
-
enhanced = self.code_snippets['logging'][0] + "\n\n" + enhanced
|
| 436 |
-
|
| 437 |
-
if 'config' in description.lower() or 'setting' in description.lower():
|
| 438 |
-
enhanced = self.code_snippets['config'][0] + "\n\n" + enhanced
|
| 439 |
-
|
| 440 |
-
return enhanced
|
| 441 |
-
|
| 442 |
-
# Global code generator
|
| 443 |
-
code_generator = CodeGenerator()
|
| 444 |
-
|
| 445 |
-
def generate_expert_code(task_description: str) -> str:
|
| 446 |
-
"""
|
| 447 |
-
Generate expert-level code (300+ lines)
|
| 448 |
-
Enhanced with intelligent template selection
|
| 449 |
-
"""
|
| 450 |
-
return code_generator.generate_code(task_description)
|
| 451 |
-
|
| 452 |
-
###############################################################################
|
| 453 |
-
# MODULE INITIALIZATION AND EXPORTS
|
| 454 |
-
###############################################################################
|
| 455 |
-
|
| 456 |
-
def initialize_modules():
|
| 457 |
-
"""Initialize all modules"""
|
| 458 |
-
print("Initializing Language Detection Module...")
|
| 459 |
-
print("Initializing Prompt Engineering Module...")
|
| 460 |
-
print("Initializing Code Generation Module...")
|
| 461 |
-
print("All modules initialized successfully!")
|
| 462 |
-
|
| 463 |
-
return {
|
| 464 |
-
'language_detector': language_detector,
|
| 465 |
-
'prompt_engine': prompt_engine,
|
| 466 |
-
'code_generator': code_generator
|
| 467 |
-
}
|
| 468 |
-
|
| 469 |
-
# Auto-initialize on import
|
| 470 |
-
_MODULES = initialize_modules()
|
| 471 |
-
|
| 472 |
-
# Export main functions
|
| 473 |
-
__all__ = [
|
| 474 |
-
'detect_input_language',
|
| 475 |
-
'get_system_prompt',
|
| 476 |
-
'generate_expert_code',
|
| 477 |
-
'language_detector',
|
| 478 |
-
'prompt_engine',
|
| 479 |
-
'code_generator'
|
| 480 |
-
]
|
| 481 |
-
|
| 482 |
-
###############################################################################
|
| 483 |
-
# USAGE EXAMPLE
|
| 484 |
-
###############################################################################
|
| 485 |
|
|
|
|
| 486 |
if __name__ == "__main__":
|
| 487 |
-
# Test
|
| 488 |
-
|
| 489 |
-
"
|
| 490 |
-
"hello
|
| 491 |
-
"hi bhai
|
| 492 |
-
"
|
| 493 |
]
|
| 494 |
|
| 495 |
-
for
|
| 496 |
-
lang = detect_input_language(
|
| 497 |
-
print(f"
|
| 498 |
-
|
| 499 |
-
# Test prompt generation
|
| 500 |
-
prompt = get_system_prompt('hindi', 'Sanjay')
|
| 501 |
-
print(f"\nGenerated prompt length: {len(prompt)} characters")
|
| 502 |
|
| 503 |
-
print("
|
| 504 |
-
print(" - Advanced language detection with confidence scoring")
|
| 505 |
-
print(" - Comprehensive prompt engineering")
|
| 506 |
-
print(" - Professional code generation (300+ lines)")
|
| 507 |
-
print(" - Ready for AumCore AI integration")
|
|
|
|
| 1 |
+
# language_detector.py - FINAL WORKING VERSION (200 lines)
|
| 2 |
from langdetect import detect, DetectorFactory
|
| 3 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
DetectorFactory.seed = 0
|
| 6 |
|
| 7 |
+
def detect_input_language(text):
|
| 8 |
+
"""Detect if text is Hindi, English or Mixed"""
|
| 9 |
+
try:
|
| 10 |
+
clean_text = re.sub(r'[^\w\s]', '', text)
|
| 11 |
+
if not clean_text.strip():
|
| 12 |
+
return 'mixed'
|
| 13 |
+
|
| 14 |
+
lang = detect(clean_text)
|
| 15 |
+
|
| 16 |
+
# Hindi detection
|
| 17 |
+
hindi_chars = re.findall(r'[\u0900-\u097F]', text)
|
| 18 |
+
if lang == 'hi' or hindi_chars:
|
| 19 |
+
# Check if mixed with English
|
| 20 |
+
english_chars = re.findall(r'[a-zA-Z]', text)
|
| 21 |
+
if hindi_chars and english_chars:
|
| 22 |
+
return 'mixed'
|
| 23 |
+
return 'hindi'
|
| 24 |
+
|
| 25 |
+
# English detection
|
| 26 |
+
if lang == 'en':
|
| 27 |
+
return 'english'
|
| 28 |
+
|
| 29 |
+
return 'mixed'
|
| 30 |
+
except:
|
| 31 |
+
return 'mixed'
|
| 32 |
+
|
| 33 |
+
def get_system_prompt(lang_mode, username):
|
| 34 |
+
"""Generate system prompt based on language and intent"""
|
| 35 |
+
|
| 36 |
+
# CORE RULES - COMMON FOR ALL
|
| 37 |
+
core_rules = f"""
|
| 38 |
+
ROLE: AumCore AI - Senior Coding Assistant
|
| 39 |
+
USER: {username}
|
| 40 |
+
|
| 41 |
+
CRITICAL RULES:
|
| 42 |
+
1. CODE vs CHAT DECISION:
|
| 43 |
+
- CODE WHEN: User says 'code', 'program', 'script', 'function', 'create', 'build'
|
| 44 |
+
- CHAT WHEN: General conversation, greetings, knowledge questions
|
| 45 |
+
- EXAMPLES:
|
| 46 |
+
* "google drive code" → RAW CODE
|
| 47 |
+
* "hello how are you" → TEXT RESPONSE
|
| 48 |
+
* "koi bhajan aata hai" → TEXT RESPONSE
|
| 49 |
+
|
| 50 |
+
2. CODE FORMAT:
|
| 51 |
+
- RAW PYTHON CODE ONLY
|
| 52 |
+
- NO markdown blocks (```python```)
|
| 53 |
+
- NO 'python' keyword in response
|
| 54 |
+
- Example: "from google.colab import drive\\ndrive.mount('/content/gdrive')"
|
| 55 |
+
|
| 56 |
+
3. ERROR HANDLING:
|
| 57 |
+
- If user shows error, analyze and provide corrected code
|
| 58 |
+
- Include brief explanation of fix
|
| 59 |
+
|
| 60 |
+
4. CODE QUALITY:
|
| 61 |
+
- Production-ready code
|
| 62 |
+
- Error handling included
|
| 63 |
+
- Proper structure
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
# LANGUAGE SPECIFIC STYLES
|
| 67 |
+
styles = {
|
| 68 |
+
'hindi': """
|
| 69 |
+
STYLE: 100% Hindi (except code)
|
| 70 |
+
EXAMPLES:
|
| 71 |
+
- User: "नमस्ते, कोड बताओ" → RAW CODE
|
| 72 |
+
- User: "क्या हाल है" → "सब ठीक है {username} भाई!"
|
| 73 |
+
- User: "त्रुटि: x परिभाषित नहीं" → "x = 10\\ny = x\\nprint(y)"
|
| 74 |
+
""",
|
| 75 |
+
|
| 76 |
+
'english': """
|
| 77 |
+
STYLE: 100% English (except code)
|
| 78 |
+
EXAMPLES:
|
| 79 |
+
- User: "hello, give code" → RAW CODE
|
| 80 |
+
- User: "how are you" → "I'm good {username}!"
|
| 81 |
+
- User: "error: x not defined" → "x = 10\\ny = x\\nprint(y)"
|
| 82 |
+
""",
|
| 83 |
+
|
| 84 |
+
'mixed': """
|
| 85 |
+
STYLE: 60% English + 40% Hindi (natural blend)
|
| 86 |
+
EXAMPLES:
|
| 87 |
+
- User: "hi bhai, code de" → RAW CODE
|
| 88 |
+
- User: "are yaar, kya haal hai" → "Sab badhiya hai {username} bhai!"
|
| 89 |
+
- User: "error aaya: x not defined" → "x = 10\\ny = x\\nprint(y)"
|
|
|
|
|
|
|
| 90 |
"""
|
| 91 |
+
}
|
| 92 |
|
| 93 |
+
# COMBINE
|
| 94 |
+
full_prompt = f"""{core_rules}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
{styles.get(lang_mode, styles['mixed'])}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
FINAL REMINDER: Be {username}'s helpful AI assistant.
|
| 99 |
+
Provide accurate code for technical requests.
|
| 100 |
+
Engage naturally in conversation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
+
return full_prompt.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# SIMPLE CODE GENERATOR (Optional - can be expanded)
|
| 106 |
+
def generate_basic_code(task):
|
| 107 |
+
"""Generate basic code templates"""
|
| 108 |
+
templates = {
|
| 109 |
+
'web': """
|
| 110 |
+
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
app = FastAPI()
|
| 114 |
+
|
| 115 |
+
@app.get("/")
|
| 116 |
+
def home():
|
| 117 |
+
return {"message": "Hello from AumCore AI"}
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 121 |
+
""",
|
| 122 |
+
'data': """
|
| 123 |
import pandas as pd
|
| 124 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
# Load data
|
| 127 |
+
df = pd.read_csv("data.csv")
|
| 128 |
+
|
| 129 |
+
# Basic analysis
|
| 130 |
+
print(f"Shape: {df.shape}")
|
| 131 |
+
print(f"Columns: {list(df.columns)}")
|
| 132 |
+
print(f"Summary:\\n{df.describe()}")
|
| 133 |
+
""",
|
| 134 |
+
'drive': """
|
| 135 |
+
from google.colab import drive
|
| 136 |
+
drive.mount('/content/gdrive')
|
| 137 |
"""
|
| 138 |
+
}
|
| 139 |
|
| 140 |
+
task_lower = task.lower()
|
| 141 |
+
if 'drive' in task_lower or 'mount' in task_lower:
|
| 142 |
+
return templates['drive']
|
| 143 |
+
elif 'web' in task_lower or 'app' in task_lower:
|
| 144 |
+
return templates['web']
|
| 145 |
+
elif 'data' in task_lower or 'analy' in task_lower:
|
| 146 |
+
return templates['data']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
return templates['drive'] # Default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
+
# Test function
|
| 151 |
if __name__ == "__main__":
|
| 152 |
+
# Test detection
|
| 153 |
+
tests = [
|
| 154 |
+
"नमस्ते",
|
| 155 |
+
"hello world",
|
| 156 |
+
"hi bhai kya haal hai",
|
| 157 |
+
"google drive mount code do"
|
| 158 |
]
|
| 159 |
|
| 160 |
+
for test in tests:
|
| 161 |
+
lang = detect_input_language(test)
|
| 162 |
+
print(f"{test[:20]:20} -> {lang}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
print("\\n✅ language_detector.py ready for AumCore AI")
|
|
|
|
|
|
|
|
|
|
|
|