Really-amin commited on
Commit
3f2244a
·
verified ·
1 Parent(s): b876dbe

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +30 -14
script.js CHANGED
@@ -15,21 +15,37 @@ document.addEventListener("DOMContentLoaded", function () {
15
  userMessage.textContent = `You: ${message}`;
16
  chatMessages.appendChild(userMessage);
17
 
18
- // ارسال پیام به سرور Streamlit
19
- const url = new URL(window.location.href);
20
- url.searchParams.set("message", message);
21
- window.location.href = url;
22
-
23
  // پاک کردن ورودی
24
  userInput.value = "";
25
- });
26
 
27
- // دریافت پاسخ از سرور
28
- const urlParams = new URLSearchParams(window.location.search);
29
- if (urlParams.has("response")) {
30
- const response = urlParams.get("response");
31
- const assistantMessage = document.createElement("p");
32
- assistantMessage.textContent = `Assistant: ${response}`;
33
- chatMessages.appendChild(assistantMessage);
34
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  });
 
15
  userMessage.textContent = `You: ${message}`;
16
  chatMessages.appendChild(userMessage);
17
 
 
 
 
 
 
18
  // پاک کردن ورودی
19
  userInput.value = "";
 
20
 
21
+ try {
22
+ // ارسال پیام به سرور
23
+ const response = await fetch("/generate", {
24
+ method: "POST",
25
+ headers: { "Content-Type": "application/json" },
26
+ body: JSON.stringify({ message }),
27
+ });
28
+
29
+ if (!response.ok) {
30
+ throw new Error(`HTTP error! status: ${response.status}`);
31
+ }
32
+
33
+ const data = await response.json();
34
+
35
+ // نمایش پاسخ مدل
36
+ const assistantMessage = document.createElement("p");
37
+ assistantMessage.textContent = `Assistant: ${data.response}`;
38
+ chatMessages.appendChild(assistantMessage);
39
+
40
+ } catch (error) {
41
+ console.error("Error fetching response:", error);
42
+ const errorMessage = document.createElement("p");
43
+ errorMessage.textContent = "خطایی در دریافت پاسخ از سرور رخ داده است.";
44
+ errorMessage.style.color = "red";
45
+ chatMessages.appendChild(errorMessage);
46
+ }
47
+
48
+ // اسکرول به پایین
49
+ chatMessages.scrollTop = chatMessages.scrollHeight;
50
+ });
51
  });