test / app.py
vishwak1's picture
Update app.py
25c6339 verified
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
# Load your model
model = load_model("model.h5")
# Configure Gemini
client = genai.Client(api_key="AIzaSyD5BzB8hxupR40iCBIOUtbEgQrppLnZAmI")
# Class index to label map
label_map = {
0: "Healthy",
1: "Powdery",
2: "Rust"
}
# Prediction + Gemini explanation function
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")
# Generate explanation using Gemini
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", # Adjust model name if necessary
contents=prompt
)
return predicted_label, response.text
except Exception as e:
return "Error", str(e)
# Gradio interface
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()