b2b-ecomm-ner / test_comprehensive.py
Purva17's picture
Upload folder using huggingface_hub
f0f2280 verified
"""
Comprehensive test of the Hugging Face B2B Ecommerce NER model
"""
import sys
import os
sys.path.append(os.path.dirname(__file__))
from model import B2BEcommerceNER
import json
def test_actual_predictions():
"""Test the model with actual predictions"""
print("🧪 Testing B2B Ecommerce NER Model - Actual Predictions")
print("=" * 60)
# Initialize model
model = B2BEcommerceNER(
model_path="spacy_model",
catalog_path="product_catalog.csv"
)
# Test cases with expected vs actual results
test_cases = [
{
"text": "Order 5 Coke Zero 650ML",
"description": "Standard beverage order"
},
{
"text": "I need 3 units of Chocolate Cleanser 500ML",
"description": "Personal care product order"
},
{
"text": "Send 10 bottles of mango juice",
"description": "Juice order without size"
},
{
"text": "We want 2 packs of biscuits",
"description": "Snack order"
},
{
"text": "Please deliver 6 units of Ziofit Golden Dates 250G",
"description": "Health food order"
}
]
for i, test_case in enumerate(test_cases, 1):
print(f"\n📝 Test Case {i}: {test_case['description']}")
print(f"Input: '{test_case['text']}'")
print("-" * 40)
# Get prediction
results = model.predict([test_case['text']])
result = results[0]
# Display entities
entities = result['entities']
print("🎯 Extracted Entities:")
for entity_type in ['quantities', 'units', 'products', 'sizes']:
if entities[entity_type]:
print(f" {entity_type.upper()}:")
for entity in entities[entity_type]:
print(f" • '{entity['text']}' ({entity['start']}-{entity['end']})")
# Display catalog matches
if entities['catalog_matches']:
print("🛒 Product Catalog Matches:")
for match in entities['catalog_matches'][:2]: # Show top 2
print(f" • {match['brand']} - {match['product']}")
print(f" SKU: {match['sku']} | Confidence: {match['match_score']}%")
else:
print("🛒 No catalog matches found")
print()
def test_batch_processing():
"""Test batch processing capabilities"""
print("📦 Testing Batch Processing")
print("=" * 30)
model = B2BEcommerceNER(
model_path="spacy_model",
catalog_path="product_catalog.csv"
)
# Batch of orders
orders = [
"Order 5 Coke Zero 650ML",
"Send 12 packets of biscuits",
"I need 3 bottles of juice 500ML",
"We want 8 units of dates 250G"
]
print(f"Processing {len(orders)} orders in batch...")
results = model.predict(orders)
# Summary
total_entities = sum(r['total_entities'] for r in results)
total_products = sum(len(r['entities']['products']) for r in results)
total_catalog_matches = sum(len(r['entities']['catalog_matches']) for r in results)
print(f"✅ Batch processing complete!")
print(f" 📊 Total entities extracted: {total_entities}")
print(f" 🏷️ Products identified: {total_products}")
print(f" 🔍 Catalog matches found: {total_catalog_matches}")
def test_edge_cases():
"""Test edge cases and error handling"""
print("\n🔧 Testing Edge Cases")
print("=" * 25)
model = B2BEcommerceNER(
model_path="spacy_model",
catalog_path="product_catalog.csv"
)
edge_cases = [
"", # Empty string
"Hello world", # No entities
"123", # Only numbers
"Order order order", # Repeated words
"मुझे 5 पैकेट मैगी चाहिए", # Hindi text
]
for case in edge_cases:
print(f"Input: '{case}'")
try:
results = model.predict([case])
entities_count = results[0]['total_entities']
print(f" ✅ Processed successfully - {entities_count} entities found")
except Exception as e:
print(f" ❌ Error: {e}")
print()
def test_pipeline_compatibility():
"""Test Hugging Face pipeline compatibility"""
print("🔄 Testing Pipeline Compatibility")
print("=" * 35)
model = B2BEcommerceNER(
model_path="spacy_model",
catalog_path="product_catalog.csv"
)
# Test pipeline method
text = "Order 5 Coke Zero 650ML"
print(f"Input: '{text}'")
try:
pipeline_result = model.pipeline(text)
print("✅ Pipeline method works!")
print(f" Entities in HF format: {len(pipeline_result)}")
for entity in pipeline_result:
print(f" • {entity['entity']}: '{entity['word']}' (score: {entity['score']})")
except Exception as e:
print(f"❌ Pipeline error: {e}")
def main():
"""Run all tests"""
print("🚀 B2B Ecommerce NER Model - Comprehensive Testing")
print("=" * 55)
print("This will test the actual functionality of the trained model")
print()
try:
# Test actual predictions
test_actual_predictions()
# Test batch processing
test_batch_processing()
# Test edge cases
test_edge_cases()
# Test pipeline compatibility
test_pipeline_compatibility()
print("\n🎉 All tests completed!")
print("\n📋 Summary:")
print("✅ Entity extraction working")
print("✅ Product catalog matching working")
print("✅ Batch processing working")
print("✅ Edge case handling working")
print("✅ Pipeline compatibility working")
print("\n🚀 Ready for Hugging Face upload!")
except Exception as e:
print(f"\n❌ Test failed with error: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()