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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# A/B Test Predictor API Documentation

Welcome! This API predicts A/B test outcomes using multimodal AI analysis of your control and variant images combined with business context data.

## πŸš€ Quick Start

### Installation

```bash
pip install gradio-client pillow
```

### Basic Example

```python
from gradio_client import Client

# Connect to the API
client = Client("SpiralyzeLLC/ABTestPredictor")

# Make a prediction
result = client.predict(
    "path/to/control.jpg",
    "path/to/variant.jpg",
    "SaaS",                    # Business Model
    "B2B",                     # Customer Type
    "High-Intent Lead Gen",    # Conversion Type
    "B2B Software & Tech",     # Industry
    "Awareness & Discovery",   # Page Type
    api_name="/predict_with_categorical_data"
)

print(f"Win Probability: {result['predictionResults']['probability']}")
print(f"Confidence: {result['predictionResults']['modelConfidence']}%")
```

## πŸ“‹ API Reference

### Endpoint

```
/predict_with_categorical_data
```

### Input Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| `control_image` | Image File | Yes | Control version image (JPG, PNG) |
| `variant_image` | Image File | Yes | Variant version image (JPG, PNG) |
| `business_model` | String | Yes | Business model type |
| `customer_type` | String | Yes | Target customer type |
| `conversion_type` | String | Yes | Type of conversion |
| `industry` | String | Yes | Business industry |
| `page_type` | String | Yes | Type of page being tested |

### Valid Category Values

#### Business Model
- `E-Commerce`
- `Lead Generation`
- `Other*`
- `SaaS`

#### Customer Type
- `B2B`
- `B2C`
- `Both`
- `Other*`

#### Conversion Type

**🎯 New Feature**: The API now accepts both **parent group values** (listed below) AND **specific values** (e.g., "Request Demo/Contact Sales"). Specific values are automatically converted to their parent groups.

**Parent Groups** (model uses these internally):
- `Direct Purchase`
- `High-Intent Lead Gen`
- `Info/Content Lead Gen`
- `Location Search`
- `Non-Profit/Community`
- `Other Conversion`

**Example Specific Values** (automatically mapped):
- `Request Demo/Contact Sales` β†’ `High-Intent Lead Gen`
- `Buy Now` β†’ `Direct Purchase`
- `Download Asset / App` β†’ `Info/Content Lead Gen`
- See `mapping.json` for complete list

#### Industry

**🎯 New Feature**: The API now accepts both **parent group values** (listed below) AND **specific values** (e.g., "Accounting Services"). Specific values are automatically converted to their parent groups.

**Parent Groups** (model uses these internally):
- `Automotive & Transportation`
- `B2B Services`
- `B2B Software & Tech`
- `Consumer Services`
- `Consumer Software & Apps`
- `Education`
- `Finance, Insurance & Real Estate`
- `Food, Hospitality & Travel`
- `Health & Wellness`
- `Industrial & Manufacturing`
- `Media & Entertainment`
- `Non-Profit & Government`
- `Other`
- `Retail & E-commerce`

**Example Specific Values** (automatically mapped):
- `Accounting Services` β†’ `B2B Services`
- `Marketing Agency` β†’ `B2B Services`
- `Cybersecurity` β†’ `B2B Software & Tech`
- `Healthcare Software` β†’ `B2B Software & Tech`
- See `mapping.json` for complete list (200+ specific industries supported)

#### Page Type

**🎯 New Feature**: The API now accepts both **parent group values** (listed below) AND **specific values** (e.g., "Homepage"). Specific values are automatically converted to their parent groups.

**Parent Groups** (model uses these internally):
- `Awareness & Discovery`
- `Consideration & Evaluation`
- `Conversion`
- `Internal & Navigation`
- `Post-Conversion & Other`

**Example Specific Values** (automatically mapped):
- `Homepage` β†’ `Awareness & Discovery`
- `Pricing Page` β†’ `Consideration & Evaluation`
- `Checkout` β†’ `Conversion`
- `Login` β†’ `Internal & Navigation`
- See `mapping.json` for complete list

### Response Format

```json
{
  "predictionResults": {
    "probability": "0.682",
    "modelConfidence": "66.1",
    "trainingDataSamples": 14634,
    "totalPredictions": 1626,
    "correctPredictions": 1074,
    "totalWinPrediction": 667,
    "totalLosePrediction": 959
  },
  "providedCategories": {
    "businessModel": "SaaS",
    "customerType": "B2B",
    "conversionType": "Request Demo/Contact Sales",
    "industry": "Cybersecurity",
    "pageType": "Homepage"
  },
  "groupedCategories": {
    "businessModel": "SaaS",
    "customerType": "B2B",
    "conversionType": "High-Intent Lead Gen",
    "industry": "B2B Software & Tech",
    "pageType": "Awareness & Discovery"
  },
  "processingInfo": {
    "totalProcessingTime": "2.34s",
    "confidenceSource": "B2B Software & Tech | Awareness & Discovery"
  }
}
```

#### Response Fields

**predictionResults**
- `probability`: Win probability for variant (0-1 scale, >0.5 means variant wins)
- `modelConfidence`: Model accuracy percentage for this category combination
- `trainingDataSamples`: Training samples used for this category
- `totalPredictions`: Total test predictions for this category
- `correctPredictions`: Correct predictions for this category
- `totalWinPrediction`: Actual wins in historical data
- `totalLosePrediction`: Actual losses in historical data

**providedCategories**
- Echo of input categorical data (as provided by you)

**groupedCategories** *(New!)*
- The parent group values used by the model for prediction
- Shows how specific values were mapped to parent groups
- This is what the model actually uses internally

**processingInfo**
- `totalProcessingTime`: API processing time
- `confidenceSource`: Category combination used for confidence scoring

## πŸ”„ Automatic Value Mapping

### How It Works

The API now automatically converts specific categorical values to their parent groups using the `mapping.json` file. This means you can send more specific data (e.g., "Accounting Services", "Homepage", "Request Demo/Contact Sales") and the API will handle the conversion automatically.

**Benefits:**
- βœ… More flexibility in input data
- βœ… No need to pre-process your data
- βœ… API handles the conversion automatically
- βœ… Both original and grouped values returned in response

### Mapping Examples

#### Industry Mapping
```
"Accounting Services" β†’ "B2B Services"
"Marketing Agency" β†’ "B2B Services"
"Cybersecurity" β†’ "B2B Software & Tech"
"CRM Software" β†’ "B2B Software & Tech"
"Healthcare" β†’ "Health & Wellness"
"Hotels" β†’ "Food, Hospitality & Travel"
```

#### Page Type Mapping
```
"Homepage" β†’ "Awareness & Discovery"
"Blog / Content" β†’ "Awareness & Discovery"
"Pricing Page" β†’ "Consideration & Evaluation"
"Demo Squeeze" β†’ "Consideration & Evaluation"
"Checkout" β†’ "Conversion"
"Contact Sales" β†’ "Conversion"
"Login" β†’ "Internal & Navigation"
```

#### Conversion Type Mapping
```
"Request Demo/Contact Sales" β†’ "High-Intent Lead Gen"
"Start Free Trial/Signup" β†’ "High-Intent Lead Gen"
"Buy Now" β†’ "Direct Purchase"
"Subscription (No Free Trial)" β†’ "Direct Purchase"
"Download Asset / App" β†’ "Info/Content Lead Gen"
"Register for Webinar/Event" β†’ "Info/Content Lead Gen"
```

### Using Specific Values in API Calls

**Example with specific values:**
```python
result = client.predict(
    "control.jpg",
    "variant.jpg",
    "SaaS",                           # Business Model (no mapping needed)
    "B2B",                            # Customer Type (no mapping needed)
    "Request Demo/Contact Sales",     # Will be mapped to "High-Intent Lead Gen"
    "Cybersecurity",                  # Will be mapped to "B2B Software & Tech"
    "Homepage",                       # Will be mapped to "Awareness & Discovery"
    api_name="/predict_with_categorical_data"
)
```

**Response will include both:**
```json
{
  "providedCategories": {
    "conversionType": "Request Demo/Contact Sales",
    "industry": "Cybersecurity",
    "pageType": "Homepage"
  },
  "groupedCategories": {
    "conversionType": "High-Intent Lead Gen",
    "industry": "B2B Software & Tech",
    "pageType": "Awareness & Discovery"
  }
}
```

## πŸ’» Code Examples

### Python

#### Complete Example with Error Handling

```python
from gradio_client import Client
from PIL import Image
import json

def predict_abtest(control_path, variant_path, categories):
    """
    Predict A/B test outcome
    
    Args:
        control_path: Path to control image
        variant_path: Path to variant image
        categories: Dict with business_model, customer_type, etc.
    
    Returns:
        dict: Prediction results
    """
    try:
        # Initialize client
        client = Client("SpiralyzeLLC/ABTestPredictor")
        
        # Make prediction
        result = client.predict(
            control_path,
            variant_path,
            categories['business_model'],
            categories['customer_type'],
            categories['conversion_type'],
            categories['industry'],
            categories['page_type'],
            api_name="/predict_with_categorical_data"
        )
        
        return {
            'success': True,
            'data': result
        }
        
    except Exception as e:
        return {
            'success': False,
            'error': str(e)
        }

# Usage
categories = {
    'business_model': 'SaaS',
    'customer_type': 'B2B',
    'conversion_type': 'High-Intent Lead Gen',
    'industry': 'B2B Software & Tech',
    'page_type': 'Awareness & Discovery'
}

result = predict_abtest('control.jpg', 'variant.jpg', categories)

if result['success']:
    prob = result['data']['predictionResults']['probability']
    conf = result['data']['predictionResults']['modelConfidence']
    
    print(f"βœ… Prediction: {prob}")
    print(f"πŸ“Š Confidence: {conf}%")
    
    if float(prob) > 0.5:
        print("πŸŽ‰ Variant is predicted to WIN")
    else:
        print("⚠️ Control is predicted to win")
else:
    print(f"❌ Error: {result['error']}")
```

#### Batch Processing Multiple Tests

```python
from gradio_client import Client
import time

def batch_predict(test_cases):
    """Process multiple A/B tests"""
    client = Client("SpiralyzeLLC/ABTestPredictor")
    results = []
    
    for i, test in enumerate(test_cases):
        print(f"Processing test {i+1}/{len(test_cases)}...")
        
        try:
            result = client.predict(
                test['control_image'],
                test['variant_image'],
                test['business_model'],
                test['customer_type'],
                test['conversion_type'],
                test['industry'],
                test['page_type'],
                api_name="/predict_with_categorical_data"
            )
            
            results.append({
                'test_id': i + 1,
                'status': 'success',
                'result': result
            })
            
        except Exception as e:
            results.append({
                'test_id': i + 1,
                'status': 'failed',
                'error': str(e)
            })
        
        # Rate limiting
        time.sleep(1)
    
    return results

# Usage
tests = [
    {
        'control_image': 'test1_control.jpg',
        'variant_image': 'test1_variant.jpg',
        'business_model': 'SaaS',
        'customer_type': 'B2B',
        'conversion_type': 'High-Intent Lead Gen',
        'industry': 'B2B Software & Tech',
        'page_type': 'Awareness & Discovery'
    },
    # Add more tests...
]

results = batch_predict(tests)
print(f"Completed {len(results)} predictions")
```

### JavaScript/Node.js

```javascript
const axios = require('axios');
const fs = require('fs');

async function predictABTest(controlPath, variantPath, categories) {
    const apiUrl = 'https://spiralyzellc-abtestpredictor.hf.space/api/predict';
    
    // Read and encode images
    const controlImage = fs.readFileSync(controlPath);
    const variantImage = fs.readFileSync(variantPath);
    
    const controlB64 = `data:image/jpeg;base64,${controlImage.toString('base64')}`;
    const variantB64 = `data:image/jpeg;base64,${variantImage.toString('base64')}`;
    
    // Prepare payload
    const payload = {
        data: [
            controlB64,
            variantB64,
            categories.businessModel,
            categories.customerType,
            categories.conversionType,
            categories.industry,
            categories.pageType
        ]
    };
    
    try {
        const response = await axios.post(apiUrl, payload, {
            headers: { 'Content-Type': 'application/json' },
            timeout: 30000
        });
        
        return {
            success: true,
            data: response.data.data[0]
        };
    } catch (error) {
        return {
            success: false,
            error: error.message
        };
    }
}

// Usage
const categories = {
    businessModel: 'SaaS',
    customerType: 'B2B',
    conversionType: 'High-Intent Lead Gen',
    industry: 'B2B Software & Tech',
    pageType: 'Awareness & Discovery'
};

predictABTest('control.jpg', 'variant.jpg', categories)
    .then(result => {
        if (result.success) {
            console.log('Win Probability:', result.data.predictionResults.probability);
            console.log('Confidence:', result.data.predictionResults.modelConfidence + '%');
        } else {
            console.error('Error:', result.error);
        }
    });
```

### cURL

```bash
#!/bin/bash

# Encode images to base64
CONTROL_B64=$(base64 -i control.jpg)
VARIANT_B64=$(base64 -i variant.jpg)

# Make API request
curl -X POST "https://spiralyzellc-abtestpredictor.hf.space/api/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "data": [
      "data:image/jpeg;base64,'"${CONTROL_B64}"'",
      "data:image/jpeg;base64,'"${VARIANT_B64}"'",
      "SaaS",
      "B2B",
      "High-Intent Lead Gen",
      "B2B Software & Tech",
      "Awareness & Discovery"
    ]
  }' | jq '.'
```

## πŸ”§ Best Practices

### Image Requirements

- **Format**: JPG, PNG, or JPEG
- **Size**: Recommended < 5MB per image
- **Resolution**: Minimum 800x600px recommended
- **Content**: Should show the actual page/element being tested

### Rate Limiting

- **Recommended**: 1 request per second
- **Maximum**: No hard limit, but please be respectful
- Add delays between batch requests

### Error Handling

Always wrap API calls in try-catch blocks:

```python
try:
    result = client.predict(...)
except Exception as e:
    # Handle timeout, network errors, invalid inputs
    print(f"API Error: {e}")
```

### Interpreting Results

**Win Probability**
- `> 0.5`: Variant predicted to win
- `< 0.5`: Control predicted to win
- `~ 0.5`: Too close to call

**Model Confidence**
- `> 70%`: High confidence
- `60-70%`: Moderate confidence
- `< 60%`: Lower confidence (less historical data for this category)

**Training Data Samples**
- `> 1000`: Robust category with lots of data
- `100-1000`: Moderate data available
- `< 100`: Limited data, prediction may be less reliable

## πŸ› Common Issues

### Issue: "Connection timeout"
**Solution**: Increase timeout to 30-60 seconds for first request (model loads)

```python
client = Client("SpiralyzeLLC/ABTestPredictor")
# First call might be slower due to model loading
```

### Issue: "Invalid category value"
**Solution**: Ensure exact match with valid values (case-sensitive)

```python
# βœ… Correct
business_model = "SaaS"

# ❌ Wrong
business_model = "saas"  # Wrong case
business_model = "Software as a Service"  # Not in valid list
```

### Issue: "Image file not found"
**Solution**: Use absolute paths or verify file exists

```python
import os

control_path = os.path.abspath("control.jpg")
if not os.path.exists(control_path):
    print(f"File not found: {control_path}")
```

## πŸ“Š Understanding Confidence Scores

Confidence scores are based on **Industry + Page Type** combinations from historical A/B test data:

- **High Sample Count** (>1000 samples): More reliable predictions
- **Low Sample Count** (<100 samples): Less reliable, use with caution
- **Accuracy**: Historical accuracy for this specific category combination

The model has been trained on thousands of real A/B tests, so confidence scores reflect actual performance on similar tests.

## 🌐 Web Interface

You can also use the web interface:

**URL**: https://huggingface.co/spaces/SpiralyzeLLC/ABTestPredictor

1. Upload control and variant images
2. Select categories from dropdowns
3. Click "Predict"
4. View results in JSON format

## πŸ“ Example Use Cases

### 1. Pre-Test Analysis
Predict outcome before running expensive A/B tests

### 2. Batch Analysis
Analyze multiple test variations to pick the best candidate

### 3. Historical Analysis
Compare predictions with actual results to calibrate decisions

### 4. Integration
Embed in your testing platform or analytics dashboard

## πŸ†˜ Support

**Issues?**
- Check the [Common Issues](#-common-issues) section
- Verify your inputs match the [Valid Category Values](#valid-category-values)
- Ensure images are valid and readable

**Questions?**
- Open an issue on the Hugging Face Space
- Check the example code for reference implementations

## πŸ“œ License

This API is provided for research and commercial use. Please use responsibly.

---

**API Version**: 1.0  
**Last Updated**: October 31, 2025  
**Model**: Multimodal Siamese Network (GGG Enhanced)  
**Hosted on**: Hugging Face Spaces