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()