Spaces:
Sleeping
Sleeping
Update static/followup-agent.html
Browse files- 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 |
-
|
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 |
-
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 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
botResponse +=
|
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 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 {
|