Spaces:
Running
Running
Refactor response handling to reset user language detection, clean chat history of system messages, and enable Enter key submission for user input
Browse files
app.py
CHANGED
|
@@ -276,11 +276,17 @@ def respond(
|
|
| 276 |
):
|
| 277 |
"""Generate response with proper error handling"""
|
| 278 |
try:
|
| 279 |
-
#
|
| 280 |
user_lang = detect_language(message)
|
| 281 |
logger.debug(f"Detected user language: {user_lang}")
|
| 282 |
|
| 283 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
language_instruction = f"\nIMPORTANT: You MUST respond in {user_lang} language ONLY."
|
| 285 |
full_system_message = system_message + language_instruction
|
| 286 |
|
|
@@ -288,7 +294,7 @@ def respond(
|
|
| 288 |
response = client.chat_completion(
|
| 289 |
messages=[
|
| 290 |
{"role": "system", "content": full_system_message},
|
| 291 |
-
*
|
| 292 |
{"role": "user", "content": message}
|
| 293 |
],
|
| 294 |
max_tokens=max_tokens,
|
|
@@ -304,13 +310,12 @@ def respond(
|
|
| 304 |
|
| 305 |
# --- Format Successful Response ---
|
| 306 |
new_history = [
|
| 307 |
-
*
|
| 308 |
{"role": "user", "content": message},
|
| 309 |
{"role": "assistant", "content": processed_response}
|
| 310 |
]
|
| 311 |
|
| 312 |
return new_history, conversation_id
|
| 313 |
-
|
| 314 |
except Exception as e:
|
| 315 |
logger.error(f"API Error: {str(e)}")
|
| 316 |
error_msg = format_friendly_error(str(e))
|
|
@@ -731,7 +736,8 @@ with gr.Blocks() as demo:
|
|
| 731 |
msg = gr.Textbox(
|
| 732 |
label="Your question",
|
| 733 |
placeholder="Enter your question...",
|
| 734 |
-
scale=4
|
|
|
|
| 735 |
)
|
| 736 |
submit_btn = gr.Button("Send", variant="primary")
|
| 737 |
clear_btn = gr.Button("Clear") # Add clear button
|
|
|
|
| 276 |
):
|
| 277 |
"""Generate response with proper error handling"""
|
| 278 |
try:
|
| 279 |
+
# Reset and determine user language for new request
|
| 280 |
user_lang = detect_language(message)
|
| 281 |
logger.debug(f"Detected user language: {user_lang}")
|
| 282 |
|
| 283 |
+
# Create clean history without system messages
|
| 284 |
+
clean_history = [
|
| 285 |
+
msg for msg in history
|
| 286 |
+
if msg["role"] != "system"
|
| 287 |
+
]
|
| 288 |
+
|
| 289 |
+
# Add fresh system message with current language instruction
|
| 290 |
language_instruction = f"\nIMPORTANT: You MUST respond in {user_lang} language ONLY."
|
| 291 |
full_system_message = system_message + language_instruction
|
| 292 |
|
|
|
|
| 294 |
response = client.chat_completion(
|
| 295 |
messages=[
|
| 296 |
{"role": "system", "content": full_system_message},
|
| 297 |
+
*clean_history,
|
| 298 |
{"role": "user", "content": message}
|
| 299 |
],
|
| 300 |
max_tokens=max_tokens,
|
|
|
|
| 310 |
|
| 311 |
# --- Format Successful Response ---
|
| 312 |
new_history = [
|
| 313 |
+
*clean_history,
|
| 314 |
{"role": "user", "content": message},
|
| 315 |
{"role": "assistant", "content": processed_response}
|
| 316 |
]
|
| 317 |
|
| 318 |
return new_history, conversation_id
|
|
|
|
| 319 |
except Exception as e:
|
| 320 |
logger.error(f"API Error: {str(e)}")
|
| 321 |
error_msg = format_friendly_error(str(e))
|
|
|
|
| 736 |
msg = gr.Textbox(
|
| 737 |
label="Your question",
|
| 738 |
placeholder="Enter your question...",
|
| 739 |
+
scale=4,
|
| 740 |
+
submit_on_enter=True # Enable Enter key submission
|
| 741 |
)
|
| 742 |
submit_btn = gr.Button("Send", variant="primary")
|
| 743 |
clear_btn = gr.Button("Clear") # Add clear button
|