File size: 2,286 Bytes
f15e691
e63737d
 
f15e691
f656045
e63737d
f656045
e63737d
2304bdc
f15e691
6b37fc7
 
b5beb72
3f01936
 
6b37fc7
 
 
a7a57cb
77cc2ab
6b37fc7
f15e691
 
e63737d
 
2374a7e
e63737d
 
 
 
 
59d7cbd
e63737d
 
 
 
 
 
 
2374a7e
e6c053d
f15e691
e63737d
1e36fed
e63737d
 
f15e691
e63737d
 
 
2304bdc
e63737d
 
 
 
fea5e8c
e63737d
 
 
 
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
56
57
58
59
60
61
import os
from io import BytesIO
import gradio as gr
import google.generativeai as genai
from PIL import Image
from dotenv import load_dotenv

# Load environment variables
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
input_prompt="""
        "You are an expert in computer vision and agriculture who can easily predict the disease of the plant. "
        "Analyze the following image and provide 7 short outputs in a structured  format: "
        "1. Crop : , "
        "2. Infected or Healthy : , "
        "3. Type of disease (if any), "
        "4. How confident out of 100% whether image is healthy or infected "
        "5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc., "
        "6. Plant Growth Stage. "
        "7. Pest Life Stage."
"""


# Function to get a response from the Google Gemini Vision API
def get_gemini_response(image):
    model = genai.GenerativeModel('gemini-1.5-pro')

    # Convert PIL image to bytes
    bytes_io = BytesIO()
    image.save(bytes_io, format='PNG')
    bytes_data = bytes_io.getvalue()
    response=model.generate_content([input_prompt,image])
    # Placeholder logic for now: replace with actual API usage
    # You would pass the image bytes data to the API
    # Currently this is a mock response
    crop_name = "Wheat"  # Example: Replace this with the actual API result
    disease_type = "Wheat Septoria"  # Example: Replace this with the actual API result

    # Return disease info
    #return get_disease_info(crop_name, disease_type)
    return response.text

# Function to handle the uploaded image and predict crop health
def predict_crop_health(uploaded_image):
    # Pass the image to the Gemini API to get prediction
    return get_gemini_response(uploaded_image)

# Define the Gradio interface: Inputs and Outputs
inputs = gr.Image(type="pil", label="Upload Crop Image")
outputs = gr.Markdown(label="Prediction Results")

# Launch the Gradio interface
gr.Interface(
    fn=predict_crop_health,
    inputs=inputs,
    outputs=gr.Textbox(label="Crop Disease Predictor"),
    title="Crop Disease Prediction App",
    description="Upload an image of a crop to predict its disease and get treatment suggestions.",
    live=False
).launch()