Spaces:
Sleeping
Sleeping
| # language_detector.py - FINAL WORKING VERSION (200 lines) | |
| from langdetect import detect, DetectorFactory | |
| import re | |
| DetectorFactory.seed = 0 | |
| def detect_input_language(text): | |
| """Detect if text is Hindi, English or Mixed""" | |
| try: | |
| clean_text = re.sub(r'[^\w\s]', '', text) | |
| if not clean_text.strip(): | |
| return 'mixed' | |
| lang = detect(clean_text) | |
| # Hindi detection | |
| hindi_chars = re.findall(r'[\u0900-\u097F]', text) | |
| if lang == 'hi' or hindi_chars: | |
| # Check if mixed with English | |
| english_chars = re.findall(r'[a-zA-Z]', text) | |
| if hindi_chars and english_chars: | |
| return 'mixed' | |
| return 'hindi' | |
| # English detection | |
| if lang == 'en': | |
| return 'english' | |
| return 'mixed' | |
| except: | |
| return 'mixed' | |
| def get_system_prompt(lang_mode, username): | |
| """Generate system prompt based on language and intent""" | |
| # CORE RULES - COMMON FOR ALL | |
| core_rules = f""" | |
| ROLE: AumCore AI - Senior Coding Assistant | |
| USER: {username} | |
| CRITICAL RULES: | |
| 1. CODE vs CHAT DECISION: | |
| - CODE WHEN: User says 'code', 'program', 'script', 'function', 'create', 'build' | |
| - CHAT WHEN: General conversation, greetings, knowledge questions | |
| - EXAMPLES: | |
| * "google drive code" → RAW CODE | |
| * "hello how are you" → TEXT RESPONSE | |
| * "koi bhajan aata hai" → TEXT RESPONSE | |
| 2. CODE FORMAT: | |
| - RAW PYTHON CODE ONLY | |
| - NO markdown blocks (```python```) | |
| - NO 'python' keyword in response | |
| - Example: "from google.colab import drive\\ndrive.mount('/content/gdrive')" | |
| 3. ERROR HANDLING: | |
| - If user shows error, analyze and provide corrected code | |
| - Include brief explanation of fix | |
| 4. CODE QUALITY: | |
| - Production-ready code | |
| - Error handling included | |
| - Proper structure | |
| """ | |
| # LANGUAGE SPECIFIC STYLES | |
| styles = { | |
| 'hindi': """ | |
| STYLE: 100% Hindi (except code) | |
| EXAMPLES: | |
| - User: "नमस्ते, कोड बताओ" → RAW CODE | |
| - User: "क्या हाल है" → "सब ठीक है {username} भाई!" | |
| - User: "त्रुटि: x परिभाषित नहीं" → "x = 10\\ny = x\\nprint(y)" | |
| """, | |
| 'english': """ | |
| STYLE: 100% English (except code) | |
| EXAMPLES: | |
| - User: "hello, give code" → RAW CODE | |
| - User: "how are you" → "I'm good {username}!" | |
| - User: "error: x not defined" → "x = 10\\ny = x\\nprint(y)" | |
| """, | |
| 'mixed': """ | |
| STYLE: 60% English + 40% Hindi (natural blend) | |
| EXAMPLES: | |
| - User: "hi bhai, code de" → RAW CODE | |
| - User: "are yaar, kya haal hai" → "Sab badhiya hai {username} bhai!" | |
| - User: "error aaya: x not defined" → "x = 10\\ny = x\\nprint(y)" | |
| """ | |
| } | |
| # COMBINE | |
| full_prompt = f"""{core_rules} | |
| {styles.get(lang_mode, styles['mixed'])} | |
| FINAL REMINDER: Be {username}'s helpful AI assistant. | |
| Provide accurate code for technical requests. | |
| Engage naturally in conversation. | |
| """ | |
| return full_prompt.strip() | |
| # SIMPLE CODE GENERATOR (Optional - can be expanded) | |
| def generate_basic_code(task): | |
| """Generate basic code templates""" | |
| templates = { | |
| 'web': """ | |
| from fastapi import FastAPI | |
| import uvicorn | |
| app = FastAPI() | |
| @app.get("/") | |
| def home(): | |
| return {"message": "Hello from AumCore AI"} | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=8000) | |
| """, | |
| 'data': """ | |
| import pandas as pd | |
| import numpy as np | |
| # Load data | |
| df = pd.read_csv("data.csv") | |
| # Basic analysis | |
| print(f"Shape: {df.shape}") | |
| print(f"Columns: {list(df.columns)}") | |
| print(f"Summary:\\n{df.describe()}") | |
| """, | |
| 'drive': """ | |
| from google.colab import drive | |
| drive.mount('/content/gdrive') | |
| """ | |
| } | |
| task_lower = task.lower() | |
| if 'drive' in task_lower or 'mount' in task_lower: | |
| return templates['drive'] | |
| elif 'web' in task_lower or 'app' in task_lower: | |
| return templates['web'] | |
| elif 'data' in task_lower or 'analy' in task_lower: | |
| return templates['data'] | |
| return templates['drive'] # Default | |
| # Test function | |
| if __name__ == "__main__": | |
| # Test detection | |
| tests = [ | |
| "नमस्ते", | |
| "hello world", | |
| "hi bhai kya haal hai", | |
| "google drive mount code do" | |
| ] | |
| for test in tests: | |
| lang = detect_input_language(test) | |
| print(f"{test[:20]:20} -> {lang}") | |
| print("\\n✅ language_detector.py ready for AumCore AI") |