nitish-spz commited on
Commit
774e5c0
Β·
1 Parent(s): 81aa4c1
Files changed (2) hide show
  1. __pycache__/app.cpython-313.pyc +0 -0
  2. 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
- c_text_str = pytesseract.image_to_string(c_img)
842
- v_text_str = pytesseract.image_to_string(v_img)
843
- print(f"πŸ“ OCR extracted - Control: {len(c_text_str)} chars, Variant: {len(v_text_str)} chars")
844
- except pytesseract.TesseractNotFoundError:
845
- print("πŸ›‘ Tesseract is not installed or not in your PATH. Skipping OCR.")
846
- c_text_str, v_text_str = "", ""
847
-
848
- # Get confidence data for this combination
849
- confidence_data = get_confidence_data(business_model, customer_type, conversion_type, industry, page_type)
850
 
851
- with torch.no_grad():
852
- c_pix = image_processor(images=c_img, return_tensors="pt").pixel_values.to(device)
853
- v_pix = image_processor(images=v_img, return_tensors="pt").pixel_values.to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
854
 
855
- # Process OCR text through the text model
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
- cat_inputs = [business_model, customer_type, conversion_type, industry, page_type]
860
- cat_codes = [category_mappings[name]['categories'].index(val) for name, val in zip(CATEGORICAL_FEATURES, cat_inputs)]
861
- cat_feats = torch.tensor([cat_codes], dtype=torch.int64).to(device)
862
-
863
- # Run the multimodal model prediction
864
- logits = model(
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
- probability = torch.sigmoid(logits).item()
 
 
872
 
873
- processing_time = time.time() - start_time
874
-
875
- # Log GPU memory usage for monitoring
876
- if torch.cuda.is_available():
877
- gpu_memory = torch.cuda.memory_allocated() / 1024**3
878
- print(f"πŸš€ Prediction completed in {processing_time:.2f}s | GPU Memory: {gpu_memory:.1f}GB")
879
- else:
880
- print(f"πŸš€ Prediction completed in {processing_time:.2f}s")
881
-
882
- # Determine winner
883
- winner = "VARIANT WINS" if probability > 0.5 else "CONTROL WINS"
884
- confidence_percentage = confidence_data['accuracy'] * 100
885
-
886
- # Create enhanced output with confidence scores and training data info
887
- result = {
888
- f"πŸ† {winner}": f"{probability:.3f}",
889
- f"πŸ“Š Model Confidence": f"{confidence_percentage:.1f}%",
890
- f"πŸ“ˆ Training Data": f"{confidence_data['training_data_count']} samples",
891
- f"βœ… Historical Accuracy": f"{confidence_data['correct_predictions']}/{confidence_data['count']} correct",
892
- f"🎯 Win/Loss Ratio": f"{confidence_data['actual_wins']} wins in {confidence_data['count']} tests"
893
- }
894
-
895
- return result
 
 
 
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):