Spaces:
Sleeping
Sleeping
Enhance language detection with detailed logging and improve response generation instructions for clarity
Browse files
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"
|
| 330 |
-
print(
|
| 331 |
-
print(f"Detected language code: {detected}")
|
| 332 |
return detected
|
| 333 |
except Exception as e:
|
| 334 |
-
print(
|
| 335 |
-
print(f"Input text: {text}")
|
| 336 |
-
print(f"Error: {str(e)}")
|
| 337 |
-
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
| 353 |
user_language = detect_language(message)
|
| 354 |
-
print(f"
|
| 355 |
-
print(f"
|
| 356 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|