Spaces:
Sleeping
Sleeping
File size: 9,856 Bytes
7ca901a |
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
"""
Hugging Face integration for dataset management and model deployment.
"""
import os
import pandas as pd
from datasets import Dataset, DatasetDict
from huggingface_hub import HfApi, create_repo, upload_file
from pathlib import Path
from typing import Optional, Dict, Any
import json
class HuggingFaceIntegration:
"""Handles Hugging Face dataset and model operations."""
def __init__(self, token: Optional[str] = None, dataset_id: str = "HackathonCRA/2024"):
self.token = token or os.environ.get("HF_TOKEN")
self.dataset_id = dataset_id
self.api = HfApi(token=self.token) if self.token else None
def prepare_dataset_from_local_files(self, data_path: str) -> Dataset:
"""Prepare dataset from local CSV/Excel files."""
from data_loader import AgriculturalDataLoader
# Load and combine all data files
loader = AgriculturalDataLoader(data_path=data_path)
df = loader.load_all_files()
# Convert to Hugging Face Dataset
dataset = Dataset.from_pandas(df)
return dataset
def upload_dataset(self, data_path: str, private: bool = False) -> str:
"""Upload agricultural data to Hugging Face Hub."""
if not self.token:
raise ValueError("HF_TOKEN required for uploading")
# Prepare dataset
dataset = self.prepare_dataset_from_local_files(data_path)
# Create repository if it doesn't exist
try:
create_repo(
repo_id=self.dataset_id,
token=self.token,
repo_type="dataset",
private=private,
exist_ok=True
)
except Exception as e:
print(f"Repository might already exist: {e}")
# Upload dataset
dataset.push_to_hub(
repo_id=self.dataset_id,
token=self.token,
private=private
)
return f"Dataset uploaded to https://huggingface.co/datasets/{self.dataset_id}"
def create_dataset_card(self) -> str:
"""Create a dataset card for the agricultural data."""
card_content = """
---
license: cc-by-4.0
task_categories:
- tabular-regression
- time-series-forecasting
language:
- fr
tags:
- agriculture
- herbicides
- weed-pressure
- crop-rotation
- france
- bretagne
size_categories:
- 1K<n<10K
---
# 🚜 Station Expérimentale de Kerguéhennec - Agricultural Interventions Dataset
## Dataset Description
This dataset contains agricultural intervention records from the Station Expérimentale de Kerguéhennec in Brittany, France, spanning from 2014 to 2024. The data includes detailed information about agricultural practices, crop rotations, herbicide treatments, and field management operations.
## Dataset Summary
- **Source**: Station Expérimentale de Kerguéhennec
- **Time Period**: 2014-2024
- **Location**: Brittany, France
- **Records**: ~10,000+ intervention records
- **Format**: CSV/Excel exports from farm management system
## Use Cases
This dataset is particularly valuable for:
1. **Weed Pressure Analysis**: Calculate and predict Treatment Frequency Index (IFT) for herbicides
2. **Crop Rotation Optimization**: Analyze the impact of different crop sequences on pest pressure
3. **Sustainable Agriculture**: Support reduction of herbicide use while maintaining productivity
4. **Precision Agriculture**: Identify suitable plots for sensitive crops (peas, beans)
5. **Agricultural Research**: Study relationships between practices and outcomes
## Data Fields
### Core Fields
- `millesime`: Year of intervention
- `nomparc`: Plot/field name
- `surfparc`: Plot surface area (hectares)
- `libelleusag`: Crop type/usage
- `datedebut`/`datefin`: Intervention start/end dates
- `libevenem`: Intervention type
- `familleprod`: Product family (herbicides, fungicides, etc.)
- `produit`: Specific product used
- `quantitetot`: Total quantity applied
- `unite`: Unit of measurement
### Derived Fields
- `year`: Intervention year
- `crop_type`: Standardized crop classification
- `is_herbicide`: Boolean flag for herbicide treatments
- `ift_herbicide`: Treatment Frequency Index calculation
## Data Quality
- All personal identifying information has been removed
- Geographic coordinates are generalized to protect farm location
- Product codes (AMM) are preserved for regulatory analysis
- Missing values are clearly marked and documented
## Methodology
### IFT Calculation
The Treatment Frequency Index (IFT) is calculated as:
```
IFT = Number of applications / Plot surface area
```
This metric is crucial for:
- Regulatory compliance monitoring
- Sustainable practice assessment
- Risk evaluation for sensitive crops
## Applications
### 1. Weed Pressure Prediction
Use machine learning models to predict future IFT values based on:
- Historical treatment patterns
- Crop rotation sequences
- Environmental factors
- Plot characteristics
### 2. Sustainable Plot Selection
Identify plots suitable for sensitive crops (peas, beans) by:
- Analyzing historical IFT trends
- Evaluating rotation impacts
- Assessing risk levels
### 3. Alternative Strategy Development
Support herbicide reduction strategies through:
- Product usage pattern analysis
- Rotation optimization recommendations
- Risk assessment frameworks
## Citation
If you use this dataset in your research, please cite:
```
@dataset{hackathon_cra_2024,
title={Station Expérimentale de Kerguéhennec Agricultural Interventions Dataset},
author={Hackathon CRA Team},
year={2024},
publisher={Hugging Face},
url={https://huggingface.co/datasets/HackathonCRA/2024}
}
```
## License
This dataset is released under CC-BY-4.0 license, allowing for both commercial and research use with proper attribution.
## Contact
For questions about this dataset or collaboration opportunities, please contact the research team through the Hugging Face dataset page.
---
**Keywords**: agriculture, herbicides, crop rotation, sustainable farming, France, Brittany, IFT, weed management, precision agriculture
"""
return card_content
def upload_app_space(self, local_app_path: str, space_name: str = "agricultural-analysis") -> str:
"""Upload the Gradio app as a Hugging Face Space."""
if not self.token:
raise ValueError("HF_TOKEN required for uploading")
repo_id = f"{self.api.whoami()['name']}/{space_name}"
# Create Space repository
try:
create_repo(
repo_id=repo_id,
token=self.token,
repo_type="space",
space_sdk="gradio",
private=False,
exist_ok=True
)
except Exception as e:
print(f"Space might already exist: {e}")
# Upload files
app_files = [
"app.py",
"requirements.txt",
"gradio_app.py",
"data_loader.py",
"analysis_tools.py",
"mcp_server.py",
"README.md"
]
for file_name in app_files:
file_path = Path(local_app_path) / file_name
if file_path.exists():
upload_file(
path_or_fileobj=str(file_path),
path_in_repo=file_name,
repo_id=repo_id,
repo_type="space",
token=self.token
)
print(f"Uploaded {file_name}")
return f"Space created at https://huggingface.co/spaces/{repo_id}"
def create_space_readme(self) -> str:
"""Create README for Hugging Face Space."""
readme_content = """
---
title: Agricultural Analysis - Kerguéhennec
emoji: 🚜
colorFrom: green
colorTo: blue
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
license: cc-by-4.0
---
# 🚜 Agricultural Analysis - Station de Kerguéhennec
Outil d'analyse des données agricoles pour l'optimisation des pratiques phytosanitaires et l'identification des parcelles adaptées aux cultures sensibles.
## Fonctionnalités
- 📊 Analyse des données d'interventions agricoles
- 🌿 Évaluation de la pression adventices (IFT)
- 🔮 Prédictions pour les 3 prochaines années
- 🔄 Analyse de l'impact des rotations culturales
- 💊 Étude des herbicides utilisés
- 🎯 Identification des parcelles pour cultures sensibles
## Utilisation
1. Sélectionnez l'onglet correspondant à votre analyse
2. Configurez les filtres selon vos besoins
3. Lancez l'analyse pour obtenir les résultats
4. Explorez les visualisations interactives
## Données
Basé sur les données de la Station Expérimentale de Kerguéhennec (2014-2024).
"""
return readme_content
def setup_environment_variables(self) -> Dict[str, str]:
"""Setup environment variables for Hugging Face deployment."""
env_vars = {
"HF_TOKEN": self.token or "your_hf_token_here",
"DATASET_ID": self.dataset_id,
"GRADIO_SERVER_NAME": "0.0.0.0",
"GRADIO_SERVER_PORT": "7860"
}
return env_vars
# Usage example
if __name__ == "__main__":
# Initialize HF integration
hf = HuggingFaceIntegration()
# Upload dataset (requires HF_TOKEN)
if hf.token:
try:
result = hf.upload_dataset("/Users/tracyandre/Downloads/OneDrive_1_9-17-2025")
print(result)
except Exception as e:
print(f"Dataset upload failed: {e}")
# Create dataset card
card = hf.create_dataset_card()
print("Dataset card created")
# Show environment setup
env_vars = hf.setup_environment_variables()
print("Environment variables:", env_vars)
|