krishisetu / app.py
prathmesh284's picture
Update app.py
e12c2ed verified
Raw
History Blame Contribute Delete
7.97 kB
# import gradio as gr
# from transformers import AutoImageProcessor, AutoModelForImageClassification
# from PIL import Image
# import torch
# # Model you selected
# MODEL_NAME = "google/vit-base-patch16-224"
# print("🔄 Loading model...")
# processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
# model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
# print("✅ Model loaded successfully!")
# def classify_image(image):
# try:
# img = Image.fromarray(image).convert("RGB")
# inputs = processor(images=img, return_tensors="pt")
# with torch.no_grad():
# outputs = model(**inputs)
# logits = outputs.logits
# pred_id = logits.argmax(-1).item()
# label = model.config.id2label[pred_id]
# return {label: float(logits.softmax(-1)[0][pred_id])}
# except Exception as e:
# return {"error": str(e)}
# # UI
# interface = gr.Interface(
# fn=classify_image,
# inputs=gr.Image(type="numpy"),
# outputs=gr.Label(num_top_classes=5),
# title="🌿 KrishiSetu — Crop Disease Classifier",
# description="Upload leaf images. The model uses `google/vit-base-patch16-224` to classify plant diseases.",
# )
# if __name__ == "__main__":
# interface.launch()
# import gradio as gr
# from transformers import (
# AutoImageProcessor,
# AutoModelForImageClassification,
# pipeline
# )
# from PIL import Image
# import torch
# MODEL_NAME = "google/vit-base-patch16-224"
# processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
# model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
# validator = pipeline(
# "zero-shot-image-classification",
# model="openai/clip-vit-base-patch32"
# )
# def is_valid_leaf_image(img):
# candidate_labels = [
# "a plant leaf",
# "a plant",
# "tree leaves",
# "crop leaf",
# "person",
# "animal",
# "vehicle",
# "food",
# "object"
# ]
# result = validator(img, candidate_labels=candidate_labels)
# top_label = result[0]["label"]
# top_score = result[0]["score"]
# valid_labels = ["a plant leaf", "a plant", "tree leaves", "crop leaf"]
# return top_label in valid_labels and top_score >= 0.30
# def classify_image(image):
# try:
# img = Image.fromarray(image).convert("RGB")
# # Step 1: Validation
# if not is_valid_leaf_image(img):
# return "❌ Invalid input, please send image containing plant and leaf", None
# # Step 2: Prediction
# inputs = processor(images=img, return_tensors="pt")
# with torch.no_grad():
# outputs = model(**inputs)
# probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
# top_k = torch.topk(probs, k=5)
# results = {}
# for score, idx in zip(top_k.values, top_k.indices):
# label = model.config.id2label[idx.item()]
# results[label] = float(score)
# return "✅ Valid leaf image", results
# except Exception as e:
# return f"Error: {str(e)}", None
# interface = gr.Interface(
# fn=classify_image,
# inputs=gr.Image(type="numpy"),
# outputs=[
# gr.Textbox(label="Status"),
# gr.Label(num_top_classes=5, label="Prediction")
# ],
# title="🌿 KrishiSetu — Crop Disease Classifier",
# description="Upload leaf images. Invalid images will be rejected.",
# )
# if __name__ == "__main__":
# interface.launch()
import gradio as gr
from transformers import AutoImageProcessor, CLIPForImageClassification, pipeline
from PIL import Image
import torch
MODEL_NAME = "VaigandlaHemanth/leaf-disease-clip-vit"
print("Loading disease model...")
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
model = CLIPForImageClassification.from_pretrained(MODEL_NAME)
model.eval()
print("Disease model loaded successfully!")
print("Loading validator model...")
validator = pipeline(
"zero-shot-image-classification",
model="openai/clip-vit-base-patch32"
)
print("Validator model loaded successfully!")
def is_valid_leaf_image(img):
candidate_labels = [
"a plant leaf",
"a crop leaf",
"a diseased leaf",
"a healthy leaf",
"a plant",
"tree leaves",
"person",
"animal",
"vehicle",
"food",
"building",
"random object"
]
result = validator(img, candidate_labels=candidate_labels)
top_label = result[0]["label"]
top_score = result[0]["score"]
valid_labels = [
"a plant leaf",
"a crop leaf",
"a diseased leaf",
"a healthy leaf",
"a plant",
"tree leaves"
]
return top_label in valid_labels and top_score >= 0.30, top_label, top_score
def clean_label(label):
return label.replace("___", " - ").replace("_", " ")
def get_confidence_status(confidence):
if confidence >= 0.70:
return "High confidence"
elif confidence >= 0.40:
return "Medium confidence"
else:
return "Low confidence"
def classify_image(image):
try:
if image is None:
return "Please upload an image."
img = Image.fromarray(image).convert("RGB")
is_valid, detected_type, validation_score = is_valid_leaf_image(img)
if not is_valid:
return (
"Invalid input, please send image containing plant and leaf\n\n"
f"Detected image type: {detected_type}\n"
f"Validation confidence: {validation_score * 100:.2f}%"
)
inputs = processor(images=img, return_tensors="pt")
with torch.no_grad():
outputs = model(**inputs)
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)[0]
top_k = torch.topk(probs, k=5)
predictions = []
for score, idx in zip(top_k.values, top_k.indices):
raw_label = model.config.id2label[idx.item()]
label = clean_label(raw_label)
confidence = float(score)
predictions.append((label, confidence))
top_label, top_confidence = predictions[0]
confidence_status = get_confidence_status(top_confidence)
response = ""
response += "Image validation: Valid plant/leaf image\n"
response += f"Validator detected: {detected_type} ({validation_score * 100:.2f}%)\n\n"
if top_confidence < 0.30:
response += "Final result: Disease/health prediction is uncertain\n"
response += (
"Reason: The image is a valid plant/leaf image, but model confidence is low. "
"Please upload a clear close-up image of a single leaf with plain background.\n\n"
)
elif "healthy" in top_label.lower():
response += f"Final result: Healthy plant ({top_confidence * 100:.2f}%)\n"
response += f"Confidence level: {confidence_status}\n\n"
else:
response += "Final result: Disease detected\n"
response += f"Disease name: {top_label}\n"
response += f"Confidence: {top_confidence * 100:.2f}%\n"
response += f"Confidence level: {confidence_status}\n\n"
response += "Top 5 predictions:\n"
for i, (label, confidence) in enumerate(predictions, start=1):
response += f"{i}. {label}: {confidence * 100:.2f}%\n"
return response
except Exception as e:
return f"Error: {str(e)}"
interface = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="numpy"),
outputs=gr.Textbox(label="Result", lines=12),
title="KrishiSetu — Crop Disease Classifier",
description=(
"Upload plant/leaf image. Invalid images will be rejected. "
"Valid images will show disease/healthy result with top predictions."
)
)
if __name__ == "__main__":
interface.launch()