amirkhanbloch's picture
Update app.py
a7a57cb verified
raw
history blame
2.29 kB
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()