Spaces:
Running
Running
File size: 12,836 Bytes
6605578 |
1 2 3 4 5 6 7 8 9 10 11 12 13 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern AI Chat</title>
<style>
/* Import Fonts */
@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@400;600&family=Poppins:wght@400;600&display=swap');
/* Highlight Title Styling for AI Response */
.highlight-title {
font-size: 1.2rem; /* Larger font size */
font-weight: 700; /* Extra bold */
color: #000; /* Black text */
display: inline-block; /* Ensure inline styling behaves properly */
margin: 5px 0; /* Add some spacing if needed */
}
/* Code Block Styling */
.code-block {
position: relative;
margin: 10px 0;
background: #f5f5f5;
border-radius: 5px;
padding: 10px;
overflow-x: auto;
}
.code-block pre {
margin: 0;
font-family: 'Courier New', Courier, monospace;
font-size: 0.9rem;
}
/* Copy Button Styling */
.copy-btn {
position: absolute;
top: 10px;
right: 10px;
padding: 5px 10px;
font-size: 0.8rem;
font-family: 'Poppins', sans-serif;
background: #6200ea;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s ease;
}
.copy-btn:hover {
background: #3700b3;
}
.copy-btn:active {
background: #311b92;
}
pre {
background: #f5f5f5;
padding: 10px;
border-radius: 5px;
font-family: 'Courier New', Courier, monospace;
font-size: 0.9rem;
overflow-x: auto; /* Enable horizontal scrolling for long code */
}
code {
background: #f5f5f5;
padding: 2px 4px;
border-radius: 3px;
font-family: 'Courier New', Courier, monospace;
font-size: 0.9rem;
}
/* Other styles for chat system */
body {
margin: 0;
padding: 0;
font-family: 'Poppins', sans-serif;
background: linear-gradient(135deg, #6E45E2, #88D3CE);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
}
/* Animation Background */
body::before {
content: '';
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
background: radial-gradient(circle, rgba(98, 0, 234, 0.6), transparent);
animation: backgroundAnimation 6s infinite linear alternate;
z-index: 0;
}
@keyframes backgroundAnimation {
to {
transform: rotate(360deg);
}
}
.chat-container {
position: relative;
z-index: 1;
width: 100%;
max-width: 500px;
margin: 0 auto;
background: #ffffff;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
display: flex;
flex-direction: column;
overflow: hidden;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
/* Header */
.chat-header {
background: linear-gradient(135deg, #6200ea, #3700b3);
color: #fff;
padding: 20px;
text-align: center;
font-size: 1.5rem;
font-weight: bold;
font-family: 'Cairo', sans-serif;
}
/* Chat Box */
.chat-box {
flex: 1;
padding: 20px;
overflow-y: auto; /* Enable vertical scrolling */
max-height: 400px; /* Restrict the height to make the scrolling work */
border-bottom: 1px solid #ddd;
display: flex;
flex-direction: column;
}
.chat-box .message {
margin-bottom: 15px;
font-size: 1rem;
padding: 10px 15px;
border-radius: 20px;
max-width: 75%;
animation: slideIn 0.3s ease-in-out;
word-wrap: break-word;
font-family: 'Cairo', 'Poppins', sans-serif;
}
@keyframes slideIn {
from {
transform: translateY(10px);
opacity: 0;
}
to {
transform: translateY(0);
opacity: 1;
}
}
.chat-box .message.user {
margin-left: auto;
background: #e3f2fd;
color: #0D47A1;
text-align: right;
}
.chat-box .message.ai {
margin-right: auto;
background: #e8f5e9;
color: #1B5E20;
text-align: left;
}
/* Typing Animation */
.typing-animation {
display: flex;
margin: 10px 0;
justify-content: left;
align-items: center;
}
.typing-animation span {
display: inline-block;
width: 8px;
height: 8px;
margin: 0 3px;
background: #1B5E20;
border-radius: 50%;
animation: typing 1.3s infinite;
}
.typing-animation span:nth-child(2) {
animation-delay: 0.2s;
}
.typing-animation span:nth-child(3) {
animation-delay: 0.4s;
}
@keyframes typing {
0%, 80% {
transform: scale(0);
}
40% {
transform: scale(1);
}
}
/* Chat Input */
.chat-input {
display: flex;
align-items: center;
padding: 15px;
background: #f9f9f9;
border-top: 1px solid #ddd;
}
.chat-input input {
flex: 1;
padding: 15px;
border: 1px solid #ddd;
border-radius: 30px;
margin-right: 10px;
font-size: 1rem;
font-family: 'Poppins', 'Cairo', sans-serif;
outline: none;
transition: all 0.3s ease;
}
.chat-input input:focus {
border-color: #6200ea;
box-shadow: 0 0 5px rgba(98, 0, 234, 0.5);
}
.chat-input button {
padding: 12px 18px;
background: linear-gradient(135deg, #6200ea, #3700b3);
border: none;
color: white;
border-radius: 20px;
cursor: pointer;
font-weight: bold;
font-size: 1rem;
font-family: 'Poppins', 'Cairo', sans-serif;
transition: all 0.3s ease;
}
.chat-input button:hover {
background: #5e35b1;
box-shadow: 0 5px 15px rgba(98, 0, 234, 0.4);
}
</style>
</head>
<body>
<div class="chat-container">
<div class="chat-header">
AI Chat with Omar's Smart Model
</div>
<div class="chat-box" id="chat-box">
<!-- Chat messages will populate here -->
</div>
<div class="chat-input">
<input type="text" id="user-input" placeholder="Type a message..." autocomplete="off">
<button id="send-btn">Send</button>
<button id="voice-btn">๐ค</button>
</div>
</div>
<script>
const apiUrl = "https://dooratre-test-flask-space.hf.space/chat";
const chatBox = document.getElementById("chat-box");
const userInput = document.getElementById("user-input");
const sendBtn = document.getElementById("send-btn");
// Initialize conversation history
let conversationHistory = [
{ "role": "system", "content": "you are AI model created and developed By MR Omar Nuwara In CORVO AI Company you are model name ALEX-9 you can chat and help user you are best model for now\n\n you will chat Omar's Freind Hussein Shkikel He is Libyan So chat with Libyan arabic accent ๐๐๐ make it fun with smart libyan brain ๐ Hussein now in the work he is working in ุงูุงุณุชุฎุจุงุฑุงุช๐๐" }
];
function formatResponse(content) {
// Replace multi-line code blocks with copy buttons
content = content.replace(/```(.*?)\n([\s\S]*?)```/g, (match, lang, code) => {
return `
<div class="code-block">
<button class="copy-btn" onclick="copyToClipboard(this)">Copy</button>
<pre><code>${code.trim()}</code></pre>
</div>
`;
});
// Replace inline code using ` for single-line code
content = content.replace(/`(.*?)`/g, '<code>$1</code>');
// Replace \n with <br> for line breaks (outside code blocks)
content = content.replace(/\n/g, '<br>');
// Replace **{title}** with <strong class="highlight-title">{title}</strong> for bold styled text
content = content.replace(/\*\*(.*?)\*\*/g, '<strong class="highlight-title">$1</strong>');
return content;
}
function copyToClipboard(button) {
// Find the <code> block by traversing the DOM
const codeBlock = button.parentNode.querySelector("code"); // Get the <code> block inside the same parent
const codeText = codeBlock.innerText; // Retrieve the text content of the <code> block
// Copy the text to the clipboard using the Clipboard API
navigator.clipboard.writeText(codeText).then(() => {
// Temporarily show "Copied!" message on the button
button.textContent = "Copied!";
setTimeout(() => {
button.textContent = "Copy"; // Reset button text after 2 seconds
}, 2000);
}).catch(err => {
console.error("Failed to copy the code:", err);
alert("Failed to copy the code. Please try again!");
});
}
// Function to add a message to the chat box
function addMessage(role, content) {
const messageDiv = document.createElement("div");
messageDiv.classList.add("message", role);
// Format content for AI messages
if (role === "ai") {
content = formatResponse(content);
}
messageDiv.innerHTML = content; // Use innerHTML to render formatted text
chatBox.appendChild(messageDiv);
// Ensure smooth scroll to the bottom
setTimeout(() => {
chatBox.scrollTop = chatBox.scrollHeight; // Auto-scroll to the bottom after rendering
}, 100);
}
// Function to show typing animation
function showTyping() {
const typingDiv = document.createElement("div");
typingDiv.classList.add("typing-animation");
typingDiv.innerHTML = `
<span></span>
<span></span>
<span></span>
`;
chatBox.appendChild(typingDiv);
// Ensure smooth scroll to include typing animation
setTimeout(() => {
chatBox.scrollTop = chatBox.scrollHeight;
}, 100);
return typingDiv; // Return the typing div to remove it later
}
// Function to play TTS for AI response
function playTTS(text) {
var tts_xhr = new XMLHttpRequest();
tts_xhr.open("POST", "https://api.braininc.net/be/vectordb/tts", true);
tts_xhr.setRequestHeader("Authorization", "token 72ec00483379076f580eb8126f29da802a5140c3");
tts_xhr.setRequestHeader("Content-Type", "application/json");
tts_xhr.responseType = 'blob';
tts_xhr.onreadystatechange = function() {
if (tts_xhr.readyState === 4 && tts_xhr.status === 200) {
var blob = tts_xhr.response;
var audio_url = URL.createObjectURL(blob);
// Create an audio element and play the audio
var audio = new Audio(audio_url);
audio.play();
}
};
var tts_payload = {
"pubsub_topic": "/studios/516104/wsuid_new-edge-67_nodeid_editor-66/customApi/1719909878183",
"text": text
};
tts_xhr.send(JSON.stringify(tts_payload));
}
// Function to send a message
async function sendMessage() {
const userMessage = userInput.value.trim();
if (!userMessage) return;
// Add user's message to the chat UI
addMessage("user", userMessage);
// Append user message to conversation history
conversationHistory.push({ "role": "user", "content": userMessage });
// Show typing animation
const typingDiv = showTyping();
// Prepare data for the API
const data = { message: conversationHistory };
try {
// Send message to AI using fetch
const response = await fetch(apiUrl, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(data)
});
if (response.ok) {
const aiResponse = await response.text();
// Remove typing animation
chatBox.removeChild(typingDiv);
// Add AI's response to the chat UI (with line breaks and bold formatting handled)
addMessage("ai", aiResponse);
// Append AI response to conversation history
conversationHistory.push({ "role": "assistant", "content": aiResponse });
// Play TTS for AI's response
playTTS(aiResponse);
} else {
throw new Error(`Error: ${response.status}`);
}
} catch (error) {
console.error("Error:", error);
chatBox.removeChild(typingDiv); // Remove typing animation
addMessage("ai", "AI: Sorry, something went wrong!");
}
// Clear input field
userInput.value = "";
}
// Attach event listeners
sendBtn.addEventListener("click", sendMessage);
userInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") sendMessage();
});
// Initialize Speech Recognition
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
recognition.lang = 'ar-LY'; // Set language to Arabic (Libya)
recognition.onstart = function() {
console.log('Voice recognition started. Speak now.');
};
recognition.onresult = function(event) {
const transcript = event.results[0][0].transcript; // Get the recognized text
userInput.value = transcript; // Set the input field to the recognized text
sendMessage(); // Send the message
};
recognition.onerror = function(event) {
console.error('Error occurred in recognition: ' + event.error);
};
// Attach event listener for the voice button
document.getElementById("voice-btn").addEventListener("click", () => {
recognition.start(); // Start voice recognition
});
</script>
</body>
</html> |