#!/usr/bin/env python3 """ Example: Visual AI QC Headless Operation Demonstrates how to use the API without the web interface """ import requests import time import json # Configuration API_BASE = "http://localhost:7183" IMAGE_PATH = "path/to/your/image.jpg" def headless_analysis_example(): """Complete example of headless Visual AI QC analysis""" print("šŸ¤– Visual AI QC - Headless Analysis Example") print("=" * 50) # 1. Get available profiles print("\nšŸ“‹ Step 1: Getting available profiles...") response = requests.get(f"{API_BASE}/api/profiles") if response.status_code == 200: profiles = response.json() print(f"āœ… Found {len(profiles)} profile categories") for category, profile_list in profiles.items(): print(f" {category}: {len(profile_list)} profiles") else: print(f"āŒ Error getting profiles: {response.status_code}") return # 2. Get available reference assets print("\nšŸ“ Step 2: Getting reference assets...") response = requests.get(f"{API_BASE}/api/brand_guidelines") if response.status_code == 200: guidelines = response.json() print(f"āœ… Found {len(guidelines.get('files', {}))} reference assets") for file_id, file_info in guidelines.get('files', {}).items(): print(f" {file_info['brand_name']}: {file_info['original_filename']}") # 3. Start analysis with async tracking print(f"\nšŸ” Step 3: Starting analysis...") with open(IMAGE_PATH, 'rb') as f: files = {'file': f} data = { 'profile': 'diageo_key_visual', # Use Diageo profile 'mode': 'json', # JSON output 'reference_asset': 'Hellmanns_20250805_113413' # Use reference asset } response = requests.post(f"{API_BASE}/api/start_analysis", files=files, data=data) if response.status_code != 200: print(f"āŒ Error starting analysis: {response.status_code}") return result = response.json() session_id = result.get('session_id') if not session_id: print("āŒ No session ID returned") return print(f"āœ… Analysis started with session ID: {session_id}") # 4. Poll for progress print("\nā³ Step 4: Monitoring progress...") while True: response = requests.get(f"{API_BASE}/api/progress/{session_id}") if response.status_code != 200: print(f"āŒ Error getting progress: {response.status_code}") break progress_data = response.json() progress = progress_data.get('progress', {}) if progress.get('stage') == 'complete': print("āœ… Analysis complete!") results = progress.get('result', {}) break else: current_step = progress.get('current_check_display', 'Processing...') percentage = progress.get('percentage', 0) completed = progress.get('completed_checks', 0) total = progress.get('total_checks', 0) print(f" Progress: {percentage}% - {current_step} ({completed}/{total})") time.sleep(2) # 5. Display results print("\nšŸ“Š Step 5: Analysis Results") print("-" * 30) summary = results.get('summary', {}) print(f"Overall Score: {summary.get('overall_score', 0)}/100") print(f"Grade: {summary.get('grade', 'Unknown')}") print(f"Checks Performed: {summary.get('checks_count', 0)}") # Show individual check results qc_results = results.get('qc_analysis', {}).get('check_results', {}) print(f"\nšŸ” Individual Check Results:") for check_name, check_result in qc_results.items(): if check_result.get('status') == 'completed': score = check_result.get('score', 0) print(f" {check_name}: {score}/10") # 6. Get output files print("\nšŸ“„ Step 6: Generated Files") response = requests.get(f"{API_BASE}/api/output_files") if response.status_code == 200: files = response.json() print(f"āœ… {len(files)} output files available") for file_info in files[:3]: # Show latest 3 print(f" {file_info['filename']} ({file_info['size']})") print("\nšŸŽ‰ Headless analysis complete!") def simple_analysis_example(): """Simplified headless analysis""" print("\nšŸš€ Simple Headless Analysis") print("-" * 30) with open(IMAGE_PATH, 'rb') as f: files = {'file': f} data = { 'profile': 'general_key_visual', 'mode': 'json' } # Direct analysis (may take longer but simpler) response = requests.post(f"{API_BASE}/api/analyze", files=files, data=data) if response.status_code == 200: results = response.json() summary = results.get('summary', {}) print(f"āœ… Score: {summary.get('overall_score', 0)}/100") print(f" Grade: {summary.get('grade', 'Unknown')}") else: print(f"āŒ Analysis failed: {response.status_code}") def brand_detection_example(): """Example of headless brand detection""" print("\nšŸ·ļø Brand Detection Example") print("-" * 30) with open(IMAGE_PATH, 'rb') as f: files = {'file': f} response = requests.post(f"{API_BASE}/api/detect_brand", files=files) if response.status_code == 200: result = response.json() print(f"āœ… Detected Brand: {result.get('brand', 'Unknown')}") print(f" Confidence: {result.get('confidence', 0)}") else: print(f"āŒ Brand detection failed: {response.status_code}") if __name__ == "__main__": # Run examples (update IMAGE_PATH first!) try: headless_analysis_example() simple_analysis_example() brand_detection_example() except FileNotFoundError: print("āŒ Please update IMAGE_PATH with a valid image file path") except requests.exceptions.ConnectionError: print("āŒ Cannot connect to API server. Make sure it's running on localhost:7183")