Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| from groq import Groq | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| class WriterAgent: | |
| def __init__(self): | |
| self.api_key = os.getenv("GROQ_API_KEY") | |
| self.client = Groq(api_key=self.api_key) if self.api_key else None | |
| self.model = "llama-3.3-70b-versatile" | |
| def write_listing(self, visual_data: dict, seo_keywords: list) -> dict: | |
| if not self.client: | |
| return {"error": "No API Key"} | |
| system_prompt = """You are a professional copywriter. | |
| Write a JSON product listing. | |
| RULES: | |
| 1. Output strictly valid JSON. | |
| 2. "description": Single paragraph, plain text, no newlines. | |
| 3. "title": Concise SEO title. | |
| 4. "features": List of 3 distinct features. | |
| Example Output: | |
| { | |
| "title": "Classic Leather Jacket", | |
| "description": "A timeless piece crafted from premium leather.", | |
| "features": ["Genuine Leather", "Slim Fit", "Zip Closure"], | |
| "price_estimate": "$100-$150" | |
| } | |
| """ | |
| user_content = f""" | |
| DATA: {json.dumps(visual_data)} | |
| KEYWORDS: {', '.join(seo_keywords)} | |
| """ | |
| try: | |
| completion = self.client.chat.completions.create( | |
| model=self.model, | |
| messages=[ | |
| {"role": "system", "content": system_prompt}, | |
| {"role": "user", "content": user_content} | |
| ], | |
| temperature=0.1, | |
| response_format={"type": "json_object"} | |
| ) | |
| return json.loads(completion.choices[0].message.content) | |
| except Exception as e: | |
| print(f"❌ Writer Error: {e}") | |
| return { | |
| "title": visual_data.get('product_type', 'Product Name'), | |
| "description": "High-quality fashion item matching your style.", | |
| "features": visual_data.get('visual_features', []), | |
| "price_estimate": "$50-$100" | |
| } | |