srivatsavdamaraju commited on
Commit
baa1a8a
1 Parent(s): 681370c

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +262 -18
index.html CHANGED
@@ -1,19 +1,263 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Flowise Chatbot</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ margin: 0;
11
+ padding: 0;
12
+ background-color: #f5f5f5;
13
+ }
14
+
15
+ #chat-container {
16
+ position: fixed;
17
+ bottom: 20px;
18
+ right: 20px;
19
+ width: 350px;
20
+ height: 500px;
21
+ background-color: #fff;
22
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
23
+ border-radius: 8px;
24
+ overflow: hidden;
25
+ }
26
+
27
+ #chat-header {
28
+ background-color: #007bff;
29
+ color: white;
30
+ padding: 10px;
31
+ text-align: center;
32
+ }
33
+
34
+ #chat-body {
35
+ padding: 10px;
36
+ height: calc(100% - 100px);
37
+ overflow-y: auto;
38
+ background-color: #fafafa;
39
+ }
40
+
41
+ #chat-footer {
42
+ padding: 10px;
43
+ background-color: #f1f1f1;
44
+ display: flex;
45
+ justify-content: space-between;
46
+ }
47
+
48
+ input[type="text"] {
49
+ width: 80%;
50
+ padding: 8px;
51
+ border-radius: 5px;
52
+ border: 1px solid #ccc;
53
+ }
54
+
55
+ button {
56
+ padding: 8px 16px;
57
+ border: none;
58
+ background-color: #007bff;
59
+ color: white;
60
+ border-radius: 5px;
61
+ cursor: pointer;
62
+ }
63
+
64
+ button:hover {
65
+ background-color: #0056b3;
66
+ }
67
+
68
+ /* Position the observer output on the left side */
69
+ #observer-output {
70
+ position: fixed;
71
+ top: 20px; /* Adjust the top margin if needed */
72
+ left: 20px; /* Move it to the left side */
73
+ width: 350px;
74
+ max-height: 500px; /* Adjust the max-height */
75
+ background-color: #fff;
76
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
77
+ border-radius: 8px;
78
+ padding: 10px;
79
+ overflow-y: auto;
80
+ }
81
+
82
+ /* Adjust the margins and layout of the observer sections */
83
+ .observer-section {
84
+ margin-bottom: 20px;
85
+ }
86
+
87
+ .observer-section h4 {
88
+ font-size: 16px;
89
+ margin: 0;
90
+ color: #007bff;
91
+ }
92
+
93
+ .observer-section pre {
94
+ margin-top: 5px;
95
+ background-color: #f0f0f0;
96
+ padding: 10px;
97
+ border-radius: 5px;
98
+ white-space: pre-wrap;
99
+ word-wrap: break-word;
100
+ }
101
+
102
+ /* Bot response section styling */
103
+ #bot-response {
104
+ margin-top: 20px;
105
+ background-color: #f0f0f0;
106
+ padding: 10px;
107
+ border-radius: 5px;
108
+ color: #007bff;
109
+ font-weight: bold;
110
+ }
111
+ </style>
112
+ </head>
113
+ <body>
114
+
115
+ <!-- Chatbot Container -->
116
+ <div id="chat-container">
117
+ <div id="chat-header">
118
+ <h3>Chatbot</h3>
119
+ </div>
120
+ <div id="chat-body"></div>
121
+ <div id="chat-footer">
122
+ <input type="text" id="user-input" placeholder="Type your message..." />
123
+ <button id="send-btn">Send</button>
124
+ </div>
125
+ </div>
126
+
127
+ <!-- Observer Output Section -->
128
+ <div id="observer-output">
129
+ <div class="observer-section" id="user-input-section">
130
+ <h4>User Input:</h4>
131
+ <pre id="user-input-output">Waiting for input...</pre>
132
+ </div>
133
+ <div class="observer-section" id="messages-section">
134
+ <h4>Messages:</h4>
135
+ <pre id="messages-output">Waiting for messages...</pre>
136
+ </div>
137
+ <div class="observer-section" id="loading-section">
138
+ <h4>Loading State:</h4>
139
+ <pre id="loading-output">Bot is idle...</pre>
140
+ </div>
141
+
142
+ <!-- Bot response section -->
143
+ <div id="bot-response">
144
+ Latest Bot Response: <span id="latest-bot-response">Waiting for bot response...</span>
145
+ </div>
146
+ </div>
147
+
148
+ <script type="module">
149
+ import Chatbot from 'https://cdn.jsdelivr.net/npm/flowise-embed/dist/web.js';
150
+
151
+ // Initialize the chatbot
152
+ Chatbot.init({
153
+ chatflowid: '7db12b23-ad83-4da7-9f8d-d0d8e20477d2', // Your chat flow ID
154
+ apiHost: 'https://srivatsavdamaraju-flowiseollama.hf.space', // Your API host URL
155
+ observersConfig: {
156
+ // When the user input changes
157
+ observeUserInput: (userInput) => {
158
+ console.log({ userInput });
159
+ // Update the User Input section in the observer output
160
+ document.getElementById('user-input-output').textContent = userInput;
161
+ },
162
+ // When the bot messages stack changes
163
+ observeMessages: (messages) => {
164
+ console.log({ messages });
165
+ // Update the Messages section in the observer output
166
+ document.getElementById('messages-output').textContent = JSON.stringify(messages, null, 2);
167
+
168
+ // Filter out only the bot's latest message
169
+ const botMessages = messages.filter(message => message.type === 'apiMessage');
170
+ if (botMessages.length > 0) {
171
+ const latestBotMessage = botMessages[botMessages.length - 1].message; // Get the last message from bot
172
+ console.log('Latest Bot response:', latestBotMessage);
173
+
174
+ // Display the latest bot response in the designated section
175
+ document.getElementById('latest-bot-response').textContent = latestBotMessage;
176
+
177
+ // Use the SpeechSynthesis API to speak out the latest bot response (only once)
178
+ speakText(latestBotMessage);
179
+ }
180
+
181
+ // Render the bot messages in the chat window
182
+ renderMessages(messages);
183
+ },
184
+ // When the bot loading state changes
185
+ observeLoading: (loading) => {
186
+ console.log({ loading });
187
+ // Update the Loading State section in the observer output
188
+ document.getElementById('loading-output').textContent = loading ? 'Bot is thinking...' : 'Bot is idle...';
189
+ },
190
+ },
191
+ });
192
+
193
+ // Function to render messages in the chat window
194
+ function renderMessages(messages) {
195
+ const chatBody = document.getElementById('chat-body');
196
+ chatBody.innerHTML = ''; // Clear previous messages
197
+
198
+ messages.forEach(message => {
199
+ const messageElement = document.createElement('div');
200
+ messageElement.classList.add(message.type === 'userMessage' ? 'user' : 'bot');
201
+ messageElement.textContent = message.message;
202
+ chatBody.appendChild(messageElement);
203
+ });
204
+
205
+ // Scroll to the bottom of the chat
206
+ chatBody.scrollTop = chatBody.scrollHeight;
207
+ }
208
+
209
+ // Function to speak the latest bot response using SpeechSynthesis API
210
+ let utterance;
211
+ function speakText(text) {
212
+ // Check if there's any ongoing speech, and stop it before starting new speech
213
+ if (window.speechSynthesis.speaking) {
214
+ window.speechSynthesis.cancel(); // Stop any current speech
215
+ }
216
+
217
+ // Create a new speech synthesis utterance
218
+ utterance = new SpeechSynthesisUtterance(text);
219
+
220
+ // Optionally set properties such as pitch, rate, and volume
221
+ utterance.pitch = 1; // Normal pitch
222
+ utterance.rate = 1; // Normal speed
223
+ utterance.volume = 1; // Full volume
224
+
225
+ // Speak the text
226
+ window.speechSynthesis.speak(utterance);
227
+ }
228
+
229
+ // Listen for the "Esc" key to stop speech
230
+ document.addEventListener('keydown', (event) => {
231
+ if (event.key === 'Escape') {
232
+ // Stop the speech if Esc is pressed
233
+ window.speechSynthesis.cancel();
234
+ }
235
+ });
236
+
237
+ // Send button functionality
238
+ document.getElementById('send-btn').addEventListener('click', () => {
239
+ const userInput = document.getElementById('user-input').value;
240
+ if (userInput.trim() !== '') {
241
+ // Add user message to the chat body
242
+ renderMessages([
243
+ ...Chatbot.messages,
244
+ { type: 'userMessage', message: userInput }
245
+ ]);
246
+
247
+ // Clear the input field
248
+ document.getElementById('user-input').value = '';
249
+
250
+ // Send the user input to the chatbot
251
+ Chatbot.sendMessage(userInput);
252
+ }
253
+ });
254
+
255
+ // Optionally, allow pressing "Enter" to send the message
256
+ document.getElementById('user-input').addEventListener('keypress', (e) => {
257
+ if (e.key === 'Enter') {
258
+ document.getElementById('send-btn').click();
259
+ }
260
+ });
261
+ </script>
262
+ </body>
263
  </html>