File size: 6,699 Bytes
5977115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/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()