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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# A/B Test Predictor API - Integration Guide for Backend
|
| 2 |
+
|
| 3 |
+
## API Endpoint
|
| 4 |
+
```
|
| 5 |
+
https://nitish-spz-abtestpredictorv2.hf.space
|
| 6 |
+
```
|
| 7 |
+
|
| 8 |
+
## Available Endpoints
|
| 9 |
+
|
| 10 |
+
### 1. Auto-Categorization Prediction
|
| 11 |
+
**Endpoint:** `/call/predict_with_auto_categorization`
|
| 12 |
+
**Method:** POST
|
| 13 |
+
**Description:** Automatically categorizes images and predicts A/B test winner
|
| 14 |
+
|
| 15 |
+
**Request Format:**
|
| 16 |
+
```json
|
| 17 |
+
{
|
| 18 |
+
"data": [
|
| 19 |
+
{
|
| 20 |
+
"path": "uploaded_control_image_path",
|
| 21 |
+
"url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_control_image_path",
|
| 22 |
+
"orig_name": "control.jpg",
|
| 23 |
+
"size": 123456,
|
| 24 |
+
"mime_type": "image/jpeg",
|
| 25 |
+
"meta": { "_type": "gradio.FileData" }
|
| 26 |
+
},
|
| 27 |
+
{
|
| 28 |
+
"path": "uploaded_variant_image_path",
|
| 29 |
+
"url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_variant_image_path",
|
| 30 |
+
"orig_name": "variant.jpg",
|
| 31 |
+
"size": 789012,
|
| 32 |
+
"mime_type": "image/jpeg",
|
| 33 |
+
"meta": { "_type": "gradio.FileData" }
|
| 34 |
+
}
|
| 35 |
+
]
|
| 36 |
+
}
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
**Response Format:**
|
| 40 |
+
```json
|
| 41 |
+
{
|
| 42 |
+
"prediction_results": {
|
| 43 |
+
"winner": "CONTROL WINS",
|
| 44 |
+
"probability": "0.041",
|
| 45 |
+
"model_confidence": "75.5%",
|
| 46 |
+
"training_data_samples": 477,
|
| 47 |
+
"historical_accuracy": "40/53 correct",
|
| 48 |
+
"win_loss_ratio": "24 wins in 53 tests"
|
| 49 |
+
},
|
| 50 |
+
"auto_detected_categories": {
|
| 51 |
+
"business_model": "SaaS",
|
| 52 |
+
"customer_type": "B2B",
|
| 53 |
+
"conversion_type": "High-Intent Lead Gen",
|
| 54 |
+
"industry": "B2B Software & Tech",
|
| 55 |
+
"page_type": "Conversion"
|
| 56 |
+
},
|
| 57 |
+
"detected_pattern": {
|
| 58 |
+
"pattern": "Double Column Form",
|
| 59 |
+
"description": "The variant implements a 'Double Column Form' modification"
|
| 60 |
+
},
|
| 61 |
+
"processing_info": {
|
| 62 |
+
"total_processing_time": "32.36s",
|
| 63 |
+
"ai_categorization": "Perplexity Sonar Reasoning Pro",
|
| 64 |
+
"pattern_detection": "Gemini Pro Vision",
|
| 65 |
+
"confidence_source": "B2B Software & Tech | Conversion",
|
| 66 |
+
"total_patterns_analyzed": 359
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
```
|
| 70 |
+
|
| 71 |
+
### 2. Manual Categorization Prediction
|
| 72 |
+
**Endpoint:** `/call/predict_single`
|
| 73 |
+
**Method:** POST
|
| 74 |
+
**Description:** Predicts A/B test winner with manually provided categories
|
| 75 |
+
|
| 76 |
+
**Request Format:**
|
| 77 |
+
```json
|
| 78 |
+
{
|
| 79 |
+
"data": [
|
| 80 |
+
{
|
| 81 |
+
"path": "uploaded_control_image_path",
|
| 82 |
+
"url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_control_image_path",
|
| 83 |
+
"orig_name": "control.jpg",
|
| 84 |
+
"size": 123456,
|
| 85 |
+
"mime_type": "image/jpeg",
|
| 86 |
+
"meta": { "_type": "gradio.FileData" }
|
| 87 |
+
},
|
| 88 |
+
{
|
| 89 |
+
"path": "uploaded_variant_image_path",
|
| 90 |
+
"url": "https://nitish-spz-abtestpredictorv2.hf.space/file=uploaded_variant_image_path",
|
| 91 |
+
"orig_name": "variant.jpg",
|
| 92 |
+
"size": 789012,
|
| 93 |
+
"mime_type": "image/jpeg",
|
| 94 |
+
"meta": { "_type": "gradio.FileData" }
|
| 95 |
+
},
|
| 96 |
+
"E-Commerce",
|
| 97 |
+
"B2C",
|
| 98 |
+
"Direct Purchase",
|
| 99 |
+
"Retail & E-commerce",
|
| 100 |
+
"Conversion"
|
| 101 |
+
]
|
| 102 |
+
}
|
| 103 |
+
```
|
| 104 |
+
|
| 105 |
+
**Response Format:**
|
| 106 |
+
```json
|
| 107 |
+
{
|
| 108 |
+
"winner": "VARIANT WINS",
|
| 109 |
+
"probability": "0.987",
|
| 110 |
+
"model_confidence": "82.3%",
|
| 111 |
+
"training_data_samples": 1205,
|
| 112 |
+
"historical_accuracy": "98/120 correct",
|
| 113 |
+
"win_loss_ratio": "67 wins in 120 tests"
|
| 114 |
+
}
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
## File Upload Process
|
| 118 |
+
|
| 119 |
+
### Step 1: Upload Images
|
| 120 |
+
**Endpoint:** `/upload`
|
| 121 |
+
**Method:** POST
|
| 122 |
+
**Content-Type:** multipart/form-data
|
| 123 |
+
|
| 124 |
+
```python
|
| 125 |
+
import requests
|
| 126 |
+
|
| 127 |
+
# Upload control image
|
| 128 |
+
with open('control.jpg', 'rb') as f:
|
| 129 |
+
files = {'files': f}
|
| 130 |
+
response = requests.post('https://nitish-spz-abtestpredictorv2.hf.space/upload', files=files)
|
| 131 |
+
control_path = response.json()[0]
|
| 132 |
+
|
| 133 |
+
# Upload variant image
|
| 134 |
+
with open('variant.jpg', 'rb') as f:
|
| 135 |
+
files = {'files': f}
|
| 136 |
+
response = requests.post('https://nitish-spz-abtestpredictorv2.hf.space/upload', files=files)
|
| 137 |
+
variant_path = response.json()[0]
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### Step 2: Make Prediction Request
|
| 141 |
+
```python
|
| 142 |
+
import requests
|
| 143 |
+
|
| 144 |
+
# Prepare FileData objects
|
| 145 |
+
control_file_data = {
|
| 146 |
+
"path": control_path,
|
| 147 |
+
"url": f"https://nitish-spz-abtestpredictorv2.hf.space/file={control_path}",
|
| 148 |
+
"orig_name": "control.jpg",
|
| 149 |
+
"size": 123456,
|
| 150 |
+
"mime_type": "image/jpeg",
|
| 151 |
+
"meta": { "_type": "gradio.FileData" }
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
variant_file_data = {
|
| 155 |
+
"path": variant_path,
|
| 156 |
+
"url": f"https://nitish-spz-abtestpredictorv2.hf.space/file={variant_path}",
|
| 157 |
+
"orig_name": "variant.jpg",
|
| 158 |
+
"size": 789012,
|
| 159 |
+
"mime_type": "image/jpeg",
|
| 160 |
+
"meta": { "_type": "gradio.FileData" }
|
| 161 |
+
}
|
| 162 |
+
|
| 163 |
+
# Make prediction request
|
| 164 |
+
response = requests.post(
|
| 165 |
+
'https://nitish-spz-abtestpredictorv2.hf.space/call/predict_with_auto_categorization',
|
| 166 |
+
json={"data": [control_file_data, variant_file_data]}
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
result = response.json()
|
| 170 |
+
```
|
| 171 |
+
|
| 172 |
+
## Response Field Descriptions
|
| 173 |
+
|
| 174 |
+
### Prediction Results
|
| 175 |
+
- **winner**: "CONTROL WINS" or "VARIANT WINS"
|
| 176 |
+
- **probability**: Float between 0-1 (probability of variant winning)
|
| 177 |
+
- **model_confidence**: Percentage confidence based on historical data
|
| 178 |
+
- **training_data_samples**: Number of training samples used for confidence
|
| 179 |
+
- **historical_accuracy**: Accuracy of predictions on similar data
|
| 180 |
+
- **win_loss_ratio**: Historical performance on similar tests
|
| 181 |
+
|
| 182 |
+
### Auto-Detected Categories
|
| 183 |
+
- **business_model**: "E-Commerce", "SaaS", "Lead Generation", "Other*"
|
| 184 |
+
- **customer_type**: "B2B", "B2C", "Both", "Other*"
|
| 185 |
+
- **conversion_type**: "Direct Purchase", "High-Intent Lead Gen", "Info/Content Lead Gen", "Location Search", "Non-Profit/Community", "Other Conversion"
|
| 186 |
+
- **industry**: 14 categories including "B2B Software & Tech", "Retail & E-commerce", etc.
|
| 187 |
+
- **page_type**: "Awareness & Discovery", "Consideration & Evaluation", "Conversion", "Internal & Navigation", "Post-Conversion & Other"
|
| 188 |
+
|
| 189 |
+
### Detected Pattern
|
| 190 |
+
- **pattern**: A/B test pattern name (e.g., "Button", "Form Over UI", "Hero Changes")
|
| 191 |
+
- **description**: Human-readable description of the pattern
|
| 192 |
+
|
| 193 |
+
### Processing Info
|
| 194 |
+
- **total_processing_time**: Time taken for prediction (typically 20-40 seconds)
|
| 195 |
+
- **ai_categorization**: AI service used for categorization
|
| 196 |
+
- **pattern_detection**: AI service used for pattern detection
|
| 197 |
+
- **confidence_source**: Data source for confidence scores
|
| 198 |
+
- **total_patterns_analyzed**: Number of patterns in the detection database
|
| 199 |
+
|
| 200 |
+
## Error Handling
|
| 201 |
+
|
| 202 |
+
### Common Error Responses
|
| 203 |
+
```json
|
| 204 |
+
{
|
| 205 |
+
"error": "Prediction failed: [error message]",
|
| 206 |
+
"model_confidence": "50.0%",
|
| 207 |
+
"training_data_samples": 0,
|
| 208 |
+
"historical_accuracy": "0/0 correct",
|
| 209 |
+
"win_loss_ratio": "0 wins in 0 tests"
|
| 210 |
+
}
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
### HTTP Status Codes
|
| 214 |
+
- **200**: Success
|
| 215 |
+
- **400**: Bad request (invalid parameters)
|
| 216 |
+
- **500**: Internal server error
|
| 217 |
+
- **503**: Service unavailable (GPU quota exceeded)
|
| 218 |
+
|
| 219 |
+
## Rate Limits
|
| 220 |
+
- Free tier: Limited concurrent requests
|
| 221 |
+
- Processing time: 20-40 seconds per prediction
|
| 222 |
+
- GPU duration: 50-60 seconds maximum
|
| 223 |
+
|
| 224 |
+
## Example Integration (Python)
|
| 225 |
+
|
| 226 |
+
```python
|
| 227 |
+
import requests
|
| 228 |
+
import time
|
| 229 |
+
|
| 230 |
+
class ABTestPredictor:
|
| 231 |
+
def __init__(self):
|
| 232 |
+
self.base_url = "https://nitish-spz-abtestpredictorv2.hf.space"
|
| 233 |
+
|
| 234 |
+
def predict(self, control_image_path, variant_image_path):
|
| 235 |
+
# Step 1: Upload images
|
| 236 |
+
control_path = self._upload_image(control_image_path)
|
| 237 |
+
variant_path = self._upload_image(variant_image_path)
|
| 238 |
+
|
| 239 |
+
# Step 2: Make prediction
|
| 240 |
+
return self._make_prediction(control_path, variant_path)
|
| 241 |
+
|
| 242 |
+
def _upload_image(self, image_path):
|
| 243 |
+
with open(image_path, 'rb') as f:
|
| 244 |
+
files = {'files': f}
|
| 245 |
+
response = requests.post(f"{self.base_url}/upload", files=files)
|
| 246 |
+
return response.json()[0]
|
| 247 |
+
|
| 248 |
+
def _make_prediction(self, control_path, variant_path):
|
| 249 |
+
control_data = self._create_file_data(control_path, "control.jpg")
|
| 250 |
+
variant_data = self._create_file_data(variant_path, "variant.jpg")
|
| 251 |
+
|
| 252 |
+
response = requests.post(
|
| 253 |
+
f"{self.base_url}/call/predict_with_auto_categorization",
|
| 254 |
+
json={"data": [control_data, variant_data]}
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
return response.json()
|
| 258 |
+
|
| 259 |
+
def _create_file_data(self, path, orig_name):
|
| 260 |
+
return {
|
| 261 |
+
"path": path,
|
| 262 |
+
"url": f"{self.base_url}/file={path}",
|
| 263 |
+
"orig_name": orig_name,
|
| 264 |
+
"size": 0, # Optional
|
| 265 |
+
"mime_type": "image/jpeg",
|
| 266 |
+
"meta": { "_type": "gradio.FileData" }
|
| 267 |
+
}
|
| 268 |
+
|
| 269 |
+
# Usage
|
| 270 |
+
predictor = ABTestPredictor()
|
| 271 |
+
result = predictor.predict("control.jpg", "variant.jpg")
|
| 272 |
+
print(f"Winner: {result['prediction_results']['winner']}")
|
| 273 |
+
print(f"Probability: {result['prediction_results']['probability']}")
|
| 274 |
+
```
|
| 275 |
+
|
| 276 |
+
## Notes
|
| 277 |
+
- Images are automatically deleted after processing
|
| 278 |
+
- Maximum image size: 10MB
|
| 279 |
+
- Supported formats: JPEG, PNG, WebP
|
| 280 |
+
- Processing time varies based on image complexity and server load
|
app.py
CHANGED
|
@@ -848,24 +848,24 @@ def predict_with_auto_categorization(control_image, variant_image):
|
|
| 848 |
|
| 849 |
# Create comprehensive result with prediction, categorization, and pattern detection
|
| 850 |
enhanced_result = {
|
| 851 |
-
"
|
| 852 |
-
"
|
| 853 |
-
"
|
| 854 |
-
"
|
| 855 |
-
"
|
| 856 |
-
"
|
| 857 |
-
"
|
| 858 |
},
|
| 859 |
-
"
|
| 860 |
-
"
|
| 861 |
-
"
|
| 862 |
},
|
| 863 |
-
"
|
| 864 |
-
"
|
| 865 |
-
"
|
| 866 |
-
"
|
| 867 |
-
"
|
| 868 |
-
"
|
| 869 |
}
|
| 870 |
}
|
| 871 |
|
|
@@ -934,11 +934,12 @@ def predict_single(control_image, variant_image, business_model, customer_type,
|
|
| 934 |
|
| 935 |
# Create enhanced output with confidence scores and training data info
|
| 936 |
result = {
|
| 937 |
-
|
| 938 |
-
|
| 939 |
-
|
| 940 |
-
|
| 941 |
-
|
|
|
|
| 942 |
}
|
| 943 |
|
| 944 |
print(f"π― Final result: {result}")
|
|
@@ -952,11 +953,11 @@ def predict_single(control_image, variant_image, business_model, customer_type,
|
|
| 952 |
|
| 953 |
# Return error result with fallback confidence data
|
| 954 |
return {
|
| 955 |
-
"
|
| 956 |
-
|
| 957 |
-
|
| 958 |
-
|
| 959 |
-
|
| 960 |
}
|
| 961 |
|
| 962 |
@spaces.GPU
|
|
|
|
| 848 |
|
| 849 |
# Create comprehensive result with prediction, categorization, and pattern detection
|
| 850 |
enhanced_result = {
|
| 851 |
+
"prediction_results": prediction_result,
|
| 852 |
+
"auto_detected_categories": {
|
| 853 |
+
"business_model": business_model,
|
| 854 |
+
"customer_type": customer_type,
|
| 855 |
+
"conversion_type": conversion_type,
|
| 856 |
+
"industry": industry,
|
| 857 |
+
"page_type": page_type
|
| 858 |
},
|
| 859 |
+
"detected_pattern": {
|
| 860 |
+
"pattern": detected_pattern,
|
| 861 |
+
"description": f"The variant implements a '{detected_pattern}' modification"
|
| 862 |
},
|
| 863 |
+
"processing_info": {
|
| 864 |
+
"total_processing_time": f"{time.time() - start_time:.2f}s",
|
| 865 |
+
"ai_categorization": "Perplexity Sonar Reasoning Pro" if PERPLEXITY_API_KEY else "Fallback Mode",
|
| 866 |
+
"pattern_detection": "Gemini Pro Vision" if GEMINI_API_KEY else "Fallback Mode",
|
| 867 |
+
"confidence_source": f"{industry} | {page_type}",
|
| 868 |
+
"total_patterns_analyzed": len(pattern_descriptions) if pattern_descriptions else 0
|
| 869 |
}
|
| 870 |
}
|
| 871 |
|
|
|
|
| 934 |
|
| 935 |
# Create enhanced output with confidence scores and training data info
|
| 936 |
result = {
|
| 937 |
+
"winner": winner,
|
| 938 |
+
"probability": f"{probability:.3f}",
|
| 939 |
+
"model_confidence": f"{confidence_percentage:.1f}%",
|
| 940 |
+
"training_data_samples": confidence_data['training_data_count'],
|
| 941 |
+
"historical_accuracy": f"{confidence_data['correct_predictions']}/{confidence_data['count']} correct",
|
| 942 |
+
"win_loss_ratio": f"{confidence_data['actual_wins']} wins in {confidence_data['count']} tests"
|
| 943 |
}
|
| 944 |
|
| 945 |
print(f"π― Final result: {result}")
|
|
|
|
| 953 |
|
| 954 |
# Return error result with fallback confidence data
|
| 955 |
return {
|
| 956 |
+
"error": f"Prediction failed: {str(e)}",
|
| 957 |
+
"model_confidence": "50.0%",
|
| 958 |
+
"training_data_samples": 0,
|
| 959 |
+
"historical_accuracy": "0/0 correct",
|
| 960 |
+
"win_loss_ratio": "0 wins in 0 tests"
|
| 961 |
}
|
| 962 |
|
| 963 |
@spaces.GPU
|