# recipe.py
import os
from praisonaiagents import Agent, Task, PraisonAIAgents
def run(input_data: dict, config: dict = None) -> dict:
"""Generate alt text for product images."""
image_path = input_data.get("image_path")
brand_tone = input_data.get("brand_tone", "neutral")
locale = input_data.get("locale", "en-US")
if not image_path:
return {"ok": False, "error": {"code": "MISSING_INPUT", "message": "image_path is required"}}
if not os.path.exists(image_path):
return {"ok": False, "error": {"code": "FILE_NOT_FOUND", "message": f"Image not found: {image_path}"}}
try:
tone_guidelines = {
"neutral": "Clear, factual descriptions",
"luxury": "Elegant, sophisticated language emphasizing quality",
"casual": "Friendly, approachable descriptions",
"technical": "Precise specifications and features"
}
# Create vision agent
vision_agent = Agent(
name="Product Analyst",
role="Visual Product Expert",
goal="Analyze product images accurately",
instructions=f"""
You are a product image analyst.
- Identify the product type and category
- Note colors, materials, and features
- Describe the product's appearance
- Consider the {brand_tone} tone: {tone_guidelines[brand_tone]}
""",
)
# Create alt text writer
alt_writer = Agent(
name="Alt Text Writer",
role="Accessibility Specialist",
goal="Write effective alt text for screen readers",
instructions=f"""
You are an accessibility expert writing alt text.
- Keep alt text under 125 characters
- Be descriptive but concise
- Include key product details
- Use {brand_tone} tone
- Write for {locale} audience
- Don't start with "Image of" or "Picture of"
""",
)
# Create tag generator
tagger = Agent(
name="Product Tagger",
role="E-commerce SEO Specialist",
goal="Generate relevant product tags",
instructions="""
You are an e-commerce tagging expert.
- Generate 5-10 relevant tags
- Include category, color, material, style
- Use lowercase, hyphenated format
- Focus on searchable terms
""",
)
# Define tasks
analyze_task = Task(
name="analyze_image",
description=f"Analyze the product image at: {image_path}",
expected_output="Detailed product description",
agent=vision_agent,
)
alt_task = Task(
name="write_alt_text",
description="Write accessible alt text based on the analysis",
expected_output="Alt text under 125 characters",
agent=alt_writer,
context=[analyze_task],
)
tag_task = Task(
name="generate_tags",
description="Generate product tags for SEO",
expected_output="List of 5-10 tags",
agent=tagger,
context=[analyze_task],
)
# Execute
agents = PraisonAIAgents(
agents=[vision_agent, alt_writer, tagger],
tasks=[analyze_task, alt_task, tag_task],
)
result = agents.start()
# Parse tags
tags_text = result.get("generate_tags", "")
tags = [t.strip().lower().replace(" ", "-") for t in tags_text.split(",") if t.strip()]
if not tags:
tags = [t.strip() for t in tags_text.split("\n") if t.strip() and not t.startswith("-")]
return {
"ok": True,
"alt_text": result.get("write_alt_text", "").strip()[:125],
"tags": tags[:10],
"artifacts": [],
"warnings": [],
}
except Exception as e:
return {"ok": False, "error": {"code": "PROCESSING_ERROR", "message": str(e)}}