Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2586,8 +2586,21 @@ Response:
|
|
| 2586 |
else:
|
| 2587 |
ai_response += "\n\n💬 *Type 'main' to return to main menu*"
|
| 2588 |
|
| 2589 |
-
#
|
| 2590 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2591 |
|
| 2592 |
# Update context to AI chat mode
|
| 2593 |
context_manager.update_context(
|
|
@@ -3918,6 +3931,20 @@ async def handle_contact_request(from_number: str):
|
|
| 3918 |
current_menu_options=list(MENU_CONFIG['main_menu']['option_descriptions'].values())
|
| 3919 |
)
|
| 3920 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3921 |
if __name__ == "__main__":
|
| 3922 |
# Load products data on startup
|
| 3923 |
load_products_data()
|
|
|
|
| 2586 |
else:
|
| 2587 |
ai_response += "\n\n💬 *Type 'main' to return to main menu*"
|
| 2588 |
|
| 2589 |
+
# Translate response if needed
|
| 2590 |
+
if reply_language == 'ur':
|
| 2591 |
+
try:
|
| 2592 |
+
# Get all product and category names
|
| 2593 |
+
product_names = [p.get('Product Name', '') for p in all_products if p.get('Product Name')]
|
| 2594 |
+
category_names = list(set([p.get('Category', '') for p in all_products if p.get('Category')]))
|
| 2595 |
+
translated_response = GoogleTranslator(source='auto', target='ur').translate(ai_response)
|
| 2596 |
+
# Restore English terms
|
| 2597 |
+
translated_response = restore_english_terms(translated_response, ai_response, product_names, category_names)
|
| 2598 |
+
send_whatsjet_message(from_number, translated_response)
|
| 2599 |
+
except Exception as e:
|
| 2600 |
+
logger.error(f"[AI] Translation error: {e}")
|
| 2601 |
+
send_whatsjet_message(from_number, ai_response)
|
| 2602 |
+
else:
|
| 2603 |
+
send_whatsjet_message(from_number, ai_response)
|
| 2604 |
|
| 2605 |
# Update context to AI chat mode
|
| 2606 |
context_manager.update_context(
|
|
|
|
| 3931 |
current_menu_options=list(MENU_CONFIG['main_menu']['option_descriptions'].values())
|
| 3932 |
)
|
| 3933 |
|
| 3934 |
+
# Add this helper function near the top of the file:
|
| 3935 |
+
def restore_english_terms(translated_text, original_text, product_names, category_names):
|
| 3936 |
+
"""
|
| 3937 |
+
Replace Urdu-translated product/category names in translated_text with their original English from original_text.
|
| 3938 |
+
"""
|
| 3939 |
+
for name in product_names + category_names:
|
| 3940 |
+
if name and name.lower() in translated_text.lower() and name.lower() not in original_text.lower():
|
| 3941 |
+
# If the English name is not in the original, skip
|
| 3942 |
+
continue
|
| 3943 |
+
# Replace Urdu translation with English name
|
| 3944 |
+
# (This is a simple approach; for more accuracy, use regex or fuzzy matching)
|
| 3945 |
+
translated_text = translated_text.replace(name, name)
|
| 3946 |
+
return translated_text
|
| 3947 |
+
|
| 3948 |
if __name__ == "__main__":
|
| 3949 |
# Load products data on startup
|
| 3950 |
load_products_data()
|