SIngtel-Bill-Scanner / verify_models.py
Cosmo125's picture
Upload 26 files
795183d verified
"""
Final verification test - Models are downloaded, let's test them!
"""
from transformers import pipeline
from PIL import Image
import time
def test_downloaded_models():
"""Test the downloaded models quickly"""
print("πŸš€ TESTING DOWNLOADED MODELS")
print("=" * 40)
try:
# Test 1: Load TrOCR pipeline (should be fast now)
print("πŸ“₯ Loading TrOCR model...")
start_time = time.time()
pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten")
load_time = time.time() - start_time
print(f"βœ… Model loaded in {load_time:.1f} seconds")
# Test 2: Process test image
print("\nπŸ” Processing test image...")
if Image.open("test_singtel_bill.png"):
img = Image.open("test_singtel_bill.png")
print("βœ… Test image loaded successfully")
# Process with AI
start_time = time.time()
result = pipe(img)
process_time = time.time() - start_time
extracted_text = result[0]['generated_text']
print(f"βœ… Processing completed in {process_time:.1f} seconds")
print(f"\nπŸ“ EXTRACTED TEXT:")
print(f"'{extracted_text}'")
# Test 3: Simple parsing
print(f"\nπŸ”§ TESTING BILL PARSING:")
# Look for key information
import re
# Check for Singtel
if "singtel" in extracted_text.lower():
print("βœ… 'Singtel' detected in text")
else:
print("❌ 'Singtel' not clearly detected")
# Check for numbers (amounts, account numbers, etc.)
numbers = re.findall(r'\d+', extracted_text)
if numbers:
print(f"βœ… Numbers detected: {numbers}")
else:
print("❌ No numbers detected")
# Check for currency patterns
currency = re.findall(r'\$[\d.]+', extracted_text)
if currency:
print(f"βœ… Currency amounts: {currency}")
else:
print("⚠️ No currency patterns detected")
print(f"\nπŸŽ‰ SUCCESS! Your TrOCR model is working!")
return True
else:
print("❌ Could not load test image")
return False
except Exception as e:
print(f"❌ Error: {e}")
return False
def test_real_bill_processing():
"""Show how to process a real bill"""
print(f"\n🎯 READY FOR REAL BILLS!")
print("=" * 30)
print("To process your Singtel bill:")
print()
print("1. πŸ“Έ Take a clear photo of your bill")
print("2. πŸ’Ύ Save it as 'my_bill.jpg' in this folder")
print("3. πŸš€ Run this code:")
print()
print("```python")
print("from transformers import pipeline")
print("from PIL import Image")
print()
print("# Load the model (fast now - already downloaded!)")
print("pipe = pipeline('image-to-text', model='microsoft/trocr-base-handwritten')")
print()
print("# Process your bill")
print("image = Image.open('my_bill.jpg')")
print("result = pipe(image)")
print("text = result[0]['generated_text']")
print()
print("print(f'Extracted: {text}')")
print("```")
print()
print("4. πŸ”§ Use singtel_scanner.py for advanced parsing!")
if __name__ == "__main__":
print("FINAL MODEL VERIFICATION")
print("Models are downloaded - testing now!")
print("=" * 50)
success = test_downloaded_models()
if success:
test_real_bill_processing()
print(f"\nβœ… COMPLETE! Your Singtel Bill Scanner is ready to use!")
print(f"⚑ All future runs will be instant (models cached)")
else:
print(f"\n❌ Something went wrong. Check the error above.")
print("\n" + "=" * 50)
input("Press Enter to finish...")