Gopikanth123 commited on
Commit
a6dac5d
·
verified ·
1 Parent(s): c6b0cd9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +327 -327
main.py CHANGED
@@ -1,361 +1,158 @@
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
- # from deep_translator import GoogleTranslator
10
- # from transformers import AutoModelForCausalLM, AutoTokenizer
11
- # import torch
12
- # from accelerate import infer_auto_device_map
13
 
14
 
15
 
16
- # # Ensure HF_TOKEN is set
17
- # HF_TOKEN = os.getenv("HF_TOKEN")
18
- # if not HF_TOKEN:
19
- # raise ValueError("HF_TOKEN environment variable not set.")
20
 
21
- # # repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
22
- # repo_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
23
- # llm_client = InferenceClient(
24
- # model=repo_id,
 
 
 
 
 
 
 
 
25
  # token=HF_TOKEN,
 
 
26
  # )
27
-
28
- # # # Configure Llama index settings
29
- # # Settings.llm = HuggingFaceInferenceAPI(
30
- # # model_name=repo_id,
31
- # # tokenizer_name=repo_id,
32
- # # context_window=3000,
33
- # # token=HF_TOKEN,
34
- # # max_new_tokens=512,
35
- # # generate_kwargs={"temperature": 0.1},
36
- # # )
37
- # # Configure Llama index settings with the new model
38
- # # Settings.llm = HuggingFaceInferenceAPI(
39
- # # model_name=repo_id,
40
- # # tokenizer_name=repo_id, # Use the same tokenizer as the model
41
- # # context_window=3000,
42
- # # token=HF_TOKEN,
43
- # # max_new_tokens=512,
44
- # # generate_kwargs={"temperature": 0.1},
45
- # # )
46
- # # Settings.embed_model = HuggingFaceEmbedding(
47
- # # model_name="BAAI/bge-small-en-v1.5"
48
- # # )
49
- # # Replace the embedding model with XLM-R
50
- # # Settings.embed_model = HuggingFaceEmbedding(
51
- # # model_name="xlm-roberta-base" # XLM-RoBERTa model for multilingual support
52
- # # )
53
- # # Settings.embed_model = HuggingFaceEmbedding(
54
- # # model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
55
- # # )
56
-
57
- # # # Configure tokenizer and model if required
58
- # # tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
59
- # # model = AutoModel.from_pretrained("xlm-roberta-base")
60
- # # Configure tokenizer and model if required
61
- # tokenizer = AutoTokenizer.from_pretrained(repo_id) # Use the tokenizer from the new model
62
- # # model = AutoModel.from_pretrained(repo_id) # Load the new model
63
- # model = AutoModelForCausalLM.from_pretrained(
64
- # repo_id,
65
- # load_in_4bit=True, # Load in 4-bit quantization
66
- # torch_dtype=torch.float16,
67
- # device_map="auto",
68
- # )
69
- # # Configure Llama index settings
70
  # Settings.llm = HuggingFaceInferenceAPI(
71
  # model_name=repo_id,
72
  # tokenizer_name=repo_id, # Use the same tokenizer as the model
73
- # context_window=2048, # Reduce context window to save memory
74
  # token=HF_TOKEN,
75
- # max_new_tokens=256, # Reduce max tokens to save memory
76
  # generate_kwargs={"temperature": 0.1},
77
  # )
78
- # # Use a smaller embedding model
 
 
 
79
  # Settings.embed_model = HuggingFaceEmbedding(
80
- # model_name="sentence-transformers/all-MiniLM-L6-v2" # Smaller and faster
81
  # )
82
- # PERSIST_DIR = "db"
83
- # PDF_DIRECTORY = 'data'
84
-
85
- # # Ensure directories exist
86
- # os.makedirs(PDF_DIRECTORY, exist_ok=True)
87
- # os.makedirs(PERSIST_DIR, exist_ok=True)
88
- # chat_history = []
89
- # current_chat_history = []
90
-
91
- # def data_ingestion_from_directory():
92
- # # Clear previous data by removing the persist directory
93
- # if os.path.exists(PERSIST_DIR):
94
- # shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
95
-
96
- # # Recreate the persist directory after removal
97
- # os.makedirs(PERSIST_DIR, exist_ok=True)
98
-
99
- # # Load new documents from the directory
100
- # new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- # # Create a new index with the new documents
103
- # index = VectorStoreIndex.from_documents(new_documents)
104
 
105
- # # Persist the new index
106
- # index.storage_context.persist(persist_dir=PERSIST_DIR)
107
-
108
- # # def handle_query(query):
109
- # # context_str = ""
110
 
111
- # # # Build context from current chat history
112
- # # for past_query, response in reversed(current_chat_history):
113
- # # if past_query.strip():
114
- # # context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
115
 
116
- # # chat_text_qa_msgs = [
117
- # # (
118
- # # "user",
119
- # # """
120
- # # You are the Taj Hotel voice chatbot and your name is Taj hotel helper. Your goal is to provide accurate, professional, and helpful answers to user queries based on the Taj hotel 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.
121
- # # {context_str}
122
- # # Question:
123
- # # {query_str}
124
- # # """
125
- # # )
126
- # # ]
127
-
128
 
 
 
129
 
130
- # # text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
 
 
 
131
 
132
- # # storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
133
- # # index = load_index_from_storage(storage_context)
134
- # # # context_str = ""
135
-
136
- # # # # Build context from current chat history
137
- # # # for past_query, response in reversed(current_chat_history):
138
- # # # if past_query.strip():
139
- # # # context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
140
-
141
- # # query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
142
- # # print(f"Querying: {query}")
143
- # # answer = query_engine.query(query)
144
-
145
- # # # Extracting the response
146
- # # if hasattr(answer, 'response'):
147
- # # response = answer.response
148
- # # elif isinstance(answer, dict) and 'response' in answer:
149
- # # response = answer['response']
150
- # # else:
151
- # # response = "I'm sorry, I couldn't find an answer to that."
152
-
153
- # # # Append to chat history
154
- # # current_chat_history.append((query, response))
155
- # # return response
156
- # def handle_query(query):
157
  # chat_text_qa_msgs = [
158
  # (
159
  # "user",
160
  # """
161
- # 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.
162
  # {context_str}
163
  # Question:
164
  # {query_str}
165
  # """
166
  # )
167
  # ]
168
- # text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
169
-
170
- # storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
171
- # index = load_index_from_storage(storage_context)
172
- # context_str = ""
173
- # for past_query, response in reversed(current_chat_history):
174
- # if past_query.strip():
175
- # context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
176
-
177
- # query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
178
- # print(query)
179
- # answer = query_engine.query(query)
180
-
181
- # if hasattr(answer, 'response'):
182
- # response = answer.response
183
- # elif isinstance(answer, dict) and 'response' in answer:
184
- # response = answer['response']
185
- # else:
186
- # response = "Sorry, I couldn't find an answer."
187
- # current_chat_history.append((query, response))
188
- # return response
189
-
190
- # app = Flask(__name__)
191
-
192
- # # Data ingestion
193
- # data_ingestion_from_directory()
194
-
195
- # # Generate Response
196
- # def generate_response(query, language):
197
- # try:
198
- # # Call the handle_query function to get the response
199
- # bot_response = handle_query(query)
200
-
201
- # # Map of supported languages
202
- # supported_languages = {
203
- # "hindi": "hi",
204
- # "bengali": "bn",
205
- # "telugu": "te",
206
- # "marathi": "mr",
207
- # "tamil": "ta",
208
- # "gujarati": "gu",
209
- # "kannada": "kn",
210
- # "malayalam": "ml",
211
- # "punjabi": "pa",
212
- # "odia": "or",
213
- # "urdu": "ur",
214
- # "assamese": "as",
215
- # "sanskrit": "sa",
216
- # "arabic": "ar",
217
- # "australian": "en-AU",
218
- # "bangla-india": "bn-IN",
219
- # "chinese": "zh-CN",
220
- # "dutch": "nl",
221
- # "french": "fr",
222
- # "filipino": "tl",
223
- # "greek": "el",
224
- # "indonesian": "id",
225
- # "italian": "it",
226
- # "japanese": "ja",
227
- # "korean": "ko",
228
- # "latin": "la",
229
- # "nepali": "ne",
230
- # "portuguese": "pt",
231
- # "romanian": "ro",
232
- # "russian": "ru",
233
- # "spanish": "es",
234
- # "swedish": "sv",
235
- # "thai": "th",
236
- # "ukrainian": "uk",
237
- # "turkish": "tr"
238
- # }
239
-
240
- # # Initialize the translated text
241
- # translated_text = bot_response
242
-
243
- # # Translate only if the language is supported and not English
244
- # try:
245
- # if language in supported_languages:
246
- # target_lang = supported_languages[language]
247
- # translated_text = GoogleTranslator(source='en', target=target_lang).translate(bot_response)
248
- # print(translated_text)
249
- # else:
250
- # print(f"Unsupported language: {language}")
251
- # except Exception as e:
252
- # # Handle translation errors
253
- # print(f"Translation error: {e}")
254
- # translated_text = "Sorry, I couldn't translate the response."
255
-
256
- # # Append to chat history
257
- # chat_history.append((query, translated_text))
258
- # return translated_text
259
- # except Exception as e:
260
- # return f"Error fetching the response: {str(e)}"
261
-
262
- # # Route for the homepage
263
- # @app.route('/')
264
- # def index():
265
- # return render_template('index.html')
266
-
267
- # # Route to handle chatbot messages
268
- # @app.route('/chat', methods=['POST'])
269
- # def chat():
270
- # try:
271
- # user_message = request.json.get("message")
272
- # language = request.json.get("language")
273
- # if not user_message:
274
- # return jsonify({"response": "Please say something!"})
275
-
276
- # bot_response = generate_response(user_message,language)
277
- # return jsonify({"response": bot_response})
278
- # except Exception as e:
279
- # return jsonify({"response": f"An error occurred: {str(e)}"})
280
-
281
- # if __name__ == '__main__':
282
- # app.run(debug=True)
283
- import os
284
- import shutil
285
- from flask import Flask, render_template, request, jsonify
286
- from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
287
- from llama_index.llms.huggingface import HuggingFaceInferenceAPI
288
- from llama_index.embeddings.huggingface import HuggingFaceEmbedding
289
- from huggingface_hub import InferenceClient
290
- from transformers import AutoTokenizer, AutoModelForCausalLM
291
- from deep_translator import GoogleTranslator
292
- import torch
293
-
294
- # Ensure HF_TOKEN is set
295
- HF_TOKEN = os.getenv("HF_TOKEN")
296
- if not HF_TOKEN:
297
- raise ValueError("HF_TOKEN environment variable not set.")
298
-
299
- # Model configuration
300
- repo_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
301
-
302
- # Load tokenizer
303
- tokenizer = AutoTokenizer.from_pretrained(repo_id)
304
-
305
- # Load quantized model
306
- try:
307
- model = AutoModelForCausalLM.from_pretrained(
308
- repo_id,
309
- device_map="auto", # Automatically distribute across available devices
310
- load_in_8bit=True, # Quantize to 8-bit precision
311
- torch_dtype=torch.float16 # Use 16-bit precision
312
- )
313
- except ImportError:
314
- raise ImportError("The 'bitsandbytes' library is required for quantization. Please install it using `pip install bitsandbytes`.")
315
-
316
- # Configure Llama index settings
317
- Settings.llm = HuggingFaceInferenceAPI(
318
- model_name=repo_id,
319
- tokenizer_name=repo_id, # Use the same tokenizer as the model
320
- context_window=2048, # Reduce context window to save memory
321
- token=HF_TOKEN,
322
- max_new_tokens=256, # Reduce max tokens to save memory
323
- generate_kwargs={"temperature": 0.1},
324
- )
325
 
326
- # Use a smaller embedding model
327
- Settings.embed_model = HuggingFaceEmbedding(
328
- model_name="sentence-transformers/all-MiniLM-L6-v2" # Smaller and faster
329
- )
330
 
331
- # Directories
332
- PERSIST_DIR = "db"
333
- PDF_DIRECTORY = 'data'
334
-
335
- # Ensure directories exist
336
- os.makedirs(PDF_DIRECTORY, exist_ok=True)
337
- os.makedirs(PERSIST_DIR, exist_ok=True)
338
-
339
- chat_history = []
340
- current_chat_history = []
341
-
342
- def data_ingestion_from_directory():
343
- # Clear previous data by removing the persist directory
344
- if os.path.exists(PERSIST_DIR):
345
- shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
346
 
347
- # Recreate the persist directory after removal
348
- os.makedirs(PERSIST_DIR, exist_ok=True)
349
 
350
- # Load new documents from the directory
351
- new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
 
352
 
353
- # Create a new index with the new documents
354
- index = VectorStoreIndex.from_documents(new_documents)
355
-
356
- # Persist the new index
357
- index.storage_context.persist(persist_dir=PERSIST_DIR)
358
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  def handle_query(query):
360
  chat_text_qa_msgs = [
361
  (
@@ -376,9 +173,11 @@ def handle_query(query):
376
  for past_query, response in reversed(current_chat_history):
377
  if past_query.strip():
378
  context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
 
379
  query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
380
  print(query)
381
  answer = query_engine.query(query)
 
382
  if hasattr(answer, 'response'):
383
  response = answer.response
384
  elif isinstance(answer, dict) and 'response' in answer:
@@ -388,10 +187,10 @@ def handle_query(query):
388
  current_chat_history.append((query, response))
389
  return response
390
 
391
- app = Flask(__name__)
392
 
393
- # Data ingestion
394
- data_ingestion_from_directory()
395
 
396
  # Generate Response
397
  def generate_response(query, language):
@@ -473,10 +272,211 @@ def chat():
473
  language = request.json.get("language")
474
  if not user_message:
475
  return jsonify({"response": "Please say something!"})
476
- bot_response = generate_response(user_message, language)
 
477
  return jsonify({"response": bot_response})
478
  except Exception as e:
479
  return jsonify({"response": f"An error occurred: {str(e)}"})
480
 
481
  if __name__ == '__main__':
482
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from deep_translator import GoogleTranslator
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer
11
+ import torch
12
+ from accelerate import infer_auto_device_map
13
 
14
 
15
 
16
+ # Ensure HF_TOKEN is set
17
+ HF_TOKEN = os.getenv("HF_TOKEN")
18
+ if not HF_TOKEN:
19
+ raise ValueError("HF_TOKEN environment variable not set.")
20
 
21
+ # repo_id = "meta-llama/Meta-Llama-3-8B-Instruct"
22
+ repo_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
23
+ llm_client = InferenceClient(
24
+ model=repo_id,
25
+ token=HF_TOKEN,
26
+ )
27
+
28
+ # # Configure Llama index settings
29
+ # Settings.llm = HuggingFaceInferenceAPI(
30
+ # model_name=repo_id,
31
+ # tokenizer_name=repo_id,
32
+ # context_window=3000,
33
  # token=HF_TOKEN,
34
+ # max_new_tokens=512,
35
+ # generate_kwargs={"temperature": 0.1},
36
  # )
37
+ # Configure Llama index settings with the new model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Settings.llm = HuggingFaceInferenceAPI(
39
  # model_name=repo_id,
40
  # tokenizer_name=repo_id, # Use the same tokenizer as the model
41
+ # context_window=3000,
42
  # token=HF_TOKEN,
43
+ # max_new_tokens=512,
44
  # generate_kwargs={"temperature": 0.1},
45
  # )
46
+ # Settings.embed_model = HuggingFaceEmbedding(
47
+ # model_name="BAAI/bge-small-en-v1.5"
48
+ # )
49
+ # Replace the embedding model with XLM-R
50
  # Settings.embed_model = HuggingFaceEmbedding(
51
+ # model_name="xlm-roberta-base" # XLM-RoBERTa model for multilingual support
52
  # )
53
+ # Settings.embed_model = HuggingFaceEmbedding(
54
+ # model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
55
+ # )
56
+
57
+ # # Configure tokenizer and model if required
58
+ # tokenizer = AutoTokenizer.from_pretrained("xlm-roberta-base")
59
+ # model = AutoModel.from_pretrained("xlm-roberta-base")
60
+ # Configure tokenizer and model if required
61
+ tokenizer = AutoTokenizer.from_pretrained(repo_id) # Use the tokenizer from the new model
62
+ # model = AutoModel.from_pretrained(repo_id) # Load the new model
63
+ model = AutoModelForCausalLM.from_pretrained(
64
+ repo_id,
65
+ load_in_4bit=True, # Load in 4-bit quantization
66
+ torch_dtype=torch.float16,
67
+ device_map="auto",
68
+ )
69
+ # Configure Llama index settings
70
+ Settings.llm = HuggingFaceInferenceAPI(
71
+ model_name=repo_id,
72
+ tokenizer_name=repo_id, # Use the same tokenizer as the model
73
+ context_window=2048, # Reduce context window to save memory
74
+ token=HF_TOKEN,
75
+ max_new_tokens=256, # Reduce max tokens to save memory
76
+ generate_kwargs={"temperature": 0.1},
77
+ )
78
+ # Use a smaller embedding model
79
+ Settings.embed_model = HuggingFaceEmbedding(
80
+ model_name="sentence-transformers/all-MiniLM-L6-v2" # Smaller and faster
81
+ )
82
+ PERSIST_DIR = "db"
83
+ PDF_DIRECTORY = 'data'
84
+
85
+ # Ensure directories exist
86
+ os.makedirs(PDF_DIRECTORY, exist_ok=True)
87
+ os.makedirs(PERSIST_DIR, exist_ok=True)
88
+ chat_history = []
89
+ current_chat_history = []
90
+
91
+ def data_ingestion_from_directory():
92
+ # Clear previous data by removing the persist directory
93
+ if os.path.exists(PERSIST_DIR):
94
+ shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
95
 
96
+ # Recreate the persist directory after removal
97
+ os.makedirs(PERSIST_DIR, exist_ok=True)
98
 
99
+ # Load new documents from the directory
100
+ new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
 
 
 
101
 
102
+ # Create a new index with the new documents
103
+ index = VectorStoreIndex.from_documents(new_documents)
 
 
104
 
105
+ # Persist the new index
106
+ index.storage_context.persist(persist_dir=PERSIST_DIR)
 
 
 
 
 
 
 
 
 
 
107
 
108
+ # def handle_query(query):
109
+ # context_str = ""
110
 
111
+ # # Build context from current chat history
112
+ # for past_query, response in reversed(current_chat_history):
113
+ # if past_query.strip():
114
+ # context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  # chat_text_qa_msgs = [
117
  # (
118
  # "user",
119
  # """
120
+ # You are the Taj Hotel voice chatbot and your name is Taj hotel helper. Your goal is to provide accurate, professional, and helpful answers to user queries based on the Taj hotel 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.
121
  # {context_str}
122
  # Question:
123
  # {query_str}
124
  # """
125
  # )
126
  # ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
 
 
 
 
128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
129
 
130
+ # text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
 
131
 
132
+ # storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
133
+ # index = load_index_from_storage(storage_context)
134
+ # # context_str = ""
135
 
136
+ # # # Build context from current chat history
137
+ # # for past_query, response in reversed(current_chat_history):
138
+ # # if past_query.strip():
139
+ # # context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
140
+
141
+ # query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
142
+ # print(f"Querying: {query}")
143
+ # answer = query_engine.query(query)
144
+
145
+ # # Extracting the response
146
+ # if hasattr(answer, 'response'):
147
+ # response = answer.response
148
+ # elif isinstance(answer, dict) and 'response' in answer:
149
+ # response = answer['response']
150
+ # else:
151
+ # response = "I'm sorry, I couldn't find an answer to that."
152
+
153
+ # # Append to chat history
154
+ # current_chat_history.append((query, response))
155
+ # return response
156
  def handle_query(query):
157
  chat_text_qa_msgs = [
158
  (
 
173
  for past_query, response in reversed(current_chat_history):
174
  if past_query.strip():
175
  context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
176
+
177
  query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
178
  print(query)
179
  answer = query_engine.query(query)
180
+
181
  if hasattr(answer, 'response'):
182
  response = answer.response
183
  elif isinstance(answer, dict) and 'response' in answer:
 
187
  current_chat_history.append((query, response))
188
  return response
189
 
190
+ app = Flask(__name__)
191
 
192
+ # Data ingestion
193
+ data_ingestion_from_directory()
194
 
195
  # Generate Response
196
  def generate_response(query, language):
 
272
  language = request.json.get("language")
273
  if not user_message:
274
  return jsonify({"response": "Please say something!"})
275
+
276
+ bot_response = generate_response(user_message,language)
277
  return jsonify({"response": bot_response})
278
  except Exception as e:
279
  return jsonify({"response": f"An error occurred: {str(e)}"})
280
 
281
  if __name__ == '__main__':
282
+ app.run(debug=True)
283
+ # import os
284
+ # import shutil
285
+ # from flask import Flask, render_template, request, jsonify
286
+ # from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
287
+ # from llama_index.llms.huggingface import HuggingFaceInferenceAPI
288
+ # from llama_index.embeddings.huggingface import HuggingFaceEmbedding
289
+ # from huggingface_hub import InferenceClient
290
+ # from transformers import AutoTokenizer, AutoModelForCausalLM
291
+ # from deep_translator import GoogleTranslator
292
+ # import torch
293
+
294
+ # # Ensure HF_TOKEN is set
295
+ # HF_TOKEN = os.getenv("HF_TOKEN")
296
+ # if not HF_TOKEN:
297
+ # raise ValueError("HF_TOKEN environment variable not set.")
298
+
299
+ # # Model configuration
300
+ # repo_id = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
301
+
302
+ # # Load tokenizer
303
+ # tokenizer = AutoTokenizer.from_pretrained(repo_id)
304
+
305
+ # # Load quantized model
306
+ # try:
307
+ # model = AutoModelForCausalLM.from_pretrained(
308
+ # repo_id,
309
+ # device_map="auto", # Automatically distribute across available devices
310
+ # load_in_8bit=True, # Quantize to 8-bit precision
311
+ # torch_dtype=torch.float16 # Use 16-bit precision
312
+ # )
313
+ # except ImportError:
314
+ # raise ImportError("The 'bitsandbytes' library is required for quantization. Please install it using `pip install bitsandbytes`.")
315
+
316
+ # # Configure Llama index settings
317
+ # Settings.llm = HuggingFaceInferenceAPI(
318
+ # model_name=repo_id,
319
+ # tokenizer_name=repo_id, # Use the same tokenizer as the model
320
+ # context_window=2048, # Reduce context window to save memory
321
+ # token=HF_TOKEN,
322
+ # max_new_tokens=256, # Reduce max tokens to save memory
323
+ # generate_kwargs={"temperature": 0.1},
324
+ # )
325
+
326
+ # # Use a smaller embedding model
327
+ # Settings.embed_model = HuggingFaceEmbedding(
328
+ # model_name="sentence-transformers/all-MiniLM-L6-v2" # Smaller and faster
329
+ # )
330
+
331
+ # # Directories
332
+ # PERSIST_DIR = "db"
333
+ # PDF_DIRECTORY = 'data'
334
+
335
+ # # Ensure directories exist
336
+ # os.makedirs(PDF_DIRECTORY, exist_ok=True)
337
+ # os.makedirs(PERSIST_DIR, exist_ok=True)
338
+
339
+ # chat_history = []
340
+ # current_chat_history = []
341
+
342
+ # def data_ingestion_from_directory():
343
+ # # Clear previous data by removing the persist directory
344
+ # if os.path.exists(PERSIST_DIR):
345
+ # shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
346
+
347
+ # # Recreate the persist directory after removal
348
+ # os.makedirs(PERSIST_DIR, exist_ok=True)
349
+
350
+ # # Load new documents from the directory
351
+ # new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
352
+
353
+ # # Create a new index with the new documents
354
+ # index = VectorStoreIndex.from_documents(new_documents)
355
+
356
+ # # Persist the new index
357
+ # index.storage_context.persist(persist_dir=PERSIST_DIR)
358
+
359
+ # def handle_query(query):
360
+ # chat_text_qa_msgs = [
361
+ # (
362
+ # "user",
363
+ # """
364
+ # 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.
365
+ # {context_str}
366
+ # Question:
367
+ # {query_str}
368
+ # """
369
+ # )
370
+ # ]
371
+ # text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
372
+
373
+ # storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
374
+ # index = load_index_from_storage(storage_context)
375
+ # context_str = ""
376
+ # for past_query, response in reversed(current_chat_history):
377
+ # if past_query.strip():
378
+ # context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
379
+ # query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
380
+ # print(query)
381
+ # answer = query_engine.query(query)
382
+ # if hasattr(answer, 'response'):
383
+ # response = answer.response
384
+ # elif isinstance(answer, dict) and 'response' in answer:
385
+ # response = answer['response']
386
+ # else:
387
+ # response = "Sorry, I couldn't find an answer."
388
+ # current_chat_history.append((query, response))
389
+ # return response
390
+
391
+ # app = Flask(__name__)
392
+
393
+ # # Data ingestion
394
+ # data_ingestion_from_directory()
395
+
396
+ # # Generate Response
397
+ # def generate_response(query, language):
398
+ # try:
399
+ # # Call the handle_query function to get the response
400
+ # bot_response = handle_query(query)
401
+
402
+ # # Map of supported languages
403
+ # supported_languages = {
404
+ # "hindi": "hi",
405
+ # "bengali": "bn",
406
+ # "telugu": "te",
407
+ # "marathi": "mr",
408
+ # "tamil": "ta",
409
+ # "gujarati": "gu",
410
+ # "kannada": "kn",
411
+ # "malayalam": "ml",
412
+ # "punjabi": "pa",
413
+ # "odia": "or",
414
+ # "urdu": "ur",
415
+ # "assamese": "as",
416
+ # "sanskrit": "sa",
417
+ # "arabic": "ar",
418
+ # "australian": "en-AU",
419
+ # "bangla-india": "bn-IN",
420
+ # "chinese": "zh-CN",
421
+ # "dutch": "nl",
422
+ # "french": "fr",
423
+ # "filipino": "tl",
424
+ # "greek": "el",
425
+ # "indonesian": "id",
426
+ # "italian": "it",
427
+ # "japanese": "ja",
428
+ # "korean": "ko",
429
+ # "latin": "la",
430
+ # "nepali": "ne",
431
+ # "portuguese": "pt",
432
+ # "romanian": "ro",
433
+ # "russian": "ru",
434
+ # "spanish": "es",
435
+ # "swedish": "sv",
436
+ # "thai": "th",
437
+ # "ukrainian": "uk",
438
+ # "turkish": "tr"
439
+ # }
440
+
441
+ # # Initialize the translated text
442
+ # translated_text = bot_response
443
+
444
+ # # Translate only if the language is supported and not English
445
+ # try:
446
+ # if language in supported_languages:
447
+ # target_lang = supported_languages[language]
448
+ # translated_text = GoogleTranslator(source='en', target=target_lang).translate(bot_response)
449
+ # print(translated_text)
450
+ # else:
451
+ # print(f"Unsupported language: {language}")
452
+ # except Exception as e:
453
+ # # Handle translation errors
454
+ # print(f"Translation error: {e}")
455
+ # translated_text = "Sorry, I couldn't translate the response."
456
+
457
+ # # Append to chat history
458
+ # chat_history.append((query, translated_text))
459
+ # return translated_text
460
+ # except Exception as e:
461
+ # return f"Error fetching the response: {str(e)}"
462
+
463
+ # # Route for the homepage
464
+ # @app.route('/')
465
+ # def index():
466
+ # return render_template('index.html')
467
+
468
+ # # Route to handle chatbot messages
469
+ # @app.route('/chat', methods=['POST'])
470
+ # def chat():
471
+ # try:
472
+ # user_message = request.json.get("message")
473
+ # language = request.json.get("language")
474
+ # if not user_message:
475
+ # return jsonify({"response": "Please say something!"})
476
+ # bot_response = generate_response(user_message, language)
477
+ # return jsonify({"response": bot_response})
478
+ # except Exception as e:
479
+ # return jsonify({"response": f"An error occurred: {str(e)}"})
480
+
481
+ # if __name__ == '__main__':
482
+ # app.run(debug=True)