Spaces:
Runtime error
Runtime error
Commit Β·
774e5c0
1
Parent(s): 81aa4c1
...
Browse files- __pycache__/app.cpython-313.pyc +0 -0
- app.py +78 -59
__pycache__/app.cpython-313.pyc
ADDED
|
Binary file (50.3 kB). View file
|
|
|
app.py
CHANGED
|
@@ -828,71 +828,90 @@ def predict_with_auto_categorization(control_image, variant_image):
|
|
| 828 |
@spaces.GPU(duration=180) # Extended duration for maximum concurrent load
|
| 829 |
def predict_single(control_image, variant_image, business_model, customer_type, conversion_type, industry, page_type):
|
| 830 |
"""Orchestrates the prediction for a single pair of images and features."""
|
| 831 |
-
if control_image is None or variant_image is None:
|
| 832 |
-
return {"Error": 1.0, "Please upload both images": 0.0}
|
| 833 |
-
|
| 834 |
-
start_time = time.time()
|
| 835 |
-
|
| 836 |
-
c_img = Image.fromarray(control_image).convert("RGB")
|
| 837 |
-
v_img = Image.fromarray(variant_image).convert("RGB")
|
| 838 |
-
|
| 839 |
-
# Extract OCR text from both images (this is crucial for model performance)
|
| 840 |
try:
|
| 841 |
-
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
print("
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
confidence_data = get_confidence_data(business_model, customer_type, conversion_type, industry, page_type)
|
| 850 |
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
|
| 855 |
-
|
| 856 |
-
c_text = tokenizer(c_text_str, padding='max_length', truncation=True, max_length=MAX_TEXT_LENGTH, return_tensors='pt').to(device)
|
| 857 |
-
v_text = tokenizer(v_text_str, padding='max_length', truncation=True, max_length=MAX_TEXT_LENGTH, return_tensors='pt').to(device)
|
| 858 |
|
| 859 |
-
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
c_pix=c_pix, v_pix=v_pix,
|
| 866 |
-
c_tok=c_text['input_ids'], c_attn=c_text['attention_mask'],
|
| 867 |
-
v_tok=v_text['input_ids'], v_attn=v_text['attention_mask'],
|
| 868 |
-
cat_feats=cat_feats
|
| 869 |
-
)
|
| 870 |
|
| 871 |
-
|
|
|
|
|
|
|
| 872 |
|
| 873 |
-
|
| 874 |
-
|
| 875 |
-
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
| 882 |
-
|
| 883 |
-
|
| 884 |
-
|
| 885 |
-
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
| 890 |
-
|
| 891 |
-
|
| 892 |
-
|
| 893 |
-
|
| 894 |
-
|
| 895 |
-
|
|
|
|
|
|
|
|
|
|
| 896 |
|
| 897 |
@spaces.GPU
|
| 898 |
def predict_batch(csv_path, control_img_dir, variant_img_dir, num_samples):
|
|
|
|
| 828 |
@spaces.GPU(duration=180) # Extended duration for maximum concurrent load
|
| 829 |
def predict_single(control_image, variant_image, business_model, customer_type, conversion_type, industry, page_type):
|
| 830 |
"""Orchestrates the prediction for a single pair of images and features."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 831 |
try:
|
| 832 |
+
if control_image is None or variant_image is None:
|
| 833 |
+
return {"Error": 1.0, "Please upload both images": 0.0}
|
| 834 |
+
|
| 835 |
+
start_time = time.time()
|
| 836 |
+
print(f"π Starting prediction with categories: {business_model} | {customer_type} | {conversion_type} | {industry} | {page_type}")
|
| 837 |
+
|
| 838 |
+
c_img = Image.fromarray(control_image).convert("RGB")
|
| 839 |
+
v_img = Image.fromarray(variant_image).convert("RGB")
|
|
|
|
| 840 |
|
| 841 |
+
# Extract OCR text from both images (this is crucial for model performance)
|
| 842 |
+
try:
|
| 843 |
+
c_text_str = pytesseract.image_to_string(c_img)
|
| 844 |
+
v_text_str = pytesseract.image_to_string(v_img)
|
| 845 |
+
print(f"π OCR extracted - Control: {len(c_text_str)} chars, Variant: {len(v_text_str)} chars")
|
| 846 |
+
except pytesseract.TesseractNotFoundError:
|
| 847 |
+
print("π Tesseract is not installed or not in your PATH. Skipping OCR.")
|
| 848 |
+
c_text_str, v_text_str = "", ""
|
| 849 |
+
|
| 850 |
+
# Get confidence data for this combination
|
| 851 |
+
confidence_data = get_confidence_data(business_model, customer_type, conversion_type, industry, page_type)
|
| 852 |
+
print(f"π Confidence data loaded: {confidence_data}")
|
| 853 |
+
|
| 854 |
+
with torch.no_grad():
|
| 855 |
+
c_pix = image_processor(images=c_img, return_tensors="pt").pixel_values.to(device)
|
| 856 |
+
v_pix = image_processor(images=v_img, return_tensors="pt").pixel_values.to(device)
|
| 857 |
+
|
| 858 |
+
# Process OCR text through the text model
|
| 859 |
+
c_text = tokenizer(c_text_str, padding='max_length', truncation=True, max_length=MAX_TEXT_LENGTH, return_tensors='pt').to(device)
|
| 860 |
+
v_text = tokenizer(v_text_str, padding='max_length', truncation=True, max_length=MAX_TEXT_LENGTH, return_tensors='pt').to(device)
|
| 861 |
+
|
| 862 |
+
cat_inputs = [business_model, customer_type, conversion_type, industry, page_type]
|
| 863 |
+
cat_codes = [category_mappings[name]['categories'].index(val) for name, val in zip(CATEGORICAL_FEATURES, cat_inputs)]
|
| 864 |
+
cat_feats = torch.tensor([cat_codes], dtype=torch.int64).to(device)
|
| 865 |
+
|
| 866 |
+
# Run the multimodal model prediction
|
| 867 |
+
logits = model(
|
| 868 |
+
c_pix=c_pix, v_pix=v_pix,
|
| 869 |
+
c_tok=c_text['input_ids'], c_attn=c_text['attention_mask'],
|
| 870 |
+
v_tok=v_text['input_ids'], v_attn=v_text['attention_mask'],
|
| 871 |
+
cat_feats=cat_feats
|
| 872 |
+
)
|
| 873 |
+
|
| 874 |
+
probability = torch.sigmoid(logits).item()
|
| 875 |
|
| 876 |
+
processing_time = time.time() - start_time
|
|
|
|
|
|
|
| 877 |
|
| 878 |
+
# Log GPU memory usage for monitoring
|
| 879 |
+
if torch.cuda.is_available():
|
| 880 |
+
gpu_memory = torch.cuda.memory_allocated() / 1024**3
|
| 881 |
+
print(f"π Prediction completed in {processing_time:.2f}s | GPU Memory: {gpu_memory:.1f}GB")
|
| 882 |
+
else:
|
| 883 |
+
print(f"π Prediction completed in {processing_time:.2f}s")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 884 |
|
| 885 |
+
# Determine winner
|
| 886 |
+
winner = "VARIANT WINS" if probability > 0.5 else "CONTROL WINS"
|
| 887 |
+
confidence_percentage = confidence_data['accuracy'] * 100
|
| 888 |
|
| 889 |
+
# Create enhanced output with confidence scores and training data info
|
| 890 |
+
result = {
|
| 891 |
+
f"π {winner}": f"{probability:.3f}",
|
| 892 |
+
f"π Model Confidence": f"{confidence_percentage:.1f}%",
|
| 893 |
+
f"π Training Data": f"{confidence_data['training_data_count']} samples",
|
| 894 |
+
f"β
Historical Accuracy": f"{confidence_data['correct_predictions']}/{confidence_data['count']} correct",
|
| 895 |
+
f"π― Win/Loss Ratio": f"{confidence_data['actual_wins']} wins in {confidence_data['count']} tests"
|
| 896 |
+
}
|
| 897 |
+
|
| 898 |
+
print(f"π― Final result: {result}")
|
| 899 |
+
return result
|
| 900 |
+
|
| 901 |
+
except Exception as e:
|
| 902 |
+
print(f"β ERROR in predict_single: {e}")
|
| 903 |
+
print(f"π Error type: {type(e).__name__}")
|
| 904 |
+
import traceback
|
| 905 |
+
traceback.print_exc()
|
| 906 |
+
|
| 907 |
+
# Return error result with fallback confidence data
|
| 908 |
+
return {
|
| 909 |
+
"β Error": f"Prediction failed: {str(e)}",
|
| 910 |
+
f"π Model Confidence": "50.0%",
|
| 911 |
+
f"π Training Data": "0 samples",
|
| 912 |
+
f"β
Historical Accuracy": "0/0 correct",
|
| 913 |
+
f"π― Win/Loss Ratio": "0 wins in 0 tests"
|
| 914 |
+
}
|
| 915 |
|
| 916 |
@spaces.GPU
|
| 917 |
def predict_batch(csv_path, control_img_dir, variant_img_dir, num_samples):
|