Spaces:
Sleeping
Sleeping
| #!/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() | |