Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3609,8 +3609,9 @@ async def send_product_image_with_caption(from_number: str, product: Dict[str, A
|
|
3609 |
product_name = product.get('Product Name', 'Unknown Product')
|
3610 |
details = generate_veterinary_product_response(product, user_context)
|
3611 |
image_url = product.get('Images', '').strip() if 'Images' in product else ''
|
3612 |
-
|
3613 |
try:
|
|
|
3614 |
if image_url:
|
3615 |
# Convert Google Drive link to direct download if needed
|
3616 |
if 'drive.google.com' in image_url:
|
@@ -3622,58 +3623,60 @@ async def send_product_image_with_caption(from_number: str, product: Dict[str, A
|
|
3622 |
file_id = ''
|
3623 |
if file_id:
|
3624 |
image_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
3625 |
-
|
3626 |
-
|
3627 |
-
|
3628 |
-
|
3629 |
-
|
3630 |
-
|
3631 |
-
|
3632 |
-
|
3633 |
-
|
3634 |
-
|
3635 |
-
|
3636 |
-
|
3637 |
-
|
3638 |
-
|
3639 |
-
|
3640 |
-
)
|
3641 |
-
|
3642 |
-
logger.info(f"[Product] Sent image from CSV link with caption for product: {product_name}")
|
3643 |
-
return
|
3644 |
-
else:
|
3645 |
-
logger.warning(f"[Product] Failed to send image from CSV link, sending text only: {product_name}")
|
3646 |
else:
|
3647 |
-
logger.warning(f"[Product]
|
3648 |
-
|
|
|
3649 |
image_path = get_product_image_path(product_name)
|
3650 |
-
if image_path and os.path.exists(image_path):
|
3651 |
media_type = get_product_image_media_type(image_path)
|
3652 |
filename = f"{product_name.replace(' ', '_')}.jpg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3653 |
success = send_whatsjet_message(
|
3654 |
from_number,
|
3655 |
details,
|
3656 |
media_type=media_type,
|
3657 |
-
media_path=
|
3658 |
filename=filename
|
3659 |
)
|
|
|
3660 |
if success:
|
3661 |
logger.info(f"[Product] Sent image with caption for product: {product_name}")
|
3662 |
else:
|
3663 |
logger.warning(f"[Product] Failed to send image, sending text only: {product_name}")
|
3664 |
send_whatsjet_message(from_number, details)
|
3665 |
else:
|
|
|
3666 |
send_whatsjet_message(from_number, details)
|
3667 |
logger.info(f"[Product] Sent product info without image: {product_name}")
|
|
|
3668 |
except Exception as e:
|
3669 |
logger.error(f"[Product] Error sending product image with caption: {e}")
|
3670 |
send_whatsjet_message(from_number, details)
|
3671 |
-
finally:
|
3672 |
-
if temp_file_path and os.path.exists(temp_file_path):
|
3673 |
-
try:
|
3674 |
-
os.remove(temp_file_path)
|
3675 |
-
except Exception:
|
3676 |
-
pass
|
3677 |
|
3678 |
@app.get("/test-product-image-with-caption")
|
3679 |
async def test_product_image_with_caption(phone: str):
|
@@ -3696,4 +3699,4 @@ async def test_product_image_with_caption(phone: str):
|
|
3696 |
if __name__ == "__main__":
|
3697 |
# Launch FastAPI app
|
3698 |
import uvicorn
|
3699 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
3609 |
product_name = product.get('Product Name', 'Unknown Product')
|
3610 |
details = generate_veterinary_product_response(product, user_context)
|
3611 |
image_url = product.get('Images', '').strip() if 'Images' in product else ''
|
3612 |
+
|
3613 |
try:
|
3614 |
+
# First, check if we have an image URL from CSV
|
3615 |
if image_url:
|
3616 |
# Convert Google Drive link to direct download if needed
|
3617 |
if 'drive.google.com' in image_url:
|
|
|
3623 |
file_id = ''
|
3624 |
if file_id:
|
3625 |
image_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
3626 |
+
|
3627 |
+
# Use the public URL directly for WhatsApp API
|
3628 |
+
media_type = 'image/jpeg'
|
3629 |
+
filename = f"{product_name.replace(' ', '_')}.jpg"
|
3630 |
+
|
3631 |
+
# Send using public URL (not local file)
|
3632 |
+
success = send_whatsjet_message(
|
3633 |
+
from_number,
|
3634 |
+
details,
|
3635 |
+
media_type=media_type,
|
3636 |
+
media_path=image_url, # Use public URL directly
|
3637 |
+
filename=filename
|
3638 |
+
)
|
3639 |
+
|
3640 |
+
if success:
|
3641 |
+
logger.info(f"[Product] Sent image from CSV link with caption for product: {product_name}")
|
3642 |
+
return
|
|
|
|
|
|
|
|
|
3643 |
else:
|
3644 |
+
logger.warning(f"[Product] Failed to send image from CSV link, trying fallback: {product_name}")
|
3645 |
+
|
3646 |
+
# Fallback to local uploads directory (public URL)
|
3647 |
image_path = get_product_image_path(product_name)
|
3648 |
+
if image_path and (image_path.startswith('http') or os.path.exists(image_path)):
|
3649 |
media_type = get_product_image_media_type(image_path)
|
3650 |
filename = f"{product_name.replace(' ', '_')}.jpg"
|
3651 |
+
|
3652 |
+
# If it's already a public URL, use it directly
|
3653 |
+
if image_path.startswith('http'):
|
3654 |
+
media_path = image_path
|
3655 |
+
else:
|
3656 |
+
# Convert local path to public URL
|
3657 |
+
media_path = f"https://dreamstream-1-chatbot.hf.space/uploads/{os.path.basename(image_path).replace(' ', '%20')}"
|
3658 |
+
|
3659 |
success = send_whatsjet_message(
|
3660 |
from_number,
|
3661 |
details,
|
3662 |
media_type=media_type,
|
3663 |
+
media_path=media_path, # Use public URL
|
3664 |
filename=filename
|
3665 |
)
|
3666 |
+
|
3667 |
if success:
|
3668 |
logger.info(f"[Product] Sent image with caption for product: {product_name}")
|
3669 |
else:
|
3670 |
logger.warning(f"[Product] Failed to send image, sending text only: {product_name}")
|
3671 |
send_whatsjet_message(from_number, details)
|
3672 |
else:
|
3673 |
+
# No image available, send text only
|
3674 |
send_whatsjet_message(from_number, details)
|
3675 |
logger.info(f"[Product] Sent product info without image: {product_name}")
|
3676 |
+
|
3677 |
except Exception as e:
|
3678 |
logger.error(f"[Product] Error sending product image with caption: {e}")
|
3679 |
send_whatsjet_message(from_number, details)
|
|
|
|
|
|
|
|
|
|
|
|
|
3680 |
|
3681 |
@app.get("/test-product-image-with-caption")
|
3682 |
async def test_product_image_with_caption(phone: str):
|
|
|
3699 |
if __name__ == "__main__":
|
3700 |
# Launch FastAPI app
|
3701 |
import uvicorn
|
3702 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|