Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """ | |
| Test script to validate Hugging Face only loading. | |
| """ | |
| import os | |
| import warnings | |
| warnings.filterwarnings('ignore') | |
| def test_hf_only_loading(): | |
| """Test that the loader only works with Hugging Face.""" | |
| print("π€ TESTING HUGGING FACE ONLY LOADING") | |
| print("=" * 50) | |
| from data_loader import AgriculturalDataLoader | |
| # Check if HF token is available | |
| hf_token = os.environ.get("HF_TOKEN") | |
| if not hf_token: | |
| print("β οΈ No HF_TOKEN found in environment variables") | |
| print("π‘ Set HF_TOKEN to test Hugging Face loading") | |
| print("π§ For this test, we'll try without token (may fail)") | |
| try: | |
| # Create loader (HF only) | |
| loader = AgriculturalDataLoader( | |
| dataset_id="HackathonCRA/2024", | |
| hf_token=hf_token | |
| ) | |
| print(f"π€ Attempting to load from dataset: {loader.dataset_id}") | |
| # Load data | |
| df = loader.load_all_files() | |
| print(f"β Success! Loaded {len(df):,} records from Hugging Face") | |
| print(f"π Years: {sorted(df['year'].unique())}") | |
| print(f"π± Crops: {df['crop_type'].nunique()}") | |
| print(f"π Plots: {df['plot_name'].nunique()}") | |
| print(f"π Herbicide applications: {df['is_herbicide'].sum()}") | |
| return True | |
| except Exception as e: | |
| print(f"β Failed to load from Hugging Face: {e}") | |
| print("π‘ This is expected if the dataset doesn't exist yet") | |
| print("π§ Make sure to upload your dataset to HF Hub first") | |
| return False | |
| def test_no_local_fallback(): | |
| """Test that there's no local fallback.""" | |
| print("\nπ« TESTING NO LOCAL FALLBACK") | |
| print("=" * 50) | |
| from data_loader import AgriculturalDataLoader | |
| try: | |
| # Create loader with non-existent dataset | |
| loader = AgriculturalDataLoader( | |
| dataset_id="nonexistent/dataset" | |
| ) | |
| # This should fail without falling back to local | |
| df = loader.load_all_files() | |
| print(f"β Unexpected success - loaded {len(df)} records") | |
| print("β οΈ This suggests local fallback is still active") | |
| return False | |
| except Exception as e: | |
| print(f"β Expected failure: {e}") | |
| print("β Confirmed: No local fallback, HF only") | |
| return True | |
| def test_simple_usage(): | |
| """Test simple usage pattern.""" | |
| print("\nπ SIMPLE USAGE EXAMPLE") | |
| print("=" * 50) | |
| print("π‘ Recommended usage pattern:") | |
| print() | |
| usage_code = ''' | |
| from data_loader import AgriculturalDataLoader | |
| # Simple HF-only loader | |
| loader = AgriculturalDataLoader(dataset_id="HackathonCRA/2024") | |
| # Load data (will use HF_TOKEN from environment) | |
| df = loader.load_all_files() | |
| # Analyze data | |
| print(f"Loaded {len(df)} records from Hugging Face") | |
| ''' | |
| print(usage_code) | |
| try: | |
| from data_loader import AgriculturalDataLoader | |
| loader = AgriculturalDataLoader(dataset_id="HackathonCRA/2024") | |
| print("β Loader created successfully") | |
| print(f"π― Target dataset: {loader.dataset_id}") | |
| print(f"π Using token: {'Yes' if loader.hf_token else 'No (from env)'}") | |
| return True | |
| except Exception as e: | |
| print(f"β Failed to create loader: {e}") | |
| return False | |
| def main(): | |
| """Run all tests.""" | |
| print("π HUGGING FACE ONLY - VALIDATION TESTS") | |
| print("=" * 60) | |
| print() | |
| results = [] | |
| # Test 1: HF loading | |
| results.append(("HF Only Loading", test_hf_only_loading())) | |
| # Test 2: No local fallback | |
| results.append(("No Local Fallback", test_no_local_fallback())) | |
| # Test 3: Simple usage | |
| results.append(("Simple Usage", test_simple_usage())) | |
| # Summary | |
| print("\nπ TEST SUMMARY") | |
| print("=" * 30) | |
| passed = 0 | |
| for test_name, result in results: | |
| status = "β PASS" if result else "β FAIL" | |
| print(f"{test_name:<20} {status}") | |
| if result: | |
| passed += 1 | |
| print(f"\nπ― Results: {passed}/{len(results)} tests passed") | |
| if passed >= 2: # Allow HF loading to fail if dataset doesn't exist | |
| print("π Validation successful! Loader is HF-only.") | |
| else: | |
| print("β οΈ Validation issues detected.") | |
| print("\nπ DEPLOYMENT CHECKLIST:") | |
| print("β Remove local file dependencies") | |
| print("β HF-only data loading") | |
| print("β No fallback mechanisms") | |
| print("π² Upload dataset to HF Hub") | |
| print("π² Set HF_TOKEN in production") | |
| print("π² Test with real HF dataset") | |
| if __name__ == "__main__": | |
| main() | |