Artem733733 commited on
Commit
e5afd2a
·
verified ·
1 Parent(s): 4b3715d

Upload 4 files

Browse files
Files changed (4) hide show
  1. index.html +238 -0
  2. script2.js +94 -0
  3. session.js +16 -0
  4. style.css +196 -0
index.html ADDED
@@ -0,0 +1,238 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <!-- Coding By CodingNepal - www.codingnepalweb.com -->
3
+ <html lang="en" dir="ltr">
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <title>Chatbot in JavaScript | CodingNepal</title>
7
+ <style>
8
+ /* Import Google font - Poppins */
9
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
10
+ * {
11
+ margin: 0;
12
+ padding: 0;
13
+ box-sizing: border-box;
14
+ font-family: "Poppins", sans-serif;
15
+ }
16
+ body {
17
+ background: #56A6BE;
18
+ position: relative;
19
+ min-height: 100vh;
20
+ }
21
+ .chatbot-toggler {
22
+ position: absolute;
23
+ bottom: 30px;
24
+ left: 50%;
25
+ transform: translateX(-50%);
26
+ outline: none;
27
+ border: none;
28
+ height: auto;
29
+ width: auto;
30
+ display: flex;
31
+ cursor: pointer;
32
+ align-items: center;
33
+ justify-content: center;
34
+ padding: 10px 20px;
35
+ background: #003366; /* Темно-синий цвет */
36
+ color: #fff; /* Белый цвет текста */
37
+ border-radius: 10px; /* Скругленные углы */
38
+ transition: all 0.2s ease;
39
+ font-size: 1rem; /* Размер шрифта */
40
+ }
41
+ body.show-chatbot .chatbot-toggler {
42
+ transform: translateX(-50%) rotate(90deg);
43
+ }
44
+ .chatbot-toggler span {
45
+ color: #fff;
46
+ position: absolute;
47
+ }
48
+ .chatbot-toggler span:last-child,
49
+ body.show-chatbot .chatbot-toggler span:first-child {
50
+ opacity: 0;
51
+ }
52
+ body.show-chatbot .chatbot-toggler span:last-child {
53
+ opacity: 1;
54
+ }
55
+ .chatbot {
56
+ position: fixed;
57
+ right: 35px;
58
+ bottom: 90px;
59
+ width: 420px;
60
+ background: #fff;
61
+ border-radius: 15px;
62
+ overflow: hidden;
63
+ opacity: 0;
64
+ pointer-events: none;
65
+ transform: scale(0.5);
66
+ transform-origin: bottom right;
67
+ box-shadow: 0 0 128px 0 rgba(0,0,0,0.1),
68
+ 0 32px 64px -48px rgba(0,0,0,0.5);
69
+ transition: all 0.1s ease;
70
+ }
71
+ body.show-chatbot .chatbot {
72
+ opacity: 1;
73
+ pointer-events: auto;
74
+ transform: scale(1);
75
+ }
76
+ .chatbot header {
77
+ padding: 16px 0;
78
+ position: relative;
79
+ text-align: center;
80
+ color: #fff;
81
+ background: #56a6be;
82
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
83
+ }
84
+ .chatbot header span {
85
+ position: absolute;
86
+ right: 15px;
87
+ top: 50%;
88
+ display: none;
89
+ cursor: pointer;
90
+ transform: translateY(-50%);
91
+ }
92
+ header h2 {
93
+ font-size: 1.4rem;
94
+ }
95
+ .chatbot .chatbox {
96
+ overflow-y: auto;
97
+ height: 410px;
98
+ padding: 30px 20px 100px;
99
+ }
100
+ .chatbot :where(.chatbox, textarea)::-webkit-scrollbar {
101
+ width: 6px;
102
+ }
103
+ .chatbot :where(.chatbox, textarea)::-webkit-scrollbar-track {
104
+ background: #fff;
105
+ border-radius: 25px;
106
+ }
107
+ .chatbot :where(.chatbox, textarea)::-webkit-scrollbar-thumb {
108
+ background: #ccc;
109
+ border-radius: 25px;
110
+ }
111
+ .chatbox .chat {
112
+ display: flex;
113
+ list-style: none;
114
+ }
115
+ .chatbox .outgoing {
116
+ margin: 20px 0;
117
+ justify-content: flex-end;
118
+ }
119
+ .chatbox .incoming span {
120
+ width: 32px;
121
+ height: 32px;
122
+ color: #fff;
123
+ cursor: default;
124
+ text-align: center;
125
+ line-height: 32px;
126
+ align-self: flex-end;
127
+ background: #56a6be;
128
+ border-radius: 4px;
129
+ margin: 0 10px 7px 0;
130
+ }
131
+ .chatbox .chat p {
132
+ white-space: pre-wrap;
133
+ padding: 12px 16px;
134
+ border-radius: 10px 10px 0 10px;
135
+ max-width: 75%;
136
+ color: #fff;
137
+ font-size: 0.95rem;
138
+ background: #56a6be;
139
+ }
140
+ .chatbox .incoming p {
141
+ border-radius: 10px 10px 10px 0;
142
+ }
143
+ .chatbox .chat p.error {
144
+ color: #721c24;
145
+ background: #f8d7da;
146
+ }
147
+ .chatbox .incoming p {
148
+ color: #000;
149
+ background: #f2f2f2;
150
+ }
151
+ .chatbot .chat-input {
152
+ display: flex;
153
+ gap: 5px;
154
+ position: absolute;
155
+ bottom: 0;
156
+ width: 100%;
157
+ background: #fff;
158
+ padding: 3px 20px;
159
+ border-top: 1px solid #ddd;
160
+ }
161
+ .chat-input textarea {
162
+ height: 55px;
163
+ width: 100%;
164
+ border: none;
165
+ outline: none;
166
+ resize: none;
167
+ max-height: 180px;
168
+ padding: 15px 15px 15px 0;
169
+ font-size: 0.95rem;
170
+ }
171
+ .chat-input span {
172
+ align-self: flex-end;
173
+ color: #724ae8;
174
+ cursor: pointer;
175
+ height: 55px;
176
+ display: flex;
177
+ align-items: center;
178
+ visibility: hidden;
179
+ font-size: 1.35rem;
180
+ }
181
+ .chat-input textarea:valid ~ span {
182
+ visibility: visible;
183
+ }
184
+ @media (max-width: 490px) {
185
+ .chatbot-toggler {
186
+ bottom: 20px;
187
+ }
188
+ .chatbot {
189
+ right: 0;
190
+ bottom: 0;
191
+ height: 100%;
192
+ border-radius: 0;
193
+ width: 100%;
194
+ }
195
+ .chatbot .chatbox {
196
+ height: 90%;
197
+ padding: 25px 15px 100px;
198
+ }
199
+ .chatbot .chat-input {
200
+ padding: 5px 15px;
201
+ }
202
+ .chatbot header span {
203
+ display: block;
204
+ }
205
+ }
206
+ </style>
207
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
208
+ <!-- Google Fonts Link For Icons -->
209
+ <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
210
+ <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,GRAD@48,400,1,0" />
211
+ <script type="module">
212
+ import { Client } from 'https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js';
213
+ </script>
214
+ <script src="session.js" defer></script>
215
+ <script src="script2.js" defer></script>
216
+ </head>
217
+ <body>
218
+ <button class="chatbot-toggler">
219
+ Чат-бот Yushkoff Australia
220
+ </button>
221
+ <div class="chatbot">
222
+ <header>
223
+ <h2>Чат-бот Yushkoff Australia</h2>
224
+ <span class="close-btn material-symbols-outlined">close</span>
225
+ </header>
226
+ <ul class="chatbox">
227
+ <li class="chat incoming">
228
+ <span class="material-symbols-outlined">smart_toy</span>
229
+ <p>Привет 👋<br>Я Виталий, консультант компании Yushkoff Australia.<br>Готов рассказать Вам о наших футболках.<br>Вы у нас были раньше?</p>
230
+ </li>
231
+ </ul>
232
+ <div class="chat-input">
233
+ <textarea placeholder="Введите сообщение..." spellcheck="false" required></textarea>
234
+ <span id="send-btn" class="material-symbols-rounded">send</span>
235
+ </div>
236
+ </div>
237
+ </body>
238
+ </html>
script2.js ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const chatbotToggler = document.querySelector(".chatbot-toggler");
2
+ const closeBtn = document.querySelector(".close-btn");
3
+ const chatbox = document.querySelector(".chatbox");
4
+ const chatInput = document.querySelector(".chat-input textarea");
5
+ const sendChatBtn = document.querySelector(".chat-input span");
6
+ // Функция для получения или генерации session_id
7
+ //function getSessionId() {
8
+ // let sessionId = localStorage.getItem('session_id');
9
+ // if (!sessionId) {
10
+ // sessionId = crypto.randomUUID();
11
+ // localStorage.setItem('session_id', sessionId);
12
+ // }
13
+ // return sessionId;
14
+
15
+ let userMessage = null; // Variable to store user's message
16
+ //const API_KEY = "PASTE-YOUR-API-KEY"; // Paste your API key here
17
+ const inputInitHeight = chatInput.scrollHeight;
18
+
19
+ const createChatLi = (message, className) => {
20
+ // Create a chat <li> element with passed message and className
21
+ const chatLi = document.createElement("li");
22
+ chatLi.classList.add("chat", `${className}`);
23
+ let chatContent = className === "outgoing" ? `<p></p>` : `<span class="material-symbols-outlined">smart_toy</span><p></p>`;
24
+ chatLi.innerHTML = chatContent;
25
+ chatLi.querySelector("p").textContent = message;
26
+ return chatLi; // return chat <li> element
27
+ }
28
+
29
+ const generateResponse = (chatElement) => {
30
+ const API_URL = "https://hgswdi5icult2y-8000.proxy.runpod.net/query";
31
+ const messageElement = chatElement.querySelector("p");
32
+ const sessionId = getSessionId();
33
+ //const sessionId = getSessionId();
34
+ //const sessionId = "1234567";
35
+ // Define the properties and message for the API request
36
+ const requestOptions = {
37
+ method: "POST",
38
+ headers: {
39
+ 'Content-Type': 'application/json',
40
+ 'X-Session-ID': sessionId // Передача session_id через заголовок
41
+ },
42
+ body: JSON.stringify(userMessage)
43
+ }
44
+
45
+ // Send POST request to API, get response and set the reponse as paragraph text
46
+ fetch(API_URL, requestOptions).then(res => res.text()).then(data => {
47
+ //messageElement.textContent = data.trim();
48
+ //messageElement.textContent = data.replace(/"/g, '').replace(/\\n/g, '<br>');
49
+ messageElement.textContent = data.replace(/"/g, '').replace(/\\n/g, ' ').trim();
50
+ }).catch(() => {
51
+ messageElement.classList.add("error");
52
+ messageElement.textContent = "Упс! Что-то пошло не так. Пожалуйста, попробуйте еще раз.";
53
+ }).finally(() => chatbox.scrollTo(0, chatbox.scrollHeight));
54
+ }
55
+
56
+ const handleChat = () => {
57
+ userMessage = chatInput.value.trim(); // Get user entered message and remove extra whitespace
58
+ if(!userMessage) return;
59
+
60
+ // Clear the input textarea and set its height to default
61
+ chatInput.value = "";
62
+ chatInput.style.height = `${inputInitHeight}px`;
63
+
64
+ // Append the user's message to the chatbox
65
+ chatbox.appendChild(createChatLi(userMessage, "outgoing"));
66
+ chatbox.scrollTo(0, chatbox.scrollHeight);
67
+
68
+ setTimeout(() => {
69
+ // Display "Thinking..." message while waiting for the response
70
+ const incomingChatLi = createChatLi("Думаю...", "incoming");
71
+ chatbox.appendChild(incomingChatLi);
72
+ chatbox.scrollTo(0, chatbox.scrollHeight);
73
+ generateResponse(incomingChatLi);
74
+ }, 600);
75
+ }
76
+
77
+ chatInput.addEventListener("input", () => {
78
+ // Adjust the height of the input textarea based on its content
79
+ chatInput.style.height = `${inputInitHeight}px`;
80
+ chatInput.style.height = `${chatInput.scrollHeight}px`;
81
+ });
82
+
83
+ chatInput.addEventListener("keydown", (e) => {
84
+ // If Enter key is pressed without Shift key and the window
85
+ // width is greater than 800px, handle the chat
86
+ if(e.key === "Enter" && !e.shiftKey && window.innerWidth > 800) {
87
+ e.preventDefault();
88
+ handleChat();
89
+ }
90
+ });
91
+
92
+ sendChatBtn.addEventListener("click", handleChat);
93
+ closeBtn.addEventListener("click", () => document.body.classList.remove("show-chatbot"));
94
+ chatbotToggler.addEventListener("click", () => document.body.classList.toggle("show-chatbot"));
session.js ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // session.js
2
+ function getSessionId() {
3
+ let sessionId = localStorage.getItem('session_id');
4
+ if (!sessionId) {
5
+ try {
6
+ sessionId = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
7
+ const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
8
+ return v.toString(16);
9
+ });
10
+ localStorage.setItem('session_id', sessionId);
11
+ } catch (error) {
12
+ console.error('Error generating session ID:', error);
13
+ }
14
+ }
15
+ return sessionId;
16
+ }
style.css ADDED
@@ -0,0 +1,196 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* Import Google font - Poppins */
2
+ @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
3
+
4
+ * {
5
+ margin: 0;
6
+ padding: 0;
7
+ box-sizing: border-box;
8
+ font-family: "Poppins", sans-serif;
9
+ }
10
+ body {
11
+ background: #56A6BE;
12
+
13
+ }
14
+ .chatbot-toggler {
15
+ position: fixed;
16
+ bottom: 30px;
17
+ right: 35px;
18
+ outline: none;
19
+ border: none;
20
+ height: 50px;
21
+ width: 50px;
22
+ display: flex;
23
+ cursor: pointer;
24
+ align-items: center;
25
+ justify-content: center;
26
+ border-radius: 50%;
27
+ background: #ffff00;
28
+ transition: all 0.2s ease;
29
+ }
30
+ body.show-chatbot .chatbot-toggler {
31
+ transform: rotate(90deg);
32
+ }
33
+ .chatbot-toggler span {
34
+ color: #56a6be;
35
+ position: absolute;
36
+ }
37
+ .chatbot-toggler span:last-child,
38
+ body.show-chatbot .chatbot-toggler span:first-child {
39
+ opacity: 0;
40
+ }
41
+ body.show-chatbot .chatbot-toggler span:last-child {
42
+ opacity: 1;
43
+ }
44
+ .chatbot {
45
+ position: fixed;
46
+ right: 35px;
47
+ bottom: 90px;
48
+ width: 420px;
49
+ background: #fff;
50
+ border-radius: 15px;
51
+ overflow: hidden;
52
+ opacity: 0;
53
+ pointer-events: none;
54
+ transform: scale(0.5);
55
+ transform-origin: bottom right;
56
+ box-shadow: 0 0 128px 0 rgba(0,0,0,0.1),
57
+ 0 32px 64px -48px rgba(0,0,0,0.5);
58
+ transition: all 0.1s ease;
59
+ }
60
+ body.show-chatbot .chatbot {
61
+ opacity: 1;
62
+ pointer-events: auto;
63
+ transform: scale(1);
64
+ }
65
+ .chatbot header {
66
+ padding: 16px 0;
67
+ position: relative;
68
+ text-align: center;
69
+ color: #fff;
70
+ background: #56a6be;
71
+ box-shadow: 0 2px 10px rgba(0,0,0,0.1);
72
+ }
73
+ .chatbot header span {
74
+ position: absolute;
75
+ right: 15px;
76
+ top: 50%;
77
+ display: none;
78
+ cursor: pointer;
79
+ transform: translateY(-50%);
80
+ }
81
+ header h2 {
82
+ font-size: 1.4rem;
83
+ }
84
+ .chatbot .chatbox {
85
+ overflow-y: auto;
86
+ height: 410px;
87
+ padding: 30px 20px 100px;
88
+ }
89
+ .chatbot :where(.chatbox, textarea)::-webkit-scrollbar {
90
+ width: 6px;
91
+ }
92
+ .chatbot :where(.chatbox, textarea)::-webkit-scrollbar-track {
93
+ background: #fff;
94
+ border-radius: 25px;
95
+ }
96
+ .chatbot :where(.chatbox, textarea)::-webkit-scrollbar-thumb {
97
+ background: #ccc;
98
+ border-radius: 25px;
99
+ }
100
+ .chatbox .chat {
101
+ display: flex;
102
+ list-style: none;
103
+ }
104
+ .chatbox .outgoing {
105
+ margin: 20px 0;
106
+ justify-content: flex-end;
107
+ }
108
+ .chatbox .incoming span {
109
+ width: 32px;
110
+ height: 32px;
111
+ color: #fff;
112
+ cursor: default;
113
+ text-align: center;
114
+ line-height: 32px;
115
+ align-self: flex-end;
116
+ background: #56a6be;
117
+ border-radius: 4px;
118
+ margin: 0 10px 7px 0;
119
+ }
120
+ .chatbox .chat p {
121
+ white-space: pre-wrap;
122
+ padding: 12px 16px;
123
+ border-radius: 10px 10px 0 10px;
124
+ max-width: 75%;
125
+ color: #fff;
126
+ font-size: 0.95rem;
127
+ background: #56a6be;
128
+ }
129
+ .chatbox .incoming p {
130
+ border-radius: 10px 10px 10px 0;
131
+ }
132
+ .chatbox .chat p.error {
133
+ color: #721c24;
134
+ background: #f8d7da;
135
+ }
136
+ .chatbox .incoming p {
137
+ color: #000;
138
+ background: #f2f2f2;
139
+ }
140
+ .chatbot .chat-input {
141
+ display: flex;
142
+ gap: 5px;
143
+ position: absolute;
144
+ bottom: 0;
145
+ width: 100%;
146
+ background: #fff;
147
+ padding: 3px 20px;
148
+ border-top: 1px solid #ddd;
149
+ }
150
+ .chat-input textarea {
151
+ height: 55px;
152
+ width: 100%;
153
+ border: none;
154
+ outline: none;
155
+ resize: none;
156
+ max-height: 180px;
157
+ padding: 15px 15px 15px 0;
158
+ font-size: 0.95rem;
159
+ }
160
+ .chat-input span {
161
+ align-self: flex-end;
162
+ color: #724ae8;
163
+ cursor: pointer;
164
+ height: 55px;
165
+ display: flex;
166
+ align-items: center;
167
+ visibility: hidden;
168
+ font-size: 1.35rem;
169
+ }
170
+ .chat-input textarea:valid ~ span {
171
+ visibility: visible;
172
+ }
173
+
174
+ @media (max-width: 490px) {
175
+ .chatbot-toggler {
176
+ right: 20px;
177
+ bottom: 20px;
178
+ }
179
+ .chatbot {
180
+ right: 0;
181
+ bottom: 0;
182
+ height: 100%;
183
+ border-radius: 0;
184
+ width: 100%;
185
+ }
186
+ .chatbot .chatbox {
187
+ height: 90%;
188
+ padding: 25px 15px 100px;
189
+ }
190
+ .chatbot .chat-input {
191
+ padding: 5px 15px;
192
+ }
193
+ .chatbot header span {
194
+ display: block;
195
+ }
196
+ }