Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import uvicorn
|
|
| 5 |
import asyncio
|
| 6 |
import importlib.util
|
| 7 |
import json
|
|
|
|
| 8 |
from pathlib import Path
|
| 9 |
from fastapi import FastAPI, Form, APIRouter
|
| 10 |
from fastapi.responses import HTMLResponse, JSONResponse
|
|
@@ -390,6 +391,39 @@ body {
|
|
| 390 |
}
|
| 391 |
.module-active { color: #7ee787; }
|
| 392 |
.module-inactive { color: #f85149; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
</style>
|
| 394 |
</head>
|
| 395 |
<body>
|
|
@@ -419,12 +453,20 @@ body {
|
|
| 419 |
function resizeInput(el){el.style.height='auto';el.style.height=el.scrollHeight+'px';}
|
| 420 |
// Handle Enter key for send
|
| 421 |
function handleKey(e){if(e.key==='Enter' && !e.shiftKey){e.preventDefault();send();}}
|
| 422 |
-
// Format code blocks
|
| 423 |
function formatCodeBlocks(text){
|
| 424 |
-
|
|
|
|
| 425 |
`<div class="code-container"><div class="code-header"><div class="code-lang">Python</div><button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy</button></div><pre><code class="language-python">$1</code></pre></div>`);
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
`<div class="code-container"><div class="code-header"><div class="code-lang">Code</div><button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy</button></div><pre><code>$1</code></pre></div>`);
|
|
|
|
| 428 |
return formatted;
|
| 429 |
}
|
| 430 |
// Copy code to clipboard
|
|
@@ -599,7 +641,7 @@ async function send(){
|
|
| 599 |
const data=await res.json();
|
| 600 |
const typingElem=document.getElementById(typingId); if(typingElem)typingElem.remove();
|
| 601 |
let formatted=formatCodeBlocks(data.response);
|
| 602 |
-
log.innerHTML+=`<div class="message-wrapper"><div class="bubble ai-text">${formatted}</div></div>`;
|
| 603 |
}catch(e){
|
| 604 |
const typingElem=document.getElementById(typingId); if(typingElem)typingElem.remove();
|
| 605 |
log.innerHTML+=`<div class="message-wrapper"><div class="error-message"><i class="fas fa-exclamation-circle"></i> Error connecting to AumCore. Please try again.</div></div>`;
|
|
@@ -687,6 +729,27 @@ async def chat(message: str = Form(...)):
|
|
| 687 |
)
|
| 688 |
ai_response = completion.choices[0].message.content.strip()
|
| 689 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
# Save to database
|
| 691 |
try:
|
| 692 |
tidb_memory.save_chat(message, ai_response, lang_mode)
|
|
|
|
| 5 |
import asyncio
|
| 6 |
import importlib.util
|
| 7 |
import json
|
| 8 |
+
import markdown # ADDED FOR MARKDOWN RENDERING
|
| 9 |
from pathlib import Path
|
| 10 |
from fastapi import FastAPI, Form, APIRouter
|
| 11 |
from fastapi.responses import HTMLResponse, JSONResponse
|
|
|
|
| 391 |
}
|
| 392 |
.module-active { color: #7ee787; }
|
| 393 |
.module-inactive { color: #f85149; }
|
| 394 |
+
/* Markdown Styling */
|
| 395 |
+
.markdown-content h1, .markdown-content h2, .markdown-content h3 {
|
| 396 |
+
color: #58a6ff;
|
| 397 |
+
margin-top: 20px;
|
| 398 |
+
margin-bottom: 10px;
|
| 399 |
+
}
|
| 400 |
+
.markdown-content p {
|
| 401 |
+
margin: 10px 0;
|
| 402 |
+
}
|
| 403 |
+
.markdown-content ul, .markdown-content ol {
|
| 404 |
+
margin: 10px 0 10px 20px;
|
| 405 |
+
}
|
| 406 |
+
.markdown-content li {
|
| 407 |
+
margin: 5px 0;
|
| 408 |
+
}
|
| 409 |
+
.markdown-content strong {
|
| 410 |
+
color: #79c0ff;
|
| 411 |
+
}
|
| 412 |
+
.markdown-content em {
|
| 413 |
+
color: #c9d1d9;
|
| 414 |
+
font-style: italic;
|
| 415 |
+
}
|
| 416 |
+
.markdown-content code:not(pre code) {
|
| 417 |
+
background: #161b22;
|
| 418 |
+
color: #7ee787;
|
| 419 |
+
padding: 2px 6px;
|
| 420 |
+
border-radius: 4px;
|
| 421 |
+
font-family: 'Fira Code', monospace;
|
| 422 |
+
font-size: 14px;
|
| 423 |
+
}
|
| 424 |
+
.markdown-content pre {
|
| 425 |
+
margin: 0;
|
| 426 |
+
}
|
| 427 |
</style>
|
| 428 |
</head>
|
| 429 |
<body>
|
|
|
|
| 453 |
function resizeInput(el){el.style.height='auto';el.style.height=el.scrollHeight+'px';}
|
| 454 |
// Handle Enter key for send
|
| 455 |
function handleKey(e){if(e.key==='Enter' && !e.shiftKey){e.preventDefault();send();}}
|
| 456 |
+
// Format code blocks - UPDATED FOR #### CodePython
|
| 457 |
function formatCodeBlocks(text){
|
| 458 |
+
// Fix for #### CodePython
|
| 459 |
+
let formatted = text.replace(/#### CodePython\s*\n?```python\s*([\s\S]*?)```/gi,
|
| 460 |
`<div class="code-container"><div class="code-header"><div class="code-lang">Python</div><button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy</button></div><pre><code class="language-python">$1</code></pre></div>`);
|
| 461 |
+
|
| 462 |
+
// Original Python code blocks
|
| 463 |
+
formatted = formatted.replace(/```python\s*([\s\S]*?)```/gi,
|
| 464 |
+
`<div class="code-container"><div class="code-header"><div class="code-lang">Python</div><button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy</button></div><pre><code class="language-python">$1</code></pre></div>`);
|
| 465 |
+
|
| 466 |
+
// Generic code blocks
|
| 467 |
+
formatted = formatted.replace(/```\s*([\s\S]*?)```/g,
|
| 468 |
`<div class="code-container"><div class="code-header"><div class="code-lang">Code</div><button class="copy-btn" onclick="copyCode(this)"><i class="fas fa-copy"></i> Copy</button></div><pre><code>$1</code></pre></div>`);
|
| 469 |
+
|
| 470 |
return formatted;
|
| 471 |
}
|
| 472 |
// Copy code to clipboard
|
|
|
|
| 641 |
const data=await res.json();
|
| 642 |
const typingElem=document.getElementById(typingId); if(typingElem)typingElem.remove();
|
| 643 |
let formatted=formatCodeBlocks(data.response);
|
| 644 |
+
log.innerHTML+=`<div class="message-wrapper"><div class="bubble ai-text markdown-content">${formatted}</div></div>`;
|
| 645 |
}catch(e){
|
| 646 |
const typingElem=document.getElementById(typingId); if(typingElem)typingElem.remove();
|
| 647 |
log.innerHTML+=`<div class="message-wrapper"><div class="error-message"><i class="fas fa-exclamation-circle"></i> Error connecting to AumCore. Please try again.</div></div>`;
|
|
|
|
| 729 |
)
|
| 730 |
ai_response = completion.choices[0].message.content.strip()
|
| 731 |
|
| 732 |
+
# ============================================
|
| 733 |
+
# RESPONSE HIJACKING: TITAN ORCHESTRATOR ACTIVATION
|
| 734 |
+
# ============================================
|
| 735 |
+
try:
|
| 736 |
+
orchestrator_module = module_manager.get_module("orchestrator")
|
| 737 |
+
if orchestrator_module and hasattr(orchestrator_module, 'process_response'):
|
| 738 |
+
print("🔗 Activating Titan Orchestrator...")
|
| 739 |
+
ai_response = await orchestrator_module.process_response(message, ai_response, client)
|
| 740 |
+
print("✅ Titan Orchestrator processing completed")
|
| 741 |
+
except Exception as e:
|
| 742 |
+
print(f"⚠️ Titan Orchestrator error: {str(e)}")
|
| 743 |
+
# ============================================
|
| 744 |
+
|
| 745 |
+
# MARKDOWN RENDERING: Convert markdown to HTML
|
| 746 |
+
try:
|
| 747 |
+
# Convert markdown to HTML
|
| 748 |
+
ai_response = markdown.markdown(ai_response, extensions=['fenced_code', 'tables', 'nl2br'])
|
| 749 |
+
except Exception as e:
|
| 750 |
+
print(f"⚠️ Markdown rendering error: {e}")
|
| 751 |
+
# Keep original response if markdown fails
|
| 752 |
+
|
| 753 |
# Save to database
|
| 754 |
try:
|
| 755 |
tidb_memory.save_chat(message, ai_response, lang_mode)
|