Spaces:
Runtime error
Runtime error
Create script.js
Browse files
script.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
let conversations = JSON.parse(localStorage.getItem('gemini_history')) || {};
|
| 2 |
+
let currentChatId = null;
|
| 3 |
+
|
| 4 |
+
function toggleSidebar() {
|
| 5 |
+
const sidebar = document.getElementById('sidebar');
|
| 6 |
+
sidebar.classList.toggle('closed');
|
| 7 |
+
}
|
| 8 |
+
|
| 9 |
+
function renderHistory() {
|
| 10 |
+
const list = document.getElementById('historyList');
|
| 11 |
+
list.innerHTML = '';
|
| 12 |
+
Object.keys(conversations).sort((a,b) => b-a).forEach(id => {
|
| 13 |
+
const item = document.createElement('div');
|
| 14 |
+
item.className = 'history-item';
|
| 15 |
+
if(id === currentChatId) item.style.backgroundColor = "#282a2c";
|
| 16 |
+
item.innerHTML = `
|
| 17 |
+
<span onclick="loadChat('${id}')" style="flex:1; overflow:hidden; text-overflow:ellipsis; white-space:nowrap;">${conversations[id].title}</span>
|
| 18 |
+
<button class="delete-chat-btn" onclick="deleteChat('${id}', event)">✕</button>
|
| 19 |
+
`;
|
| 20 |
+
list.appendChild(item);
|
| 21 |
+
});
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
function startNewChat() {
|
| 25 |
+
currentChatId = 'chat_' + Date.now();
|
| 26 |
+
conversations[currentChatId] = { title: "New Chat", messages: [] };
|
| 27 |
+
saveToLocalStorage();
|
| 28 |
+
renderHistory();
|
| 29 |
+
|
| 30 |
+
document.getElementById('welcomeScreen').style.display = 'block';
|
| 31 |
+
document.getElementById('chatMessages').style.display = 'none';
|
| 32 |
+
document.getElementById('chatMessages').innerHTML = '';
|
| 33 |
+
|
| 34 |
+
if(window.innerWidth <= 768) toggleSidebar();
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
function loadChat(id) {
|
| 38 |
+
currentChatId = id;
|
| 39 |
+
renderHistory();
|
| 40 |
+
|
| 41 |
+
document.getElementById('welcomeScreen').style.display = 'none';
|
| 42 |
+
const msgDiv = document.getElementById('chatMessages');
|
| 43 |
+
msgDiv.style.display = 'flex';
|
| 44 |
+
msgDiv.innerHTML = '';
|
| 45 |
+
|
| 46 |
+
conversations[id].messages.forEach(msg => {
|
| 47 |
+
appendBubble(msg.role, msg.text);
|
| 48 |
+
});
|
| 49 |
+
|
| 50 |
+
if(window.innerWidth <= 768) toggleSidebar();
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
function deleteChat(id, event) {
|
| 54 |
+
event.stopPropagation();
|
| 55 |
+
delete conversations[id];
|
| 56 |
+
saveToLocalStorage();
|
| 57 |
+
renderHistory();
|
| 58 |
+
if (currentChatId === id) {
|
| 59 |
+
currentChatId = null;
|
| 60 |
+
document.getElementById('welcomeScreen').style.display = 'block';
|
| 61 |
+
document.getElementById('chatMessages').style.display = 'none';
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
function appendBubble(role, text) {
|
| 66 |
+
const container = document.getElementById('chatMessages');
|
| 67 |
+
const bubble = document.createElement('div');
|
| 68 |
+
bubble.className = `chat-bubble ${role}`;
|
| 69 |
+
|
| 70 |
+
const avatar = role === 'user' ? '👤' : '✨';
|
| 71 |
+
bubble.innerHTML = `
|
| 72 |
+
<div class="bubble-avatar">${avatar}</div>
|
| 73 |
+
<div class="bubble-text">${text}</div>
|
| 74 |
+
`;
|
| 75 |
+
container.appendChild(bubble);
|
| 76 |
+
const chatContainer = document.getElementById('chatContainer');
|
| 77 |
+
chatContainer.scrollTop = chatContainer.scrollHeight;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
function useSuggestion(text) {
|
| 81 |
+
document.getElementById('userInput').value = text;
|
| 82 |
+
sendMessage();
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
async function sendMessage() {
|
| 86 |
+
const input = document.getElementById('userInput');
|
| 87 |
+
const text = input.value.trim();
|
| 88 |
+
if (!text) return;
|
| 89 |
+
|
| 90 |
+
if (!currentChatId) {
|
| 91 |
+
currentChatId = 'chat_' + Date.now();
|
| 92 |
+
conversations[currentChatId] = { title: text.substring(0, 24), messages: [] };
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
// স্ক্রিন পরিবর্তন
|
| 96 |
+
document.getElementById('welcomeScreen').style.display = 'none';
|
| 97 |
+
document.getElementById('chatMessages').style.display = 'flex';
|
| 98 |
+
|
| 99 |
+
// ইউজার মেসেজ রেন্ডার ও সেভ
|
| 100 |
+
appendBubble('user', text);
|
| 101 |
+
conversations[currentChatId].messages.push({ role: 'user', text: text });
|
| 102 |
+
|
| 103 |
+
if(conversations[currentChatId].title === "New Chat") {
|
| 104 |
+
conversations[currentChatId].title = text.substring(0, 24);
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
input.value = '';
|
| 108 |
+
saveToLocalStorage();
|
| 109 |
+
renderHistory();
|
| 110 |
+
|
| 111 |
+
// লোডিং ইন্ডিকেটর শো করা
|
| 112 |
+
appendBubble('bot', 'Thinking...');
|
| 113 |
+
const lastBubble = document.getElementById('chatMessages').lastChild.querySelector('.bubble-text');
|
| 114 |
+
|
| 115 |
+
try {
|
| 116 |
+
// সরাসরি Gradio /predict endpoint এ পোস্ট রিকোয়েস্ট পাঠানো
|
| 117 |
+
const response = await fetch("/call/predict", {
|
| 118 |
+
method: "POST",
|
| 119 |
+
headers: { "Content-Type": "application/json" },
|
| 120 |
+
body: JSON.stringify({ data: [text] })
|
| 121 |
+
});
|
| 122 |
+
const resJson = await response.json();
|
| 123 |
+
const reply = resJson.data[0];
|
| 124 |
+
|
| 125 |
+
// লোডিং টেক্সট সরিয়ে বটের আসল উত্তর বসানো
|
| 126 |
+
lastBubble.innerText = reply;
|
| 127 |
+
conversations[currentChatId].messages.push({ role: 'bot', text: reply });
|
| 128 |
+
saveToLocalStorage();
|
| 129 |
+
} catch (e) {
|
| 130 |
+
lastBubble.innerText = "Error: Could not connect to Gemini backend.";
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
|
| 134 |
+
function handleKeyPress(e) {
|
| 135 |
+
if (e.key === 'Enter') sendMessage();
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
function saveToLocalStorage() {
|
| 139 |
+
localStorage.setItem('gemini_history', JSON.stringify(conversations));
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// ইনিশিয়াল লোড
|
| 143 |
+
setTimeout(() => {
|
| 144 |
+
renderHistory();
|
| 145 |
+
// মোবাইল স্ক্রিনে ডিফল্ট সাইডবার বন্ধ রাখা
|
| 146 |
+
if(window.innerWidth <= 768) document.getElementById('sidebar').classList.add('closed');
|
| 147 |
+
}, 300);
|