AumCoreAI commited on
Commit
eb81b58
·
verified ·
1 Parent(s): 5493110

Update core/language_detector.py

Browse files
Files changed (1) hide show
  1. core/language_detector.py +112 -192
core/language_detector.py CHANGED
@@ -1,205 +1,125 @@
1
- # language_detector.py - UPDATED FOR CODE FORMATTING (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
- # AUMCORE AI SYSTEM KNOWLEDGE (CRITICAL - ADDED NEW)
37
- SYSTEM_KNOWLEDGE = f"""AUMCORE AI SYSTEM INFORMATION (NEVER FORGET):
38
- 1. ENDPOINTS: /system/status, /system/task, /reset, /chat, /
39
- 2. /system/status → AumCoreMaster orchestrator status check
40
- 3. /system/task → Heavy computational tasks
41
- 4. URL: https://aumcoreai-aumcore-ai.hf.space
42
- 5. Version: 2.0.1-Stable, Logs: logs/aumcore_main.log
43
- 6. When asked about system, ALWAYS mention these endpoints"""
44
-
45
- # CORE RULES - UPDATED FOR MARKDOWN FORMATTING
46
- core_rules = f"""
47
- ROLE: AumCore AI - Senior Coding Assistant
48
- USER: {username}
49
-
50
- CRITICAL RULES:
51
- 1. CODE vs CHAT DECISION:
52
- - CODE WHEN: User says 'code', 'program', 'script', 'function', 'create', 'build'
53
- - CHAT WHEN: General conversation, greetings, knowledge questions
54
- - EXAMPLES:
55
- * "google drive code" → CODE IN MARKDOWN BLOCKS
56
- * "hello how are you" → TEXT RESPONSE
57
- * "koi bhajan aata hai" → TEXT RESPONSE
58
-
59
- 2. CODE FORMATTING (CRITICAL):
60
- - ALWAYS use markdown code blocks for Python code
61
- - FORMAT: ```python\\ncode here\\n```
62
- - NEVER output raw code without markdown blocks
63
- - EXAMPLES:
64
- ✅ CORRECT: ```python\\nfrom google.colab import drive\\ndrive.mount('/content/gdrive')\\n```
65
- ❌ WRONG: from google.colab import drive
66
-
67
- 3. ERROR HANDLING:
68
- - If user shows error, analyze and provide corrected code IN MARKDOWN
69
- - Include brief explanation before/after code block
70
-
71
- 4. CODE QUALITY:
72
- - Production-ready code
73
- - Error handling included
74
- - Proper structure
75
-
76
- 5. RESPONSE STRUCTURE:
77
- - Code responses: Explanation (if needed) + Markdown code block
78
- - Chat responses: Plain text only
79
- - Error fixes: Error analysis + Corrected code in markdown
80
-
81
- 6. SYSTEM KNOWLEDGE: {SYSTEM_KNOWLEDGE}
82
- """
83
 
84
- # LANGUAGE SPECIFIC STYLES - UPDATED WITH SYSTEM EXAMPLES
85
- styles = {
86
- 'hindi': f"""
87
- STYLE: 100% Hindi (code markdown blocks ke alawa)
88
- EXAMPLES:
89
- - User: "नमस्ते, कोड बताओ"
90
- You: ```python\\nfrom google.colab import drive\\ndrive.mount('/content/gdrive')\\n```
91
- - User: "system status check करो"
92
- You: "/system/status endpoint से AumCoreMaster orchestrator की status check करें"
93
- - User: "क्या हाल है"
94
- You: "सब ठीक है {username} भाई!"
95
- - User: "endpoints बताओ"
96
- You: "/system/status, /system/task, /reset, /chat, / - ये सभी endpoints हैं"
97
- """,
98
-
99
- 'english': f"""
100
- STYLE: 100% English (except code in markdown blocks)
101
- EXAMPLES:
102
- - User: "hello, give code"
103
- You: ```python\\nfrom google.colab import drive\\ndrive.mount('/content/gdrive')\\n```
104
- - User: "check system orchestrator status"
105
- You: "Use /system/status endpoint to check AumCoreMaster orchestrator"
106
- - User: "how are you"
107
- You: "I'm good {username}!"
108
- - User: "list endpoints"
109
- You: "Endpoints: /system/status, /system/task, /reset, /chat, /"
110
- """,
111
-
112
- 'mixed': f"""
113
- STYLE: 60% English + 40% Hindi (natural blend)
114
- EXAMPLES:
115
- - User: "hi bhai, code de"
116
- You: ```python\\nfrom google.colab import drive\\ndrive.mount('/content/gdrive')\\n```
117
- - User: "system ki status bata"
118
- You: "/system/status endpoint se system orchestrator ki status check karo"
119
- - User: "are yaar, kya haal hai"
120
- You: "Sab badhiya hai {username} bhai!"
121
- - User: "endpoints batana"
122
- You: "Endpoints hai: /system/status, /system/task, /reset, /chat, /"
123
- """
124
  }
125
 
126
- # COMBINE EVERYTHING
127
- full_prompt = f"""{core_rules}
128
-
129
- {styles.get(lang_mode, styles['mixed'])}
130
-
131
- FINAL REMINDER:
132
- - You are {username}'s helpful AI assistant
133
- - FOR CODE: ALWAYS use ```python\\ncode\\n``` format
134
- - KNOW YOUR SYSTEM: AumCore-AI with specific endpoints
135
- - When asked about system, REFER to /system/status and /system/task
136
- """
 
 
 
 
 
 
 
 
 
137
 
138
- return full_prompt.strip()
 
 
 
 
 
 
 
 
139
 
140
- # SIMPLE CODE GENERATOR WITH MARKDOWN
141
- def generate_basic_code(task):
142
- """Generate basic code templates with markdown"""
143
- templates = {
144
- 'web': """```python
145
- from fastapi import FastAPI
146
- import uvicorn
147
 
148
- app = FastAPI()
 
 
149
 
150
- @app.get("/")
151
- def home():
152
- return {"message": "Hello from AumCore AI"}
153
 
154
- if __name__ == "__main__":
155
- uvicorn.run(app, host="0.0.0.0", port=8000)
156
- ```""",
157
- 'data': """```python
158
- import pandas as pd
159
- import numpy as np
 
 
 
 
 
 
 
 
 
160
 
161
- # Load data
162
- df = pd.read_csv("data.csv")
163
 
164
- # Basic analysis
165
- print(f"Shape: {df.shape}")
166
- print(f"Columns: {list(df.columns)}")
167
- print(f"Summary:\\n{df.describe()}")
168
- ```""",
169
- 'drive': """```python
170
- from google.colab import drive
171
- drive.mount('/content/gdrive')
172
- ```"""
173
- }
 
 
 
 
 
 
 
174
 
175
- task_lower = task.lower()
176
  if 'drive' in task_lower or 'mount' in task_lower:
177
- return templates['drive']
178
- elif 'web' in task_lower or 'app' in task_lower:
179
- return templates['web']
180
- elif 'data' in task_lower or 'analy' in task_lower:
181
- return templates['data']
182
-
183
- return templates['drive'] # Default
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- # Test function
186
- if __name__ == "__main__":
187
- # Test detection
188
- tests = [
189
- "नमस्ते",
190
- "hello world",
191
- "hi bhai kya haal hai",
192
- "google drive mount code do"
193
- ]
194
-
195
- for test in tests:
196
- lang = detect_input_language(test)
197
- print(f"{test[:20]:20} -> {lang}")
198
-
199
- # Test code generation with markdown
200
- test_code = generate_basic_code("google drive")
201
- print(f"\nGenerated code sample:\n{test_code[:100]}...")
202
-
203
- print("\n✅ language_detector.py UPDATED for markdown formatting")
204
- print(" AI will now output code in ```python blocks")
205
- print(" This enables: Code boxes, Copy button, Syntax highlighting")
 
1
+ # SIMPLE CODE GENERATOR WITH COLORFUL SYNTAX HIGHLIGHTING
2
+ def generate_basic_code(task):
3
+ """Generate basic code templates with colorful syntax highlighting"""
4
+ task_lower = task.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ # GitHub-like syntax highlighting colors
7
+ colors = {
8
+ 'keyword': '#569CD6', # def, import, from
9
+ 'function': '#DCDCAA', # function names
10
+ 'string': '#CE9178', # strings
11
+ 'comment': '#608B4E', # comments
12
+ 'number': '#B5CEA8', # numbers
13
+ 'operator': '#D4D4D4', # =, (), ., :
14
+ 'class': '#4EC9B0', # class names
15
+ 'decorator': '#C586C0', # @app.get
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  }
17
 
18
+ # Colorful Google Drive Mount Code
19
+ drive_code = f'''<div class="code-container">
20
+ <div class="code-header">
21
+ <div class="code-lang"><i class="fab fa-google-drive"></i> Google Colab</div>
22
+ <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy Code</button>
23
+ </div>
24
+ <pre><code style="color: #f8f8f2; background: #0d1117; padding: 20px; border-radius: 0 0 8px 8px; display: block; overflow-x: auto; font-family: 'Fira Code', monospace; line-height: 1.5;">
25
+ <span style="color: {colors['comment']}"># Mount Google Drive in Google Colab</span>
26
+ <span style="color: {colors['keyword']}">from</span> <span style="color: {colors['function']}">google.colab</span> <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">drive</span>
27
+
28
+ <span style="color: {colors['function']}">drive</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">mount</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&#x27;/content/gdrive&#x27;</span><span style="color: {colors['operator']}">)</span>
29
+
30
+ <span style="color: {colors['comment']}"># After authorization, access your drive</span>
31
+ <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">os</span>
32
+
33
+ <span style="color: {colors['comment']}"># List files in drive</span>
34
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;Mounted at: /content/gdrive&quot;</span><span style="color: {colors['operator']}">)</span>
35
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;Files:&quot;</span><span style="color: {colors['operator']}">,</span> <span style="color: {colors['function']}">os</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">listdir</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&#x27;/content/gdrive&#x27;</span><span style="color: {colors['operator']}">))</span>
36
+ </code></pre>
37
+ </div>'''
38
 
39
+ # Colorful FastAPI Web App Code
40
+ web_code = f'''<div class="code-container">
41
+ <div class="code-header">
42
+ <div class="code-lang"><i class="fas fa-globe"></i> FastAPI Web App</div>
43
+ <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy Code</button>
44
+ </div>
45
+ <pre><code style="color: #f8f8f2; background: #0d1117; padding: 20px; border-radius: 0 0 8px 8px; display: block; overflow-x: auto; font-family: 'Fira Code', monospace; line-height: 1.5;">
46
+ <span style="color: {colors['keyword']}">from</span> <span style="color: {colors['function']}">fastapi</span> <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">FastAPI</span>
47
+ <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">uvicorn</span>
48
 
49
+ <span style="color: {colors['comment']}"># Create FastAPI app</span>
50
+ <span style="color: {colors['function']}">app</span> <span style="color: {colors['operator']}">=</span> <span style="color: {colors['class']}">FastAPI</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['operator']}">)</span>
 
 
 
 
 
51
 
52
+ <span style="color: {colors['decorator']}">@app</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">get</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;/&quot;</span><span style="color: {colors['operator']}">)</span>
53
+ <span style="color: {colors['keyword']}">def</span> <span style="color: {colors['function']}">home</span><span style="color: {colors['operator']}">():</span>
54
+ <span style="color: {colors['keyword']}">return</span> <span style="color: {colors['operator']}">{{</span><span style="color: {colors['string']}">&quot;message&quot;</span><span style="color: {colors['operator']}">:</span> <span style="color: {colors['string']}">&quot;Hello from AumCore AI! 🚀&quot;</span><span style="color: {colors['operator']}">}}</span>
55
 
56
+ <span style="color: {colors['decorator']}">@app</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">get</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;/health&quot;</span><span style="color: {colors['operator']}">)</span>
57
+ <span style="color: {colors['keyword']}">def</span> <span style="color: {colors['function']}">health_check</span><span style="color: {colors['operator']}">():</span>
58
+ <span style="color: {colors['keyword']}">return</span> <span style="color: {colors['operator']}">{{</span><span style="color: {colors['string']}">&quot;status&quot;</span><span style="color: {colors['operator']}">:</span> <span style="color: {colors['string']}">&quot;healthy&quot;</span><span style="color: {colors['operator']}">,</span> <span style="color: {colors['string']}">&quot;service&quot;</span><span style="color: {colors['operator']}">:</span> <span style="color: {colors['string']}">&quot;AumCore AI&quot;</span><span style="color: {colors['operator']}">}}</span>
59
 
60
+ <span style="color: {colors['keyword']}">if</span> <span style="color: {colors['variable']}">__name__</span> <span style="color: {colors['operator']}">==</span> <span style="color: {colors['string']}">&quot;__main__&quot;</span><span style="color: {colors['operator']}">:</span>
61
+ <span style="color: {colors['function']}">uvicorn</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">run</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['function']}">app</span><span style="color: {colors['operator']}">,</span> <span style="color: {colors['variable']}">host</span><span style="color: {colors['operator']}">=</span><span style="color: {colors['string']}">&quot;0.0.0.0&quot;</span><span style="color: {colors['operator']}">,</span> <span style="color: {colors['variable']}">port</span><span style="color: {colors['operator']}">=</span><span style="color: {colors['number']}">8000</span><span style="color: {colors['operator']}">)</span>
62
+ </code></pre>
63
+ </div>'''
64
+
65
+ # Colorful Data Analysis Code
66
+ data_code = f'''<div class="code-container">
67
+ <div class="code-header">
68
+ <div class="code-lang"><i class="fas fa-chart-bar"></i> Data Analysis</div>
69
+ <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy Code</button>
70
+ </div>
71
+ <pre><code style="color: #f8f8f2; background: #0d1117; padding: 20px; border-radius: 0 0 8px 8px; display: block; overflow-x: auto; font-family: 'Fira Code', monospace; line-height: 1.5;">
72
+ <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">pandas</span> <span style="color: {colors['keyword']}">as</span> <span style="color: {colors['function']}">pd</span>
73
+ <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">numpy</span> <span style="color: {colors['keyword']}">as</span> <span style="color: {colors['function']}">np</span>
74
+ <span style="color: {colors['keyword']}">import</span> <span style="color: {colors['function']}">matplotlib.pyplot</span> <span style="color: {colors['keyword']}">as</span> <span style="color: {colors['function']}">plt</span>
75
 
76
+ <span style="color: {colors['comment']}"># Load dataset</span>
77
+ <span style="color: {colors['function']}">df</span> <span style="color: {colors['operator']}">=</span> <span style="color: {colors['function']}">pd</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">read_csv</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;data.csv&quot;</span><span style="color: {colors['operator']}">)</span>
78
 
79
+ <span style="color: {colors['comment']}"># Basic information</span>
80
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">f&quot;📊 Dataset Shape: </span><span style="color: {colors['operator']}">{{</span><span style="color: {colors['function']}">df</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">shape</span><span style="color: {colors['operator']}">}}</span><span style="color: {colors['string']}">&quot;</span><span style="color: {colors['operator']}">)</span>
81
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">f&quot;📋 Columns: </span><span style="color: {colors['operator']}">{{</span><span style="color: {colors['function']}">list</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['function']}">df</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">columns</span><span style="color: {colors['operator']}">)}}</span><span style="color: {colors['string']}">&quot;</span><span style="color: {colors['operator']}">)</span>
82
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;\\n📈 Basic Statistics:&quot;</span><span style="color: {colors['operator']}">)</span>
83
+ <span style="color: {colors='function'}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['function']}">df</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">describe</span><span style="color: {colors['operator']}">())</span>
84
+
85
+ <span style="color: {colors['comment']}"># Check for missing values</span>
86
+ <span style="color: {colors['function']}">missing</span> <span style="color: {colors['operator']}">=</span> <span style="color: {colors['function']}">df</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">isnull</span><span style="color: {colors['operator']}">()</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">sum</span><span style="color: {colors['operator']}">()</span>
87
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">f&quot;\\n⚠️ Missing Values:\\n</span><span style="color: {colors['operator']}">{{</span><span style="color: {colors['function']}">missing</span><span style="color: {colors['operator']}">}}</span><span style="color: {colors['string']}">&quot;</span><span style="color: {colors['operator']}">)</span>
88
+
89
+ <span style="color: {colors['comment']}"># Create a simple visualization</span>
90
+ <span style="color: {colors['function']}">plt</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">figure</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['variable']}">figsize</span><span style="color: {colors['operator']}">=</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['number']}">10</span><span style="color: {colors['operator']}">,</span> <span style="color: {colors['number']}">6</span><span style="color: {colors['operator']}">))</span>
91
+ <span style="color: {colors['function']}">df</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">hist</span><span style="color: {colors['operator']}">()</span>
92
+ <span style="color: {colors['function']}">plt</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">tight_layout</span><span style="color: {colors['operator']}">()</span>
93
+ <span style="color: {colors['function']}">plt</span><span style="color: {colors['operator']}">.</span><span style="color: {colors['function']}">show</span><span style="color: {colors['operator']}">()</span>
94
+ </code></pre>
95
+ </div>'''
96
 
97
+ # Determine which code to return
98
  if 'drive' in task_lower or 'mount' in task_lower:
99
+ return drive_code
100
+ elif 'web' in task_lower or 'app' in task_lower or 'api' in task_lower:
101
+ return web_code
102
+ elif 'data' in task_lower or 'analy' in task_lower or 'pandas' in task_lower:
103
+ return data_code
104
+ else:
105
+ # Default colorful hello world
106
+ return f'''<div class="code-container">
107
+ <div class="code-header">
108
+ <div class="code-lang"><i class="fas fa-code"></i> Python</div>
109
+ <button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy Code</button>
110
+ </div>
111
+ <pre><code style="color: #f8f8f2; background: #0d1117; padding: 20px; border-radius: 0 0 8px 8px; display: block; overflow-x: auto; font-family: 'Fira Code', monospace; line-height: 1.5;">
112
+ <span style="color: {colors['keyword']}">def</span> <span style="color: {colors['function']}">hello_aumcore</span><span style="color: {colors['operator']}">():</span>
113
+ <span style="color: {colors['string']}">&quot;&quot;&quot;</span>
114
+ <span style="color: {colors['string']}> AumCore AI Sample Function</span>
115
+ <span style="color: {colors['string']}> With GitHub-like syntax highlighting</span>
116
+ <span style="color: {colors['string']}> &quot;&quot;&quot;</span>
117
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;🚀 Welcome to AumCore AI!&quot;</span><span style="color: {colors['operator']}">)</span>
118
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}">&quot;🌈 Colorful syntax highlighting enabled&quot;</span><span style="color: {colors['operator']}">)</span>
119
+ <span style="color: {colors['keyword']}">return</span> <span style="color: {colors['string']}">&quot;Code execution successful!&quot;</span>
120
 
121
+ <span style="color: {colors['comment']}># Execute the function</span>
122
+ <span style="color: {colors['function']}">result</span> <span style="color: {colors['operator']}">=</span> <span style="color: {colors['function']}">hello_aumcore</span><span style="color: {colors['operator']}">()</span>
123
+ <span style="color: {colors['function']}">print</span><span style="color: {colors['operator']}">(</span><span style="color: {colors['string']}>f&quot;✅ Result: </span><span style="color: {colors['operator']}">{{</span><span style="color: {colors['function']}">result</span><span style="color: {colors['operator']}">}}</span><span style="color: {colors['string']}">&quot;</span><span style="color: {colors['operator']}">)</span>
124
+ </code></pre>
125
+ </div>'''