""" 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()