Spaces:
Sleeping
Sleeping
| import logging | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | |
| def select_relevant_image(images: list, text: str) -> str: | |
| """Select a contextually relevant image from the list based on text.""" | |
| if not images: | |
| logging.info("No images available, using empty media URL.") | |
| return "" | |
| # Prioritize images that are likely content-related (avoid logos, icons) | |
| for img in images: | |
| if not any(keyword in img.lower() for keyword in ["logo", "icon", "banner", "ad"]): | |
| logging.info(f"Selected image: {img}") | |
| return img | |
| # Fallback to first image if no clear content image | |
| logging.info(f"No content image found, using first image: {images[0]}") | |
| return images[0] | |
| def build_rich_card(scraped_data: dict, summary: dict) -> dict: | |
| """Build the rich card JSON using only scraped data and summary.""" | |
| logging.info(f"Building rich card with scraped_data: {scraped_data}, summary: {summary}") | |
| # Select relevant image | |
| media_url = select_relevant_image(scraped_data.get("images", []), scraped_data.get("text", "")) | |
| # Use scraped URL | |
| page_url = scraped_data.get("url", "") | |
| # Use summary description | |
| description = summary.get("description", "Explore news and insights.") | |
| rich_card = { | |
| "targets": [{"ids": [1368], "targetType": "humans"}], | |
| "text": description, | |
| "mediaType": "image", | |
| "media": media_url, | |
| "buttons": [ | |
| { | |
| "type": "weburl", | |
| "title": "View Now", | |
| "payload": page_url | |
| }, | |
| { | |
| "type": "postback", | |
| "title": "Learn More", | |
| "payload": "learn_more", | |
| "execute": None | |
| } | |
| ], | |
| "quickReplies": [ | |
| { | |
| "type": "postback", | |
| "title": "Show Similar", | |
| "payload": "similar_content" | |
| }, | |
| { | |
| "type": "call", | |
| "title": "Contact Support", | |
| "payload": "+12345678901" | |
| } | |
| ], | |
| "richCard": { | |
| "cardOrientation": "VERTICAL", | |
| "mediaHeight": "MEDIUM" | |
| } | |
| } | |
| logging.info(f"Generated rich card: {rich_card}") | |
| return rich_card |