pvanand commited on
Commit
842550d
·
verified ·
1 Parent(s): 5f097d2

Update static/followup-agent.html

Browse files
Files changed (1) hide show
  1. static/followup-agent.html +100 -98
static/followup-agent.html CHANGED
@@ -171,12 +171,10 @@
171
  padding: 0;
172
  margin-top: 10px;
173
  }
174
-
175
  .audio-button svg {
176
  width: 24px;
177
  height: 24px;
178
  }
179
-
180
  .audio-button:focus {
181
  outline: none;
182
  }
@@ -236,106 +234,112 @@
236
  sanitize: false
237
  });
238
 
239
- const app = new Vue({
240
- el: '#app',
241
- data: {
242
- messages: [],
243
- userInput: '',
244
- selectedOptions: {},
245
- conversationId: '',
246
- currentAudio: null,
247
- },
248
- methods: {
249
- async sendMessage() {
250
- if (!this.userInput.trim()) return;
251
-
252
- this.messages.push({ type: 'user', content: marked.parse(this.userInput) });
253
- const message = this.userInput;
254
- this.userInput = '';
255
- this.selectedOptions = {};
256
-
257
- try {
258
- const response = await fetch('https://pvanand-general-chat.hf.space/v2/followup-agent', {
259
- method: 'POST',
260
- headers: {
261
- 'Content-Type': 'application/json',
262
- 'X-API-Key': '44d5c2ac18ced6fc25c1e57dcd06fc0b31fb4ad97bf56e67540671a647465df4'
263
- },
264
- body: JSON.stringify({
265
- query: message,
266
- model_id: 'openai/gpt-4o-mini',
267
- conversation_id: this.conversationId,
268
- user_id: 'string'
269
- })
270
- });
271
-
272
- const reader = response.body.getReader();
273
- let rawResponse = '';
274
- let jsonResponse = null;
275
- let streamingIndex = this.messages.push({ type: 'bot', content: 'Thinking...' }) - 1;
276
-
277
- while (true) {
278
- const { done, value } = await reader.read();
279
- if (done) break;
280
-
281
- const chunk = new TextDecoder().decode(value);
282
- if (chunk.startsWith('<json>')) {
283
- jsonResponse = JSON.parse(chunk.slice(6));
284
- } else {
285
  rawResponse += chunk;
286
  this.$set(this.messages, streamingIndex, { type: 'bot', content: marked.parse(rawResponse) });
287
  }
 
 
 
 
 
 
 
 
 
 
 
 
288
  }
289
-
290
- this.messages.splice(streamingIndex, 1);
291
- this.renderParsedResponse(rawResponse, jsonResponse);
292
- } catch (error) {
293
- console.error('Error:', error);
294
- this.messages.push({ type: 'bot', content: 'An error occurred while processing your request.' });
295
- }
296
- this.$nextTick(() => this.scrollToBottom());
297
- },
298
- renderParsedResponse(rawResponse, jsonResponse) {
299
- let botResponse = rawResponse + '\n\n';
300
- let responseForAudio = rawResponse.replace(/#/g, '');
301
-
302
- if (jsonResponse && jsonResponse.clarification) {
303
- jsonResponse.clarification.forEach((item, questionIndex) => {
304
- botResponse += `**${item.question}**\n\n`;
305
- botResponse += '<div class="option-buttons">';
306
- item.options.forEach((option, optionIndex) => {
307
- const escapedOption = option.replace(/'/g, "\\'");
308
- botResponse += `<button class="option-button" onclick="app.toggleOption('${escapedOption}', ${questionIndex}, ${optionIndex})">${option}</button>`;
309
- });
310
- botResponse += '</div>\n\n';
311
- });
312
- }
313
-
314
- if (botResponse) {
315
- const markedContent = marked.parse(botResponse);
316
- const messageIndex = this.messages.push({
317
- type: 'bot',
318
- content: markedContent,
319
- audio: null,
320
- isPlaying: false
321
- }) - 1;
322
- this.$nextTick(async () => {
323
- document.querySelectorAll('pre code').forEach((block) => {
324
- hljs.highlightBlock(block);
325
  });
326
- this.scrollToBottom();
327
- this.updateButtonStates();
328
-
329
- if (responseForAudio) {
330
- const audioUrl = await this.convertToSpeech(responseForAudio);
331
- if (audioUrl) {
332
- this.$set(this.messages[messageIndex], 'audio', audioUrl);
333
- this.$forceUpdate();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
334
  }
335
- }
336
- });
337
- }
338
- },
339
  toggleOption(option, questionIndex, optionIndex) {
340
  if (!this.selectedOptions[questionIndex]) {
341
  this.$set(this.selectedOptions, questionIndex, []);
@@ -410,7 +414,6 @@
410
  }
411
  });
412
  }
413
-
414
  if (!message.audioElement) {
415
  message.audioElement = new Audio(message.audio);
416
  message.audioElement.addEventListener('ended', () => {
@@ -418,7 +421,6 @@
418
  this.$forceUpdate();
419
  });
420
  }
421
-
422
  if (message.isPlaying) {
423
  message.audioElement.pause();
424
  } else {
 
171
  padding: 0;
172
  margin-top: 10px;
173
  }
 
174
  .audio-button svg {
175
  width: 24px;
176
  height: 24px;
177
  }
 
178
  .audio-button:focus {
179
  outline: none;
180
  }
 
234
  sanitize: false
235
  });
236
 
237
+ const app = new Vue({
238
+ el: '#app',
239
+ data: {
240
+ messages: [],
241
+ userInput: '',
242
+ selectedOptions: {},
243
+ conversationId: '',
244
+ currentAudio: null,
245
+ },
246
+ methods: {
247
+ async sendMessage() {
248
+ if (!this.userInput.trim()) return;
249
+
250
+ this.messages.push({ type: 'user', content: marked.parse(this.userInput) });
251
+ const message = this.userInput;
252
+ this.userInput = '';
253
+ this.selectedOptions = {};
254
+
255
+ try {
256
+ const response = await fetch('https://pvanand-general-chat.hf.space/followup-agent', {
257
+ method: 'POST',
258
+ headers: {
259
+ 'Content-Type': 'application/json',
260
+ 'X-API-Key': '44d5c2ac18ced6fc25c1e57dcd06fc0b31fb4ad97bf56e67540671a647465df4'
261
+ },
262
+ body: JSON.stringify({
263
+ query: message,
264
+ model_id: 'openai/gpt-4o-mini',
265
+ conversation_id: this.conversationId,
266
+ user_id: 'string'
267
+ })
268
+ });
269
+
270
+ const reader = response.body.getReader();
271
+ let rawResponse = '';
272
+ let streamingIndex = this.messages.push({ type: 'bot', content: 'Thinking...' }) - 1;
273
+
274
+ while (true) {
275
+ const { done, value } = await reader.read();
276
+ if (done) break;
277
+
278
+ const chunk = new TextDecoder().decode(value);
 
 
 
 
279
  rawResponse += chunk;
280
  this.$set(this.messages, streamingIndex, { type: 'bot', content: marked.parse(rawResponse) });
281
  }
282
+
283
+ this.messages.splice(streamingIndex, 1);
284
+
285
+ const jsonStart = rawResponse.lastIndexOf('\n\n');
286
+ if (jsonStart !== -1) {
287
+ const jsonStr = rawResponse.slice(jsonStart + 2);
288
+ const parsedResponse = JSON.parse(jsonStr);
289
+ this.renderParsedResponse(parsedResponse);
290
+ }
291
+ } catch (error) {
292
+ console.error('Error:', error);
293
+ this.messages.push({ type: 'bot', content: 'An error occurred while processing your request.' });
294
  }
295
+ this.$nextTick(() => this.scrollToBottom());
296
+ },
297
+ async renderParsedResponse(parsedResponse) {
298
+ let botResponse = '';
299
+ let responseForAudio = '';
300
+
301
+ if (parsedResponse.response) {
302
+ botResponse += parsedResponse.response + '\n\n';
303
+ responseForAudio = parsedResponse.response.replace(/#/g, '');
304
+ }
305
+
306
+ if (parsedResponse.clarification) {
307
+ parsedResponse.clarification.forEach((item, questionIndex) => {
308
+ botResponse += `**${item.question}**\n\n`;
309
+ botResponse += '<div class="option-buttons">';
310
+ item.options.forEach((option, optionIndex) => {
311
+ const escapedOption = option.replace(/'/g, "\\'");
312
+ botResponse += `<button class="option-button" onclick="app.toggleOption('${escapedOption}', ${questionIndex}, ${optionIndex})">${option}</button>`;
313
+ });
314
+ botResponse += '</div>\n\n';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
315
  });
316
+ }
317
+
318
+ if (botResponse) {
319
+ const markedContent = marked.parse(botResponse);
320
+ const messageIndex = this.messages.push({
321
+ type: 'bot',
322
+ content: markedContent,
323
+ audio: null,
324
+ isPlaying: false
325
+ }) - 1;
326
+ this.$nextTick(async () => {
327
+ document.querySelectorAll('pre code').forEach((block) => {
328
+ hljs.highlightBlock(block);
329
+ });
330
+ this.scrollToBottom();
331
+ this.updateButtonStates();
332
+
333
+ if (responseForAudio) {
334
+ const audioUrl = await this.convertToSpeech(responseForAudio);
335
+ if (audioUrl) {
336
+ this.$set(this.messages[messageIndex], 'audio', audioUrl);
337
+ this.$forceUpdate();
338
+ }
339
  }
340
+ });
341
+ }
342
+ },
 
343
  toggleOption(option, questionIndex, optionIndex) {
344
  if (!this.selectedOptions[questionIndex]) {
345
  this.$set(this.selectedOptions, questionIndex, []);
 
414
  }
415
  });
416
  }
 
417
  if (!message.audioElement) {
418
  message.audioElement = new Audio(message.audio);
419
  message.audioElement.addEventListener('ended', () => {
 
421
  this.$forceUpdate();
422
  });
423
  }
 
424
  if (message.isPlaying) {
425
  message.audioElement.pause();
426
  } else {