Spaces:
Sleeping
Sleeping
File size: 1,895 Bytes
f15e691 f656045 f15e691 f656045 f15e691 8dd85af f15e691 |
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 |
from dotenv import load_dotenv
import os
import google.generativeai as genai
from PIL import Image
import gradio as gr
# Load all the environment variables
load_dotenv()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
# Function to load Google Gemini Pro Vision API and get a response
def get_gemini_response(input_prompt, uploaded_image):
model = genai.GenerativeModel('gemini-1.5-flash')
# Convert the uploaded image to bytes
bytes_data = uploaded_image.read()
image_parts = [
{
"mime_type": uploaded_image.type,
"data": bytes_data
}
]
# Generate the content
response = model.generate_content([input_prompt, image_parts[0], ""])
return response.text
# Input prompt for the model
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 6 outputs in a structured table format: "
"1. Crop in the image, "
"2. Whether it is 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. Precautions for it."
"""
# Define the Gradio interface
def predict_crop_health(uploaded_image):
if uploaded_image is None:
return "No image uploaded."
response = get_gemini_response(input_prompt, uploaded_image)
return response
# Create a Gradio interface
iface = gr.Interface(
fn=predict_crop_health,
inputs=gr.Image(type="file", label="Upload Crop Image"),
outputs="text",
title="Gemini Crop Disease Detection App",
description="Upload an image of a crop to predict its health and identify any diseases."
)
# Launch the Gradio app
iface.launch()
|