Spaces:
Sleeping
Sleeping
Gopikanth123
commited on
Update main.py
Browse files
main.py
CHANGED
@@ -1,480 +1,151 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
--input-border: #D1C4E9;
|
60 |
-
--accent-color: #7B1FA2;
|
61 |
-
}
|
62 |
-
|
63 |
-
.theme-bright-summer {
|
64 |
-
--background-color: #FFEB3B;
|
65 |
-
--bot-message-bg: #FFF9C4;
|
66 |
-
--user-message-bg: #F44336;
|
67 |
-
--user-message-text: #FFFFFF;
|
68 |
-
--input-bg: #FFFFFF;
|
69 |
-
--input-border: #FBC02D;
|
70 |
-
--accent-color: #C62828;
|
71 |
-
}
|
72 |
-
|
73 |
-
* {
|
74 |
-
margin: 0;
|
75 |
-
padding: 0;
|
76 |
-
box-sizing: border-box;
|
77 |
-
}
|
78 |
-
|
79 |
-
body {
|
80 |
-
font-family: 'Roboto', sans-serif;
|
81 |
-
background-color: var(--background-color);
|
82 |
-
transition: background 0.5s ease;
|
83 |
-
overflow: hidden; /* Prevent scroll on body */
|
84 |
-
}
|
85 |
-
|
86 |
-
.container {
|
87 |
-
display: flex;
|
88 |
-
flex-direction: column;
|
89 |
-
height: 100vh;
|
90 |
-
width: 100%;
|
91 |
-
overflow: hidden; /* Prevent scroll on container */
|
92 |
-
}
|
93 |
-
|
94 |
-
/* Header */
|
95 |
-
.header {
|
96 |
-
background: var(--accent-color);
|
97 |
-
color: #fff;
|
98 |
-
font-size: 1.5rem;
|
99 |
-
font-weight: 700;
|
100 |
-
text-align: center;
|
101 |
-
padding: 20px;
|
102 |
-
box-shadow: 0 4px 8px var(--shadow-color);
|
103 |
-
position: relative;
|
104 |
-
z-index: 1000;
|
105 |
-
}
|
106 |
-
|
107 |
-
/* Chatbox */
|
108 |
-
.chat-box {
|
109 |
-
flex: 1;
|
110 |
-
display: flex;
|
111 |
-
flex-direction: column;
|
112 |
-
overflow-y: auto;
|
113 |
-
padding: 20px;
|
114 |
-
background: linear-gradient(to bottom right, #f5f7fa, #c3cfe2);
|
115 |
-
border-radius: 10px;
|
116 |
-
margin: 20px;
|
117 |
-
box-shadow: 0 4px 8px var(--shadow-color);
|
118 |
-
position: relative;
|
119 |
-
z-index: 10;
|
120 |
-
}
|
121 |
-
|
122 |
-
.message {
|
123 |
-
max-width: 75%;
|
124 |
-
padding: 12px 18px;
|
125 |
-
border-radius: 20px;
|
126 |
-
box-shadow: 0 3px 6px var(--shadow-color);
|
127 |
-
margin-bottom: 10px;
|
128 |
-
opacity: 0;
|
129 |
-
animation: fadeIn 0.3s forwards; /* Changed to forwards for delay effect */
|
130 |
-
}
|
131 |
-
|
132 |
-
.user-message {
|
133 |
-
align-self: flex-end;
|
134 |
-
background: var(--user-message-bg);
|
135 |
-
color: var(--user-message-text);
|
136 |
-
border-radius: 15px 20px 20px 20px;
|
137 |
-
animation: slideInRight 0.5s forwards; /* Sliding effect on user message */
|
138 |
-
}
|
139 |
-
|
140 |
-
.bot-message {
|
141 |
-
align-self: flex-start;
|
142 |
-
background: var(--bot-message-bg);
|
143 |
-
color: #333;
|
144 |
-
border-radius: 20px 15px 20px 20px;
|
145 |
-
animation: slideInLeft 0.5s forwards; /* Sliding effect on bot message */
|
146 |
-
}
|
147 |
-
|
148 |
-
/* Footer */
|
149 |
-
.footer {
|
150 |
-
background: #ffffff;
|
151 |
-
padding: 15px;
|
152 |
-
display: flex;
|
153 |
-
justify-content: center;
|
154 |
-
align-items: center;
|
155 |
-
box-shadow: 0 -4px 8px var(--shadow-color);
|
156 |
-
position: relative;
|
157 |
-
z-index: 1000;
|
158 |
-
}
|
159 |
-
|
160 |
-
.footer input[type="text"] {
|
161 |
-
width: 75%;
|
162 |
-
padding: 15px;
|
163 |
-
border: 1px solid var(--input-border);
|
164 |
-
border-radius: 20px;
|
165 |
-
margin-right: 10px;
|
166 |
-
box-shadow: 0 2px 4px var(--shadow-color);
|
167 |
-
transition: border 0.3s, box-shadow 0.3s; /* Added shadow transition */
|
168 |
-
background-color: var(--input-bg);
|
169 |
-
outline: none;
|
170 |
-
}
|
171 |
-
|
172 |
-
.footer input[type="text"]:focus {
|
173 |
-
border-color: var(--accent-color);
|
174 |
-
box-shadow: 0 0 10px var(--accent-color);
|
175 |
-
}
|
176 |
-
|
177 |
-
button {
|
178 |
-
background: var(--accent-color);
|
179 |
-
color: #fff;
|
180 |
-
border: none;
|
181 |
-
padding: 10px 20px;
|
182 |
-
border-radius: 20px;
|
183 |
-
font-size: 1rem;
|
184 |
-
cursor: pointer;
|
185 |
-
box-shadow: 0 4px 10px var(--shadow-color);
|
186 |
-
transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.2s ease;
|
187 |
-
}
|
188 |
-
|
189 |
-
button:hover {
|
190 |
-
background: #4b0082;
|
191 |
-
transform: scale(1.05);
|
192 |
-
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5); /* Shadow effect on hover */
|
193 |
-
}
|
194 |
-
|
195 |
-
/* Settings */
|
196 |
-
.settings {
|
197 |
-
display: flex;
|
198 |
-
justify-content: space-between;
|
199 |
-
align-items: center;
|
200 |
-
padding: 10px;
|
201 |
-
background: #f0f2f5;
|
202 |
-
box-shadow: 0 2px 5px var(--shadow-color);
|
203 |
-
position: relative;
|
204 |
-
z-index: 1000;
|
205 |
-
}
|
206 |
-
|
207 |
-
.color-picker,
|
208 |
-
.theme-toggle {
|
209 |
-
display: flex;
|
210 |
-
align-items: center;
|
211 |
-
position:relative;
|
212 |
-
}
|
213 |
-
|
214 |
-
.color-circle {
|
215 |
-
width: 20px;
|
216 |
-
height: 20px;
|
217 |
-
border-radius: 50%;
|
218 |
-
margin: 0 5px;
|
219 |
-
cursor: pointer;
|
220 |
-
box-shadow: 0 2px 5px var(--shadow-color);
|
221 |
-
transition: transform 0.3s; /* Adding circle hover effect */
|
222 |
-
}
|
223 |
-
|
224 |
-
.color-circle:hover {
|
225 |
-
transform: scale(1.2); /* Scale up on hover */
|
226 |
-
}
|
227 |
-
|
228 |
-
/* Animations */
|
229 |
-
@keyframes fadeIn {
|
230 |
-
from {
|
231 |
-
opacity: 0;
|
232 |
-
transform: translateY(20px);
|
233 |
-
}
|
234 |
-
|
235 |
-
to {
|
236 |
-
opacity: 1;
|
237 |
-
transform: translateY(0);
|
238 |
-
}
|
239 |
-
}
|
240 |
-
|
241 |
-
@keyframes slideInRight {
|
242 |
-
from {
|
243 |
-
opacity: 0;
|
244 |
-
transform: translateX(100%);
|
245 |
-
}
|
246 |
-
to {
|
247 |
-
opacity: 1;
|
248 |
-
transform: translateX(0);
|
249 |
-
}
|
250 |
-
}
|
251 |
-
|
252 |
-
@keyframes slideInLeft {
|
253 |
-
from {
|
254 |
-
opacity: 0;
|
255 |
-
transform: translateX(-100%);
|
256 |
-
}
|
257 |
-
to {
|
258 |
-
opacity: 1;
|
259 |
-
transform: translateX(0);
|
260 |
-
}
|
261 |
-
}
|
262 |
-
|
263 |
-
/* Background animation */
|
264 |
-
@keyframes backgroundAnimate {
|
265 |
-
0% {
|
266 |
-
background-color: rgba(255, 255, 255, 0.05);
|
267 |
-
}
|
268 |
-
50% {
|
269 |
-
background-color: rgba(255, 255, 255, 0.1);
|
270 |
-
}
|
271 |
-
100% {
|
272 |
-
background-color: rgba(255, 255, 255, 0.05);
|
273 |
-
}
|
274 |
-
}
|
275 |
-
|
276 |
-
/* VFX related styles */
|
277 |
-
.vfx {
|
278 |
-
position: absolute;
|
279 |
-
top: 0;
|
280 |
-
left: 0;
|
281 |
-
width: 100%;
|
282 |
-
height: 100%;
|
283 |
-
pointer-events: none;
|
284 |
-
animation: backgroundAnimate 5s infinite; /* Continuously animate background */
|
285 |
-
z-index: 0; /* Background layer */
|
286 |
-
}
|
287 |
-
</style>
|
288 |
-
</head>
|
289 |
-
|
290 |
-
<body>
|
291 |
-
<div class="vfx"></div> <!-- Background effects -->
|
292 |
-
|
293 |
-
<div class="container">
|
294 |
-
<!-- Settings -->
|
295 |
-
<div class="settings">
|
296 |
-
<div class="theme-toggle">
|
297 |
-
<label for="theme-select">Select Theme:</label>
|
298 |
-
<select id="theme-select">
|
299 |
-
<option value="default">Default</option>
|
300 |
-
<option value="calm-azure">Calm Azure</option>
|
301 |
-
<option value="elegant-charcoal">Elegant Charcoal</option>
|
302 |
-
<option value="fresh-greenery">Fresh Greenery</option>
|
303 |
-
<option value="soft-lavender">Soft Lavender</option>
|
304 |
-
<option value="bright-summer">Bright Summer</option>
|
305 |
-
</select>
|
306 |
-
</div>
|
307 |
-
<div class="color-picker">
|
308 |
-
<label>Accent Color:</label>
|
309 |
-
<div class="color-circle" style="background-color: #6a0dad;" onclick="changeColor('#6a0dad')"></div>
|
310 |
-
<div class="color-circle" style="background-color: #ff4500;" onclick="changeColor('#ff4500')"></div>
|
311 |
-
<div class="color-circle" style="background-color: #007bff;" onclick="changeColor('#007bff')"></div>
|
312 |
-
<div class="color-circle" style="background-color: #28a745;" onclick="changeColor('#28a745')"></div>
|
313 |
-
</div>
|
314 |
-
</div>
|
315 |
-
|
316 |
-
<!-- Header -->
|
317 |
-
<div class="header">TAJ HOTEL CHATBOT</div>
|
318 |
-
|
319 |
-
<!-- Chatbox -->
|
320 |
-
<div class="chat-box" id="chat-box"></div>
|
321 |
-
|
322 |
-
<!-- Footer -->
|
323 |
-
<div class="footer">
|
324 |
-
<input type="text" id="user-input" placeholder="Type your message..." />
|
325 |
-
<button id="send-btn">Send</button>
|
326 |
-
<button id="voice-btn">🎤 Start Voice Input</button>
|
327 |
-
|
328 |
-
<!-- Language dropdown -->
|
329 |
-
<select id="language-select">
|
330 |
-
<option value="english" selected>English</option>
|
331 |
-
<option value="telugu">Telugu</option>
|
332 |
-
<option value="hindi">Hindi</option>
|
333 |
-
</select>
|
334 |
-
</div>
|
335 |
-
|
336 |
-
</div>
|
337 |
-
|
338 |
-
<script>
|
339 |
-
const chatBox = document.getElementById('chat-box');
|
340 |
-
const voiceBtn = document.getElementById('voice-btn');
|
341 |
-
const sendBtn = document.getElementById('send-btn');
|
342 |
-
const userInput = document.getElementById('user-input');
|
343 |
-
const themeSelect = document.getElementById('theme-select');
|
344 |
-
const languageSelect = document.getElementById('language-select');
|
345 |
-
|
346 |
-
// Add message to chatbox with visual effects
|
347 |
-
function addMessage(sender, text) {
|
348 |
-
const msgDiv = document.createElement('div');
|
349 |
-
msgDiv.classList.add('message', sender);
|
350 |
-
msgDiv.textContent = text;
|
351 |
-
chatBox.appendChild(msgDiv);
|
352 |
-
chatBox.scrollTop = chatBox.scrollHeight;
|
353 |
-
}
|
354 |
-
|
355 |
-
// Speech Recognition Setup
|
356 |
-
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
357 |
-
// recognition.lang = 'en-US';
|
358 |
-
// Function to set language for speech recognition
|
359 |
-
function setRecognitionLanguage() {
|
360 |
-
const selectedLanguage = languageSelect.value;
|
361 |
-
switch (selectedLanguage) {
|
362 |
-
case 'telugu':
|
363 |
-
recognition.lang = 'te-IN'; // Telugu
|
364 |
-
break;
|
365 |
-
case 'hindi':
|
366 |
-
recognition.lang = 'hi-IN'; // Hindi
|
367 |
-
break;
|
368 |
-
default:
|
369 |
-
recognition.lang = 'en-US'; // English
|
370 |
-
break;
|
371 |
-
}
|
372 |
-
}
|
373 |
-
// voiceBtn.addEventListener('click', () => recognition.start());
|
374 |
-
voiceBtn.addEventListener('click', () => {
|
375 |
-
setRecognitionLanguage(); // Set the language before starting speech recognition
|
376 |
-
recognition.start();
|
377 |
-
});
|
378 |
-
|
379 |
-
recognition.addEventListener('result', (e) => {
|
380 |
-
const transcript = e.results[0][0].transcript;
|
381 |
-
addMessage('user-message', transcript);
|
382 |
-
sendUserMessage(transcript);
|
383 |
-
});
|
384 |
-
|
385 |
-
// Function to change the accent color
|
386 |
-
function changeColor(color) {
|
387 |
-
document.documentElement.style.setProperty('--accent-color', color);
|
388 |
-
}
|
389 |
-
|
390 |
-
// Function to change the theme
|
391 |
-
function changeTheme(theme) {
|
392 |
-
document.documentElement.classList.remove('theme-calm-azure', 'theme-elegant-charcoal', 'theme-fresh-greenery', 'theme-soft-lavender', 'theme-bright-summer');
|
393 |
-
if (theme !== 'default') {
|
394 |
-
document.documentElement.classList.add('theme-' + theme);
|
395 |
-
}
|
396 |
-
}
|
397 |
-
|
398 |
-
// Function to send user input and selected language to backend
|
399 |
-
function sendUserMessage(message) {
|
400 |
-
const selectedLanguage = languageSelect.value; // Get the selected language
|
401 |
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
|
407 |
-
|
408 |
-
|
409 |
-
|
410 |
-
language: selectedLanguage, // Include the selected language in the request body
|
411 |
-
}),
|
412 |
-
})
|
413 |
-
.then(response => response.json())
|
414 |
-
.then(data => {
|
415 |
-
const botResponse = data.response;
|
416 |
-
addMessage('bot-message', botResponse);
|
417 |
-
speakResponse(botResponse);
|
418 |
-
})
|
419 |
-
.catch(error => {
|
420 |
-
console.error("Error:", error);
|
421 |
-
addMessage('bot-message', "Sorry, I couldn't process that.");
|
422 |
-
});
|
423 |
-
}
|
424 |
|
425 |
-
|
426 |
-
|
427 |
-
// const utterance = new SpeechSynthesisUtterance(text);
|
428 |
-
// utterance.lang = 'en-US';
|
429 |
-
// window.speechSynthesis.speak(utterance);
|
430 |
-
// }
|
431 |
-
// Text-to-Speech Function
|
432 |
-
function speakResponse(text) {
|
433 |
-
const utterance = new SpeechSynthesisUtterance(text);
|
434 |
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
utterance.lang = 'te-IN'; // Telugu
|
440 |
-
break;
|
441 |
-
case 'hindi':
|
442 |
-
utterance.lang = 'hi-IN'; // Hindi
|
443 |
-
break;
|
444 |
-
default:
|
445 |
-
utterance.lang = 'en-US'; // English
|
446 |
-
break;
|
447 |
-
}
|
448 |
|
449 |
-
|
450 |
-
|
451 |
-
|
452 |
-
|
453 |
-
|
454 |
-
|
455 |
-
|
456 |
-
|
457 |
-
|
458 |
-
|
459 |
-
|
460 |
-
});
|
461 |
|
462 |
-
// Handle pressing 'Enter' key for sending messages
|
463 |
-
userInput.addEventListener('keypress', (e) => {
|
464 |
-
if (e.key === 'Enter') {
|
465 |
-
sendBtn.click(); // Trigger button click
|
466 |
-
}
|
467 |
-
});
|
468 |
-
|
469 |
-
// Update theme when selected from dropdown
|
470 |
-
themeSelect.addEventListener('change', (e) => {
|
471 |
-
changeTheme(e.target.value);
|
472 |
-
});
|
473 |
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import shutil
|
3 |
+
from flask import Flask, render_template, request, jsonify
|
4 |
+
from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
|
5 |
+
from llama_index.llms.huggingface import HuggingFaceInferenceAPI
|
6 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
7 |
+
from huggingface_hub import InferenceClient
|
8 |
+
from transformers import AutoTokenizer, AutoModel
|
9 |
+
|
10 |
+
|
11 |
+
# Ensure HF_TOKEN is set
|
12 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
13 |
+
if not HF_TOKEN:
|
14 |
+
raise ValueError("HF_TOKEN environment variable not set.")
|
15 |
+
|
16 |
+
repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
|
17 |
+
llm_client = InferenceClient(
|
18 |
+
model=repo_id,
|
19 |
+
token=HF_TOKEN,
|
20 |
+
)
|
21 |
+
|
22 |
+
# Configure Llama index settings
|
23 |
+
Settings.llm = HuggingFaceInferenceAPI(
|
24 |
+
model_name=repo_id,
|
25 |
+
tokenizer_name=repo_id,
|
26 |
+
context_window=3000,
|
27 |
+
token=HF_TOKEN,
|
28 |
+
max_new_tokens=512,
|
29 |
+
generate_kwargs={"temperature": 0.1},
|
30 |
+
)
|
31 |
+
# Settings.embed_model = HuggingFaceEmbedding(
|
32 |
+
# model_name="BAAI/bge-small-en-v1.5"
|
33 |
+
# )
|
34 |
+
# Replace the embedding model with XLM-R
|
35 |
+
Settings.embed_model = HuggingFaceEmbedding(
|
36 |
+
model_name="xlm-roberta-base" # XLM-RoBERTa model for multilingual support
|
37 |
+
)
|
38 |
+
|
39 |
+
# Configure tokenizer and model if required
|
40 |
+
tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
|
41 |
+
model = AutoModel.from_pretrained("xlm-roberta-base")
|
42 |
+
|
43 |
+
PERSIST_DIR = "db"
|
44 |
+
PDF_DIRECTORY = 'data'
|
45 |
+
|
46 |
+
# Ensure directories exist
|
47 |
+
os.makedirs(PDF_DIRECTORY, exist_ok=True)
|
48 |
+
os.makedirs(PERSIST_DIR, exist_ok=True)
|
49 |
+
chat_history = []
|
50 |
+
current_chat_history = []
|
51 |
+
|
52 |
+
def data_ingestion_from_directory():
|
53 |
+
# Clear previous data by removing the persist directory
|
54 |
+
if os.path.exists(PERSIST_DIR):
|
55 |
+
shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
|
56 |
+
|
57 |
+
# Recreate the persist directory after removal
|
58 |
+
os.makedirs(PERSIST_DIR, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
# Load new documents from the directory
|
61 |
+
new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
|
62 |
+
|
63 |
+
# Create a new index with the new documents
|
64 |
+
index = VectorStoreIndex.from_documents(new_documents)
|
65 |
+
|
66 |
+
# Persist the new index
|
67 |
+
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
def handle_query(query):
|
70 |
+
context_str = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
# Build context from current chat history
|
73 |
+
for past_query, response in reversed(current_chat_history):
|
74 |
+
if past_query.strip():
|
75 |
+
context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
+
chat_text_qa_msgs = [
|
78 |
+
(
|
79 |
+
"user",
|
80 |
+
"""
|
81 |
+
You are the Hotel voice chatbot and your name is hotel helper. Your goal is to provide accurate, professional, and helpful answers to user queries based on the hotel's data. Always ensure your responses are clear and concise. Give response within 10-15 words only. You need to give an answer in the same language used by the user.
|
82 |
+
{context_str}
|
83 |
+
Question:
|
84 |
+
{query_str}
|
85 |
+
"""
|
86 |
+
)
|
87 |
+
]
|
|
|
88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
+
|
91 |
+
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
92 |
+
|
93 |
+
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
94 |
+
index = load_index_from_storage(storage_context)
|
95 |
+
# context_str = ""
|
96 |
+
|
97 |
+
# # Build context from current chat history
|
98 |
+
# for past_query, response in reversed(current_chat_history):
|
99 |
+
# if past_query.strip():
|
100 |
+
# context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
|
101 |
+
|
102 |
+
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
103 |
+
print(f"Querying: {query}")
|
104 |
+
answer = query_engine.query(query)
|
105 |
+
|
106 |
+
# Extracting the response
|
107 |
+
if hasattr(answer, 'response'):
|
108 |
+
response = answer.response
|
109 |
+
elif isinstance(answer, dict) and 'response' in answer:
|
110 |
+
response = answer['response']
|
111 |
+
else:
|
112 |
+
response = "I'm sorry, I couldn't find an answer to that."
|
113 |
+
|
114 |
+
# Append to chat history
|
115 |
+
current_chat_history.append((query, response))
|
116 |
+
return response
|
117 |
+
|
118 |
+
app = Flask(__name__)
|
119 |
+
|
120 |
+
# Data ingestion
|
121 |
+
data_ingestion_from_directory()
|
122 |
+
|
123 |
+
# Generate Response
|
124 |
+
def generate_response(query):
|
125 |
+
try:
|
126 |
+
# Call the handle_query function to get the response
|
127 |
+
bot_response = handle_query(query)
|
128 |
+
return bot_response
|
129 |
+
except Exception as e:
|
130 |
+
return f"Error fetching the response: {str(e)}"
|
131 |
+
|
132 |
+
# Route for the homepage
|
133 |
+
@app.route('/')
|
134 |
+
def index():
|
135 |
+
return render_template('index.html')
|
136 |
+
|
137 |
+
# Route to handle chatbot messages
|
138 |
+
@app.route('/chat', methods=['POST'])
|
139 |
+
def chat():
|
140 |
+
try:
|
141 |
+
user_message = request.json.get("message")
|
142 |
+
if not user_message:
|
143 |
+
return jsonify({"response": "Please say something!"})
|
144 |
+
|
145 |
+
bot_response = generate_response(user_message)
|
146 |
+
return jsonify({"response": bot_response})
|
147 |
+
except Exception as e:
|
148 |
+
return jsonify({"response": f"An error occurred: {str(e)}"})
|
149 |
+
|
150 |
+
if __name__ == '__main__':
|
151 |
+
app.run(debug=True)
|