File size: 1,802 Bytes
0b66774
 
 
 
 
a717cce
0b66774
a717cce
0b66774
 
a717cce
 
25c6339
5dbeaf8
 
 
 
 
 
0b66774
a717cce
 
0b66774
 
 
 
 
5dbeaf8
 
a717cce
 
 
05588c4
 
 
 
d16269d
 
 
 
0b66774
a717cce
0b66774
a717cce
0b66774
a717cce
0b66774
a717cce
0b66774
a717cce
 
 
0b66774
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()