ABTestPredictor / API_CURL_EXAMPLES.sh
nitish-spz's picture
build error - fix 1
e93a798
#!/bin/bash
# ============================================================================
# A/B Test Predictor - cURL API Examples
# ============================================================================
# Configuration
API_URL="http://localhost:7860" # Change to your deployment URL
# For Hugging Face Spaces: API_URL="https://your-username-abtestpredictor.hf.space"
# ============================================================================
# Example 1: Basic Prediction with Image Files
# ============================================================================
# Convert images to base64
CONTROL_IMAGE_B64=$(base64 -i control_image.jpg)
VARIANT_IMAGE_B64=$(base64 -i variant_image.jpg)
# Send POST request to Gradio API
curl -X POST "${API_URL}/api/predict" \
-H "Content-Type: application/json" \
-d '{
"data": [
"data:image/jpeg;base64,'"${CONTROL_IMAGE_B64}"'",
"data:image/jpeg;base64,'"${VARIANT_IMAGE_B64}"'",
"SaaS",
"B2B",
"High-Intent Lead Gen",
"B2B Software & Tech",
"Awareness & Discovery"
],
"fn_index": 0
}'
# ============================================================================
# Example 2: Using a Function to Send Requests
# ============================================================================
predict_abtest() {
local CONTROL_IMG=$1
local VARIANT_IMG=$2
local BUSINESS_MODEL=$3
local CUSTOMER_TYPE=$4
local CONVERSION_TYPE=$5
local INDUSTRY=$6
local PAGE_TYPE=$7
# Encode images
local CONTROL_B64=$(base64 -i "$CONTROL_IMG")
local VARIANT_B64=$(base64 -i "$VARIANT_IMG")
# Make API call
curl -X POST "${API_URL}/api/predict" \
-H "Content-Type: application/json" \
-d '{
"data": [
"data:image/jpeg;base64,'"${CONTROL_B64}"'",
"data:image/jpeg;base64,'"${VARIANT_B64}"'",
"'"${BUSINESS_MODEL}"'",
"'"${CUSTOMER_TYPE}"'",
"'"${CONVERSION_TYPE}"'",
"'"${INDUSTRY}"'",
"'"${PAGE_TYPE}"'"
]
}' | jq .
}
# Usage
predict_abtest \
"control.jpg" \
"variant.jpg" \
"SaaS" \
"B2B" \
"High-Intent Lead Gen" \
"B2B Software & Tech" \
"Awareness & Discovery"
# ============================================================================
# Example 3: Multiple Predictions in a Loop
# ============================================================================
# Read test cases from CSV
while IFS=',' read -r control variant business customer conversion industry page
do
echo "Processing: $control vs $variant"
predict_abtest \
"$control" \
"$variant" \
"$business" \
"$customer" \
"$conversion" \
"$industry" \
"$page"
sleep 1 # Rate limiting
done < test_cases.csv
# ============================================================================
# Example 4: Save Results to File
# ============================================================================
predict_and_save() {
local OUTPUT_FILE=$1
predict_abtest \
"control.jpg" \
"variant.jpg" \
"SaaS" \
"B2B" \
"High-Intent Lead Gen" \
"B2B Software & Tech" \
"Awareness & Discovery" > "$OUTPUT_FILE"
echo "Results saved to $OUTPUT_FILE"
}
predict_and_save "prediction_result.json"
# ============================================================================
# Example 5: Parse and Extract Specific Fields
# ============================================================================
# Get just the win probability
get_win_probability() {
predict_abtest "$@" | jq -r '.data[0].predictionResults.probability'
}
# Get model confidence
get_confidence() {
predict_abtest "$@" | jq -r '.data[0].predictionResults.modelConfidence'
}
# Usage
PROB=$(get_win_probability "control.jpg" "variant.jpg" "SaaS" "B2B" "High-Intent Lead Gen" "B2B Software & Tech" "Awareness & Discovery")
CONF=$(get_confidence "control.jpg" "variant.jpg" "SaaS" "B2B" "High-Intent Lead Gen" "B2B Software & Tech" "Awareness & Discovery")
echo "Win Probability: $PROB"
echo "Model Confidence: $CONF%"
# ============================================================================
# Valid Category Values (for reference)
# ============================================================================
# Business Model options:
# - "E-Commerce"
# - "Lead Generation"
# - "Other*"
# - "SaaS"
# Customer Type options:
# - "B2B"
# - "B2C"
# - "Both"
# - "Other*"
# Conversion Type options:
# - "Direct Purchase"
# - "High-Intent Lead Gen"
# - "Info/Content Lead Gen"
# - "Location Search"
# - "Non-Profit/Community"
# - "Other Conversion"
# Industry options:
# - "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"
# Page Type options:
# - "Awareness & Discovery"
# - "Consideration & Evaluation"
# - "Conversion"
# - "Internal & Navigation"
# - "Post-Conversion & Other"