Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,86 +11,62 @@ from autogen_agentchat.teams import SelectorGroupChat
|
|
| 11 |
from autogen_ext.models.openai import OpenAIChatCompletionClient
|
| 12 |
from autogen_ext.agents.web_surfer import MultimodalWebSurfer
|
| 13 |
|
| 14 |
-
# Enable nested event loops
|
| 15 |
nest_asyncio.apply()
|
| 16 |
|
| 17 |
class AIShoppingAnalyzer:
|
| 18 |
def __init__(self, api_key: str):
|
| 19 |
self.api_key = api_key
|
|
|
|
| 20 |
os.environ["OPENAI_API_KEY"] = api_key
|
| 21 |
self.model_client = OpenAIChatCompletionClient(model="gpt-4o")
|
| 22 |
self.termination = MaxMessageTermination(max_messages=20) | TextMentionTermination("TERMINATE")
|
| 23 |
|
| 24 |
def create_websurfer(self) -> MultimodalWebSurfer:
|
| 25 |
"""Initialize the web surfer agent for e-commerce research"""
|
| 26 |
-
description = (
|
| 27 |
-
"E-commerce research specialist that:\n"
|
| 28 |
-
"1. Searches multiple retailers for product options\n"
|
| 29 |
-
"2. Compares prices and reviews\n"
|
| 30 |
-
"3. Checks product specifications and availability\n"
|
| 31 |
-
"4. Analyzes website structure and findability\n"
|
| 32 |
-
"5. Detects and analyzes structured data (Schema.org, JSON-LD, Microdata)\n"
|
| 33 |
-
"6. Evaluates product markup and rich snippets\n"
|
| 34 |
-
"7. Checks for proper semantic HTML and data organization"
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
return MultimodalWebSurfer(
|
| 38 |
name="websurfer_agent",
|
| 39 |
-
description=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
model_client=self.model_client,
|
| 41 |
-
headless=True
|
| 42 |
-
browser_type="chromium",
|
| 43 |
-
playwright_browser_launch_kwargs={
|
| 44 |
-
"args": [
|
| 45 |
-
"--disable-dev-shm-usage",
|
| 46 |
-
"--no-sandbox",
|
| 47 |
-
"--disable-setuid-sandbox"
|
| 48 |
-
]
|
| 49 |
-
}
|
| 50 |
)
|
| 51 |
|
| 52 |
def create_assistant(self) -> AssistantAgent:
|
| 53 |
"""Initialize the shopping assistant agent"""
|
| 54 |
-
system_message = (
|
| 55 |
-
"You are an expert shopping assistant and e-commerce analyst. "
|
| 56 |
-
"Analyze websites and provide reports in this format:\n\n"
|
| 57 |
-
"π E-COMMERCE ANALYSIS REPORT\n"
|
| 58 |
-
"============================\n"
|
| 59 |
-
"Site: {url}\n"
|
| 60 |
-
"Date: {date}\n\n"
|
| 61 |
-
"π FINDABILITY SCORE: [β
β
β
β
β]\n"
|
| 62 |
-
"-----------------------------\n"
|
| 63 |
-
"β’ Category Organization\n"
|
| 64 |
-
"β’ Navigation Structure\n"
|
| 65 |
-
"β’ Filter Systems\n\n"
|
| 66 |
-
"π INFORMATION QUALITY: [β
β
β
β
β]\n"
|
| 67 |
-
"------------------------------\n"
|
| 68 |
-
"β’ Product Details\n"
|
| 69 |
-
"β’ Image Quality\n"
|
| 70 |
-
"β’ Technical Specs\n"
|
| 71 |
-
"β’ Structured Data\n\n"
|
| 72 |
-
"π NAVIGATION & SEARCH: [β
β
β
β
β]\n"
|
| 73 |
-
"------------------------------\n"
|
| 74 |
-
"β’ Search Features\n"
|
| 75 |
-
"β’ User Experience\n"
|
| 76 |
-
"β’ Mobile Design\n\n"
|
| 77 |
-
"π° PRICING TRANSPARENCY: [β
β
β
β
β]\n"
|
| 78 |
-
"------------------------------\n"
|
| 79 |
-
"β’ Price Display\n"
|
| 80 |
-
"β’ Special Offers\n"
|
| 81 |
-
"β’ Comparison Tools\n\n"
|
| 82 |
-
"π OVERALL ASSESSMENT\n"
|
| 83 |
-
"-------------------\n"
|
| 84 |
-
"[Summary]\n\n"
|
| 85 |
-
"π§ TECHNICAL INSIGHTS\n"
|
| 86 |
-
"-------------------\n"
|
| 87 |
-
"[Technical Details]"
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
return AssistantAgent(
|
| 91 |
name="assistant_agent",
|
| 92 |
description="E-commerce shopping advisor and website analyzer",
|
| 93 |
-
system_message=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
model_client=self.model_client
|
| 95 |
)
|
| 96 |
|
|
@@ -101,17 +77,17 @@ class AIShoppingAnalyzer:
|
|
| 101 |
description="An e-commerce site owner looking for AI shopping analysis"
|
| 102 |
)
|
| 103 |
|
| 104 |
-
selector_prompt = (
|
| 105 |
-
"You are coordinating an e-commerce analysis system. Select the next role from these participants:\n"
|
| 106 |
-
"- The websurfer_agent searches products and analyzes website structure\n"
|
| 107 |
-
"- The assistant_agent evaluates findings and makes recommendations\n"
|
| 108 |
-
"- The user_proxy provides input when needed\n\n"
|
| 109 |
-
"Return only the role name."
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
return SelectorGroupChat(
|
| 113 |
participants=[websurfer_agent, assistant_agent, user_proxy],
|
| 114 |
-
selector_prompt=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
model_client=self.model_client,
|
| 116 |
termination_condition=self.termination
|
| 117 |
)
|
|
@@ -123,21 +99,24 @@ class AIShoppingAnalyzer:
|
|
| 123 |
"""Run the analysis with proper cleanup"""
|
| 124 |
websurfer = None
|
| 125 |
try:
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
)
|
| 133 |
|
| 134 |
if specific_product:
|
| 135 |
query += f"\n5. Detailed analysis of this specific product: {specific_product}"
|
| 136 |
|
|
|
|
| 137 |
websurfer = self.create_websurfer()
|
| 138 |
assistant = self.create_assistant()
|
|
|
|
|
|
|
| 139 |
team = self.create_team(websurfer, assistant)
|
| 140 |
|
|
|
|
| 141 |
try:
|
| 142 |
result = []
|
| 143 |
async for message in team.run_stream(task=query):
|
|
@@ -157,8 +136,9 @@ class AIShoppingAnalyzer:
|
|
| 157 |
await websurfer.close()
|
| 158 |
except Exception as e:
|
| 159 |
print(f"Cleanup error: {str(e)}")
|
|
|
|
| 160 |
|
| 161 |
-
def create_gradio_interface() -> gr.
|
| 162 |
"""Create the Gradio interface for the AI Shopping Analyzer"""
|
| 163 |
|
| 164 |
css = """
|
|
@@ -195,6 +175,7 @@ def create_gradio_interface() -> gr.Blocks:
|
|
| 195 |
background-color: #e5e7eb;
|
| 196 |
}
|
| 197 |
|
|
|
|
| 198 |
.gr-form {
|
| 199 |
background: transparent !important;
|
| 200 |
border: none !important;
|
|
@@ -229,13 +210,17 @@ def create_gradio_interface() -> gr.Blocks:
|
|
| 229 |
background-color: #3a3ab8 !important;
|
| 230 |
}
|
| 231 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
async def run_analysis(api_key: str,
|
| 234 |
website_url: str,
|
| 235 |
product_category: str,
|
| 236 |
specific_product: str) -> str:
|
| 237 |
"""Handle the analysis submission"""
|
| 238 |
-
if not api_key
|
| 239 |
return "Please enter a valid OpenAI API key (should start with 'sk-')"
|
| 240 |
|
| 241 |
if not website_url:
|
|
@@ -255,72 +240,34 @@ def create_gradio_interface() -> gr.Blocks:
|
|
| 255 |
except Exception as e:
|
| 256 |
return f"Error during analysis: {str(e)}"
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
website_url = gr.Textbox(
|
| 275 |
-
label="Website URL",
|
| 276 |
-
placeholder="https://your-store.com",
|
| 277 |
-
container=True
|
| 278 |
-
)
|
| 279 |
-
|
| 280 |
-
product_category = gr.Textbox(
|
| 281 |
-
label="Product Category",
|
| 282 |
-
placeholder="e.g., Electronics, Clothing, etc.",
|
| 283 |
-
container=True
|
| 284 |
-
)
|
| 285 |
-
|
| 286 |
-
specific_product = gr.Textbox(
|
| 287 |
-
label="Specific Product (Optional)",
|
| 288 |
-
placeholder="e.g., Blue Widget Model X",
|
| 289 |
-
container=True
|
| 290 |
-
)
|
| 291 |
-
|
| 292 |
-
analyze_button = gr.Button(
|
| 293 |
-
"Analyze Site",
|
| 294 |
-
variant="primary"
|
| 295 |
-
)
|
| 296 |
-
|
| 297 |
-
analysis_output = gr.Textbox(
|
| 298 |
-
label="Analysis Results",
|
| 299 |
-
lines=20,
|
| 300 |
-
container=True
|
| 301 |
-
)
|
| 302 |
-
|
| 303 |
-
analyze_button.click(
|
| 304 |
-
fn=run_analysis,
|
| 305 |
-
inputs=[api_key, website_url, product_category, specific_product],
|
| 306 |
-
outputs=analysis_output
|
| 307 |
-
)
|
| 308 |
-
|
| 309 |
-
return demo
|
| 310 |
|
| 311 |
if __name__ == "__main__":
|
| 312 |
-
|
|
|
|
| 313 |
try:
|
| 314 |
-
|
| 315 |
-
subprocess.run(
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
capture_output=True,
|
| 319 |
-
text=True
|
| 320 |
-
)
|
| 321 |
except Exception as e:
|
| 322 |
-
print(f"
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
|
|
|
| 11 |
from autogen_ext.models.openai import OpenAIChatCompletionClient
|
| 12 |
from autogen_ext.agents.web_surfer import MultimodalWebSurfer
|
| 13 |
|
| 14 |
+
# Enable nested event loops for Jupyter compatibility
|
| 15 |
nest_asyncio.apply()
|
| 16 |
|
| 17 |
class AIShoppingAnalyzer:
|
| 18 |
def __init__(self, api_key: str):
|
| 19 |
self.api_key = api_key
|
| 20 |
+
# Set the API key in environment
|
| 21 |
os.environ["OPENAI_API_KEY"] = api_key
|
| 22 |
self.model_client = OpenAIChatCompletionClient(model="gpt-4o")
|
| 23 |
self.termination = MaxMessageTermination(max_messages=20) | TextMentionTermination("TERMINATE")
|
| 24 |
|
| 25 |
def create_websurfer(self) -> MultimodalWebSurfer:
|
| 26 |
"""Initialize the web surfer agent for e-commerce research"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return MultimodalWebSurfer(
|
| 28 |
name="websurfer_agent",
|
| 29 |
+
description="""E-commerce research specialist that:
|
| 30 |
+
1. Searches multiple retailers for product options
|
| 31 |
+
2. Compares prices and reviews
|
| 32 |
+
3. Checks product specifications and availability
|
| 33 |
+
4. Analyzes website structure and findability
|
| 34 |
+
5. Detects and analyzes structured data (Schema.org, JSON-LD, Microdata)
|
| 35 |
+
6. Evaluates product markup and rich snippets
|
| 36 |
+
7. Checks for proper semantic HTML and data organization""",
|
| 37 |
model_client=self.model_client,
|
| 38 |
+
headless=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
def create_assistant(self) -> AssistantAgent:
|
| 42 |
"""Initialize the shopping assistant agent"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return AssistantAgent(
|
| 44 |
name="assistant_agent",
|
| 45 |
description="E-commerce shopping advisor and website analyzer",
|
| 46 |
+
system_message="""You are an expert shopping assistant and e-commerce analyst. Your role is to:
|
| 47 |
+
1. Help find products based on user needs
|
| 48 |
+
2. Compare prices and features across different sites
|
| 49 |
+
3. Analyze website usability and product findability
|
| 50 |
+
4. Evaluate product presentation and information quality
|
| 51 |
+
5. Assess the overall e-commerce experience
|
| 52 |
+
6. Analyze structured data implementation:
|
| 53 |
+
- Check for Schema.org markup
|
| 54 |
+
- Validate JSON-LD implementation
|
| 55 |
+
- Evaluate microdata usage
|
| 56 |
+
- Assess rich snippet potential
|
| 57 |
+
7. Report on data structure quality:
|
| 58 |
+
- Product markup completeness
|
| 59 |
+
- Price and availability markup
|
| 60 |
+
- Review and rating markup
|
| 61 |
+
- Inventory status markup
|
| 62 |
+
|
| 63 |
+
When working with the websurfer_agent:
|
| 64 |
+
- Guide their research effectively
|
| 65 |
+
- Verify the information they find
|
| 66 |
+
- Analyze how easy it was to find products
|
| 67 |
+
- Evaluate product page quality
|
| 68 |
+
- Say 'keep going' if more research is needed
|
| 69 |
+
- Say 'TERMINATE' only when you have a complete analysis""",
|
| 70 |
model_client=self.model_client
|
| 71 |
)
|
| 72 |
|
|
|
|
| 77 |
description="An e-commerce site owner looking for AI shopping analysis"
|
| 78 |
)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
return SelectorGroupChat(
|
| 81 |
participants=[websurfer_agent, assistant_agent, user_proxy],
|
| 82 |
+
selector_prompt="""You are coordinating an e-commerce analysis system. The following roles are available:
|
| 83 |
+
{roles}
|
| 84 |
+
|
| 85 |
+
Given the conversation history {history}, select the next role from {participants}.
|
| 86 |
+
- The websurfer_agent searches products and analyzes website structure
|
| 87 |
+
- The assistant_agent evaluates findings and makes recommendations
|
| 88 |
+
- The user_proxy provides input when needed
|
| 89 |
+
|
| 90 |
+
Return only the role name.""",
|
| 91 |
model_client=self.model_client,
|
| 92 |
termination_condition=self.termination
|
| 93 |
)
|
|
|
|
| 99 |
"""Run the analysis with proper cleanup"""
|
| 100 |
websurfer = None
|
| 101 |
try:
|
| 102 |
+
# Set up the analysis query
|
| 103 |
+
query = f"""Analyze the e-commerce experience for {website_url} focusing on:
|
| 104 |
+
1. Product findability in the {product_category} category
|
| 105 |
+
2. Product information quality
|
| 106 |
+
3. Navigation and search functionality
|
| 107 |
+
4. Price visibility and comparison features"""
|
|
|
|
| 108 |
|
| 109 |
if specific_product:
|
| 110 |
query += f"\n5. Detailed analysis of this specific product: {specific_product}"
|
| 111 |
|
| 112 |
+
# Initialize agents with automatic browser management
|
| 113 |
websurfer = self.create_websurfer()
|
| 114 |
assistant = self.create_assistant()
|
| 115 |
+
|
| 116 |
+
# Create team
|
| 117 |
team = self.create_team(websurfer, assistant)
|
| 118 |
|
| 119 |
+
# Modified execution to handle EOF errors
|
| 120 |
try:
|
| 121 |
result = []
|
| 122 |
async for message in team.run_stream(task=query):
|
|
|
|
| 136 |
await websurfer.close()
|
| 137 |
except Exception as e:
|
| 138 |
print(f"Cleanup error: {str(e)}")
|
| 139 |
+
# Continue even if cleanup fails
|
| 140 |
|
| 141 |
+
def create_gradio_interface() -> gr.Interface:
|
| 142 |
"""Create the Gradio interface for the AI Shopping Analyzer"""
|
| 143 |
|
| 144 |
css = """
|
|
|
|
| 175 |
background-color: #e5e7eb;
|
| 176 |
}
|
| 177 |
|
| 178 |
+
/* Custom styling for form elements */
|
| 179 |
.gr-form {
|
| 180 |
background: transparent !important;
|
| 181 |
border: none !important;
|
|
|
|
| 210 |
background-color: #3a3ab8 !important;
|
| 211 |
}
|
| 212 |
"""
|
| 213 |
+
|
| 214 |
+
def validate_api_key(api_key: str) -> bool:
|
| 215 |
+
"""Validate the OpenAI API key format"""
|
| 216 |
+
return api_key.startswith("sk-") and len(api_key) > 20
|
| 217 |
|
| 218 |
async def run_analysis(api_key: str,
|
| 219 |
website_url: str,
|
| 220 |
product_category: str,
|
| 221 |
specific_product: str) -> str:
|
| 222 |
"""Handle the analysis submission"""
|
| 223 |
+
if not validate_api_key(api_key):
|
| 224 |
return "Please enter a valid OpenAI API key (should start with 'sk-')"
|
| 225 |
|
| 226 |
if not website_url:
|
|
|
|
| 240 |
except Exception as e:
|
| 241 |
return f"Error during analysis: {str(e)}"
|
| 242 |
|
| 243 |
+
# Create the interface
|
| 244 |
+
return gr.Interface(
|
| 245 |
+
fn=run_analysis,
|
| 246 |
+
inputs=[
|
| 247 |
+
gr.Textbox(label="OpenAI API Key", placeholder="sk-...", type="password"),
|
| 248 |
+
gr.Textbox(label="Website URL", placeholder="https://your-store.com"),
|
| 249 |
+
gr.Textbox(label="Product Category", placeholder="e.g., Electronics, Clothing, etc."),
|
| 250 |
+
gr.Textbox(label="Specific Product (Optional)", placeholder="e.g., Blue Widget Model X")
|
| 251 |
+
],
|
| 252 |
+
outputs=gr.Textbox(label="Analysis Results", lines=20),
|
| 253 |
+
title="AI Shopping Agent Analyzer",
|
| 254 |
+
description="""Analyze how your e-commerce site performs when the shopper is an AI agent.
|
| 255 |
+
This tool helps you understand your site's effectiveness for AI-powered shopping assistants.""",
|
| 256 |
+
theme="default",
|
| 257 |
+
allow_flagging="never"
|
| 258 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
if __name__ == "__main__":
|
| 261 |
+
# Install Playwright browsers and dependencies
|
| 262 |
+
import subprocess
|
| 263 |
try:
|
| 264 |
+
subprocess.run(["playwright", "install"], check=True)
|
| 265 |
+
subprocess.run(["playwright", "install-deps"], check=True)
|
| 266 |
+
except subprocess.CalledProcessError as e:
|
| 267 |
+
print(f"Error installing Playwright dependencies: {e}")
|
|
|
|
|
|
|
|
|
|
| 268 |
except Exception as e:
|
| 269 |
+
print(f"Unexpected error during setup: {e}")
|
| 270 |
+
|
| 271 |
+
# Create and launch the interface
|
| 272 |
+
iface = create_gradio_interface()
|
| 273 |
+
iface.launch()
|