Upload huggingface_setup.py
Browse files- huggingface_setup.py +75 -0
huggingface_setup.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from torchvision import models
|
| 6 |
+
|
| 7 |
+
def ensure_files_exist():
|
| 8 |
+
"""
|
| 9 |
+
Ensures that all required files exist for the application to run.
|
| 10 |
+
Creates empty or default files if they don't exist.
|
| 11 |
+
"""
|
| 12 |
+
print("Checking for required files...")
|
| 13 |
+
|
| 14 |
+
# Check for model file
|
| 15 |
+
if not os.path.exists('plant_disease_model.pth'):
|
| 16 |
+
print("Model file not found, creating empty file...")
|
| 17 |
+
with open('plant_disease_model.pth', 'w') as f:
|
| 18 |
+
f.write('')
|
| 19 |
+
|
| 20 |
+
# Check for class names file
|
| 21 |
+
if not os.path.exists('class_names.json'):
|
| 22 |
+
print("Class names file not found, creating default...")
|
| 23 |
+
default_classes = [
|
| 24 |
+
"Apple___Apple_scab", "Apple___Black_rot", "Apple___Cedar_apple_rust", "Apple___healthy",
|
| 25 |
+
"Cherry___healthy", "Cherry___Powdery_mildew", "Corn___Cercospora_leaf_spot",
|
| 26 |
+
"Corn___Common_rust", "Corn___healthy", "Corn___Northern_Leaf_Blight",
|
| 27 |
+
"Grape___Black_rot", "Grape___Esca_(Black_Measles)", "Grape___healthy",
|
| 28 |
+
"Grape___Leaf_blight_(Isariopsis_Leaf_Spot)", "Orange___Haunglongbing_(Citrus_greening)",
|
| 29 |
+
"Peach___Bacterial_spot", "Peach___healthy", "Pepper,_bell___Bacterial_spot",
|
| 30 |
+
"Pepper,_bell___healthy", "Potato___Early_blight", "Potato___healthy",
|
| 31 |
+
"Potato___Late_blight", "Squash___Powdery_mildew", "Strawberry___healthy",
|
| 32 |
+
"Strawberry___Leaf_scorch", "Tomato___Bacterial_spot", "Tomato___Early_blight",
|
| 33 |
+
"Tomato___healthy", "Tomato___Late_blight", "Tomato___Leaf_Mold",
|
| 34 |
+
"Tomato___Septoria_leaf_spot", "Tomato___Spider_mites Two-spotted_spider_mite",
|
| 35 |
+
"Tomato___Target_Spot", "Tomato___Tomato_mosaic_virus",
|
| 36 |
+
"Tomato___Tomato_Yellow_Leaf_Curl_Virus"
|
| 37 |
+
]
|
| 38 |
+
with open('class_names.json', 'w') as f:
|
| 39 |
+
json.dump(default_classes, f)
|
| 40 |
+
|
| 41 |
+
# Check for treatments CSV
|
| 42 |
+
if not os.path.exists('crop_diseases_treatments.csv'):
|
| 43 |
+
print("Treatments CSV not found, creating it...")
|
| 44 |
+
try:
|
| 45 |
+
import create_treatments_csv
|
| 46 |
+
create_treatments_csv.create_treatments_csv()
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error creating treatments CSV: {e}")
|
| 49 |
+
|
| 50 |
+
# Create example_images directory
|
| 51 |
+
if not os.path.exists('example_images'):
|
| 52 |
+
print("Creating example_images directory...")
|
| 53 |
+
os.makedirs('example_images', exist_ok=True)
|
| 54 |
+
|
| 55 |
+
print("File check complete.")
|
| 56 |
+
|
| 57 |
+
def create_minimal_model():
|
| 58 |
+
"""
|
| 59 |
+
Creates a minimal model file that can be loaded without errors.
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
print("Creating minimal model file...")
|
| 63 |
+
model = models.resnet18(weights=None)
|
| 64 |
+
num_classes = 38
|
| 65 |
+
model.fc = nn.Linear(model.fc.in_features, num_classes)
|
| 66 |
+
torch.save(model.state_dict(), 'plant_disease_model.pth')
|
| 67 |
+
print(f"Created minimal model file: {os.path.getsize('plant_disease_model.pth')} bytes")
|
| 68 |
+
except Exception as e:
|
| 69 |
+
print(f"Error creating minimal model: {e}")
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
ensure_files_exist()
|
| 73 |
+
if os.path.getsize('plant_disease_model.pth') == 0:
|
| 74 |
+
create_minimal_model()
|
| 75 |
+
print("Setup complete!")
|