File size: 9,888 Bytes
505fc99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Deployment Guide - Hugging Face Spaces

This guide walks you through deploying the Plant Disease Detection app to Hugging Face Spaces.

## Prerequisites

- Hugging Face account (free): https://huggingface.co/join
- Trained model weights
- Git installed locally

## Deployment Options

You have three options for deploying your model:

### Option 1: Upload Model Weights to Space (Recommended)
**Pros:** Simple, no external dependencies
**Cons:** Weights are part of the repo

### Option 2: Load from Hugging Face Model Hub
**Pros:** Separate model versioning, smaller Space repo
**Cons:** Need to upload model separately

### Option 3: Fetch from ClearML
**Pros:** Direct integration with training pipeline
**Cons:** Requires ClearML credentials in Space secrets

## Step-by-Step: Option 1 (Upload Weights)

### 1. Create a Hugging Face Space

1. Go to https://huggingface.co/spaces
2. Click "Create new Space"
3. Fill in details:
   - **Name:** `plant-disease-detection`
   - **License:** Apache 2.0
   - **Space SDK:** Gradio
   - **Visibility:** Public
4. Click "Create Space"

### 2. Prepare Files for Deployment

Create a new directory for your Space:

```bash
mkdir hf-space-deployment
cd hf-space-deployment
```

Copy and flatten the UI structure:

```bash
# Copy main app file
cp ../ui/app.py ./

# Copy supporting files
cp ../ui/config.py ./
cp ../ui/model_loader.py ./
cp ../ui/utils.py ./
cp ../models/mock_model.py ./

# Copy requirements
cp ../requirements.txt ./

# Copy your trained model weights
cp ../path/to/best_model.pth ./
```

### 3. Modify app.py for Deployment

Edit `app.py` to work with the flat structure:

```python
# Change imports at the top of app.py
import config
from model_loader import ModelLoader
from utils import preprocess_image, postprocess_predictions, ...
from mock_model import create_mock_predictions

# At the bottom, change:
demo = create_interface(use_mock=False)  # Use real model
demo.launch()  # Remove server_name and server_port
```

### 4. Modify model_loader.py

Update the model loading to use your actual architecture:

```python
# In _load_real_model method, replace mock model with your actual model:

def _load_real_model(self, model_name, model_path=None):
    if model_config["model_type"] == "cnn":
        # Import your actual model class
        from your_model_module import YourCNNModel
        model = YourCNNModel(num_classes=len(config.CLASS_NAMES))

    # Load weights
    if model_path:
        model.load_state_dict(torch.load(model_path, map_location=self.device))
    else:
        # Load default model
        model.load_state_dict(torch.load("best_model.pth", map_location=self.device))

    return model
```

### 5. Create README for Space

Create `README.md` in the deployment directory:

```markdown
---
title: Plant Disease Detection
emoji: 🌱
colorFrom: green
colorTo: blue
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
license: apache-2.0
---

# 🌱 Plant Disease Detection

AI-powered plant disease detection from leaf images.

## About

This application uses a Convolutional Neural Network (CNN) trained on the PlantVillage
dataset to identify plant diseases from leaf images.

**Developed by:** [Your Team Name]
**Course:** 5CCSAGAP - AI Group Project, King's College London
**Academic Year:** 2024-2025

## Features

- Upload plant leaf images for disease detection
- Support for 39 different plant disease categories
- Multiple model options (CNN, Transfer Learning)
- Batch processing capability
- Confidence threshold adjustment

## How to Use

1. Go to the "Single Image" tab
2. Upload a photo of a plant leaf
3. Click "Predict Disease"
4. View the top predictions with confidence scores

## Dataset

Trained on the [PlantVillage dataset](https://huggingface.co/datasets/EdBianchi/plant-village)
containing 55,400 images across 39 disease categories.

## Model Performance

- **Test Accuracy:** [Add your test accuracy]
- **Architecture:** Custom CNN / ResNet18 Transfer Learning
- **Training Framework:** PyTorch
- **Experiment Tracking:** ClearML

## Links

- **GitHub:** [Add your repo link]
- **Team Project:** [Add project documentation link]

---

**Disclaimer:** This is an educational project. Predictions should be verified by agricultural experts.
```

### 6. Upload to Hugging Face Space

Initialize Git and push:

```bash
# Initialize git
git init

# Add Hugging Face Space as remote
git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/plant-disease-detection

# Create .gitignore
cat > .gitignore << EOF
__pycache__/
*.pyc
.DS_Store
EOF

# Add files
git add .

# Commit
git commit -m "Initial deployment of Plant Disease Detection app"

# Push to Hugging Face
git push origin main
```

You'll be prompted for credentials:
- **Username:** Your HF username
- **Password:** Your HF access token (create at https://huggingface.co/settings/tokens)

### 7. Verify Deployment

1. Go to your Space URL: `https://huggingface.co/spaces/YOUR_USERNAME/plant-disease-detection`
2. Wait for the build to complete (check "Building" status)
3. Test the app with sample images

## Step-by-Step: Option 2 (HF Model Hub)

### 1. Upload Model to HF Model Hub

```python
from huggingface_hub import HfApi, create_repo

# Create repository for model
repo_id = "YOUR_USERNAME/plant-disease-cnn"
create_repo(repo_id=repo_id, repo_type="model", exist_ok=True)

# Upload model
api = HfApi()
api.upload_file(
    path_or_fileobj="path/to/best_model.pth",
    path_in_repo="pytorch_model.pth",
    repo_id=repo_id,
    repo_type="model"
)

# Create model card
model_card = """
---
license: apache-2.0
tags:
- image-classification
- plant-disease
- pytorch
---

# Plant Disease CNN

CNN model for plant disease detection, trained on PlantVillage dataset.

## Model Details

- **Architecture:** Custom CNN
- **Framework:** PyTorch
- **Classes:** 39 plant disease categories
- **Input Size:** 256x256 RGB images
- **Test Accuracy:** [Add your accuracy]

## Usage

```python
import torch
from huggingface_hub import hf_hub_download

model_path = hf_hub_download(
    repo_id="YOUR_USERNAME/plant-disease-cnn",
    filename="pytorch_model.pth"
)
model.load_state_dict(torch.load(model_path))
```
"""

api.upload_file(
    path_or_fileobj=model_card.encode(),
    path_in_repo="README.md",
    repo_id=repo_id,
    repo_type="model"
)
```

### 2. Modify model_loader.py

```python
# In model_loader.py, set default to load from HF:

def _load_real_model(self, model_name, model_path=None):
    if model_path is None:
        # Download from HF Hub
        from huggingface_hub import hf_hub_download
        model_path = hf_hub_download(
            repo_id="YOUR_USERNAME/plant-disease-cnn",
            filename="pytorch_model.pth"
        )

    # Load model architecture
    model = YourCNNModel(num_classes=39)
    model.load_state_dict(torch.load(model_path, map_location=self.device))

    return model
```

### 3. Deploy to Space

Follow steps 1-7 from Option 1, but you don't need to include the .pth file.

## Step-by-Step: Option 3 (ClearML)

### 1. Get ClearML Credentials

1. Log in to https://5ccsagap.er.kcl.ac.uk/
2. Go to Settings β†’ Workspace β†’ Create new credentials
3. Copy the credentials

### 2. Add Secrets to Space

1. Go to your Space settings
2. Click "Repository secrets"
3. Add three secrets:
   - `CLEARML_API_HOST`: `https://5ccsagap.er.kcl.ac.uk`
   - `CLEARML_API_ACCESS_KEY`: Your access key
   - `CLEARML_API_SECRET_KEY`: Your secret key

### 3. Modify model_loader.py

```python
def _load_real_model(self, model_name, model_path=None):
    if model_path is None:
        # Load from ClearML
        import os
        from clearml import Task, Model

        # Initialize ClearML with environment variables
        Task.init(
            project_name="Plant Disease Detection",
            task_name="UI Model Loading",
            api_host=os.environ.get("CLEARML_API_HOST"),
            api_access_key=os.environ.get("CLEARML_API_ACCESS_KEY"),
            api_secret_key=os.environ.get("CLEARML_API_SECRET_KEY")
        )

        # Get the model
        model_id = "YOUR_MODEL_ID"  # Get this from ClearML
        model_obj = Model(model_id)
        model_path = model_obj.get_local_copy()

    # Load model
    model = YourCNNModel(num_classes=39)
    model.load_state_dict(torch.load(model_path, map_location=self.device))

    return model
```

## Troubleshooting

### Build Failures

**Error: "Out of Memory"**
- Your model might be too large
- Try using a smaller model or quantization

**Error: "Module not found"**
- Check all dependencies are in requirements.txt
- Verify imports work with flat file structure

### Runtime Errors

**Error: "Model file not found"**
- Verify the .pth file is uploaded
- Check file path in model_loader.py

**Error: "Incompatible architecture"**
- Ensure your model class definition matches the saved weights
- Check num_classes=39

## Updating the Deployment

To update your deployed app:

```bash
# Make changes to files
# Commit and push
git add .
git commit -m "Update: description of changes"
git push origin main
```

The Space will automatically rebuild.

## Best Practices

1. **Test locally first:** Always test the app locally before deploying
2. **Use small example images:** Don't upload large images to the repo
3. **Version your models:** Tag model versions in HF Hub or ClearML
4. **Monitor usage:** Check Space analytics to see usage patterns
5. **Update README:** Keep the model card and space README up to date

## Resources

- [HF Spaces Documentation](https://huggingface.co/docs/hub/spaces)
- [Gradio Documentation](https://gradio.app/docs/)
- [HF Hub Python API](https://huggingface.co/docs/huggingface_hub/)

## Support

If you encounter issues:
1. Check the Space build logs
2. Test locally with the exact same file structure
3. Consult the course TAs
4. Check HF Community forums