Spaces:
Running
Running
Implement language detection with detailed logging and enhance system message instructions for accurate response language
Browse files
app.py
CHANGED
|
@@ -323,6 +323,19 @@ def load_vector_store():
|
|
| 323 |
print(traceback.format_exc())
|
| 324 |
return None
|
| 325 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
def respond(
|
| 327 |
message,
|
| 328 |
history,
|
|
@@ -336,25 +349,28 @@ def respond(
|
|
| 336 |
"""Generate response using the current model with fallback option"""
|
| 337 |
global fallback_model_attempted
|
| 338 |
|
| 339 |
-
#
|
| 340 |
-
from langdetect import detect
|
| 341 |
-
|
| 342 |
-
def detect_language(text):
|
| 343 |
-
try:
|
| 344 |
-
return detect(text)
|
| 345 |
-
except:
|
| 346 |
-
return "en" # Default to English if detection fails
|
| 347 |
-
|
| 348 |
user_language = detect_language(message)
|
| 349 |
-
print(f"
|
|
|
|
|
|
|
| 350 |
|
| 351 |
# Create ID for new conversation
|
| 352 |
if not conversation_id:
|
| 353 |
import uuid
|
| 354 |
conversation_id = str(uuid.uuid4())
|
| 355 |
|
| 356 |
-
# Add language
|
| 357 |
-
language_instruction = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
enhanced_system_message = language_instruction + system_message
|
| 359 |
|
| 360 |
messages = [{"role": "system", "content": enhanced_system_message}]
|
|
@@ -1211,4 +1227,4 @@ if __name__ == "__main__":
|
|
| 1211 |
if not load_vector_store():
|
| 1212 |
print("Knowledge base not found. Please create it through the interface.")
|
| 1213 |
|
| 1214 |
-
demo.launch()
|
|
|
|
| 323 |
print(traceback.format_exc())
|
| 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,
|
| 341 |
history,
|
|
|
|
| 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:
|
| 360 |
import uuid
|
| 361 |
conversation_id = str(uuid.uuid4())
|
| 362 |
|
| 363 |
+
# Add stronger language instruction
|
| 364 |
+
language_instruction = f"""
|
| 365 |
+
CRITICAL INSTRUCTION: User message language is detected as '{user_language}'.
|
| 366 |
+
YOU MUST RESPOND IN {user_language} LANGUAGE ONLY.
|
| 367 |
+
ЗАПРЕЩЕНО ОТВЕЧАТЬ НА ЛЮБОМ ЯЗЫКЕ КРОМЕ ЯЗЫКА ВОПРОСА.
|
| 368 |
+
THIS IS THE MOST IMPORTANT RULE.
|
| 369 |
+
|
| 370 |
+
Original message: {message}
|
| 371 |
+
Detected language: {user_language}
|
| 372 |
+
"""
|
| 373 |
+
|
| 374 |
enhanced_system_message = language_instruction + system_message
|
| 375 |
|
| 376 |
messages = [{"role": "system", "content": enhanced_system_message}]
|
|
|
|
| 1227 |
if not load_vector_store():
|
| 1228 |
print("Knowledge base not found. Please create it through the interface.")
|
| 1229 |
|
| 1230 |
+
demo.launch(share=True)
|