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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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>AI Chatbot</title>
|
| 7 |
+
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
|
| 8 |
+
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
| 9 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/uuid/8.3.2/uuid.min.js"></script>
|
| 10 |
+
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
|
| 11 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/styles/default.min.css">
|
| 12 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.5.1/highlight.min.js"></script>
|
| 13 |
+
<style>
|
| 14 |
+
:root {
|
| 15 |
+
--accent-color: #003366; /* Navy blue */
|
| 16 |
+
}
|
| 17 |
+
body {
|
| 18 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 19 |
+
line-height: 1.6;
|
| 20 |
+
margin: 0 auto;
|
| 21 |
+
padding: 0px;
|
| 22 |
+
background-color: #f5f7fa;
|
| 23 |
+
color: #333;
|
| 24 |
+
}
|
| 25 |
+
h2 {
|
| 26 |
+
color: var(--accent-color);
|
| 27 |
+
border-bottom: 2px solid var(--accent-color);
|
| 28 |
+
padding-bottom: 10px;
|
| 29 |
+
margin-top: 0;
|
| 30 |
+
}
|
| 31 |
+
#app {
|
| 32 |
+
max-width: 800px;
|
| 33 |
+
margin: 0 auto;
|
| 34 |
+
padding: 10px;
|
| 35 |
+
}
|
| 36 |
+
.chat-container {
|
| 37 |
+
background-color: #ffffff;
|
| 38 |
+
border-radius: 10px;
|
| 39 |
+
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
| 40 |
+
overflow: hidden;
|
| 41 |
+
display: flex;
|
| 42 |
+
flex-direction: column;
|
| 43 |
+
height: 99vh;
|
| 44 |
+
}
|
| 45 |
+
.messages {
|
| 46 |
+
flex-grow: 1;
|
| 47 |
+
overflow-y: auto;
|
| 48 |
+
padding: 20px;
|
| 49 |
+
display: flex;
|
| 50 |
+
flex-direction: column;
|
| 51 |
+
}
|
| 52 |
+
.message {
|
| 53 |
+
max-width: 80%;
|
| 54 |
+
margin-bottom: 20px;
|
| 55 |
+
padding: 12px 16px;
|
| 56 |
+
border-radius: 18px;
|
| 57 |
+
font-size: 14px;
|
| 58 |
+
line-height: 1.4;
|
| 59 |
+
word-wrap: break-word;
|
| 60 |
+
overflow-wrap: break-word;
|
| 61 |
+
}
|
| 62 |
+
.user-message {
|
| 63 |
+
background-color: #364e67;
|
| 64 |
+
color: #ffffff;
|
| 65 |
+
align-self: flex-end;
|
| 66 |
+
}
|
| 67 |
+
.bot-message {
|
| 68 |
+
background-color: #f0f2f5;
|
| 69 |
+
color: #333;
|
| 70 |
+
align-self: flex-start;
|
| 71 |
+
background-color: #ffffff;
|
| 72 |
+
padding: 30px;
|
| 73 |
+
border-radius: 8px;
|
| 74 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 75 |
+
overflow-wrap: break-word;
|
| 76 |
+
word-wrap: break-word;
|
| 77 |
+
word-break: break-word;
|
| 78 |
+
}
|
| 79 |
+
.input-area {
|
| 80 |
+
display: flex;
|
| 81 |
+
padding: 10px;
|
| 82 |
+
background-color: #ffffff;
|
| 83 |
+
border-top: 1px solid #e0e0e0;
|
| 84 |
+
}
|
| 85 |
+
#user-input {
|
| 86 |
+
flex-grow: 1;
|
| 87 |
+
padding: 12px;
|
| 88 |
+
border: 1px solid #d0d0d0;
|
| 89 |
+
border-radius: 20px;
|
| 90 |
+
font-size: 14px;
|
| 91 |
+
outline: none;
|
| 92 |
+
}
|
| 93 |
+
.send-button {
|
| 94 |
+
background-color: #007bff;
|
| 95 |
+
color: #ffffff;
|
| 96 |
+
border: none;
|
| 97 |
+
border-radius: 50%;
|
| 98 |
+
width: 40px;
|
| 99 |
+
height: 40px;
|
| 100 |
+
margin-left: 10px;
|
| 101 |
+
cursor: pointer;
|
| 102 |
+
display: flex;
|
| 103 |
+
align-items: center;
|
| 104 |
+
justify-content: center;
|
| 105 |
+
transition: background-color 0.3s;
|
| 106 |
+
}
|
| 107 |
+
.send-button:hover {
|
| 108 |
+
background-color: #0056b3;
|
| 109 |
+
}
|
| 110 |
+
.send-button svg {
|
| 111 |
+
width: 20px;
|
| 112 |
+
height: 20px;
|
| 113 |
+
}
|
| 114 |
+
.reset-button {
|
| 115 |
+
background-color: #f44336;
|
| 116 |
+
color: #ffffff;
|
| 117 |
+
border: none;
|
| 118 |
+
border-radius: 20px;
|
| 119 |
+
padding: 8px 16px;
|
| 120 |
+
margin-left: 10px;
|
| 121 |
+
cursor: pointer;
|
| 122 |
+
font-size: 14px;
|
| 123 |
+
transition: background-color 0.3s;
|
| 124 |
+
}
|
| 125 |
+
.reset-button:hover {
|
| 126 |
+
background-color: #d32f2f;
|
| 127 |
+
}
|
| 128 |
+
.option-buttons {
|
| 129 |
+
display: flex;
|
| 130 |
+
flex-wrap: wrap;
|
| 131 |
+
gap: 10px;
|
| 132 |
+
margin-bottom: 15px;
|
| 133 |
+
}
|
| 134 |
+
.option-button {
|
| 135 |
+
background-color: #f0f2f5;
|
| 136 |
+
border: 1px solid #d0d0d0;
|
| 137 |
+
border-radius: 20px;
|
| 138 |
+
padding: 8px 16px;
|
| 139 |
+
font-size: 14px;
|
| 140 |
+
cursor: pointer;
|
| 141 |
+
transition: background-color 0.3s, transform 0.1s;
|
| 142 |
+
}
|
| 143 |
+
.option-button:hover {
|
| 144 |
+
background-color: #e4e6e9;
|
| 145 |
+
transform: translateY(-2px);
|
| 146 |
+
}
|
| 147 |
+
.option-button:active {
|
| 148 |
+
transform: translateY(0);
|
| 149 |
+
}
|
| 150 |
+
.option-button.selected {
|
| 151 |
+
background-color: #9fb4cd;
|
| 152 |
+
border-color: #c9d9ec;
|
| 153 |
+
}
|
| 154 |
+
.play-button {
|
| 155 |
+
background-color: #4CAF50;
|
| 156 |
+
border: none;
|
| 157 |
+
color: white;
|
| 158 |
+
padding: 10px 20px;
|
| 159 |
+
text-align: center;
|
| 160 |
+
text-decoration: none;
|
| 161 |
+
display: inline-block;
|
| 162 |
+
font-size: 16px;
|
| 163 |
+
margin: 4px 2px;
|
| 164 |
+
cursor: pointer;
|
| 165 |
+
border-radius: 4px;
|
| 166 |
+
}
|
| 167 |
+
.audio-button {
|
| 168 |
+
background: none;
|
| 169 |
+
border: none;
|
| 170 |
+
cursor: pointer;
|
| 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 |
+
}
|
| 181 |
+
@media (max-width: 600px) {
|
| 182 |
+
#app {
|
| 183 |
+
padding: 10px;
|
| 184 |
+
}
|
| 185 |
+
.chat-container {
|
| 186 |
+
height: 90vh;
|
| 187 |
+
}
|
| 188 |
+
.message {
|
| 189 |
+
max-width: 90%;
|
| 190 |
+
}
|
| 191 |
+
}
|
| 192 |
+
</style>
|
| 193 |
+
</head>
|
| 194 |
+
<body>
|
| 195 |
+
<div id="app">
|
| 196 |
+
<div class="chat-container">
|
| 197 |
+
<div class="messages" ref="messageContainer">
|
| 198 |
+
<div v-for="(message, index) in messages" :key="index"
|
| 199 |
+
:class="['message', message.type === 'user' ? 'user-message' : 'bot-message']">
|
| 200 |
+
<div v-html="message.content"></div>
|
| 201 |
+
<button v-if="message.type === 'bot' && message.audio" @click="toggleAudio(index)" class="audio-button">
|
| 202 |
+
<svg v-if="!message.isPlaying" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 203 |
+
<polygon points="5 3 19 12 5 21 5 3"></polygon>
|
| 204 |
+
</svg>
|
| 205 |
+
<svg v-else xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 206 |
+
<rect x="6" y="4" width="4" height="16"></rect>
|
| 207 |
+
<rect x="14" y="4" width="4" height="16"></rect>
|
| 208 |
+
</svg>
|
| 209 |
+
</button>
|
| 210 |
+
</div>
|
| 211 |
+
</div>
|
| 212 |
+
<div class="input-area">
|
| 213 |
+
<input type="text" id="user-input" v-model="userInput" @keyup.enter="sendMessage" placeholder="Type your message...">
|
| 214 |
+
<button class="send-button" @click="sendMessage">
|
| 215 |
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
| 216 |
+
<line x1="22" y1="2" x2="11" y2="13"></line>
|
| 217 |
+
<polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
|
| 218 |
+
</svg>
|
| 219 |
+
</button>
|
| 220 |
+
<button class="reset-button" @click="resetConversation">Reset</button>
|
| 221 |
+
</div>
|
| 222 |
+
</div>
|
| 223 |
+
</div>
|
| 224 |
+
|
| 225 |
+
<script>
|
| 226 |
+
marked.setOptions({
|
| 227 |
+
highlight: function (code, lang) {
|
| 228 |
+
if (lang && hljs.getLanguage(lang)) {
|
| 229 |
+
return hljs.highlight(code, { language: lang }).value;
|
| 230 |
+
} else {
|
| 231 |
+
return hljs.highlightAuto(code).value;
|
| 232 |
+
}
|
| 233 |
+
},
|
| 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 |
+
try {
|
| 255 |
+
const response = await fetch('https://pvanand-general-chat.hf.space/v2/followup-agent', {
|
| 256 |
+
method: 'POST',
|
| 257 |
+
headers: {
|
| 258 |
+
'Content-Type': 'application/json',
|
| 259 |
+
'X-API-Key': '44d5c2ac18ced6fc25c1e57dcd06fc0b31fb4ad97bf56e67540671a647465df4'
|
| 260 |
+
},
|
| 261 |
+
body: JSON.stringify({
|
| 262 |
+
query: message,
|
| 263 |
+
model_id: 'openai/gpt-4o-mini',
|
| 264 |
+
conversation_id: this.conversationId,
|
| 265 |
+
user_id: 'string'
|
| 266 |
+
})
|
| 267 |
+
});
|
| 268 |
+
const reader = response.body.getReader();
|
| 269 |
+
let rawResponse = '';
|
| 270 |
+
let jsonData = null;
|
| 271 |
+
let streamingIndex = this.messages.push({ type: 'bot', content: '' }) - 1;
|
| 272 |
+
while (true) {
|
| 273 |
+
const { done, value } = await reader.read();
|
| 274 |
+
if (done) break;
|
| 275 |
+
const chunk = new TextDecoder().decode(value);
|
| 276 |
+
if (chunk.includes('<json>')) {
|
| 277 |
+
const [textPart, jsonPart] = chunk.split('<json>');
|
| 278 |
+
rawResponse += textPart;
|
| 279 |
+
jsonData = JSON.parse(jsonPart);
|
| 280 |
+
} else {
|
| 281 |
+
rawResponse += chunk;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// Remove <interact> </interact> section
|
| 285 |
+
rawResponse = rawResponse.replace(/<interact>[\s\S]*?<\/interact>/g, '');
|
| 286 |
+
|
| 287 |
+
this.$set(this.messages[streamingIndex], 'content', marked.parse(rawResponse));
|
| 288 |
+
}
|
| 289 |
+
if (jsonData) {
|
| 290 |
+
this.renderClarificationQuestions(jsonData.clarification, streamingIndex);
|
| 291 |
+
}
|
| 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 |
+
renderClarificationQuestions(clarification, messageIndex) {
|
| 299 |
+
if (!clarification || clarification.length === 0) return;
|
| 300 |
+
|
| 301 |
+
let clarificationHtml = '';
|
| 302 |
+
clarification.forEach((item, questionIndex) => {
|
| 303 |
+
clarificationHtml += `<strong>${item.question}</strong><br>`;
|
| 304 |
+
clarificationHtml += '<div class="option-buttons">';
|
| 305 |
+
item.options.forEach((option, optionIndex) => {
|
| 306 |
+
const escapedOption = option.replace(/'/g, "\\'");
|
| 307 |
+
clarificationHtml += `<button class="option-button" onclick="app.toggleOption('${escapedOption}', ${questionIndex}, ${optionIndex})">${option}</button>`;
|
| 308 |
+
});
|
| 309 |
+
clarificationHtml += '</div><br>';
|
| 310 |
+
});
|
| 311 |
+
|
| 312 |
+
this.$set(this.messages[messageIndex], 'content', this.messages[messageIndex].content + marked.parse(clarificationHtml));
|
| 313 |
+
this.$nextTick(() => {
|
| 314 |
+
this.scrollToBottom();
|
| 315 |
+
this.updateButtonStates();
|
| 316 |
+
});
|
| 317 |
+
},
|
| 318 |
+
toggleOption(option, questionIndex, optionIndex) {
|
| 319 |
+
if (!this.selectedOptions[questionIndex]) {
|
| 320 |
+
this.$set(this.selectedOptions, questionIndex, []);
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
const index = this.selectedOptions[questionIndex].indexOf(option);
|
| 324 |
+
if (index > -1) {
|
| 325 |
+
this.selectedOptions[questionIndex].splice(index, 1);
|
| 326 |
+
} else {
|
| 327 |
+
this.selectedOptions[questionIndex].push(option);
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
this.updateInputFromSelectedOptions();
|
| 331 |
+
this.updateButtonStates();
|
| 332 |
+
},
|
| 333 |
+
updateInputFromSelectedOptions() {
|
| 334 |
+
this.userInput = Object.entries(this.selectedOptions)
|
| 335 |
+
.map(([questionIndex, options]) =>
|
| 336 |
+
`Q${parseInt(questionIndex) + 1}: ${options.join(', ')}`)
|
| 337 |
+
.join(' | ');
|
| 338 |
+
this.$nextTick(() => document.getElementById('user-input').focus());
|
| 339 |
+
},
|
| 340 |
+
updateButtonStates() {
|
| 341 |
+
Object.entries(this.selectedOptions).forEach(([questionIndex, options]) => {
|
| 342 |
+
const buttons = document.querySelectorAll(`.option-buttons:nth-of-type(${parseInt(questionIndex) + 1}) .option-button`);
|
| 343 |
+
buttons.forEach((button) => {
|
| 344 |
+
if (options.includes(button.textContent)) {
|
| 345 |
+
button.classList.add('selected');
|
| 346 |
+
} else {
|
| 347 |
+
button.classList.remove('selected');
|
| 348 |
+
}
|
| 349 |
+
});
|
| 350 |
+
});
|
| 351 |
+
},
|
| 352 |
+
resetConversation() {
|
| 353 |
+
this.conversationId = uuid.v4();
|
| 354 |
+
this.messages = [{ type: 'bot', content: 'Conversation reset. How can I help you?' }];
|
| 355 |
+
this.selectedOptions = {};
|
| 356 |
+
this.userInput = '';
|
| 357 |
+
},
|
| 358 |
+
scrollToBottom() {
|
| 359 |
+
const container = this.$refs.messageContainer;
|
| 360 |
+
container.scrollTop = container.scrollHeight;
|
| 361 |
+
},
|
| 362 |
+
async convertToSpeech(text) {
|
| 363 |
+
const voice = 'en-US-JennyNeural';
|
| 364 |
+
const encodedText = encodeURIComponent(text);
|
| 365 |
+
try {
|
| 366 |
+
const response = await fetch(`https://pvanand-audio-chat.hf.space/tts?text=${encodedText}&voice=${voice}`, {
|
| 367 |
+
method: 'GET',
|
| 368 |
+
headers: {
|
| 369 |
+
'accept': 'application/json'
|
| 370 |
+
}
|
| 371 |
+
});
|
| 372 |
+
if (!response.ok) {
|
| 373 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 374 |
+
}
|
| 375 |
+
const blob = await response.blob();
|
| 376 |
+
return URL.createObjectURL(blob);
|
| 377 |
+
} catch (error) {
|
| 378 |
+
console.error('Error:', error);
|
| 379 |
+
return null;
|
| 380 |
+
}
|
| 381 |
+
},
|
| 382 |
+
toggleAudio(index) {
|
| 383 |
+
const message = this.messages[index];
|
| 384 |
+
if (this.currentAudio && this.currentAudio !== message.audioElement) {
|
| 385 |
+
this.currentAudio.pause();
|
| 386 |
+
this.messages.forEach(m => {
|
| 387 |
+
if (m.audioElement === this.currentAudio) {
|
| 388 |
+
m.isPlaying = false;
|
| 389 |
+
}
|
| 390 |
+
});
|
| 391 |
+
}
|
| 392 |
+
if (!message.audioElement) {
|
| 393 |
+
message.audioElement = new Audio(message.audio);
|
| 394 |
+
message.audioElement.addEventListener('ended', () => {
|
| 395 |
+
message.isPlaying = false;
|
| 396 |
+
this.$forceUpdate();
|
| 397 |
+
});
|
| 398 |
+
}
|
| 399 |
+
if (message.isPlaying) {
|
| 400 |
+
message.audioElement.pause();
|
| 401 |
+
} else {
|
| 402 |
+
message.audioElement.play();
|
| 403 |
+
this.currentAudio = message.audioElement;
|
| 404 |
+
}
|
| 405 |
+
message.isPlaying = !message.isPlaying;
|
| 406 |
+
this.$forceUpdate();
|
| 407 |
+
}
|
| 408 |
+
},
|
| 409 |
+
mounted() {
|
| 410 |
+
this.resetConversation();
|
| 411 |
+
}
|
| 412 |
+
});
|
| 413 |
+
</script>
|
| 414 |
+
</body>
|
| 415 |
+
</html>
|