File size: 23,337 Bytes
e1b1713
 
 
 
 
 
 
8257a36
e1b1713
8257a36
e1b1713
 
 
 
 
8257a36
 
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
8257a36
e1b1713
 
8257a36
 
 
 
e1b1713
8257a36
 
e1b1713
 
8257a36
e1b1713
 
8257a36
e1b1713
 
 
8257a36
 
 
e1b1713
 
8257a36
 
e1b1713
 
8257a36
e1b1713
 
8257a36
e1b1713
8257a36
e1b1713
 
 
 
8257a36
e1b1713
 
 
 
8257a36
e1b1713
 
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
8257a36
e1b1713
 
8257a36
e1b1713
 
 
 
 
 
8257a36
 
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
8257a36
 
e1b1713
 
 
 
 
 
 
 
8257a36
 
 
e1b1713
 
 
8257a36
 
 
 
 
e1b1713
 
8257a36
e1b1713
 
 
 
 
 
8257a36
e1b1713
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
8257a36
 
 
e1b1713
8257a36
 
 
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
 
e1b1713
 
 
 
 
 
 
 
8257a36
 
 
 
 
e1b1713
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
8257a36
e1b1713
8257a36
e1b1713
48e4504
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1b1713
48e4504
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48e4504
 
 
 
e1b1713
48e4504
e1b1713
 
48e4504
 
 
 
 
 
 
 
 
e1b1713
 
 
 
 
8257a36
e1b1713
8257a36
e1b1713
 
 
8257a36
e1b1713
 
 
 
 
 
8257a36
e1b1713
 
 
 
8257a36
48e4504
 
 
 
 
 
 
 
 
 
 
e1b1713
8257a36
e1b1713
 
8257a36
e1b1713
 
 
 
 
 
8257a36
e1b1713
8257a36
e1b1713
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8257a36
e1b1713
 
 
 
 
 
 
 
 
 
8257a36
 
e1b1713
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#!/usr/bin/env python3
"""
AI Model Image Evaluation Tool
A comprehensive tool for evaluating and comparing AI-generated images across multiple models.
Works both in HuggingFace Spaces and locally without internet connection.
"""

import os
import sys
import json
import numpy as np
import pandas as pd
from pathlib import Path
from typing import Dict, List, Tuple, Optional, Any
import argparse
from datetime import datetime
import warnings
warnings.filterwarnings('ignore')

# Core dependencies
try:
    from PIL import Image, ImageStat, ImageFilter, ImageEnhance
    import cv2
    from skimage import measure, filters, feature, exposure
    from skimage.metrics import structural_similarity as ssim
    import matplotlib.pyplot as plt
    import seaborn as sns
except ImportError as e:
    print(f"Missing required package: {e}")
    sys.exit(1)

# Optional dependencies for enhanced evaluation
try:
    import torch
    import torchvision.transforms as transforms
    from transformers import CLIPProcessor, CLIPModel
    CLIP_AVAILABLE = True
except ImportError:
    CLIP_AVAILABLE = False
    print("CLIP not available - aesthetic scoring will be limited")

try:
    import gradio as gr
    GRADIO_AVAILABLE = True
except ImportError:
    GRADIO_AVAILABLE = False
    print("Gradio not available - web interface disabled")

class ImageMetrics:
    """Calculate various image quality and aesthetic metrics"""
    
    def __init__(self):
        self.clip_model = None
        self.clip_processor = None
        if CLIP_AVAILABLE:
            try:
                self.clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
                self.clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
            except:
                print("Could not load CLIP model - using fallback aesthetic scoring")
    
    def calculate_sharpness(self, image: Image.Image) -> float:
        """Calculate image sharpness using Laplacian variance"""
        gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
        return cv2.Laplacian(gray, cv2.CV_64F).var()
    
    def calculate_contrast(self, image: Image.Image) -> float:
        """Calculate image contrast using RMS contrast"""
        gray = np.array(image.convert('L'))
        return gray.std()
    
    def calculate_brightness(self, image: Image.Image) -> float:
        """Calculate average brightness"""
        gray = np.array(image.convert('L'))
        return gray.mean()
    
    def calculate_saturation(self, image: Image.Image) -> float:
        """Calculate color saturation"""
        hsv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2HSV)
        return hsv[:, :, 1].mean()
    
    def calculate_colorfulness(self, image: Image.Image) -> float:
        """Calculate colorfulness metric based on Hasler and Süsstrunk"""
        img = np.array(image)
        R, G, B = img[:,:,0], img[:,:,1], img[:,:,2]
        
        rg = R - G
        yb = 0.5 * (R + G) - B
        
        std_rg = np.std(rg)
        std_yb = np.std(yb)
        mean_rg = np.mean(rg)
        mean_yb = np.mean(yb)
        
        std_root = np.sqrt(std_rg**2 + std_yb**2)
        mean_root = np.sqrt(mean_rg**2 + mean_yb**2)
        
        return std_root + 0.3 * mean_root
    
    def calculate_noise_level(self, image: Image.Image) -> float:
        """Estimate noise level using high-pass filtering"""
        gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
        kernel = np.array([[-1,-1,-1], [-1,8,-1], [-1,-1,-1]])
        filtered = cv2.filter2D(gray, -1, kernel)
        return filtered.std()
    
    def calculate_dynamic_range(self, image: Image.Image) -> float:
        """Calculate dynamic range (difference between max and min luminance)"""
        gray = np.array(image.convert('L'))
        return float(gray.max() - gray.min())
    
    def calculate_edge_density(self, image: Image.Image) -> float:
        """Calculate edge density using Canny edge detection"""
        gray = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY)
        edges = cv2.Canny(gray, 50, 150)
        return np.sum(edges > 0) / edges.size
    
    def calculate_composition_score(self, image: Image.Image) -> float:
        """Simple composition score based on rule of thirds and balance"""
        gray = np.array(image.convert('L'))
        h, w = gray.shape
        
        # Rule of thirds intersection points
        third_h, third_w = h // 3, w // 3
        intersections = [
            (third_h, third_w), (third_h, 2*third_w),
            (2*third_h, third_w), (2*third_h, 2*third_w)
        ]
        
        # Calculate interest at rule of thirds points
        interest_score = 0
        for y, x in intersections:
            region = gray[max(0, y-10):min(h, y+10), max(0, x-10):min(w, x+10)]
            if region.size > 0:
                interest_score += region.std()
        
        # Balance score (difference between left/right halves)
        left_half = gray[:, :w//2].mean()
        right_half = gray[:, w//2:].mean()
        balance_score = 1.0 / (1.0 + abs(left_half - right_half))
        
        return (interest_score / 4.0 + balance_score * 100) / 2
    
    def calculate_aesthetic_score_clip(self, image: Image.Image) -> float:
        """Calculate aesthetic score using CLIP embeddings"""
        if not self.clip_model or not self.clip_processor:
            return self.calculate_aesthetic_score_fallback(image)
        
        try:
            # Aesthetic prompts
            positive_prompts = [
                "beautiful", "aesthetic", "artistic", "high quality", "masterpiece",
                "visually appealing", "well composed", "stunning", "gorgeous"
            ]
            negative_prompts = [
                "ugly", "low quality", "blurry", "distorted", "amateur",
                "poorly composed", "unappealing", "bad", "terrible"
            ]
            
            inputs = self.clip_processor(
                text=positive_prompts + negative_prompts,
                images=image,
                return_tensors="pt",
                padding=True
            )
            
            with torch.no_grad():
                outputs = self.clip_model(**inputs)
                logits_per_image = outputs.logits_per_image
                probs = logits_per_image.softmax(dim=-1)
                
                # Average positive vs negative sentiment
                positive_score = probs[0][:len(positive_prompts)].mean().item()
                negative_score = probs[0][len(positive_prompts):].mean().item()
                
                return positive_score / (positive_score + negative_score) * 100
        
        except Exception as e:
            print(f"CLIP aesthetic scoring failed: {e}")
            return self.calculate_aesthetic_score_fallback(image)
    
    def calculate_aesthetic_score_fallback(self, image: Image.Image) -> float:
        """Fallback aesthetic score based on traditional metrics"""
        # Combine multiple metrics for aesthetic approximation
        sharpness = min(self.calculate_sharpness(image) / 1000, 1.0)
        contrast = min(self.calculate_contrast(image) / 50, 1.0)
        colorfulness = min(self.calculate_colorfulness(image) / 100, 1.0)
        composition = min(self.calculate_composition_score(image) / 100, 1.0)
        
        # Weighted combination
        aesthetic_score = (
            sharpness * 0.3 +
            contrast * 0.25 +
            colorfulness * 0.25 +
            composition * 0.2
        ) * 100
        
        return aesthetic_score
    
    def calculate_technical_quality(self, image: Image.Image) -> float:
        """Overall technical quality score"""
        sharpness = min(self.calculate_sharpness(image) / 1000, 1.0)
        contrast = min(self.calculate_contrast(image) / 50, 1.0)
        noise_penalty = max(0, 1.0 - self.calculate_noise_level(image) / 50)
        dynamic_range = min(self.calculate_dynamic_range(image) / 255, 1.0)
        
        return (sharpness * 0.4 + contrast * 0.3 + noise_penalty * 0.2 + dynamic_range * 0.1) * 100

class ModelEvaluator:
    """Main evaluation system for comparing AI model outputs"""
    
    def __init__(self):
        self.metrics_calculator = ImageMetrics()
        self.results = []
    
    def evaluate_image(self, image_path: str, model_name: str, prompt: str = "") -> Dict[str, Any]:
        """Evaluate a single image and return metrics"""
        try:
            image = Image.open(image_path).convert('RGB')
            
            metrics = {
                'model_name': model_name,
                'image_path': image_path,
                'prompt': prompt,
                'file_size_kb': os.path.getsize(image_path) / 1024,
                'resolution': f"{image.size[0]}x{image.size[1]}",
                'aspect_ratio': round(image.size[0] / image.size[1], 2),
                
                # Technical metrics
                'sharpness': round(self.metrics_calculator.calculate_sharpness(image), 2),
                'contrast': round(self.metrics_calculator.calculate_contrast(image), 2),
                'brightness': round(self.metrics_calculator.calculate_brightness(image), 2),
                'saturation': round(self.metrics_calculator.calculate_saturation(image), 2),
                'colorfulness': round(self.metrics_calculator.calculate_colorfulness(image), 2),
                'noise_level': round(self.metrics_calculator.calculate_noise_level(image), 2),
                'dynamic_range': round(self.metrics_calculator.calculate_dynamic_range(image), 2),
                'edge_density': round(self.metrics_calculator.calculate_edge_density(image), 4),
                
                # Quality scores
                'technical_quality': round(self.metrics_calculator.calculate_technical_quality(image), 2),
                'composition_score': round(self.metrics_calculator.calculate_composition_score(image), 2),
                'aesthetic_score': round(self.metrics_calculator.calculate_aesthetic_score_clip(image), 2),
                
                # Overall score (weighted combination)
                'overall_score': 0.0
            }
            
            # Calculate overall score
            metrics['overall_score'] = round(
                metrics['technical_quality'] * 0.4 +
                metrics['aesthetic_score'] * 0.4 +
                metrics['composition_score'] * 0.2, 2
            )
            
            return metrics
            
        except Exception as e:
            print(f"Error evaluating {image_path}: {e}")
            return None
    
    def evaluate_batch(self, image_paths: List[str], model_names: List[str], 
                      prompts: List[str] = None) -> pd.DataFrame:
        """Evaluate multiple images and return comparison DataFrame"""
        if prompts is None:
            prompts = [""] * len(image_paths)
        
        self.results = []
        for i, (img_path, model_name) in enumerate(zip(image_paths, model_names)):
            prompt = prompts[i] if i < len(prompts) else ""
            result = self.evaluate_image(img_path, model_name, prompt)
            if result:
                self.results.append(result)
        
        return pd.DataFrame(self.results)
    
    def create_comparison_report(self, df: pd.DataFrame, output_path: str = None) -> str:
        """Create a detailed comparison report"""
        if df.empty:
            return "No valid results to compare."
        
        report = []
        report.append("# AI Model Image Evaluation Report")
        report.append(f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
        report.append(f"Total images evaluated: {len(df)}")
        report.append("")
        
        # Summary statistics
        report.append("## Summary Statistics")
        numeric_cols = df.select_dtypes(include=[np.number]).columns
        summary = df[numeric_cols].describe()
        report.append(summary.to_string())
        report.append("")
        
        # Model rankings
        report.append("## Model Rankings by Overall Score")
        model_scores = df.groupby('model_name')['overall_score'].agg(['mean', 'std', 'count'])
        model_scores = model_scores.sort_values('mean', ascending=False)
        report.append(model_scores.to_string())
        report.append("")
        
        # Best performing models by category
        categories = ['technical_quality', 'aesthetic_score', 'composition_score', 'sharpness', 'colorfulness']
        report.append("## Best Performing Models by Category")
        for category in categories:
            if category in df.columns:
                best_model = df.loc[df[category].idxmax()]
                report.append(f"**{category.replace('_', ' ').title()}**: {best_model['model_name']} ({best_model[category]:.2f})")
        report.append("")
        
        # Detailed comparison table
        report.append("## Detailed Results")
        comparison_cols = ['model_name', 'overall_score', 'technical_quality', 'aesthetic_score', 
                          'composition_score', 'sharpness', 'contrast', 'colorfulness']
        comparison_df = df[comparison_cols].round(2)
        report.append(comparison_df.to_string(index=False))
        
        report_text = "\n".join(report)
        
        if output_path:
            with open(output_path, 'w') as f:
                f.write(report_text)
            print(f"Report saved to {output_path}")
        
        return report_text
    
    def create_visualization(self, df: pd.DataFrame, output_dir: str = "."):
        """Create visualization plots for the comparison"""
        if df.empty:
            print("No data to visualize")
            return
        
        plt.style.use('seaborn-v0_8')
        fig, axes = plt.subplots(2, 2, figsize=(15, 12))
        
        # Overall score comparison
        model_scores = df.groupby('model_name')['overall_score'].mean().sort_values(ascending=True)
        axes[0, 0].barh(model_scores.index, model_scores.values)
        axes[0, 0].set_title('Overall Score by Model')
        axes[0, 0].set_xlabel('Score')
        
        # Quality metrics radar chart data prep
        metrics_cols = ['technical_quality', 'aesthetic_score', 'composition_score']
        model_means = df.groupby('model_name')[metrics_cols].mean()
        
        # Scatter plot: Technical vs Aesthetic
        for model in df['model_name'].unique():
            model_data = df[df['model_name'] == model]
            axes[0, 1].scatter(model_data['technical_quality'], model_data['aesthetic_score'], 
                             label=model, alpha=0.7, s=60)
        axes[0, 1].set_xlabel('Technical Quality')
        axes[0, 1].set_ylabel('Aesthetic Score')
        axes[0, 1].set_title('Technical vs Aesthetic Quality')
        axes[0, 1].legend()
        
        # Distribution of overall scores
        for model in df['model_name'].unique():
            model_data = df[df['model_name'] == model]['overall_score']
            axes[1, 0].hist(model_data, alpha=0.6, label=model, bins=10)
        axes[1, 0].set_xlabel('Overall Score')
        axes[1, 0].set_ylabel('Frequency')
        axes[1, 0].set_title('Distribution of Overall Scores')
        axes[1, 0].legend()
        
        # Correlation heatmap of metrics
        numeric_cols = df.select_dtypes(include=[np.number]).columns
        correlation_matrix = df[numeric_cols].corr()
        sns.heatmap(correlation_matrix, ax=axes[1, 1], cmap='coolwarm', center=0, 
                   square=True, cbar_kws={'shrink': 0.8})
        axes[1, 1].set_title('Metrics Correlation Heatmap')
        
        plt.tight_layout()
        plot_path = os.path.join(output_dir, 'model_comparison.png')
        plt.savefig(plot_path, dpi=300, bbox_inches='tight')
        plt.close()
        
        print(f"Visualization saved to {plot_path}")

def create_gradio_interface():
    """Create Gradio web interface"""
    if not GRADIO_AVAILABLE:
        print("Gradio not available - cannot create web interface")
        return None
    
    evaluator = ModelEvaluator()
    
    def evaluate_images(images, model_names, prompts=""):
        try:
            if not images or not model_names.strip():
                return "Please provide images and model names", None, None
            
            # Parse model names and prompts
            model_list = [name.strip() for name in model_names.split(',') if name.strip()]
            prompt_list = [p.strip() for p in prompts.split('\n')] if prompts else [""] * len(images)
            
            # Save uploaded images temporarily and evaluate
            image_paths = []
            for i, img in enumerate(images):
                temp_path = f"temp_image_{i}.png"
                img.save(temp_path)
                image_paths.append(temp_path)
            
            # Ensure we have the right number of model names
            if len(model_list) == 1 and len(images) > 1:
                model_list = model_list * len(images)
            elif len(model_list) != len(images):
                return f"Number of model names ({len(model_list)}) must match number of images ({len(images)})", None, None
            
            # Evaluate images
            df = evaluator.evaluate_batch(image_paths, model_list, prompt_list)
            
            if df.empty:
                return "No images could be evaluated", None, None
            
            # Create report and visualization
            report = evaluator.create_comparison_report(df)
            evaluator.create_visualization(df, ".")
            
            # Clean up temp files
            for path in image_paths:
                try:
                    os.remove(path)
                except:
                    pass
            
            return report, df, "model_comparison.png"
        
        except Exception as e:
            return f"Error during evaluation: {str(e)}", None, None
    
    # Create interface
    with gr.Blocks(title="AI Model Image Evaluator") as interface:
        gr.Markdown("# AI Model Image Evaluation Tool")
        gr.Markdown("Upload images from different AI models to compare their quality, aesthetics, and technical metrics.")
        
        with gr.Row():
            with gr.Column():
                images_input = gr.File(file_count="multiple", file_types=["image"], label="Upload Images")
                model_names_input = gr.Textbox(
                    label="Model Names", 
                    placeholder="model1, model2, model3 (comma-separated)",
                    info="If you provide one name for multiple images, it will be applied to all"
                )
                prompts_input = gr.Textbox(
                    label="Prompts (Optional)", 
                    placeholder="One prompt per line",
                    lines=3
                )
                evaluate_btn = gr.Button("Evaluate Images", variant="primary")
        
        with gr.Tabs():
            with gr.TabItem("Report"):
                report_output = gr.Textbox(label="Evaluation Report", lines=25, max_lines=50)
            with gr.TabItem("Data Table"):
                dataframe_output = gr.Dataframe(label="Detailed Results")
            with gr.TabItem("Visualization"):
                plot_output = gr.Image(label="Comparison Visualization")
        
        # Examples
        gr.Examples(
            examples=[
                [[], "DALL-E, Midjourney, Stable Diffusion", "beautiful landscape\nportrait of a cat\nabstract art"],
                [[], "model_v1, model_v2", ""],
            ],
            inputs=[images_input, model_names_input, prompts_input],
        )
        
        evaluate_btn.click(
            evaluate_images,
            inputs=[images_input, model_names_input, prompts_input],
            outputs=[report_output, dataframe_output, plot_output]
        )
    
    return interface

def main():
    parser = argparse.ArgumentParser(description="AI Model Image Evaluation Tool")
    subparsers = parser.add_subparsers(dest='command', help='Available commands')
    
    # CLI evaluation command
    eval_parser = subparsers.add_parser('evaluate', help='Evaluate images from command line')
    eval_parser.add_argument('--images', nargs='+', required=True, help='Paths to images')
    eval_parser.add_argument('--models', nargs='+', required=True, help='Model names')
    eval_parser.add_argument('--prompts', nargs='*', help='Prompts used (optional)')
    eval_parser.add_argument('--output', help='Output directory for results')
    
    # Web interface command
    web_parser = subparsers.add_parser('web', help='Launch web interface')
    web_parser.add_argument('--port', type=int, default=7860, help='Port for web interface')
    web_parser.add_argument('--share', action='store_true', help='Create public link')
    
    # Check if running in HuggingFace Spaces or no args provided
    if len(sys.argv) == 1 or os.getenv('SPACE_ID'):
        # Auto-launch web interface for HF Spaces or when no args
        print("Auto-launching web interface...")
        interface = create_gradio_interface()
        if interface:
            interface.launch(server_name="0.0.0.0", server_port=7860)
        else:
            print("Web interface not available - Gradio not installed")
        return
    
    args = parser.parse_args()
    
    if args.command == 'evaluate':
        evaluator = ModelEvaluator()
        
        # Validate inputs
        if len(args.models) == 1 and len(args.images) > 1:
            args.models = args.models * len(args.images)
        elif len(args.models) != len(args.images):
            print(f"Error: Number of models ({len(args.models)}) must match number of images ({len(args.images)})")
            return
        
        prompts = args.prompts if args.prompts else [""] * len(args.images)
        
        # Evaluate images
        print("Evaluating images...")
        df = evaluator.evaluate_batch(args.images, args.models, prompts)
        
        if df.empty:
            print("No images could be evaluated")
            return
        
        # Create output directory
        output_dir = args.output or "evaluation_results"
        os.makedirs(output_dir, exist_ok=True)
        
        # Generate report and visualization
        report_path = os.path.join(output_dir, "evaluation_report.txt")
        csv_path = os.path.join(output_dir, "detailed_results.csv")
        
        report = evaluator.create_comparison_report(df, report_path)
        df.to_csv(csv_path, index=False)
        evaluator.create_visualization(df, output_dir)
        
        print(f"Results saved to {output_dir}/")
        print("\nTop 3 Models by Overall Score:")
        top_models = df.nlargest(3, 'overall_score')[['model_name', 'overall_score']]
        print(top_models.to_string(index=False))
    
    elif args.command == 'web':
        interface = create_gradio_interface()
        if interface:
            print(f"Launching web interface on port {args.port}...")
            interface.launch(server_port=args.port, share=args.share)
        else:
            print("Web interface not available - Gradio not installed")
    
    else:
        parser.print_help()

if __name__ == "__main__":
    main()