iamomtiwari commited on
Commit
94ffad8
·
verified ·
1 Parent(s): 74aa40d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -7,26 +7,26 @@ from PIL import Image
7
  model = ViTForImageClassification.from_pretrained("iamomtiwari/VITPEST")
8
  feature_extractor = ViTFeatureExtractor.from_pretrained("iamomtiwari/VITPEST")
9
 
10
- # Define class labels and treatment advice
11
  class_labels = {
12
- 0:"Corn___Common_Rust": "Apply fungicides as soon as symptoms are noticed. Practice crop rotation and remove infected plants.",
13
- 1:"Corn___Gray_Leaf_Spot": "Rotate crops to non-host plants, apply resistant varieties, and use fungicides as needed.",
14
- 2:"Corn___Healthy": "Continue good agricultural practices: ensure proper irrigation, nutrient supply, and monitor for pests.",
15
- 3:"Corn___Northern_Leaf_Blight": "Remove and destroy infected plant debris, apply fungicides, and rotate crops.",
16
- 4:"Rice___Brown_Spot": "Use resistant varieties, improve field drainage, and apply fungicides if necessary.",
17
- 5:"Rice___Healthy": "Maintain proper irrigation, fertilization, and pest control measures.",
18
- 6:"Rice___Leaf_Blast": "Use resistant varieties, apply fungicides during high-risk periods, and practice good field management.",
19
- 7:"Rice___Neck_Blast": "Plant resistant varieties, improve nutrient management, and apply fungicides if symptoms appear.",
20
- 8:"Wheat___Brown_Rust": "Apply fungicides and practice crop rotation with non-host crops.",
21
- 9:"Wheat___Healthy": "Continue with good management practices, including proper fertilization and weed control.",
22
- 10:"Wheat___Yellow_Rust": "Use resistant varieties, apply fungicides, and rotate crops.",
23
- 11:"Sugarcane__Red_Rot": "Plant resistant varieties and ensure good drainage.",
24
- 12:"Sugarcane__Healthy": "Maintain healthy soil conditions and proper irrigation.",
25
- 13:"Sugarcane__Bacterial Blight": "Use disease-free planting material, practice crop rotation, and destroy infected plants."
26
  }
27
 
28
  # Mapping label indices to class labels
29
- labels_list = list(class_labels.keys())
30
 
31
  # Inference function
32
  def predict(image):
@@ -35,10 +35,12 @@ def predict(image):
35
  outputs = model(**inputs)
36
  predicted_class_idx = outputs.logits.argmax(-1).item()
37
  predicted_label = labels_list[predicted_class_idx]
38
- treatment_advice = class_labels[predicted_label]
 
 
39
 
40
  return f"Disease: {predicted_label}\n\nTreatment Advice: {treatment_advice}"
41
 
42
  # Create Gradio Interface
43
  interface = gr.Interface(fn=predict, inputs="image", outputs="text")
44
- interface.launch()
 
7
  model = ViTForImageClassification.from_pretrained("iamomtiwari/VITPEST")
8
  feature_extractor = ViTFeatureExtractor.from_pretrained("iamomtiwari/VITPEST")
9
 
10
+ # Define class labels and treatment advice with a numeric index
11
  class_labels = {
12
+ 1: {"label": "Corn___Common_Rust", "treatment": "Apply fungicides as soon as symptoms are noticed. Practice crop rotation and remove infected plants."},
13
+ 2: {"label": "Corn___Gray_Leaf_Spot", "treatment": "Rotate crops to non-host plants, apply resistant varieties, and use fungicides as needed."},
14
+ 3: {"label": "Corn___Healthy", "treatment": "Continue good agricultural practices: ensure proper irrigation, nutrient supply, and monitor for pests."},
15
+ 4: {"label": "Corn___Northern_Leaf_Blight", "treatment": "Remove and destroy infected plant debris, apply fungicides, and rotate crops."},
16
+ 5: {"label": "Rice___Brown_Spot", "treatment": "Use resistant varieties, improve field drainage, and apply fungicides if necessary."},
17
+ 6: {"label": "Rice___Healthy", "treatment": "Maintain proper irrigation, fertilization, and pest control measures."},
18
+ 7: {"label": "Rice___Leaf_Blast", "treatment": "Use resistant varieties, apply fungicides during high-risk periods, and practice good field management."},
19
+ 8: {"label": "Rice___Neck_Blast", "treatment": "Plant resistant varieties, improve nutrient management, and apply fungicides if symptoms appear."},
20
+ 9: {"label": "Wheat___Brown_Rust", "treatment": "Apply fungicides and practice crop rotation with non-host crops."},
21
+ 10: {"label": "Wheat___Healthy", "treatment": "Continue with good management practices, including proper fertilization and weed control."},
22
+ 11: {"label": "Wheat___Yellow_Rust", "treatment": "Use resistant varieties, apply fungicides, and rotate crops."},
23
+ 12: {"label": "Sugarcane__Red_Rot", "treatment": "Plant resistant varieties and ensure good drainage."},
24
+ 13: {"label": "Sugarcane__Healthy", "treatment": "Maintain healthy soil conditions and proper irrigation."},
25
+ 14: {"label": "Sugarcane__Bacterial Blight", "treatment": "Use disease-free planting material, practice crop rotation, and destroy infected plants."}
26
  }
27
 
28
  # Mapping label indices to class labels
29
+ labels_list = [class_labels[i]["label"] for i in range(1, 15)]
30
 
31
  # Inference function
32
  def predict(image):
 
35
  outputs = model(**inputs)
36
  predicted_class_idx = outputs.logits.argmax(-1).item()
37
  predicted_label = labels_list[predicted_class_idx]
38
+
39
+ # Find corresponding treatment
40
+ treatment_advice = class_labels[predicted_class_idx + 1]["treatment"]
41
 
42
  return f"Disease: {predicted_label}\n\nTreatment Advice: {treatment_advice}"
43
 
44
  # Create Gradio Interface
45
  interface = gr.Interface(fn=predict, inputs="image", outputs="text")
46
+ interface.launch()