#!/usr/bin/env python3 """ Example usage of the agricultural data loader with Hugging Face integration. Shows different ways to load and use the data. """ import os import warnings warnings.filterwarnings('ignore') from data_loader import AgriculturalDataLoader from analysis_tools import AgriculturalAnalyzer def example_local_usage(): """Example: Load from local files.""" print("šŸ“ EXAMPLE 1: Loading from local files") print("-" * 40) # Create loader for local files loader = AgriculturalDataLoader.create_local_loader( data_path="/Users/tracyandre/Downloads/OneDrive_1_9-17-2025" ) # Load and analyze data df = loader.load_all_files() print(f"āœ… Loaded {len(df):,} records from local files") # Basic analysis analyzer = AgriculturalAnalyzer(loader) trends = analyzer.analyze_weed_pressure_trends() print(f"šŸ“Š Average IFT: {trends['summary']['mean_ift']:.2f}") return df def example_hf_usage(): """Example: Load from Hugging Face (if available).""" print("\nšŸ¤— EXAMPLE 2: Loading from Hugging Face") print("-" * 40) # Check if HF token is available if not os.environ.get("HF_TOKEN"): print("āš ļø No HF_TOKEN found - skipping HF example") print("šŸ’” Set HF_TOKEN environment variable to use this feature") return None try: # Create loader for Hugging Face loader = AgriculturalDataLoader.create_hf_loader( dataset_id="HackathonCRA/2024" ) # Load and analyze data df = loader.load_all_files() print(f"āœ… Loaded {len(df):,} records from Hugging Face") # Basic analysis analyzer = AgriculturalAnalyzer(loader) trends = analyzer.analyze_weed_pressure_trends() print(f"šŸ“Š Average IFT: {trends['summary']['mean_ift']:.2f}") return df except Exception as e: print(f"āŒ Failed to load from Hugging Face: {e}") return None def example_automatic_fallback(): """Example: Automatic fallback from HF to local.""" print("\nšŸ”„ EXAMPLE 3: Automatic fallback") print("-" * 40) # Create loader with HF preferred but local fallback loader = AgriculturalDataLoader( data_path="/Users/tracyandre/Downloads/OneDrive_1_9-17-2025", dataset_id="HackathonCRA/2024", use_hf=True # Try HF first ) # This will try HF first, then fallback to local if needed df = loader.load_all_files() print(f"āœ… Loaded {len(df):,} records (with automatic source selection)") return df def example_dynamic_switching(): """Example: Dynamic switching between sources.""" print("\nšŸ”€ EXAMPLE 4: Dynamic source switching") print("-" * 40) # Create loader loader = AgriculturalDataLoader( data_path="/Users/tracyandre/Downloads/OneDrive_1_9-17-2025", dataset_id="HackathonCRA/2024" ) # Load from local first loader.set_data_source(use_hf=False) df_local = loader.load_all_files() print(f"šŸ“ Local source: {len(df_local):,} records") # Switch to HF (if available) if os.environ.get("HF_TOKEN"): try: loader.set_data_source(use_hf=True) df_hf = loader.load_all_files() print(f"šŸ¤— HF source: {len(df_hf):,} records") # Compare if len(df_local) == len(df_hf): print("āœ… Data consistency verified") else: print(f"āš ļø Data mismatch: {abs(len(df_local) - len(df_hf))} record difference") except Exception as e: print(f"šŸ¤— HF switching failed: {e}") else: print("āš ļø No HF_TOKEN - skipping HF switch test") return df_local def example_production_deployment(): """Example: Production deployment configuration.""" print("\nšŸš€ EXAMPLE 5: Production deployment setup") print("-" * 40) # Production configuration # This is how you'd set it up for Hugging Face Spaces deployment print("šŸ’” For Hugging Face Spaces deployment:") print("1. Set HF_TOKEN as a Space secret") print("2. Configure the loader as follows:") print() config_code = ''' # In your app.py or gradio_app.py import os from data_loader import AgriculturalDataLoader # Production configuration hf_token = os.environ.get("HF_TOKEN") dataset_id = "HackathonCRA/2024" if hf_token: # Use HF dataset in production data_loader = AgriculturalDataLoader.create_hf_loader( dataset_id=dataset_id, hf_token=hf_token ) print("šŸ¤— Using Hugging Face dataset") else: # Fallback for local development data_loader = AgriculturalDataLoader.create_local_loader( data_path="./data" # Local data directory ) print("šŸ“ Using local files") ''' print(config_code) # Example of actual production setup try: hf_token = os.environ.get("HF_TOKEN") if hf_token: loader = AgriculturalDataLoader.create_hf_loader("HackathonCRA/2024", hf_token) print("āœ… Production setup: HF dataset configured") else: loader = AgriculturalDataLoader.create_local_loader("/Users/tracyandre/Downloads/OneDrive_1_9-17-2025") print("āœ… Development setup: Local files configured") df = loader.load_all_files() print(f"šŸ“Š Ready for production: {len(df):,} records available") except Exception as e: print(f"āŒ Production setup failed: {e}") def main(): """Run all examples.""" print("🚜 AGRICULTURAL DATA LOADER - USAGE EXAMPLES") print("=" * 60) # Run examples example_local_usage() example_hf_usage() example_automatic_fallback() example_dynamic_switching() example_production_deployment() print("\n" + "=" * 60) print("šŸŽÆ SUMMARY") print("=" * 60) print(""" The AgriculturalDataLoader now supports: āœ… Local file loading (CSV/Excel) āœ… Hugging Face dataset loading āœ… Automatic fallback (HF → Local) āœ… Dynamic source switching āœ… Production deployment ready Key benefits: šŸ”„ Seamless data source switching šŸš€ Cloud deployment ready šŸ“Š Same analysis tools work with both sources šŸ”§ Easy configuration management """) print("šŸ› ļø Next steps:") print("1. Upload your dataset to Hugging Face Hub") print("2. Set HF_TOKEN environment variable") print("3. Deploy to Hugging Face Spaces") print("4. Enjoy cloud-based agricultural analysis!") if __name__ == "__main__": main()