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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Local testing script for the Subdivision Plan Analyzer
|
| 4 |
+
Run this before deploying to Hugging Face Spaces to ensure everything works
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import sys
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
def test_imports():
|
| 11 |
+
"""Test if all required packages can be imported"""
|
| 12 |
+
print("Testing imports...")
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
import gradio as gr
|
| 16 |
+
print("β Gradio imported successfully")
|
| 17 |
+
except ImportError as e:
|
| 18 |
+
print(f"β Failed to import Gradio: {e}")
|
| 19 |
+
return False
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
import pandas as pd
|
| 23 |
+
print("β Pandas imported successfully")
|
| 24 |
+
except ImportError as e:
|
| 25 |
+
print(f"β Failed to import Pandas: {e}")
|
| 26 |
+
return False
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
import numpy as np
|
| 30 |
+
print("β NumPy imported successfully")
|
| 31 |
+
except ImportError as e:
|
| 32 |
+
print(f"β Failed to import NumPy: {e}")
|
| 33 |
+
return False
|
| 34 |
+
|
| 35 |
+
try:
|
| 36 |
+
import PIL
|
| 37 |
+
print("β PIL imported successfully")
|
| 38 |
+
except ImportError as e:
|
| 39 |
+
print(f"β Failed to import PIL: {e}")
|
| 40 |
+
return False
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
import easyocr
|
| 44 |
+
print("β EasyOCR imported successfully")
|
| 45 |
+
except ImportError as e:
|
| 46 |
+
print(f"β Failed to import EasyOCR: {e}")
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
import cv2
|
| 51 |
+
print("β OpenCV imported successfully")
|
| 52 |
+
except ImportError as e:
|
| 53 |
+
print(f"β Failed to import OpenCV: {e}")
|
| 54 |
+
return False
|
| 55 |
+
|
| 56 |
+
try:
|
| 57 |
+
import skimage
|
| 58 |
+
print("β scikit-image imported successfully")
|
| 59 |
+
except ImportError as e:
|
| 60 |
+
print(f"β Failed to import scikit-image: {e}")
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
import scipy
|
| 65 |
+
print("β SciPy imported successfully")
|
| 66 |
+
except ImportError as e:
|
| 67 |
+
print(f"β Failed to import SciPy: {e}")
|
| 68 |
+
return False
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
import matplotlib
|
| 72 |
+
print("β Matplotlib imported successfully")
|
| 73 |
+
except ImportError as e:
|
| 74 |
+
print(f"β Failed to import Matplotlib: {e}")
|
| 75 |
+
return False
|
| 76 |
+
|
| 77 |
+
return True
|
| 78 |
+
|
| 79 |
+
def test_easyocr_init():
|
| 80 |
+
"""Test if EasyOCR can be initialized"""
|
| 81 |
+
print("\nTesting EasyOCR initialization...")
|
| 82 |
+
|
| 83 |
+
try:
|
| 84 |
+
import easyocr
|
| 85 |
+
reader = easyocr.Reader(['en'])
|
| 86 |
+
print("β EasyOCR reader initialized successfully")
|
| 87 |
+
print(" Note: First run will download models (~64MB)")
|
| 88 |
+
return True
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"β Failed to initialize EasyOCR: {e}")
|
| 91 |
+
return False
|
| 92 |
+
|
| 93 |
+
def test_app_import():
|
| 94 |
+
"""Test if the main app can be imported"""
|
| 95 |
+
print("\nTesting app.py import...")
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
import app
|
| 99 |
+
print("β app.py imported successfully")
|
| 100 |
+
return True
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"β Failed to import app.py: {e}")
|
| 103 |
+
print(f" Error: {e}")
|
| 104 |
+
return False
|
| 105 |
+
|
| 106 |
+
def create_test_image():
|
| 107 |
+
"""Create a simple test image"""
|
| 108 |
+
print("\nCreating test image...")
|
| 109 |
+
|
| 110 |
+
try:
|
| 111 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 112 |
+
|
| 113 |
+
# Create a white image
|
| 114 |
+
img = Image.new('RGB', (800, 600), color='white')
|
| 115 |
+
draw = ImageDraw.Draw(img)
|
| 116 |
+
|
| 117 |
+
# Draw some lot boundaries
|
| 118 |
+
draw.rectangle([100, 100, 300, 250], outline='black', width=2)
|
| 119 |
+
draw.rectangle([300, 100, 500, 250], outline='black', width=2)
|
| 120 |
+
draw.rectangle([100, 250, 300, 400], outline='black', width=2)
|
| 121 |
+
draw.rectangle([300, 250, 500, 400], outline='black', width=2)
|
| 122 |
+
|
| 123 |
+
# Add lot numbers and areas
|
| 124 |
+
try:
|
| 125 |
+
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
|
| 126 |
+
except:
|
| 127 |
+
font = ImageFont.load_default()
|
| 128 |
+
|
| 129 |
+
draw.text((150, 120), "Lot 692", fill='black', font=font)
|
| 130 |
+
draw.text((150, 160), "234mΒ²", fill='black', font=font)
|
| 131 |
+
draw.text((150, 180), "15.6", fill='black', font=font)
|
| 132 |
+
draw.text((150, 200), "15.0", fill='black', font=font)
|
| 133 |
+
|
| 134 |
+
draw.text((350, 120), "Lot 690", fill='black', font=font)
|
| 135 |
+
draw.text((350, 160), "117mΒ²", fill='black', font=font)
|
| 136 |
+
draw.text((350, 180), "7.8", fill='black', font=font)
|
| 137 |
+
draw.text((350, 200), "15.0", fill='black', font=font)
|
| 138 |
+
|
| 139 |
+
img.save('test_subdivision.png')
|
| 140 |
+
print("β Test image created: test_subdivision.png")
|
| 141 |
+
return True
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print(f"β Failed to create test image: {e}")
|
| 144 |
+
return False
|
| 145 |
+
|
| 146 |
+
def main():
|
| 147 |
+
"""Run all tests"""
|
| 148 |
+
print("=== Subdivision Plan Analyzer - Local Testing ===\n")
|
| 149 |
+
|
| 150 |
+
all_passed = True
|
| 151 |
+
|
| 152 |
+
# Test imports
|
| 153 |
+
if not test_imports():
|
| 154 |
+
all_passed = False
|
| 155 |
+
print("\nβ οΈ Some imports failed. Please install missing packages:")
|
| 156 |
+
print(" pip install -r requirements.txt")
|
| 157 |
+
|
| 158 |
+
# Test EasyOCR
|
| 159 |
+
if not test_easyocr_init():
|
| 160 |
+
all_passed = False
|
| 161 |
+
print("\nβ οΈ EasyOCR initialization failed.")
|
| 162 |
+
|
| 163 |
+
# Test app import
|
| 164 |
+
if not test_app_import():
|
| 165 |
+
all_passed = False
|
| 166 |
+
print("\nβ οΈ Failed to import app.py. Check for syntax errors.")
|
| 167 |
+
|
| 168 |
+
# Create test image
|
| 169 |
+
if not create_test_image():
|
| 170 |
+
all_passed = False
|
| 171 |
+
print("\nβ οΈ Failed to create test image.")
|
| 172 |
+
|
| 173 |
+
# Summary
|
| 174 |
+
print("\n=== Test Summary ===")
|
| 175 |
+
if all_passed:
|
| 176 |
+
print("β
All tests passed! Your app is ready for deployment.")
|
| 177 |
+
print("\nTo run the app locally:")
|
| 178 |
+
print(" python app.py")
|
| 179 |
+
print("\nTo deploy to Hugging Face Spaces:")
|
| 180 |
+
print(" 1. Create a new Space on huggingface.co")
|
| 181 |
+
print(" 2. Upload app.py, requirements.txt, and README.md")
|
| 182 |
+
print(" 3. Wait for the build to complete")
|
| 183 |
+
else:
|
| 184 |
+
print("β Some tests failed. Please fix the issues before deployment.")
|
| 185 |
+
|
| 186 |
+
return all_passed
|
| 187 |
+
|
| 188 |
+
if __name__ == "__main__":
|
| 189 |
+
success = main()
|
| 190 |
+
sys.exit(0 if success else 1)
|