Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -147,13 +147,11 @@ MENU_CONFIG = {
|
|
147 |
},
|
148 |
'product_inquiry': {
|
149 |
'name': 'Product Inquiry Menu',
|
150 |
-
'valid_options': ['1', '2', '3'
|
151 |
'option_descriptions': {
|
152 |
'1': 'Talk to Veterinary Consultant',
|
153 |
'2': 'Inquire about Product Availability',
|
154 |
-
'3': '
|
155 |
-
'4': 'Download Detailed PDF',
|
156 |
-
'5': 'Back to Main Menu'
|
157 |
}
|
158 |
},
|
159 |
'ai_chat': {
|
@@ -647,22 +645,18 @@ context_manager = VeterinaryContextManager()
|
|
647 |
# Enhanced product response with veterinary domain expertise
|
648 |
def generate_veterinary_product_response(product_info: Dict[str, Any], user_context: Dict[str, Any]) -> str:
|
649 |
"""Generate comprehensive veterinary product response with intelligent information handling"""
|
650 |
-
|
651 |
def clean_text(text):
|
652 |
if pd.isna(text) or text is None:
|
653 |
return "Not specified"
|
654 |
return str(text).strip()
|
655 |
-
|
656 |
# Extract product details
|
657 |
product_name = clean_text(product_info.get('Product Name', ''))
|
658 |
product_type = clean_text(product_info.get('Type', ''))
|
659 |
category = clean_text(product_info.get('Category', ''))
|
660 |
indications = clean_text(product_info.get('Indications', ''))
|
661 |
-
|
662 |
# Check for PDF link in the CSV data
|
663 |
pdf_link = ""
|
664 |
try:
|
665 |
-
# Load CSV data to check for PDF link
|
666 |
csv_data = pd.read_csv('Veterinary.csv')
|
667 |
product_row = csv_data[csv_data['Product Name'] == product_name]
|
668 |
if not product_row.empty:
|
@@ -671,27 +665,15 @@ def generate_veterinary_product_response(product_info: Dict[str, Any], user_cont
|
|
671 |
pdf_link = brochure_link.strip()
|
672 |
except Exception as e:
|
673 |
logger.warning(f"Error checking PDF link for {product_name}: {e}")
|
674 |
-
|
675 |
-
# Build the response
|
676 |
-
response = f"""π§ͺ *Name:* {product_name}
|
677 |
-
π¦ *Type:* {product_type}
|
678 |
-
π₯ *Category:* {category}
|
679 |
-
π *Used For:* {indications}"""
|
680 |
-
|
681 |
-
# Add PDF link if available, in the requested format
|
682 |
if pdf_link:
|
683 |
response += f"\n\nπ Product Brochure Available\nπ {product_name} PDF:\n{pdf_link}"
|
684 |
-
|
685 |
-
# Add menu options
|
686 |
response += f"""
|
687 |
-
|
688 |
-
π¬ *Available Actions:*
|
689 |
1οΈβ£ Talk to Veterinary Consultant
|
690 |
2οΈβ£ Inquire About Availability
|
691 |
3οΈβ£ Back to Main Menu
|
692 |
-
|
693 |
-
π¬ Select an option or ask about related products"""
|
694 |
-
|
695 |
return response
|
696 |
|
697 |
def clean_text_for_pdf(text: str) -> str:
|
@@ -2982,33 +2964,6 @@ async def handle_veterinary_product_followup(selection: str, from_number: str) -
|
|
2982 |
await handle_availability_inquiry(from_number, user_context)
|
2983 |
|
2984 |
elif selection == '3':
|
2985 |
-
# View Product Image
|
2986 |
-
await send_product_with_image(from_number, current_product, user_context)
|
2987 |
-
# Return to product inquiry menu
|
2988 |
-
response_with_media = generate_veterinary_product_response_with_media(current_product, user_context)
|
2989 |
-
send_whatsjet_message(from_number, response_with_media['text'])
|
2990 |
-
context_manager.update_context(
|
2991 |
-
from_number,
|
2992 |
-
current_state='product_inquiry',
|
2993 |
-
current_menu='product_inquiry',
|
2994 |
-
current_menu_options=list(MENU_CONFIG['product_inquiry']['option_descriptions'].values())
|
2995 |
-
)
|
2996 |
-
|
2997 |
-
elif selection == '4':
|
2998 |
-
# Download Detailed PDF
|
2999 |
-
await send_enhanced_pdf(from_number, current_product)
|
3000 |
-
# Return to product inquiry menu
|
3001 |
-
response_with_media = generate_veterinary_product_response_with_media(current_product, user_context)
|
3002 |
-
send_whatsjet_message(from_number, response_with_media['text'])
|
3003 |
-
context_manager.update_context(
|
3004 |
-
from_number,
|
3005 |
-
current_state='product_inquiry',
|
3006 |
-
current_menu='product_inquiry',
|
3007 |
-
current_menu_options=list(MENU_CONFIG['product_inquiry']['option_descriptions'].values())
|
3008 |
-
)
|
3009 |
-
|
3010 |
-
elif selection == '5':
|
3011 |
-
# Back to Main Menu
|
3012 |
welcome_msg = generate_veterinary_welcome_message(from_number, user_context)
|
3013 |
send_whatsjet_message(from_number, welcome_msg)
|
3014 |
context_manager.update_context(
|
@@ -3019,7 +2974,7 @@ async def handle_veterinary_product_followup(selection: str, from_number: str) -
|
|
3019 |
)
|
3020 |
return
|
3021 |
else:
|
3022 |
-
send_whatsjet_message(from_number, "β Invalid selection. Please choose 1, 2,
|
3023 |
return
|
3024 |
except Exception as e:
|
3025 |
logger.error(f"Error in product follow-up: {e}")
|
@@ -3365,7 +3320,7 @@ def get_menu_validation_message(current_state: str, user_context: dict) -> str:
|
|
3365 |
return "β No products available in this category. Please type 'main' to return to main menu."
|
3366 |
|
3367 |
elif current_state == 'product_inquiry':
|
3368 |
-
return "β Invalid selection for Product Options. Please choose:\n1οΈβ£ Talk to Veterinary Consultant\n2οΈβ£ Inquire about Product Availability\n3οΈβ£
|
3369 |
|
3370 |
elif current_state == 'ai_chat_mode':
|
3371 |
return "π¬ *You're in AI Chat mode. Ask me anything about veterinary care!*\n\nType 'main' to return to main menu."
|
@@ -3402,7 +3357,7 @@ def is_valid_menu_selection(selection: str, current_state: str, user_context: di
|
|
3402 |
return 1 <= selection_num <= total_products
|
3403 |
|
3404 |
elif current_state == 'product_inquiry':
|
3405 |
-
return 1 <= selection_num <=
|
3406 |
|
3407 |
elif current_state == 'ai_chat_mode':
|
3408 |
return mapped_selection == 'main'
|
@@ -3573,22 +3528,16 @@ def generate_veterinary_product_response_with_media(product_info: Dict[str, Any]
|
|
3573 |
Generate comprehensive veterinary product response with media information
|
3574 |
Returns a dictionary with text response and media info
|
3575 |
"""
|
3576 |
-
|
3577 |
def clean_text(text):
|
3578 |
if pd.isna(text) or text is None:
|
3579 |
return "Not specified"
|
3580 |
return str(text).strip()
|
3581 |
-
|
3582 |
-
# Extract product details
|
3583 |
product_name = clean_text(product_info.get('Product Name', ''))
|
3584 |
product_type = clean_text(product_info.get('Type', ''))
|
3585 |
category = clean_text(product_info.get('Category', ''))
|
3586 |
indications = clean_text(product_info.get('Indications', ''))
|
3587 |
-
|
3588 |
-
# Check for PDF link in the CSV data
|
3589 |
pdf_link = ""
|
3590 |
try:
|
3591 |
-
# Load CSV data to check for PDF link
|
3592 |
csv_data = pd.read_csv('Veterinary.csv')
|
3593 |
product_row = csv_data[csv_data['Product Name'] == product_name]
|
3594 |
if not product_row.empty:
|
@@ -3597,33 +3546,17 @@ def generate_veterinary_product_response_with_media(product_info: Dict[str, Any]
|
|
3597 |
pdf_link = brochure_link.strip()
|
3598 |
except Exception as e:
|
3599 |
logger.warning(f"Error checking PDF link for {product_name}: {e}")
|
3600 |
-
|
3601 |
-
# Build the response
|
3602 |
-
response_text = f"""π§ͺ *Name:* {product_name}
|
3603 |
-
π¦ *Type:* {product_type}
|
3604 |
-
π₯ *Category:* {category}
|
3605 |
-
π *Used For:* {indications}"""
|
3606 |
-
|
3607 |
-
# Add PDF link if available
|
3608 |
if pdf_link:
|
3609 |
response_text += f"\n\nπ Product Brochure Available\nπ {product_name} PDF:\n{pdf_link}"
|
3610 |
-
|
3611 |
-
# Add menu options
|
3612 |
response_text += f"""
|
3613 |
-
|
3614 |
-
π¬ *Available Actions:*
|
3615 |
1οΈβ£ Talk to Veterinary Consultant
|
3616 |
2οΈβ£ Inquire About Availability
|
3617 |
-
3οΈβ£
|
3618 |
-
|
3619 |
-
5οΈβ£ Back to Main Menu
|
3620 |
-
|
3621 |
-
π¬ Select an option or ask about related products"""
|
3622 |
-
|
3623 |
-
# Check for product image
|
3624 |
image_path = get_product_image_path(product_name)
|
3625 |
has_image = image_path is not None and os.path.exists(image_path)
|
3626 |
-
|
3627 |
return {
|
3628 |
'text': response_text,
|
3629 |
'has_image': has_image,
|
|
|
147 |
},
|
148 |
'product_inquiry': {
|
149 |
'name': 'Product Inquiry Menu',
|
150 |
+
'valid_options': ['1', '2', '3'],
|
151 |
'option_descriptions': {
|
152 |
'1': 'Talk to Veterinary Consultant',
|
153 |
'2': 'Inquire about Product Availability',
|
154 |
+
'3': 'Back to Main Menu'
|
|
|
|
|
155 |
}
|
156 |
},
|
157 |
'ai_chat': {
|
|
|
645 |
# Enhanced product response with veterinary domain expertise
|
646 |
def generate_veterinary_product_response(product_info: Dict[str, Any], user_context: Dict[str, Any]) -> str:
|
647 |
"""Generate comprehensive veterinary product response with intelligent information handling"""
|
|
|
648 |
def clean_text(text):
|
649 |
if pd.isna(text) or text is None:
|
650 |
return "Not specified"
|
651 |
return str(text).strip()
|
|
|
652 |
# Extract product details
|
653 |
product_name = clean_text(product_info.get('Product Name', ''))
|
654 |
product_type = clean_text(product_info.get('Type', ''))
|
655 |
category = clean_text(product_info.get('Category', ''))
|
656 |
indications = clean_text(product_info.get('Indications', ''))
|
|
|
657 |
# Check for PDF link in the CSV data
|
658 |
pdf_link = ""
|
659 |
try:
|
|
|
660 |
csv_data = pd.read_csv('Veterinary.csv')
|
661 |
product_row = csv_data[csv_data['Product Name'] == product_name]
|
662 |
if not product_row.empty:
|
|
|
665 |
pdf_link = brochure_link.strip()
|
666 |
except Exception as e:
|
667 |
logger.warning(f"Error checking PDF link for {product_name}: {e}")
|
668 |
+
response = f"""π§ͺ *Name:* {product_name}\nπ¦ *Type:* {product_type}\nπ₯ *Category:* {category}\nπ *Used For:* {indications}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
669 |
if pdf_link:
|
670 |
response += f"\n\nπ Product Brochure Available\nπ {product_name} PDF:\n{pdf_link}"
|
|
|
|
|
671 |
response += f"""
|
672 |
+
\n㪠*Available Actions:*
|
|
|
673 |
1οΈβ£ Talk to Veterinary Consultant
|
674 |
2οΈβ£ Inquire About Availability
|
675 |
3οΈβ£ Back to Main Menu
|
676 |
+
\n㪠Select an option or ask about related products"""
|
|
|
|
|
677 |
return response
|
678 |
|
679 |
def clean_text_for_pdf(text: str) -> str:
|
|
|
2964 |
await handle_availability_inquiry(from_number, user_context)
|
2965 |
|
2966 |
elif selection == '3':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2967 |
welcome_msg = generate_veterinary_welcome_message(from_number, user_context)
|
2968 |
send_whatsjet_message(from_number, welcome_msg)
|
2969 |
context_manager.update_context(
|
|
|
2974 |
)
|
2975 |
return
|
2976 |
else:
|
2977 |
+
send_whatsjet_message(from_number, "β Invalid selection. Please choose 1, 2, or 3.")
|
2978 |
return
|
2979 |
except Exception as e:
|
2980 |
logger.error(f"Error in product follow-up: {e}")
|
|
|
3320 |
return "β No products available in this category. Please type 'main' to return to main menu."
|
3321 |
|
3322 |
elif current_state == 'product_inquiry':
|
3323 |
+
return "β Invalid selection for Product Options. Please choose:\n1οΈβ£ Talk to Veterinary Consultant\n2οΈβ£ Inquire about Product Availability\n3οΈβ£ Back to Main Menu"
|
3324 |
|
3325 |
elif current_state == 'ai_chat_mode':
|
3326 |
return "π¬ *You're in AI Chat mode. Ask me anything about veterinary care!*\n\nType 'main' to return to main menu."
|
|
|
3357 |
return 1 <= selection_num <= total_products
|
3358 |
|
3359 |
elif current_state == 'product_inquiry':
|
3360 |
+
return 1 <= selection_num <= 3
|
3361 |
|
3362 |
elif current_state == 'ai_chat_mode':
|
3363 |
return mapped_selection == 'main'
|
|
|
3528 |
Generate comprehensive veterinary product response with media information
|
3529 |
Returns a dictionary with text response and media info
|
3530 |
"""
|
|
|
3531 |
def clean_text(text):
|
3532 |
if pd.isna(text) or text is None:
|
3533 |
return "Not specified"
|
3534 |
return str(text).strip()
|
|
|
|
|
3535 |
product_name = clean_text(product_info.get('Product Name', ''))
|
3536 |
product_type = clean_text(product_info.get('Type', ''))
|
3537 |
category = clean_text(product_info.get('Category', ''))
|
3538 |
indications = clean_text(product_info.get('Indications', ''))
|
|
|
|
|
3539 |
pdf_link = ""
|
3540 |
try:
|
|
|
3541 |
csv_data = pd.read_csv('Veterinary.csv')
|
3542 |
product_row = csv_data[csv_data['Product Name'] == product_name]
|
3543 |
if not product_row.empty:
|
|
|
3546 |
pdf_link = brochure_link.strip()
|
3547 |
except Exception as e:
|
3548 |
logger.warning(f"Error checking PDF link for {product_name}: {e}")
|
3549 |
+
response_text = f"""π§ͺ *Name:* {product_name}\nπ¦ *Type:* {product_type}\nπ₯ *Category:* {category}\nπ *Used For:* {indications}"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3550 |
if pdf_link:
|
3551 |
response_text += f"\n\nπ Product Brochure Available\nπ {product_name} PDF:\n{pdf_link}"
|
|
|
|
|
3552 |
response_text += f"""
|
3553 |
+
\n㪠*Available Actions:*
|
|
|
3554 |
1οΈβ£ Talk to Veterinary Consultant
|
3555 |
2οΈβ£ Inquire About Availability
|
3556 |
+
3οΈβ£ Back to Main Menu
|
3557 |
+
\n㪠Select an option or ask about related products"""
|
|
|
|
|
|
|
|
|
|
|
3558 |
image_path = get_product_image_path(product_name)
|
3559 |
has_image = image_path is not None and os.path.exists(image_path)
|
|
|
3560 |
return {
|
3561 |
'text': response_text,
|
3562 |
'has_image': has_image,
|