|
import gradio as gr |
|
import torch |
|
from transformers import ViTFeatureExtractor, ViTForImageClassification |
|
from PIL import Image |
|
|
|
|
|
model_name = "KhadijaAsehnoune12/LeafDiseaseDetectorForOranges" |
|
model = ViTForImageClassification.from_pretrained(model_name, num_labels=10, ignore_mismatched_sizes=True) |
|
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name) |
|
|
|
|
|
id2label = { |
|
"0": "Aleurocanthus spiniferus", |
|
"1": "Chancre citrique", |
|
"2": "Cochenille blanche", |
|
"3": "Dépérissement des agrumes", |
|
"4": "Feuille saine", |
|
"5": "Jaunissement des feuilles", |
|
"6": "Maladie de l'oïdium", |
|
"7": "Maladie du dragon jaune", |
|
"8": "Mineuse des agrumes", |
|
"9": "Trou de balle" |
|
} |
|
|
|
def predict(image): |
|
|
|
inputs = feature_extractor(images=image, return_tensors="pt") |
|
|
|
|
|
outputs = model(**inputs) |
|
|
|
|
|
logits = outputs.logits |
|
predicted_class_idx = logits.argmax(-1).item() |
|
confidence_score = torch.nn.functional.softmax(logits, dim=-1)[0, predicted_class_idx].item() |
|
|
|
|
|
predicted_label = id2label[str(predicted_class_idx)] |
|
|
|
|
|
return f"{predicted_label}: {confidence_score:.2f}" |
|
|
|
|
|
|
|
|
|
image = gr.Image(type="pil") |
|
label = gr.Textbox(label="Prediction") |
|
|
|
gr.Interface(fn=predict, |
|
inputs="image", |
|
outputs="text", |
|
title="Citrus Disease Classification", |
|
description="Upload an image of a citrus leaf to classify its disease. The model is trained on the following diseases: Aleurocanthus spiniferus, Chancre citrique, Cochenille blanche, Dépérissement des agrumes, Feuille saine, Jaunissement des feuilles, Maladie de l'oïdium, Maladie du dragon jaune, Mineuse des agrumes, Trou de balle.", |
|
examples=["maladie_du_dragon_jaune.jpg", "mineuse_des_agrumes.jpg","feuille_saine.jpg"]).launch(share=True) |