iamomtiwari commited on
Commit
364842e
·
verified ·
1 Parent(s): f71136d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import torchvision.transforms as transforms
4
+ from torchvision.models import resnet50
5
+ from PIL import Image
6
+
7
+ # Load the model
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+ model = resnet50(pretrained=False)
10
+ model.fc = torch.nn.Linear(model.fc.in_features, 14) # Adjust for 14 classes
11
+ model.load_state_dict(torch.load("resnet50_model_hf.pt", map_location=device))
12
+ model.eval()
13
+
14
+ # Define image transformations
15
+ transform = transforms.Compose([
16
+ transforms.Resize((224, 224)),
17
+ transforms.ToTensor(),
18
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
19
+ ])
20
+
21
+ # Class labels (adjust according to your dataset)
22
+ class_labels = [
23
+ "Corn___Common_Rust", "Corn___Gray_Leaf_Spot", "Corn___Healthy", "Corn___Northern_Leaf_Blight",
24
+ "Rice___Brown_Spot", "Rice___Healthy", "Rice___Leaf_Blast", "Rice___Neck_Blast",
25
+ "Wheat___Brown_Rust", "Wheat___Healthy", "Wheat___Yellow_Rust",
26
+ "Sugarcane__Red_Rot", "Sugarcane__Healthy", "Sugarcane__Bacterial Blight"
27
+ ]
28
+
29
+ # Prediction function
30
+ def predict(image):
31
+ image = transform(image).unsqueeze(0).to(device) # Add batch dimension
32
+ with torch.no_grad():
33
+ outputs = model(image)
34
+ _, predicted_class = torch.max(outputs, 1)
35
+ return class_labels[predicted_class.item()]
36
+
37
+ # Gradio interface
38
+ interface = gr.Interface(
39
+ fn=predict,
40
+ inputs=gr.Image(type="pil"),
41
+ outputs=gr.Label(),
42
+ title="Crop Disease Classification",
43
+ description="Upload an image to classify crop diseases using ResNet-50."
44
+ )
45
+
46
+ interface.launch()