Spaces:
Running
Running
iamomtiwari
commited on
Update app.py
Browse files
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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
}
|
27 |
|
28 |
# Mapping label indices to class labels
|
29 |
-
labels_list =
|
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 |
-
|
|
|
|
|
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()
|