File size: 20,773 Bytes
ca366fb 7a200bf ca366fb 7a200bf ca366fb 7710970 ca366fb 7a200bf ca366fb 7710970 ca366fb 7710970 ca366fb 7a200bf ca366fb 7710970 ca366fb 7710970 ca366fb |
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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans:wght@300;400;700&display=swap" rel="stylesheet">
<title>聊天助手</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Noto Sans', sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f5f5f5;
}
select {
width: 20%;
padding: 12px 15px;
font-size: 18px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #fafafa;
color: #333;
cursor: pointer;
box-sizing: border-box;
}
select:focus {
border-color: #007bff;
outline: none;
}
option {
padding: 10px;
}
.chatbox {
height: fit-content;
width: 90%;
background-color: white;
border-radius: 20px;
margin-top: 10px;
margin-bottom: 10px;
}
.myInputArea {
display: flex;
justify-content: center;
margin-top: 10px;
}
.myMessageArea {
display: flex;
justify-content: flex-end;
margin-top: 10px;
}
.GPTmessageArea {
display: flex;
justify-content: flex-start;
margin-top: 10px;
}
.myMessageBox {
height: fit-content;
width: fit-content;
margin: 10px;
padding: 10px;
border-radius: 10px;
background-color: rgb(195, 252, 233);
}
.GPTmessageBox {
height: fit-content;
width: fit-content;
margin: 10px;
padding: 10px;
border-radius: 10px;
background-color: rgb(252, 206, 238);
}
.myInputBox {
height: fit-content;
width: 85%;
margin: 10px;
padding: 10px;
border-radius: 10px;
background-color: #f5f5f5;
}
.addMessageButton {
width: 35px;
height: 35px;
font-size: 15px;
font-weight: bold;
border: solid 1px #a7a7a7;
border-radius: 10px;
background-color: white;
cursor: pointer;
}
.sendMessageButton {
background-color: #007bff;
/* 藍色 */
color: white;
font-size: 16px;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background 0.3s;
}
.sendMessageButton:hover {
background-color: #0056b3;
}
.addMessageButton:hover {
background-color: rgb(209, 209, 209);
}
.messagePending {
margin-left: 5px;
width: 70%;
padding: 12px 15px;
font-size: 16px;
border: 2px solid #ccc;
border-radius: 8px;
background-color: #fafafa;
color: #333;
outline: none;
box-sizing: border-box;
transition: border 0.3s ease, box-shadow 0.3s ease;
}
.messagePending:focus {
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
.messagePending::placeholder {
color: #888;
font-style: italic;
}
#imgInputBox {
width: 100%;
border: solid 1px #a7a7a7;
border-radius: 10px;
height: 50px;
}
#textInputBox {
margin-top: 10px;
width: 100%;
border: solid 1px #a7a7a7;
border-radius: 10px;
height: 200px;
padding: 10px;
overflow: auto;
}
</style>
</head>
<body>
<div id="chatbox" class="chatbox">
<!--對話輸入框-->
<div class="myInputArea">
<div class="myInputBox">
<div id="imgInputBox">
<div style="display: flex; justify-content: center; align-items: center; height: 100%;">
<input type="file" id="imageUpload" accept="image/*" style="display: none;">
<button onclick="document.getElementById('imageUpload').click()" style="padding: 5px 10px; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 5px; cursor: pointer;">選擇圖片</button>
<span id="selectedFileName" style="margin-left: 10px; font-size: 14px;"></span>
</div>
</div>
<div style="text-align: center;">
或
</div>
<div id="textInputBox">
<!--已建立的訊息填寫框-->
<div style="display: flex;flex-wrap: nowrap;justify-content: end; margin-top: 10px;">
<select id="character0" name="character" style="width: 90px;">
<option value="self">自己</option>
<option value="usr">對方</option>
</select>
<input type="text" id="messagePending0" name="messagePending" class="messagePending">
</div>
<!--建立新的訊息填寫框-->
<div style="display: flex;justify-content: end; margin-top: 10px;">
<button class="addMessageButton" onclick="addMessageBox()">+</button>
</div>
</div>
<!--傳送訊息-->
<div style="display: flex;justify-content: end;margin-top: 10px;">
<button class="sendMessageButton" onclick="sendMessage()">傳送</button>
</div>
</div>
</div>
<!--對話紀錄-我方-->
<!--
<div class="myMessageArea">
<div class="myMessageBox">
<p>這是我傳的訊息或圖片</p>
</div>
</div>
<div class="GPTmessageArea">
<div class="GPTmessageBox">
<p>這是GPT傳的訊息</p>
</div>
</div>
-->
</div>
<script>
let messageCount = 1; // 記錄訊息框數量
let selectedImage = null; // 儲存選擇的圖片
// 當頁面載入完成後,添加事件監聽器
document.addEventListener('DOMContentLoaded', function() {
// 為圖片上傳添加事件監聽器
const imageUpload = document.getElementById('imageUpload');
if (imageUpload) {
imageUpload.addEventListener('change', handleImageSelect);
}
});
// 處理圖片選擇
function handleImageSelect(event) {
const file = event.target.files[0];
if (file) {
selectedImage = file;
document.getElementById('selectedFileName').textContent = file.name;
// 選擇圖片後立即上傳,不需要額外的上傳按鈕
uploadImage();
}
}
// 上傳圖片到伺服器
function uploadImage() {
if (!selectedImage) {
alert('請先選擇一張圖片');
return;
}
const formData = new FormData();
formData.append('file', selectedImage);
// 暫時刪除 myInputArea
const myInputArea = document.querySelector(".myInputArea");
if (myInputArea) {
myInputArea.remove();
}
// 新增 myMessageArea (顯示上傳的圖片)
const chatbox = document.getElementById("chatbox");
const myMessageArea = document.createElement("div");
myMessageArea.className = "myMessageArea";
myMessageArea.innerHTML = `
<div class="myMessageBox">
<p>上傳的圖片: ${selectedImage.name}</p>
</div>
`;
chatbox.appendChild(myMessageArea);
// 新增 GPTmessageArea (初始內容: 等待回應...)
const GPTmessageArea = document.createElement("div");
GPTmessageArea.className = "GPTmessageArea";
GPTmessageArea.innerHTML = `
<div class="GPTmessageBox">
<p>(等待回應......)</p>
</div>
`;
chatbox.appendChild(GPTmessageArea);
// 發送 API 請求
fetch('/api/process-image', {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Server Response:", data);
// 確保回應包含 analysis 和 suggestion
const analysis = data.analysis || "無分析";
const suggestion = data.suggestion || "無建議";
const chatText = data.chat_text || "";
// 處理 chatText,去掉外層的 <chat> 標記
let formattedChatText = chatText;
if (formattedChatText.startsWith("<chat>") && formattedChatText.endsWith("</chat>")) {
formattedChatText = formattedChatText.substring(6, formattedChatText.length - 7);
}
// 將 XML 格式轉換為更友好的格式
formattedChatText = formattedChatText
.replace(/<self>(.*?)<\/self>/g, "我:$1")
.replace(/<usr>(.*?)<\/usr>/g, "你:$1");
// 直接顯示分析和建議,不顯示識別的聊天內容
GPTmessageArea.innerHTML = `
<div class="GPTmessageBox">
<p><strong>分析:</strong><br>${analysis}</p>
<p><strong>建議回覆:</strong><br>${suggestion}</p>
</div>
`;
})
.catch(error => {
console.error("Error uploading image:", error);
GPTmessageArea.innerHTML = `
<div class="GPTmessageBox">
<p>(錯誤:無法取得回應)</p>
<p>錯誤詳情:${error.message}</p>
</div>
`;
})
.finally(() => {
// 重新新增 myInputArea
resetInputArea();
});
}
// 重設輸入區域
function resetInputArea() {
const chatbox = document.getElementById("chatbox");
const newMyInputArea = document.createElement("div");
newMyInputArea.className = "myInputArea";
newMyInputArea.innerHTML = `
<div class="myInputBox">
<div id="imgInputBox">
<div style="display: flex; justify-content: center; align-items: center; height: 100%;">
<input type="file" id="imageUpload" accept="image/*" style="display: none;">
<button onclick="document.getElementById('imageUpload').click()" style="padding: 5px 10px; background-color: #f0f0f0; border: 1px solid #ccc; border-radius: 5px; cursor: pointer;">選擇圖片</button>
<span id="selectedFileName" style="margin-left: 10px; font-size: 14px;"></span>
</div>
</div>
<div style="text-align: center;">
或
</div>
<div id="textInputBox">
<!--已建立的訊息填寫框-->
<div style="display: flex;flex-wrap: nowrap;justify-content: end; margin-top: 10px;">
<select id="character0" name="character">
<option value="self">自己</option>
<option value="usr">對方</option>
</select>
<input type="text" id="messagePending0" name="messagePending" class="messagePending">
</div>
<!--建立新的訊息填寫框-->
<div style="display: flex;justify-content: end; margin-top: 10px;">
<button class="addMessageButton" onclick="addMessageBox()">+</button>
</div>
</div>
<!--傳送訊息-->
<div style="display: flex;justify-content: end;margin-top: 10px;">
<button class="sendMessageButton" onclick="sendMessage()">傳送</button>
</div>
</div>
`;
chatbox.appendChild(newMyInputArea);
// 重新添加事件監聽器
const imageUpload = document.getElementById('imageUpload');
if (imageUpload) {
imageUpload.addEventListener('change', handleImageSelect);
}
// 重設選擇的圖片
selectedImage = null;
messageCount = 1;
}
// 添加新的訊息填寫框
function addMessageBox() {
const newMessageBox = document.createElement('div');
newMessageBox.style.display = "flex";
newMessageBox.style.justifyContent = "end";
newMessageBox.style.marginTop = "10px";
// 新增一個X按鈕
const deleteButton = document.createElement('button');
deleteButton.innerText = 'X';
deleteButton.style.marginRight = '10px';
deleteButton.style.cursor = 'pointer';
deleteButton.style.background = 'red';
deleteButton.style.color = 'white';
deleteButton.style.border = 'none';
deleteButton.style.borderRadius = '50%';
deleteButton.style.width = '25px';
deleteButton.style.height = '25px';
deleteButton.style.textAlign = 'center';
deleteButton.style.fontSize = '16px';
// 點擊刪除按鈕時刪除對應的訊息框
deleteButton.addEventListener('click', function() {
newMessageBox.remove();
});
// 設定新增訊息框的內容
newMessageBox.innerHTML = `
<select id="character${messageCount}" name="character" style="width: 90px;">
<option value="self">自己</option>
<option value="usr">對方</option>
</select>
<input type="text" id="messagePending${messageCount}" name="messagePending" class="messagePending">
`;
// 在訊息框前插入X按鈕
newMessageBox.insertBefore(deleteButton, newMessageBox.firstChild);
// 把新的訊息框插入到#textInputBox
document.getElementById('textInputBox').insertBefore(newMessageBox, document.querySelector('.addMessageButton').parentNode);
messageCount++;
}
function sendMessage() {
let result = "<chat>";
let formattedMessage = ""; // 用於顯示在 myMessageArea 的格式化文字
for (let i = 0; i < messageCount; i++) {
try {
const character = document.getElementById(`character${i}`);
const messageElement = document.getElementById(`messagePending${i}`);
if (!character || !messageElement) continue; // 跳過已刪除的元素
const message = messageElement.value.trim();
if (message) {
result += `<${character.value}>${message}</${character.value}>`;
formattedMessage += `${character.value === 'self' ? "您" : "對方"}: ${message}<br>`; // **換行**
}
} catch (error) {
console.warn(`Message box ${i} does not exist. Skipping.`);
continue;
}
}
result += "</chat>";
// **1. 暫時刪除 myInputArea**
const myInputArea = document.querySelector(".myInputArea");
if (myInputArea) {
myInputArea.remove();
}
// **2. 新增 myMessageArea (符合提供的 HTML 結構)**
const chatbox = document.getElementById("chatbox");
const myMessageArea = document.createElement("div");
myMessageArea.className = "myMessageArea";
myMessageArea.innerHTML = `
<div class="myMessageBox">
<p>${formattedMessage}</p>
</div>
`;
chatbox.appendChild(myMessageArea);
// **3. 新增 GPTmessageArea (初始內容: 等待回應...)**
const GPTmessageArea = document.createElement("div");
GPTmessageArea.className = "GPTmessageArea";
GPTmessageArea.innerHTML = `
<div class="GPTmessageBox">
<p>(等待回應......)</p>
</div>
`;
chatbox.appendChild(GPTmessageArea);
// **4. 傳送 API 請求**
const data = {
chat_text: result
};
fetch('/api/process-text', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log("Server Response:", data);
// 確保回應包含 analysis 和 suggestion
const analysis = data.analysis || "無分析";
const suggestion = data.suggestion || "無建議";
// 處理 result,去掉外層的 <chat> 標記
let formattedChatText = result;
if (formattedChatText.startsWith("<chat>") && formattedChatText.endsWith("</chat>")) {
formattedChatText = formattedChatText.substring(6, formattedChatText.length - 7);
}
// 將 XML 格式轉換為更友好的格式
formattedChatText = formattedChatText
.replace(/<self>(.*?)<\/self>/g, "我:$1")
.replace(/<usr>(.*?)<\/usr>/g, "你:$1");
// 直接顯示分析和建議,不顯示識別的聊天內容
GPTmessageArea.innerHTML = `
<div class="GPTmessageBox">
<p><strong>分析:</strong><br>${analysis}</p>
<p><strong>建議回覆:</strong><br>${suggestion}</p>
</div>
`;
})
.catch(error => {
console.error("Error sending message:", error);
GPTmessageArea.innerHTML = `
<div class="GPTmessageBox">
<p>(錯誤:無法取得回應)</p>
<p>錯誤詳情:${error.message}</p>
</div>
`;
})
.finally(() => {
// 使用重設輸入區域函數
resetInputArea();
});
}
</script>
</body>
</html> |