Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# app.py -
|
| 2 |
import os
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, Form
|
|
@@ -12,7 +12,7 @@ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
|
| 12 |
# Configuration
|
| 13 |
USERNAME = "Sanjay"
|
| 14 |
|
| 15 |
-
# UI
|
| 16 |
HTML_UI = '''
|
| 17 |
<!DOCTYPE html>
|
| 18 |
<html lang="en">
|
|
@@ -24,73 +24,487 @@ HTML_UI = '''
|
|
| 24 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 25 |
<style>
|
| 26 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Fira+Code:wght@400;500&display=swap');
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
</style>
|
| 46 |
</head>
|
| 47 |
<body>
|
| 48 |
<div class="sidebar">
|
| 49 |
-
<button class="nav-item new-chat-btn" onclick="window.location.reload()"
|
|
|
|
|
|
|
| 50 |
<div class="nav-item"><i class="fas fa-history"></i> History</div>
|
| 51 |
<div class="mt-auto">
|
| 52 |
-
<div class="nav-item reset-btn" onclick="confirmReset()"
|
|
|
|
|
|
|
| 53 |
<div class="nav-item"><i class="fas fa-cog"></i> Settings</div>
|
| 54 |
</div>
|
| 55 |
-
</div>
|
| 56 |
-
|
|
|
|
| 57 |
<div id="chat-log" class="chat-box"></div>
|
|
|
|
| 58 |
<div class="input-area">
|
| 59 |
<div class="input-container">
|
| 60 |
-
<textarea id="user-input" rows="1"
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
</div>
|
| 63 |
</div>
|
| 64 |
</div>
|
|
|
|
| 65 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
async function confirmReset() {
|
| 67 |
-
if(confirm("Sanjay bhai, memory
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
}
|
|
|
|
|
|
|
| 72 |
async function send() {
|
| 73 |
const input = document.getElementById('user-input');
|
| 74 |
const log = document.getElementById('chat-log');
|
| 75 |
const text = input.value.trim();
|
|
|
|
| 76 |
if(!text) return;
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
log.scrollTop = log.scrollHeight;
|
|
|
|
| 80 |
try {
|
| 81 |
const res = await fetch('/chat', {
|
| 82 |
method: 'POST',
|
| 83 |
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
| 84 |
body: 'message=' + encodeURIComponent(text)
|
| 85 |
});
|
|
|
|
| 86 |
const data = await res.json();
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
} catch (e) {
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
|
|
|
|
|
|
| 92 |
log.scrollTop = log.scrollHeight;
|
| 93 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
</script>
|
| 95 |
</body>
|
| 96 |
</html>
|
|
@@ -102,8 +516,13 @@ async def get_ui():
|
|
| 102 |
|
| 103 |
@app.post("/reset")
|
| 104 |
async def reset():
|
| 105 |
-
# TiDB reset logic (
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
@app.post("/chat")
|
| 109 |
async def chat(message: str = Form(...)):
|
|
@@ -114,42 +533,61 @@ async def chat(message: str = Form(...)):
|
|
| 114 |
lang_mode = detect_input_language(message)
|
| 115 |
system_prompt = get_system_prompt(lang_mode, USERNAME)
|
| 116 |
|
| 117 |
-
# β
GET HISTORY FROM TiDB (
|
| 118 |
recent_chats = []
|
| 119 |
try:
|
| 120 |
recent_chats = tidb_memory.get_recent_chats(limit=10)
|
| 121 |
except Exception as e:
|
| 122 |
-
print(f"TiDB history fetch error: {e}")
|
|
|
|
| 123 |
|
| 124 |
-
# Build messages
|
| 125 |
api_messages = [{"role": "system", "content": system_prompt}]
|
| 126 |
|
| 127 |
-
# Add history from TiDB
|
| 128 |
for chat_row in recent_chats:
|
| 129 |
user_input, ai_response, _ = chat_row
|
| 130 |
api_messages.append({"role": "user", "content": user_input})
|
| 131 |
api_messages.append({"role": "assistant", "content": ai_response})
|
| 132 |
|
|
|
|
| 133 |
api_messages.append({"role": "user", "content": message})
|
| 134 |
|
| 135 |
try:
|
|
|
|
| 136 |
completion = client.chat.completions.create(
|
| 137 |
model="llama-3.3-70b-versatile",
|
| 138 |
messages=api_messages,
|
| 139 |
temperature=0.3,
|
| 140 |
-
max_tokens=
|
|
|
|
| 141 |
)
|
|
|
|
| 142 |
ai_response = completion.choices[0].message.content.strip()
|
| 143 |
|
| 144 |
# β
SAVE TO TiDB
|
| 145 |
try:
|
| 146 |
tidb_memory.save_chat(message, ai_response, lang_mode)
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
-
print(f"TiDB save error: {e}")
|
| 149 |
|
| 150 |
return {"response": ai_response}
|
|
|
|
| 151 |
except Exception as e:
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py - UPDATED WITH CODE FORMATTING & COPY BUTTON (300+ lines)
|
| 2 |
import os
|
| 3 |
import uvicorn
|
| 4 |
from fastapi import FastAPI, Form
|
|
|
|
| 12 |
# Configuration
|
| 13 |
USERNAME = "Sanjay"
|
| 14 |
|
| 15 |
+
# UI WITH ENHANCED CODE FORMATTING
|
| 16 |
HTML_UI = '''
|
| 17 |
<!DOCTYPE html>
|
| 18 |
<html lang="en">
|
|
|
|
| 24 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 25 |
<style>
|
| 26 |
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600&family=Fira+Code:wght@400;500&display=swap');
|
| 27 |
+
|
| 28 |
+
/* Base Styles */
|
| 29 |
+
body {
|
| 30 |
+
background-color: #0d1117;
|
| 31 |
+
color: #c9d1d9;
|
| 32 |
+
font-family: 'Inter', sans-serif;
|
| 33 |
+
display: flex;
|
| 34 |
+
height: 100vh;
|
| 35 |
+
overflow: hidden;
|
| 36 |
+
margin: 0;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/* Sidebar */
|
| 40 |
+
.sidebar {
|
| 41 |
+
width: 260px;
|
| 42 |
+
background: #010409;
|
| 43 |
+
border-right: 1px solid #30363d;
|
| 44 |
+
display: flex;
|
| 45 |
+
flex-direction: column;
|
| 46 |
+
padding: 15px;
|
| 47 |
+
flex-shrink: 0;
|
| 48 |
+
}
|
| 49 |
+
.nav-item {
|
| 50 |
+
padding: 12px;
|
| 51 |
+
margin-bottom: 5px;
|
| 52 |
+
border-radius: 8px;
|
| 53 |
+
cursor: pointer;
|
| 54 |
+
display: flex;
|
| 55 |
+
align-items: center;
|
| 56 |
+
gap: 12px;
|
| 57 |
+
color: #8b949e;
|
| 58 |
+
transition: all 0.2s ease;
|
| 59 |
+
}
|
| 60 |
+
.nav-item:hover {
|
| 61 |
+
background: #161b22;
|
| 62 |
+
color: white;
|
| 63 |
+
}
|
| 64 |
+
.new-chat-btn {
|
| 65 |
+
background: #238636;
|
| 66 |
+
color: white !important;
|
| 67 |
+
font-weight: 600;
|
| 68 |
+
margin-bottom: 20px;
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
/* Main Chat Area */
|
| 72 |
+
.main-chat {
|
| 73 |
+
flex: 1;
|
| 74 |
+
display: flex;
|
| 75 |
+
flex-direction: column;
|
| 76 |
+
background: #0d1117;
|
| 77 |
+
position: relative;
|
| 78 |
+
}
|
| 79 |
+
.chat-box {
|
| 80 |
+
flex: 1;
|
| 81 |
+
overflow-y: auto;
|
| 82 |
+
display: flex;
|
| 83 |
+
flex-direction: column;
|
| 84 |
+
align-items: center;
|
| 85 |
+
padding: 60px 20px 120px 20px;
|
| 86 |
+
scroll-behavior: smooth;
|
| 87 |
+
}
|
| 88 |
+
.message-wrapper {
|
| 89 |
+
width: 100%;
|
| 90 |
+
max-width: 760px;
|
| 91 |
+
display: flex;
|
| 92 |
+
flex-direction: column;
|
| 93 |
+
margin-bottom: 35px;
|
| 94 |
+
animation: fadeIn 0.3s ease;
|
| 95 |
+
}
|
| 96 |
+
@keyframes fadeIn {
|
| 97 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 98 |
+
to { opacity: 1; transform: translateY(0); }
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/* Message Bubbles */
|
| 102 |
+
.bubble {
|
| 103 |
+
padding: 5px 0;
|
| 104 |
+
font-size: 17px;
|
| 105 |
+
line-height: 1.8;
|
| 106 |
+
width: 100%;
|
| 107 |
+
word-wrap: break-word;
|
| 108 |
+
white-space: pre-wrap;
|
| 109 |
+
}
|
| 110 |
+
.user-text {
|
| 111 |
+
color: #58a6ff;
|
| 112 |
+
font-weight: 600;
|
| 113 |
+
letter-spacing: -0.2px;
|
| 114 |
+
}
|
| 115 |
+
.ai-text {
|
| 116 |
+
color: #e6edf3;
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
/* ENHANCED CODE BLOCK STYLING */
|
| 120 |
+
.code-container {
|
| 121 |
+
background: #0d1117;
|
| 122 |
+
border: 1px solid #30363d;
|
| 123 |
+
border-radius: 12px;
|
| 124 |
+
margin: 20px 0;
|
| 125 |
+
overflow: hidden;
|
| 126 |
+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
.code-header {
|
| 130 |
+
background: #161b22;
|
| 131 |
+
padding: 12px 18px;
|
| 132 |
+
display: flex;
|
| 133 |
+
justify-content: space-between;
|
| 134 |
+
align-items: center;
|
| 135 |
+
border-bottom: 1px solid #30363d;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
.code-lang {
|
| 139 |
+
color: #79c0ff;
|
| 140 |
+
font-family: 'Fira Code', monospace;
|
| 141 |
+
font-size: 14px;
|
| 142 |
+
font-weight: 600;
|
| 143 |
+
display: flex;
|
| 144 |
+
align-items: center;
|
| 145 |
+
gap: 8px;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
.code-lang::before {
|
| 149 |
+
content: "β¦";
|
| 150 |
+
color: #7ee787;
|
| 151 |
+
font-size: 12px;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
.copy-btn {
|
| 155 |
+
background: #238636;
|
| 156 |
+
color: white;
|
| 157 |
+
border: none;
|
| 158 |
+
padding: 6px 14px;
|
| 159 |
+
border-radius: 6px;
|
| 160 |
+
cursor: pointer;
|
| 161 |
+
font-size: 13px;
|
| 162 |
+
font-family: 'Inter', sans-serif;
|
| 163 |
+
font-weight: 500;
|
| 164 |
+
transition: all 0.2s ease;
|
| 165 |
+
display: flex;
|
| 166 |
+
align-items: center;
|
| 167 |
+
gap: 6px;
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
.copy-btn:hover {
|
| 171 |
+
background: #2ea043;
|
| 172 |
+
transform: translateY(-1px);
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
.copy-btn:active {
|
| 176 |
+
transform: translateY(0);
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
.copy-btn.copied {
|
| 180 |
+
background: #7ee787;
|
| 181 |
+
color: #0d1117;
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
.code-container pre {
|
| 185 |
+
background: transparent;
|
| 186 |
+
border: none;
|
| 187 |
+
margin: 0;
|
| 188 |
+
padding: 20px;
|
| 189 |
+
overflow-x: auto;
|
| 190 |
+
scrollbar-width: thin;
|
| 191 |
+
scrollbar-color: #30363d #0d1117;
|
| 192 |
+
}
|
| 193 |
+
|
| 194 |
+
.code-container pre::-webkit-scrollbar {
|
| 195 |
+
height: 8px;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.code-container pre::-webkit-scrollbar-track {
|
| 199 |
+
background: #0d1117;
|
| 200 |
+
border-radius: 4px;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
.code-container pre::-webkit-scrollbar-thumb {
|
| 204 |
+
background: #30363d;
|
| 205 |
+
border-radius: 4px;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
.code-container pre::-webkit-scrollbar-thumb:hover {
|
| 209 |
+
background: #484f58;
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
.code-container code {
|
| 213 |
+
color: #79c0ff;
|
| 214 |
+
font-size: 14.5px;
|
| 215 |
+
line-height: 1.7;
|
| 216 |
+
font-family: 'Fira Code', monospace;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
/* Syntax Highlighting */
|
| 220 |
+
.code-container .keyword { color: #ff7b72; }
|
| 221 |
+
.code-container .function { color: #d2a8ff; }
|
| 222 |
+
.code-container .string { color: #a5d6ff; }
|
| 223 |
+
.code-container .comment { color: #8b949e; font-style: italic; }
|
| 224 |
+
.code-container .number { color: #79c0ff; }
|
| 225 |
+
.code-container .operator { color: #ff7b72; }
|
| 226 |
+
|
| 227 |
+
/* Input Area */
|
| 228 |
+
.input-area {
|
| 229 |
+
position: absolute;
|
| 230 |
+
bottom: 0;
|
| 231 |
+
left: 0;
|
| 232 |
+
width: 100%;
|
| 233 |
+
background: linear-gradient(transparent, #0d1117 40%);
|
| 234 |
+
padding-bottom: 30px;
|
| 235 |
+
}
|
| 236 |
+
.input-container {
|
| 237 |
+
max-width: 800px;
|
| 238 |
+
margin: 0 auto;
|
| 239 |
+
background: #161b22;
|
| 240 |
+
border: 1px solid #30363d;
|
| 241 |
+
border-radius: 16px;
|
| 242 |
+
padding: 14px 20px;
|
| 243 |
+
display: flex;
|
| 244 |
+
align-items: flex-end;
|
| 245 |
+
gap: 14px;
|
| 246 |
+
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
|
| 247 |
+
}
|
| 248 |
+
#user-input {
|
| 249 |
+
background: transparent;
|
| 250 |
+
flex: 1;
|
| 251 |
+
outline: none;
|
| 252 |
+
border: none;
|
| 253 |
+
color: white;
|
| 254 |
+
font-size: 16px;
|
| 255 |
+
resize: none;
|
| 256 |
+
max-height: 200px;
|
| 257 |
+
min-height: 32px;
|
| 258 |
+
line-height: 1.5;
|
| 259 |
+
padding: 4px 0;
|
| 260 |
+
font-family: 'Inter', sans-serif;
|
| 261 |
+
}
|
| 262 |
+
.send-btn {
|
| 263 |
+
color: #58a6ff;
|
| 264 |
+
transition: transform 0.1s ease;
|
| 265 |
+
margin-bottom: 6px;
|
| 266 |
+
background: transparent;
|
| 267 |
+
border: none;
|
| 268 |
+
cursor: pointer;
|
| 269 |
+
font-size: 18px;
|
| 270 |
+
}
|
| 271 |
+
.send-btn:hover {
|
| 272 |
+
color: #79c0ff;
|
| 273 |
+
transform: scale(1.1);
|
| 274 |
+
}
|
| 275 |
+
.send-btn:active {
|
| 276 |
+
transform: scale(0.95);
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
/* Error Messages */
|
| 280 |
+
.error-message {
|
| 281 |
+
color: #f85149;
|
| 282 |
+
background: rgba(248, 81, 73, 0.1);
|
| 283 |
+
border: 1px solid rgba(248, 81, 73, 0.3);
|
| 284 |
+
padding: 12px 16px;
|
| 285 |
+
border-radius: 8px;
|
| 286 |
+
font-weight: 500;
|
| 287 |
+
margin: 10px 0;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
/* Loading Animation */
|
| 291 |
+
.typing-indicator {
|
| 292 |
+
display: flex;
|
| 293 |
+
gap: 4px;
|
| 294 |
+
padding: 10px 0;
|
| 295 |
+
}
|
| 296 |
+
|
| 297 |
+
.typing-dot {
|
| 298 |
+
width: 8px;
|
| 299 |
+
height: 8px;
|
| 300 |
+
background: #58a6ff;
|
| 301 |
+
border-radius: 50%;
|
| 302 |
+
animation: typingAnimation 1.4s infinite ease-in-out;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
.typing-dot:nth-child(1) { animation-delay: -0.32s; }
|
| 306 |
+
.typing-dot:nth-child(2) { animation-delay: -0.16s; }
|
| 307 |
+
|
| 308 |
+
@keyframes typingAnimation {
|
| 309 |
+
0%, 80%, 100% { transform: scale(0); }
|
| 310 |
+
40% { transform: scale(1); }
|
| 311 |
+
}
|
| 312 |
</style>
|
| 313 |
</head>
|
| 314 |
<body>
|
| 315 |
<div class="sidebar">
|
| 316 |
+
<button class="nav-item new-chat-btn" onclick="window.location.reload()">
|
| 317 |
+
<i class="fas fa-plus"></i> New Chat
|
| 318 |
+
</button>
|
| 319 |
<div class="nav-item"><i class="fas fa-history"></i> History</div>
|
| 320 |
<div class="mt-auto">
|
| 321 |
+
<div class="nav-item reset-btn" onclick="confirmReset()">
|
| 322 |
+
<i class="fas fa-trash-alt"></i> Reset Memory
|
| 323 |
+
</div>
|
| 324 |
<div class="nav-item"><i class="fas fa-cog"></i> Settings</div>
|
| 325 |
</div>
|
| 326 |
+
</div>
|
| 327 |
+
|
| 328 |
+
<div class="main-chat">
|
| 329 |
<div id="chat-log" class="chat-box"></div>
|
| 330 |
+
|
| 331 |
<div class="input-area">
|
| 332 |
<div class="input-container">
|
| 333 |
+
<textarea id="user-input" rows="1"
|
| 334 |
+
placeholder="Type your message to AumCore..."
|
| 335 |
+
autocomplete="off"
|
| 336 |
+
oninput="resizeInput(this)"
|
| 337 |
+
onkeydown="handleKey(event)"></textarea>
|
| 338 |
+
<button onclick="send()" class="send-btn">
|
| 339 |
+
<i class="fas fa-paper-plane fa-lg"></i>
|
| 340 |
+
</button>
|
| 341 |
</div>
|
| 342 |
</div>
|
| 343 |
</div>
|
| 344 |
+
|
| 345 |
<script>
|
| 346 |
+
// Utility Functions
|
| 347 |
+
function resizeInput(el) {
|
| 348 |
+
el.style.height = 'auto';
|
| 349 |
+
el.style.height = el.scrollHeight + 'px';
|
| 350 |
+
}
|
| 351 |
+
|
| 352 |
+
function handleKey(e) {
|
| 353 |
+
if (e.key === 'Enter' && !e.shiftKey) {
|
| 354 |
+
e.preventDefault();
|
| 355 |
+
send();
|
| 356 |
+
}
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
// Code Block Formatter with Syntax Highlighting
|
| 360 |
+
function formatCodeBlocks(text) {
|
| 361 |
+
// Process Python code blocks
|
| 362 |
+
let formatted = text.replace(/```python\s*([\s\S]*?)```/g,
|
| 363 |
+
`<div class="code-container">
|
| 364 |
+
<div class="code-header">
|
| 365 |
+
<div class="code-lang">Python</div>
|
| 366 |
+
<button class="copy-btn" onclick="copyCode(this)">
|
| 367 |
+
<i class="fas fa-copy"></i> Copy
|
| 368 |
+
</button>
|
| 369 |
+
</div>
|
| 370 |
+
<pre><code class="language-python">$1</code></pre>
|
| 371 |
+
</div>`
|
| 372 |
+
);
|
| 373 |
+
|
| 374 |
+
// Process generic code blocks (no language specified)
|
| 375 |
+
formatted = formatted.replace(/```\s*([\s\S]*?)```/g,
|
| 376 |
+
`<div class="code-container">
|
| 377 |
+
<div class="code-header">
|
| 378 |
+
<div class="code-lang">Code</div>
|
| 379 |
+
<button class="copy-btn" onclick="copyCode(this)">
|
| 380 |
+
<i class="fas fa-copy"></i> Copy
|
| 381 |
+
</button>
|
| 382 |
+
</div>
|
| 383 |
+
<pre><code>$1</code></pre>
|
| 384 |
+
</div>`
|
| 385 |
+
);
|
| 386 |
+
|
| 387 |
+
return formatted;
|
| 388 |
+
}
|
| 389 |
+
|
| 390 |
+
// Copy Code Function
|
| 391 |
+
function copyCode(button) {
|
| 392 |
+
const codeBlock = button.parentElement.nextElementSibling;
|
| 393 |
+
const codeText = codeBlock.innerText;
|
| 394 |
+
|
| 395 |
+
navigator.clipboard.writeText(codeText).then(() => {
|
| 396 |
+
const originalHTML = button.innerHTML;
|
| 397 |
+
const originalClass = button.className;
|
| 398 |
+
|
| 399 |
+
// Visual feedback
|
| 400 |
+
button.innerHTML = '<i class="fas fa-check"></i> Copied!';
|
| 401 |
+
button.className = 'copy-btn copied';
|
| 402 |
+
|
| 403 |
+
// Revert after 2 seconds
|
| 404 |
+
setTimeout(() => {
|
| 405 |
+
button.innerHTML = originalHTML;
|
| 406 |
+
button.className = originalClass;
|
| 407 |
+
}, 2000);
|
| 408 |
+
}).catch(err => {
|
| 409 |
+
console.error('Copy failed:', err);
|
| 410 |
+
button.innerHTML = '<i class="fas fa-times"></i> Failed';
|
| 411 |
+
setTimeout(() => {
|
| 412 |
+
button.innerHTML = '<i class="fas fa-copy"></i> Copy';
|
| 413 |
+
}, 2000);
|
| 414 |
+
});
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
// Reset Memory Confirmation
|
| 418 |
async function confirmReset() {
|
| 419 |
+
if(confirm("Sanjay bhai, kya aap sach mein saari memory delete karna chahte hain?")) {
|
| 420 |
+
try {
|
| 421 |
+
const res = await fetch('/reset', { method: 'POST' });
|
| 422 |
+
const data = await res.json();
|
| 423 |
+
alert(data.message);
|
| 424 |
+
window.location.reload();
|
| 425 |
+
} catch (e) {
|
| 426 |
+
alert("Reset failed: " + e.message);
|
| 427 |
+
}
|
| 428 |
}
|
| 429 |
}
|
| 430 |
+
|
| 431 |
+
// Main Send Function with Enhanced Features
|
| 432 |
async function send() {
|
| 433 |
const input = document.getElementById('user-input');
|
| 434 |
const log = document.getElementById('chat-log');
|
| 435 |
const text = input.value.trim();
|
| 436 |
+
|
| 437 |
if(!text) return;
|
| 438 |
+
|
| 439 |
+
// Add user message
|
| 440 |
+
log.innerHTML += `
|
| 441 |
+
<div class="message-wrapper">
|
| 442 |
+
<div class="bubble user-text">${text}</div>
|
| 443 |
+
</div>`;
|
| 444 |
+
|
| 445 |
+
// Clear and resize input
|
| 446 |
+
input.value = '';
|
| 447 |
+
input.style.height = 'auto';
|
| 448 |
+
|
| 449 |
+
// Add typing indicator
|
| 450 |
+
const typingId = 'typing-' + Date.now();
|
| 451 |
+
log.innerHTML += `
|
| 452 |
+
<div class="message-wrapper" id="${typingId}">
|
| 453 |
+
<div class="typing-indicator">
|
| 454 |
+
<div class="typing-dot"></div>
|
| 455 |
+
<div class="typing-dot"></div>
|
| 456 |
+
<div class="typing-dot"></div>
|
| 457 |
+
</div>
|
| 458 |
+
</div>`;
|
| 459 |
+
|
| 460 |
+
// Scroll to bottom
|
| 461 |
log.scrollTop = log.scrollHeight;
|
| 462 |
+
|
| 463 |
try {
|
| 464 |
const res = await fetch('/chat', {
|
| 465 |
method: 'POST',
|
| 466 |
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
| 467 |
body: 'message=' + encodeURIComponent(text)
|
| 468 |
});
|
| 469 |
+
|
| 470 |
const data = await res.json();
|
| 471 |
+
|
| 472 |
+
// Remove typing indicator
|
| 473 |
+
const typingElem = document.getElementById(typingId);
|
| 474 |
+
if (typingElem) typingElem.remove();
|
| 475 |
+
|
| 476 |
+
// Format response with enhanced code blocks
|
| 477 |
+
let formatted = formatCodeBlocks(data.response);
|
| 478 |
+
|
| 479 |
+
// Add AI response
|
| 480 |
+
log.innerHTML += `
|
| 481 |
+
<div class="message-wrapper">
|
| 482 |
+
<div class="bubble ai-text">${formatted}</div>
|
| 483 |
+
</div>`;
|
| 484 |
+
|
| 485 |
} catch (e) {
|
| 486 |
+
// Remove typing indicator on error
|
| 487 |
+
const typingElem = document.getElementById(typingId);
|
| 488 |
+
if (typingElem) typingElem.remove();
|
| 489 |
+
|
| 490 |
+
log.innerHTML += `
|
| 491 |
+
<div class="message-wrapper">
|
| 492 |
+
<div class="error-message">
|
| 493 |
+
<i class="fas fa-exclamation-circle"></i>
|
| 494 |
+
Error connecting to AumCore. Please try again.
|
| 495 |
+
</div>
|
| 496 |
+
</div>`;
|
| 497 |
}
|
| 498 |
+
|
| 499 |
+
// Scroll to bottom
|
| 500 |
log.scrollTop = log.scrollHeight;
|
| 501 |
}
|
| 502 |
+
|
| 503 |
+
// Auto-focus input on load
|
| 504 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 505 |
+
const input = document.getElementById('user-input');
|
| 506 |
+
if (input) input.focus();
|
| 507 |
+
});
|
| 508 |
</script>
|
| 509 |
</body>
|
| 510 |
</html>
|
|
|
|
| 516 |
|
| 517 |
@app.post("/reset")
|
| 518 |
async def reset():
|
| 519 |
+
# TiDB reset logic (will add later)
|
| 520 |
+
try:
|
| 521 |
+
from memory_db import tidb_memory
|
| 522 |
+
# Add reset logic here when TiDB is stable
|
| 523 |
+
return {"message": "Memory clear ho gayi hai!"}
|
| 524 |
+
except Exception as e:
|
| 525 |
+
return {"message": f"Reset completed with note: {str(e)}"}
|
| 526 |
|
| 527 |
@app.post("/chat")
|
| 528 |
async def chat(message: str = Form(...)):
|
|
|
|
| 533 |
lang_mode = detect_input_language(message)
|
| 534 |
system_prompt = get_system_prompt(lang_mode, USERNAME)
|
| 535 |
|
| 536 |
+
# β
GET HISTORY FROM TiDB (instead of JSON)
|
| 537 |
recent_chats = []
|
| 538 |
try:
|
| 539 |
recent_chats = tidb_memory.get_recent_chats(limit=10)
|
| 540 |
except Exception as e:
|
| 541 |
+
print(f"β οΈ TiDB history fetch error: {e}")
|
| 542 |
+
# Fallback to empty history if TiDB fails
|
| 543 |
|
| 544 |
+
# Build messages for AI
|
| 545 |
api_messages = [{"role": "system", "content": system_prompt}]
|
| 546 |
|
| 547 |
+
# Add conversation history from TiDB
|
| 548 |
for chat_row in recent_chats:
|
| 549 |
user_input, ai_response, _ = chat_row
|
| 550 |
api_messages.append({"role": "user", "content": user_input})
|
| 551 |
api_messages.append({"role": "assistant", "content": ai_response})
|
| 552 |
|
| 553 |
+
# Add current message
|
| 554 |
api_messages.append({"role": "user", "content": message})
|
| 555 |
|
| 556 |
try:
|
| 557 |
+
# Get AI response
|
| 558 |
completion = client.chat.completions.create(
|
| 559 |
model="llama-3.3-70b-versatile",
|
| 560 |
messages=api_messages,
|
| 561 |
temperature=0.3,
|
| 562 |
+
max_tokens=1000, # Increased for better code responses
|
| 563 |
+
stop=["```"] # Prevent markdown blocks
|
| 564 |
)
|
| 565 |
+
|
| 566 |
ai_response = completion.choices[0].message.content.strip()
|
| 567 |
|
| 568 |
# β
SAVE TO TiDB
|
| 569 |
try:
|
| 570 |
tidb_memory.save_chat(message, ai_response, lang_mode)
|
| 571 |
+
print(f"β
Chat saved to TiDB - Language: {lang_mode}")
|
| 572 |
except Exception as e:
|
| 573 |
+
print(f"β οΈ TiDB save error: {e}")
|
| 574 |
|
| 575 |
return {"response": ai_response}
|
| 576 |
+
|
| 577 |
except Exception as e:
|
| 578 |
+
error_msg = f"System Error: {str(e)}"
|
| 579 |
+
print(f"β API Error: {error_msg}")
|
| 580 |
+
return {"response": error_msg}
|
| 581 |
|
| 582 |
if __name__ == "__main__":
|
| 583 |
+
print("=" * 60)
|
| 584 |
+
print("π AUMCORE AI - FINAL BUILD STARTING")
|
| 585 |
+
print("=" * 60)
|
| 586 |
+
print(f"π Username: {USERNAME}")
|
| 587 |
+
print(f"π Server: http://0.0.0.0:7860")
|
| 588 |
+
print(f"π€ AI Model: llama-3.3-70b-versatile")
|
| 589 |
+
print(f"πΎ Database: TiDB Cloud")
|
| 590 |
+
print(f"π¨ UI Features: Code formatting + Copy button")
|
| 591 |
+
print("=" * 60)
|
| 592 |
+
|
| 593 |
+
uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")
|