Rulga commited on
Commit
2d961bf
·
1 Parent(s): 99324de

Enhance language detection with detailed logging and improve response generation instructions for clarity

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -324,17 +324,21 @@ def load_vector_store():
324
  return None
325
 
326
  def detect_language(text):
 
327
  try:
 
 
328
  detected = detect(text)
329
- print(f"\nLanguage Detection Debug:")
330
- print(f"Input text: {text}")
331
- print(f"Detected language code: {detected}")
332
  return detected
333
  except Exception as e:
334
- print(f"\nLanguage Detection Error:")
335
- print(f"Input text: {text}")
336
- print(f"Error: {str(e)}")
337
- return "en" # Default to English if detection fails
 
 
338
 
339
  def respond(
340
  message,
@@ -349,11 +353,23 @@ def respond(
349
  """Generate response using the current model with fallback option"""
350
  global fallback_model_attempted
351
 
352
- # Detect language with detailed logging
 
 
353
  user_language = detect_language(message)
354
- print(f"\nResponse Generation Debug:")
355
- print(f"User message: {message}")
356
- print(f"Detected language: {user_language}")
 
 
 
 
 
 
 
 
 
 
357
 
358
  # Create ID for new conversation
359
  if not conversation_id:
 
324
  return None
325
 
326
  def detect_language(text):
327
+ """Detect language with detailed logging"""
328
  try:
329
+ print("\n=== Language Detection Start ===")
330
+ print(f"Input text to analyze: '{text}'")
331
  detected = detect(text)
332
+ print(f"Detected language code: '{detected}'")
333
+ print("=== Language Detection End ===\n")
 
334
  return detected
335
  except Exception as e:
336
+ print("\n=== Language Detection Error ===")
337
+ print(f"Input text: '{text}'")
338
+ print(f"Error details: {str(e)}")
339
+ print("Defaulting to 'en'")
340
+ print("=== Language Detection End ===\n")
341
+ return "en"
342
 
343
  def respond(
344
  message,
 
353
  """Generate response using the current model with fallback option"""
354
  global fallback_model_attempted
355
 
356
+ print("\n=== Response Generation Start ===")
357
+
358
+ # Detect language
359
  user_language = detect_language(message)
360
+ print(f"Processing message: '{message}'")
361
+ print(f"Using detected language: '{user_language}'")
362
+
363
+ # Create stronger language instruction
364
+ language_instruction = f"""
365
+ CRITICAL: Message language detected as '{user_language}'
366
+ YOU MUST RESPOND IN {user_language} LANGUAGE ONLY.
367
+ ЗАПРЕЩЕНО ОТВЕЧАТЬ НА ЛЮБОМ ЯЗЫКЕ КРОМЕ ЯЗЫКА ВОПРОСА ({user_language}).
368
+ THIS IS THE MOST IMPORTANT RULE.
369
+ """
370
+
371
+ print(f"Added language instruction for: {user_language}")
372
+ print("=== Response Generation Setup Complete ===\n")
373
 
374
  # Create ID for new conversation
375
  if not conversation_id: