Sadiaa commited on
Commit
edb1567
·
verified ·
1 Parent(s): 2788b1e

Update chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +86 -84
chatbot.py CHANGED
@@ -138,90 +138,92 @@ class Comsatsbot:
138
  continue
139
  return "Sorry, unable to provide an answer at this time."
140
 
141
- def detect_language(self, question):
142
- for api_key in self.api_keys:
143
- self.client = Groq(api_key=api_key)
144
- for model in self.models:
145
- try:
146
- chat_completion = self.client.chat.completions.create(
147
- messages=[
148
- {
149
- "role": "system",
150
- "content": """
151
- You are an expert agent and your task is to detect the language.
152
- Return a JSON: {'detected_language': 'urdu' or 'english'}
153
- """
154
- },
155
- {
156
- "role": "user",
157
- "content": f"Detect the language for: {question}"
158
- }
159
- ],
160
- model=model,
161
- max_tokens=256,
162
- response_format={"type": "json_object"},
163
- )
164
- response = json.loads(chat_completion.choices[0].message.content)
165
- return response['detected_language'].lower()
166
- except Exception:
167
- time.sleep(2)
168
- continue
169
- return "english"
170
-
171
- def translate_urdu(self, text):
172
- for api_key in self.api_keys:
173
- self.client = Groq(api_key=api_key)
174
- for model in self.models:
175
- try:
176
- chat_completion = self.client.chat.completions.create(
177
- messages=[
178
- {
179
- "role": "system",
180
- "content": """
181
- Translate the following text into proper Urdu. Return a JSON:
182
- {'text': 'translated urdu text'}
183
- """
184
- },
185
- {
186
- "role": "user",
187
- "content": f"Translate this: {text}"
188
- }
189
- ],
190
- model=model,
191
- max_tokens=512,
192
- response_format={"type": "json_object"},
193
- )
194
- response = json.loads(chat_completion.choices[0].message.content)
195
- return response['text']
196
- except Exception:
197
- time.sleep(2)
198
- continue
199
- return text
200
-
201
- def response(self, question, chat_id):
202
- chat_history = self.load_chat(chat_id)
203
-
204
- for entry in chat_history:
205
- self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
206
-
207
- language = self.detect_language(question)
208
-
209
- if language == 'urdu':
210
- question_translation = GoogleTranslator(source='ur', target='en').translate(question)
211
- context = self.faiss_retriever.invoke(question_translation)
212
- else:
213
- context = self.faiss_retriever.invoke(question)
214
-
215
- combined_context = '\n'.join([doc.page_content for doc in context])
216
- answer = self.generate_response(question, self.memory.load_memory_variables({})['history'], combined_context)
217
-
218
- self.update_chat(chat_id, question, answer)
219
-
220
- if language == 'urdu':
221
- return self.translate_urdu(answer)
222
- return answer
223
-
224
-
 
 
225
 
226
 
227
 
 
138
  continue
139
  return "Sorry, unable to provide an answer at this time."
140
 
141
+ def detect_language(self, question):
142
+ for api_key in self.api_keys:
143
+ self.client = Groq(api_key=api_key)
144
+ for model in self.models:
145
+ try:
146
+ chat_completion = self.client.chat.completions.create(
147
+ messages=[
148
+ {
149
+ "role": "system",
150
+ "content": """
151
+ You are an expert agent and your task is to detect the language.
152
+ Return a JSON: {'detected_language': 'urdu' or 'english'}
153
+ """
154
+ },
155
+ {
156
+ "role": "user",
157
+ "content": f"Detect the language for: {question}"
158
+ }
159
+ ],
160
+ model=model,
161
+ max_tokens=256,
162
+ response_format={"type": "json_object"},
163
+ )
164
+ response = json.loads(chat_completion.choices[0].message.content)
165
+ return response['detected_language'].lower()
166
+ except Exception:
167
+ time.sleep(2)
168
+ continue
169
+ return "english"
170
+
171
+ def translate_urdu(self, text):
172
+ for api_key in self.api_keys:
173
+ self.client = Groq(api_key=api_key)
174
+ for model in self.models:
175
+ try:
176
+ chat_completion = self.client.chat.completions.create(
177
+ messages=[
178
+ {
179
+ "role": "system",
180
+ "content": """
181
+ Translate the following text into proper Urdu. Return a JSON:
182
+ {'text': 'translated urdu text'}
183
+ """
184
+ },
185
+ {
186
+ "role": "user",
187
+ "content": f"Translate this: {text}"
188
+ }
189
+ ],
190
+ model=model,
191
+ max_tokens=512,
192
+ response_format={"type": "json_object"},
193
+ )
194
+ response = json.loads(chat_completion.choices[0].message.content)
195
+ return response['text']
196
+ except Exception:
197
+ time.sleep(2)
198
+ continue
199
+ return text
200
+
201
+ def response(self, question, chat_id):
202
+ chat_history = self.load_chat(chat_id)
203
+
204
+ for entry in chat_history:
205
+ self.memory.save_context({"input": entry["question"]}, {"output": entry["answer"]})
206
+
207
+ language = self.detect_language(question)
208
+
209
+ if language == 'urdu':
210
+ question_translation = GoogleTranslator(source='ur', target='en').translate(question)
211
+ context = self.faiss_retriever.invoke(question_translation)
212
+ else:
213
+ context = self.faiss_retriever.invoke(question)
214
+
215
+ combined_context = '\n'.join([doc.page_content for doc in context])
216
+ answer = self.generate_response(question, self.memory.load_memory_variables({})['history'], combined_context)
217
+
218
+ self.update_chat(chat_id, question, answer)
219
+
220
+ if language == 'urdu':
221
+ return self.translate_urdu(answer)
222
+ return answer
223
+
224
+
225
+
226
+
227
 
228
 
229