File size: 8,922 Bytes
05bf721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84c9af7
05bf721
87dad1f
 
 
 
84c9af7
87dad1f
05bf721
84c9af7
 
 
 
05bf721
84c9af7
05bf721
84c9af7
05bf721
 
 
84c9af7
 
 
 
 
 
05bf721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87dad1f
 
 
 
 
 
05bf721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87dad1f
 
 
 
 
84c9af7
87dad1f
05bf721
 
84c9af7
 
 
05bf721
84c9af7
05bf721
 
 
 
 
 
84c9af7
 
 
 
 
05bf721
 
 
 
 
 
 
87dad1f
 
 
 
84c9af7
87dad1f
05bf721
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87dad1f
 
84c9af7
87dad1f
 
 
 
84c9af7
 
05bf721
 
 
 
 
 
 
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
# A/B Test Predictor API - Integration Guide for Backend

## API Endpoint
```
https://nitish-spz-abtestpredictorv2.hf.space
```

## Available Endpoints

### 1. Auto-Categorization Prediction
**Endpoint:** `/call/predict_with_auto_categorization`
**Method:** POST
**Description:** Automatically categorizes images and predicts A/B test winner

**Request Format:**
```json
{
  "data": [
    {
      "path": "uploaded_control_image_path",
      "url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_control_image_path",
      "orig_name": "control.jpg",
      "size": 123456,
      "mime_type": "image/jpeg",
      "meta": { "_type": "gradio.FileData" }
    },
    {
      "path": "uploaded_variant_image_path", 
      "url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_variant_image_path",
      "orig_name": "variant.jpg",
      "size": 789012,
      "mime_type": "image/jpeg",
      "meta": { "_type": "gradio.FileData" }
    }
  ]
}
```

**Response Format:**
```json
{
  "predictionResults": {
    "probability": "0.041",
    "modelConfidence": "75.5%",
    "trainingDataSamples": 477,
    "totalPredictions": 53,
    "correctPredictions": 40,
    "totalWinPrediction": 24,
    "totalLosePrediction": 29
  },
  "autoDetectedCategories": {
    "businessModel": "SaaS",
    "customerType": "B2B", 
    "conversionType": "High-Intent Lead Gen",
    "industry": "B2B Software & Tech",
    "pageType": "Conversion"
  },
  "detectedPattern": {
    "pattern": "Double Column Form",
    "description": "The variant implements a 'Double Column Form' modification"
  },
  "processingInfo": {
    "totalProcessingTime": "32.36s",
    "aiCategorization": "Perplexity Sonar Reasoning Pro",
    "patternDetection": "Gemini Pro Vision", 
    "confidenceSource": "B2B Software & Tech | Conversion",
    "totalPatternsAnalyzed": 359
  }
}
```

### 2. Manual Categorization Prediction
**Endpoint:** `/call/predict_single`
**Method:** POST
**Description:** Predicts A/B test winner with manually provided categories

**Request Format:**
```json
{
  "data": [
    {
      "path": "uploaded_control_image_path",
      "url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_control_image_path",
      "orig_name": "control.jpg",
      "size": 123456,
      "mime_type": "image/jpeg",
      "meta": { "_type": "gradio.FileData" }
    },
    {
      "path": "uploaded_variant_image_path",
      "url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_variant_image_path", 
      "orig_name": "variant.jpg",
      "size": 789012,
      "mime_type": "image/jpeg",
      "meta": { "_type": "gradio.FileData" }
    },
    "E-Commerce",
    "B2C", 
    "Direct Purchase",
    "Retail & E-commerce",
    "Conversion"
  ]
}
```

**Response Format:**
```json
{
  "probability": "0.987",
  "modelConfidence": "82.3%",
  "trainingDataSamples": 1205,
  "totalPredictions": 120,
  "correctPredictions": 98,
  "totalWinsPrediction": 67,
  "totalLosePrediction": 53
}
```

## File Upload Process

### Step 1: Upload Images
**Endpoint:** `/upload`
**Method:** POST
**Content-Type:** multipart/form-data

```python
import requests

# Upload control image
with open('control.jpg', 'rb') as f:
    files = {'files': f}
    response = requests.post('https://nitish-spz-abtestpredictorv2.hf.space/upload', files=files)
    control_path = response.json()[0]

# Upload variant image  
with open('variant.jpg', 'rb') as f:
    files = {'files': f}
    response = requests.post('https://nitish-spz-abtestpredictorv2.hf.space/upload', files=files)
    variant_path = response.json()[0]
```

### Step 2: Make Prediction Request
```python
import requests

# Prepare FileData objects
control_file_data = {
    "path": control_path,
    "url": f"https://nitish-spz-abtestpredictorv2.hf.space/file={control_path}",
    "orig_name": "control.jpg",
    "size": 123456,
    "mime_type": "image/jpeg",
    "meta": { "_type": "gradio.FileData" }
}

variant_file_data = {
    "path": variant_path,
    "url": f"https://nitish-spz-abtestpredictorv2.hf.space/file={variant_path}",
    "orig_name": "variant.jpg", 
    "size": 789012,
    "mime_type": "image/jpeg",
    "meta": { "_type": "gradio.FileData" }
}

# Make prediction request
response = requests.post(
    'https://nitish-spz-abtestpredictorv2.hf.space/call/predict_with_auto_categorization',
    json={"data": [control_file_data, variant_file_data]}
)

result = response.json()
```

## Response Field Descriptions

### Prediction Results
- **probability**: Float between 0-1 (probability of variant winning). Values > 0.5 mean variant wins, < 0.5 mean control wins
- **modelConfidence**: Percentage confidence based on historical data
- **trainingDataSamples**: Number of training samples used for confidence calculation
- **totalPredictions**: Total number of historical predictions in this category
- **correctPredictions**: Number of correct predictions in historical data
- **totalWinPrediction**: Number of times variant won in historical data
- **totalLosePrediction**: Number of times control won in historical data

### Auto-Detected Categories
- **businessModel**: "E-Commerce", "SaaS", "Lead Generation", "Other*"
- **customerType**: "B2B", "B2C", "Both", "Other*"
- **conversionType**: "Direct Purchase", "High-Intent Lead Gen", "Info/Content Lead Gen", "Location Search", "Non-Profit/Community", "Other Conversion"
- **industry**: 14 categories including "B2B Software & Tech", "Retail & E-commerce", etc.
- **pageType**: "Awareness & Discovery", "Consideration & Evaluation", "Conversion", "Internal & Navigation", "Post-Conversion & Other"

### Detected Pattern
- **pattern**: A/B test pattern name (e.g., "Button", "Form Over UI", "Hero Changes")
- **description**: Human-readable description of the pattern

### Processing Info
- **totalProcessingTime**: Time taken for prediction (typically 20-40 seconds)
- **aiCategorization**: AI service used for categorization
- **patternDetection**: AI service used for pattern detection
- **confidenceSource**: Data source for confidence scores
- **totalPatternsAnalyzed**: Number of patterns in the detection database

## Error Handling

### Common Error Responses
```json
{
  "error": "Prediction failed: [error message]",
  "modelConfidence": "50.0%",
  "trainingDataSamples": 0,
  "totalPredictions": 0,
  "correctPredictions": 0,
  "totalWinPrediction": 0,
  "totalLosePrediction": 0
}
```

### HTTP Status Codes
- **200**: Success
- **400**: Bad request (invalid parameters)
- **500**: Internal server error
- **503**: Service unavailable (GPU quota exceeded)

## Rate Limits
- Free tier: Limited concurrent requests
- Processing time: 20-40 seconds per prediction
- GPU duration: 50-60 seconds maximum

## Example Integration (Python)

```python
import requests
import time

class ABTestPredictor:
    def __init__(self):
        self.base_url = "https://nitish-spz-abtestpredictorv2.hf.space"
    
    def predict(self, control_image_path, variant_image_path):
        # Step 1: Upload images
        control_path = self._upload_image(control_image_path)
        variant_path = self._upload_image(variant_image_path)
        
        # Step 2: Make prediction
        return self._make_prediction(control_path, variant_path)
    
    def _upload_image(self, image_path):
        with open(image_path, 'rb') as f:
            files = {'files': f}
            response = requests.post(f"{self.base_url}/upload", files=files)
            return response.json()[0]
    
    def _make_prediction(self, control_path, variant_path):
        control_data = self._create_file_data(control_path, "control.jpg")
        variant_data = self._create_file_data(variant_path, "variant.jpg")
        
        response = requests.post(
            f"{self.base_url}/call/predict_with_auto_categorization",
            json={"data": [control_data, variant_data]}
        )
        
        return response.json()
    
    def _create_file_data(self, path, orig_name):
        return {
            "path": path,
            "url": f"{self.base_url}/file={path}",
            "orig_name": orig_name,
            "size": 0,  # Optional
            "mime_type": "image/jpeg",
            "meta": { "_type": "gradio.FileData" }
        }

# Usage
predictor = ABTestPredictor()
result = predictor.predict("control.jpg", "variant.jpg")

# Get prediction results
prob = float(result['predictionResults']['probability'])
winner = "VARIANT WINS" if prob > 0.5 else "CONTROL WINS"

print(f"Winner: {winner}")
print(f"Probability: {prob}")
print(f"Confidence: {result['predictionResults']['modelConfidence']}")
print(f"Accuracy: {result['predictionResults']['correctPredictions']}/{result['predictionResults']['totalPredictions']}")
```

## Notes
- Images are automatically deleted after processing
- Maximum image size: 10MB
- Supported formats: JPEG, PNG, WebP
- Processing time varies based on image complexity and server load