Gopikanth123 commited on
Commit
6d91be2
·
verified ·
1 Parent(s): 2064f47

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +177 -177
main.py CHANGED
@@ -1,3 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # import os
2
  # import shutil
3
  # from flask import Flask, render_template, request, jsonify
@@ -245,180 +422,3 @@
245
 
246
  # if __name__ == '__main__':
247
  # app.run(debug=True)
248
-
249
- import os
250
- import shutil
251
- from flask import Flask, render_template, request, jsonify
252
- from llama_index.core import StorageContext, load_index_from_storage, VectorStoreIndex, SimpleDirectoryReader, ChatPromptTemplate, Settings
253
- from llama_index.embeddings.huggingface import HuggingFaceEmbedding
254
- from deep_translator import GoogleTranslator
255
- import google.generativeai as genai
256
-
257
- # Ensure GOOGLE_API_KEY is set
258
- GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
259
- if not GOOGLE_API_KEY:
260
- raise ValueError("GOOGLE_API_KEY environment variable not set.")
261
-
262
- # Configure Gemini model
263
- genai.configure(api_key=GOOGLE_API_KEY)
264
- gemini_model = genai.GenerativeModel('gemini-flash-1.0')
265
-
266
- # Configure Llama index settings
267
- Settings.embed_model = HuggingFaceEmbedding(
268
- model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
269
- )
270
-
271
- PERSIST_DIR = "db"
272
- PDF_DIRECTORY = 'data'
273
-
274
- # Ensure directories exist
275
- os.makedirs(PDF_DIRECTORY, exist_ok=True)
276
- os.makedirs(PERSIST_DIR, exist_ok=True)
277
- chat_history = []
278
- current_chat_history = []
279
-
280
- def data_ingestion_from_directory():
281
- # Clear previous data by removing the persist directory
282
- if os.path.exists(PERSIST_DIR):
283
- shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
284
-
285
- # Recreate the persist directory after removal
286
- os.makedirs(PERSIST_DIR, exist_ok=True)
287
-
288
- # Load new documents from the directory
289
- new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
290
-
291
- # Create a new index with the new documents
292
- index = VectorStoreIndex.from_documents(new_documents)
293
-
294
- # Persist the new index
295
- index.storage_context.persist(persist_dir=PERSIST_DIR)
296
-
297
- def handle_query(query):
298
- chat_text_qa_msgs = [
299
- (
300
- "user",
301
- """
302
- 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.
303
- {context_str}
304
- Question:
305
- {query_str}
306
- """
307
- )
308
- ]
309
- text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
310
-
311
- storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
312
- index = load_index_from_storage(storage_context)
313
- context_str = ""
314
- for past_query, response in reversed(current_chat_history):
315
- if past_query.strip():
316
- context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
317
-
318
- query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
319
- print(query)
320
-
321
- # Use Gemini for generating the response
322
- prompt = f"""
323
- Context: {context_str}
324
- Question: {query}
325
- """
326
- gemini_response = gemini_model.generate_content(prompt)
327
- response = gemini_response.text
328
-
329
- current_chat_history.append((query, response))
330
- return response
331
-
332
- app = Flask(__name__)
333
-
334
- # Data ingestion
335
- data_ingestion_from_directory()
336
-
337
- # Generate Response
338
- def generate_response(query, language):
339
- try:
340
- # Call the handle_query function to get the response
341
- bot_response = handle_query(query)
342
-
343
- # Map of supported languages
344
- supported_languages = {
345
- "hindi": "hi",
346
- "bengali": "bn",
347
- "telugu": "te",
348
- "marathi": "mr",
349
- "tamil": "ta",
350
- "gujarati": "gu",
351
- "kannada": "kn",
352
- "malayalam": "ml",
353
- "punjabi": "pa",
354
- "odia": "or",
355
- "urdu": "ur",
356
- "assamese": "as",
357
- "sanskrit": "sa",
358
- "arabic": "ar",
359
- "australian": "en-AU",
360
- "bangla-india": "bn-IN",
361
- "chinese": "zh-CN",
362
- "dutch": "nl",
363
- "french": "fr",
364
- "filipino": "tl",
365
- "greek": "el",
366
- "indonesian": "id",
367
- "italian": "it",
368
- "japanese": "ja",
369
- "korean": "ko",
370
- "latin": "la",
371
- "nepali": "ne",
372
- "portuguese": "pt",
373
- "romanian": "ro",
374
- "russian": "ru",
375
- "spanish": "es",
376
- "swedish": "sv",
377
- "thai": "th",
378
- "ukrainian": "uk",
379
- "turkish": "tr"
380
- }
381
-
382
- # Initialize the translated text
383
- translated_text = bot_response
384
-
385
- # Translate only if the language is supported and not English
386
- try:
387
- if language in supported_languages:
388
- target_lang = supported_languages[language]
389
- translated_text = GoogleTranslator(source='en', target=target_lang).translate(bot_response)
390
- print(translated_text)
391
- else:
392
- print(f"Unsupported language: {language}")
393
- except Exception as e:
394
- # Handle translation errors
395
- print(f"Translation error: {e}")
396
- translated_text = "Sorry, I couldn't translate the response."
397
-
398
- # Append to chat history
399
- chat_history.append((query, translated_text))
400
- return translated_text
401
- except Exception as e:
402
- return f"Error fetching the response: {str(e)}"
403
-
404
- # Route for the homepage
405
- @app.route('/')
406
- def index():
407
- return render_template('index.html')
408
-
409
- # Route to handle chatbot messages
410
- @app.route('/chat', methods=['POST'])
411
- def chat():
412
- try:
413
- user_message = request.json.get("message")
414
- language = request.json.get("language")
415
- if not user_message:
416
- return jsonify({"response": "Please say something!"})
417
-
418
- bot_response = generate_response(user_message, language)
419
- return jsonify({"response": bot_response})
420
- except Exception as e:
421
- return jsonify({"response": f"An error occurred: {str(e)}"})
422
-
423
- if __name__ == '__main__':
424
- 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.embeddings.huggingface import HuggingFaceEmbedding
6
+ from deep_translator import GoogleTranslator
7
+ import google.generativeai as genai
8
+
9
+ # Ensure GOOGLE_API_KEY is set
10
+ GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
11
+ if not GOOGLE_API_KEY:
12
+ raise ValueError("GOOGLE_API_KEY environment variable not set.")
13
+
14
+ # Configure Gemini model
15
+ genai.configure(api_key=GOOGLE_API_KEY)
16
+ gemini_model = genai.GenerativeModel('gemini-flash-1.0')
17
+
18
+ # Configure Llama index settings
19
+ Settings.embed_model = HuggingFaceEmbedding(
20
+ model_name="sentence-transformers/paraphrase-multilingual-mpnet-base-v2"
21
+ )
22
+
23
+ PERSIST_DIR = "db"
24
+ PDF_DIRECTORY = 'data'
25
+
26
+ # Ensure directories exist
27
+ os.makedirs(PDF_DIRECTORY, exist_ok=True)
28
+ os.makedirs(PERSIST_DIR, exist_ok=True)
29
+ chat_history = []
30
+ current_chat_history = []
31
+
32
+ def data_ingestion_from_directory():
33
+ # Clear previous data by removing the persist directory
34
+ if os.path.exists(PERSIST_DIR):
35
+ shutil.rmtree(PERSIST_DIR) # Remove the persist directory and all its contents
36
+
37
+ # Recreate the persist directory after removal
38
+ os.makedirs(PERSIST_DIR, exist_ok=True)
39
+
40
+ # Load new documents from the directory
41
+ new_documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
42
+
43
+ # Create a new index with the new documents
44
+ index = VectorStoreIndex.from_documents(new_documents)
45
+
46
+ # Persist the new index
47
+ index.storage_context.persist(persist_dir=PERSIST_DIR)
48
+
49
+ def handle_query(query):
50
+ chat_text_qa_msgs = [
51
+ (
52
+ "user",
53
+ """
54
+ 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.
55
+ {context_str}
56
+ Question:
57
+ {query_str}
58
+ """
59
+ )
60
+ ]
61
+ text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
62
+
63
+ storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
64
+ index = load_index_from_storage(storage_context)
65
+ context_str = ""
66
+ for past_query, response in reversed(current_chat_history):
67
+ if past_query.strip():
68
+ context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
69
+
70
+ query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
71
+ print(query)
72
+
73
+ # Use Gemini for generating the response
74
+ prompt = f"""
75
+ Context: {context_str}
76
+ Question: {query}
77
+ """
78
+ gemini_response = gemini_model.generate_content(prompt)
79
+ response = gemini_response.text
80
+
81
+ current_chat_history.append((query, response))
82
+ return response
83
+
84
+ app = Flask(__name__)
85
+
86
+ # Data ingestion
87
+ data_ingestion_from_directory()
88
+
89
+ # Generate Response
90
+ def generate_response(query, language):
91
+ try:
92
+ # Call the handle_query function to get the response
93
+ bot_response = handle_query(query)
94
+
95
+ # Map of supported languages
96
+ supported_languages = {
97
+ "hindi": "hi",
98
+ "bengali": "bn",
99
+ "telugu": "te",
100
+ "marathi": "mr",
101
+ "tamil": "ta",
102
+ "gujarati": "gu",
103
+ "kannada": "kn",
104
+ "malayalam": "ml",
105
+ "punjabi": "pa",
106
+ "odia": "or",
107
+ "urdu": "ur",
108
+ "assamese": "as",
109
+ "sanskrit": "sa",
110
+ "arabic": "ar",
111
+ "australian": "en-AU",
112
+ "bangla-india": "bn-IN",
113
+ "chinese": "zh-CN",
114
+ "dutch": "nl",
115
+ "french": "fr",
116
+ "filipino": "tl",
117
+ "greek": "el",
118
+ "indonesian": "id",
119
+ "italian": "it",
120
+ "japanese": "ja",
121
+ "korean": "ko",
122
+ "latin": "la",
123
+ "nepali": "ne",
124
+ "portuguese": "pt",
125
+ "romanian": "ro",
126
+ "russian": "ru",
127
+ "spanish": "es",
128
+ "swedish": "sv",
129
+ "thai": "th",
130
+ "ukrainian": "uk",
131
+ "turkish": "tr"
132
+ }
133
+
134
+ # Initialize the translated text
135
+ translated_text = bot_response
136
+
137
+ # Translate only if the language is supported and not English
138
+ try:
139
+ if language in supported_languages:
140
+ target_lang = supported_languages[language]
141
+ translated_text = GoogleTranslator(source='en', target=target_lang).translate(bot_response)
142
+ print(translated_text)
143
+ else:
144
+ print(f"Unsupported language: {language}")
145
+ except Exception as e:
146
+ # Handle translation errors
147
+ print(f"Translation error: {e}")
148
+ translated_text = "Sorry, I couldn't translate the response."
149
+
150
+ # Append to chat history
151
+ chat_history.append((query, translated_text))
152
+ return translated_text
153
+ except Exception as e:
154
+ return f"Error fetching the response: {str(e)}"
155
+
156
+ # Route for the homepage
157
+ @app.route('/')
158
+ def index():
159
+ return render_template('index.html')
160
+
161
+ # Route to handle chatbot messages
162
+ @app.route('/chat', methods=['POST'])
163
+ def chat():
164
+ try:
165
+ user_message = request.json.get("message")
166
+ language = request.json.get("language")
167
+ if not user_message:
168
+ return jsonify({"response": "Please say something!"})
169
+
170
+ bot_response = generate_response(user_message, language)
171
+ return jsonify({"response": bot_response})
172
+ except Exception as e:
173
+ return jsonify({"response": f"An error occurred: {str(e)}"})
174
+
175
+ if __name__ == '__main__':
176
+ app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))
177
+
178
  # import os
179
  # import shutil
180
  # from flask import Flask, render_template, request, jsonify
 
422
 
423
  # if __name__ == '__main__':
424
  # app.run(debug=True)