Spaces:
Sleeping
Sleeping
| import os | |
| from crewai import Agent, Task, Crew, Process | |
| from crewai_tools import ScrapeWebsiteTool | |
| from shortener_tool import ShortenerTool | |
| class SocialMediaCrew: | |
| def __init__(self): | |
| self.scrape_tool = ScrapeWebsiteTool() | |
| self.shortener_tool = ShortenerTool() | |
| self.product_analyst = Agent( | |
| role='Product Analyst', | |
| goal='Analyze the provided URL and extract key product information', | |
| backstory=("""You are an expert in analyzing product pages and extracting the most important information. | |
| You can identify the product name, the price, discount if any, its main features, and the target audience."""), | |
| verbose=True, | |
| tools=[self.scrape_tool, self.shortener_tool], | |
| ) | |
| self.social_media_copywriter = Agent( | |
| role='Social Media Copywriter', | |
| goal='Create a compelling social media post in Portuguese to sell the product', | |
| backstory=("""You are a creative copywriter specialized in the beauty and fragrance market. | |
| You know how to craft posts that are engaging, persuasive, and tailored for a Portuguese-speaking audience. | |
| You are an expert in using emojis and hashtags to increase engagement."""), | |
| verbose=True, | |
| ) | |
| def run_crew(self, product_url: str) -> str: | |
| analyze_product_task = Task( | |
| description=(f"""Using the 'scrape_tool', scrape the content of the URL: {product_url} and provide a summary of the product. | |
| Focus on the product name, its key characteristics, the FINAL PRICE, any DISCOUNT available. | |
| Then, use the 'URL Shortener Tool' to generate a short URL for {product_url}. If the shortener tool returns an error, use the original URL. | |
| Finally, provide all this information, including the generated short URL (or the original if shortener failed)."""), | |
| agent=self.product_analyst, | |
| expected_output="A concise summary of the product including its name, key features, unique selling points, FINAL PRICE, any DISCOUNT available, and the SHORT SHAREABLE URL (or the original URL if shortener failed)." | |
| ) | |
| create_post_task = Task( | |
| description=("""Based on the product analysis, create a CONCISE and DIRECT social media post in Portuguese, suitable for a WhatsApp group. | |
| The post should be exciting and highlight the main benefits of the perfume, including the FINAL PRICE, any DISCOUNT, and the SHORT SHAREABLE URL. | |
| Ensure a URL is always present in the output. Include a clear call to action and a MAXIMUM of 2 relevant emojis. DO NOT include hashtags. Keep it short and impactful."""), | |
| agent=self.social_media_copywriter, | |
| expected_output="A short, direct, and impactful social media post in Portuguese for WhatsApp, including the FINAL PRICE, any DISCOUNT, the SHORT SHAREABLE URL, a call to action, and up to 2 emojis. No hashtags should be present. A URL must always be present in the final output.", | |
| context=[analyze_product_task] | |
| ) | |
| crew = Crew( | |
| agents=[self.product_analyst, self.social_media_copywriter], | |
| tasks=[analyze_product_task, create_post_task], | |
| process=Process.sequential | |
| ) | |
| print(f"Crew is kicking off for URL: {product_url}") | |
| result = crew.kickoff() | |
| return result |