|
|
import gradio as gr |
|
|
from tensorflow.keras.models import load_model |
|
|
from tensorflow.keras.preprocessing.image import img_to_array |
|
|
from PIL import Image |
|
|
import numpy as np |
|
|
from google import genai |
|
|
|
|
|
|
|
|
model = load_model("model.h5") |
|
|
|
|
|
|
|
|
|
|
|
client = genai.Client(api_key="AIzaSyD5BzB8hxupR40iCBIOUtbEgQrppLnZAmI") |
|
|
|
|
|
label_map = { |
|
|
0: "Healthy", |
|
|
1: "Powdery", |
|
|
2: "Rust" |
|
|
} |
|
|
|
|
|
|
|
|
def predict_and_explain(image): |
|
|
try: |
|
|
image = image.resize((225, 225)) |
|
|
img_array = img_to_array(image) / 255.0 |
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
|
|
|
|
predictions = model.predict(img_array) |
|
|
predicted_index = int(np.argmax(predictions, axis=1)[0]) |
|
|
predicted_label = label_map.get(predicted_index, "Unknown") |
|
|
|
|
|
|
|
|
if predicted_label=="Healthy": |
|
|
prompt = f"the crop is '{predicted_label}'explain growth methods to my farmer." |
|
|
else: |
|
|
prompt = f"Explain about the plant disease '{predicted_label}' explain in two main headings 1. describe symptoms with minimal points,2. suggest treatment with minimal points." |
|
|
response = client.models.generate_content( |
|
|
model="gemini-2.0-flash", |
|
|
contents=prompt |
|
|
) |
|
|
|
|
|
return predicted_label, response.text |
|
|
except Exception as e: |
|
|
return "Error", str(e) |
|
|
|
|
|
|
|
|
interface = gr.Interface( |
|
|
fn=predict_and_explain, |
|
|
inputs=gr.Image(type="pil"), |
|
|
outputs=["text", "text"], |
|
|
title="Plant Disease Detector + Gemini Explainer", |
|
|
description="Upload a plant image to detect disease and get treatment advice using Gemini LLM." |
|
|
) |
|
|
|
|
|
interface.launch() |