askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
test_one.py · 2.2 KB · 82 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Quick test of AI vision analysis on one component""" |
| 3 | |
| 4 | import os |
| 5 | import base64 |
| 6 | from pathlib import Path |
| 7 | from anthropic import Anthropic |
| 8 | |
| 9 | # Load .env |
| 10 | env_path = Path(__file__).parent / '.env' |
| 11 | if env_path.exists(): |
| 12 | with open(env_path) as f: |
| 13 | for line in f: |
| 14 | line = line.strip() |
| 15 | if line and not line.startswith('#') and '=' in line: |
| 16 | key, value = line.split('=', 1) |
| 17 | os.environ[key.strip()] = value.strip() |
| 18 | |
| 19 | api_key = os.environ.get('ANTHROPIC_API_KEY') |
| 20 | if not api_key: |
| 21 | print("❌ No API key found") |
| 22 | exit(1) |
| 23 | |
| 24 | client = Anthropic(api_key=api_key) |
| 25 | |
| 26 | # Test on semantic-action screenshot |
| 27 | screenshot = Path(__file__).parent / 'screenshots' / 'semantic-action.html.png' |
| 28 | |
| 29 | if not screenshot.exists(): |
| 30 | print(f"❌ Screenshot not found: {screenshot}") |
| 31 | print("Run: python3 test_visual_ai.py first to capture screenshots") |
| 32 | exit(1) |
| 33 | |
| 34 | print(f"🤖 Testing AI vision on {screenshot.name}...\n") |
| 35 | |
| 36 | # Encode image |
| 37 | with open(screenshot, 'rb') as f: |
| 38 | image_data = base64.standard_b64encode(f.read()).decode('utf-8') |
| 39 | |
| 40 | # Prompt |
| 41 | prompt = """Analyze this component screenshot for WCAG AA contrast compliance: |
| 42 | |
| 43 | 1. Identify all text elements |
| 44 | 2. Measure approximate contrast ratios between text and backgrounds |
| 45 | 3. Flag any text below 4.5:1 contrast (normal text) or 3:1 (large text 18pt+) |
| 46 | 4. List specific failures with colors if detected |
| 47 | |
| 48 | Return ONLY valid JSON (no markdown): |
| 49 | { |
| 50 | "passed": true/false, |
| 51 | "issues": [ |
| 52 | {"element": "Save button", "contrast": "3.2:1", "colors": "white on #007aff", "severity": "fail"} |
| 53 | ] |
| 54 | }""" |
| 55 | |
| 56 | # Call API |
| 57 | print("Sending to Claude Sonnet 4.5...") |
| 58 | message = client.messages.create( |
| 59 | model="claude-sonnet-4-5", |
| 60 | max_tokens=1024, |
| 61 | messages=[{ |
| 62 | "role": "user", |
| 63 | "content": [ |
| 64 | { |
| 65 | "type": "image", |
| 66 | "source": { |
| 67 | "type": "base64", |
| 68 | "media_type": "image/png", |
| 69 | "data": image_data |
| 70 | } |
| 71 | }, |
| 72 | { |
| 73 | "type": "text", |
| 74 | "text": prompt |
| 75 | } |
| 76 | ] |
| 77 | }] |
| 78 | ) |
| 79 | |
| 80 | print("\n✅ Response received!\n") |
| 81 | print("Raw response:") |
| 82 | print(message.content[0].text) |