Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from io import BytesIO
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
import cv2
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
from model import classify_image # Assuming this is a custom model for classification
|
| 10 |
+
|
| 11 |
+
# Load environment variables for Google Gemini API key
|
| 12 |
+
load_dotenv()
|
| 13 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 14 |
+
|
| 15 |
+
# Input prompt for Gemini model
|
| 16 |
+
input_prompt = """
|
| 17 |
+
"You are an expert in computer vision and agriculture who can easily predict the disease of the plant. "
|
| 18 |
+
"Analyze the following image and provide 7 short outputs in a structured format: "
|
| 19 |
+
"1. Crop: , "
|
| 20 |
+
"2. Infected or Healthy: , "
|
| 21 |
+
"3. Type of disease (if any): , "
|
| 22 |
+
"4. Confidence out of 100% whether image is healthy or infected: , "
|
| 23 |
+
"5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc.: , "
|
| 24 |
+
"6. Plant Growth Stage: , "
|
| 25 |
+
"7. Pest Life Stage: ."
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
# Function to get a response from the Google Gemini API
|
| 29 |
+
def get_gemini_response(image):
|
| 30 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 31 |
+
|
| 32 |
+
# Convert PIL image to bytes
|
| 33 |
+
bytes_io = BytesIO()
|
| 34 |
+
image.save(bytes_io, format='PNG')
|
| 35 |
+
bytes_data = bytes_io.getvalue()
|
| 36 |
+
|
| 37 |
+
# Send image and prompt to Gemini API
|
| 38 |
+
response = model.generate_content([input_prompt, image])
|
| 39 |
+
|
| 40 |
+
# Extract the relevant response (mock response logic for now)
|
| 41 |
+
return response.text
|
| 42 |
+
|
| 43 |
+
# Image classification using custom model (e.g., for detecting specific plant diseases)
|
| 44 |
+
def classify_crop_image(img):
|
| 45 |
+
img = cv2.resize(img, (224, 224))
|
| 46 |
+
img = img / 255.0
|
| 47 |
+
img = np.expand_dims(img, axis=0)
|
| 48 |
+
|
| 49 |
+
# Use the custom classification model to predict the disease
|
| 50 |
+
label, accuracy = classify_image(img) # Assuming 'classify_image' returns label and accuracy
|
| 51 |
+
return {label: accuracy}
|
| 52 |
+
|
| 53 |
+
# Function to handle the uploaded image, predict crop health, and provide a structured output
|
| 54 |
+
def predict_crop_health(uploaded_image):
|
| 55 |
+
# Pass the image to the custom classifier for plant disease prediction
|
| 56 |
+
classification_result = classify_crop_image(np.array(uploaded_image))
|
| 57 |
+
|
| 58 |
+
# Pass the image to the Gemini API for detailed disease analysis
|
| 59 |
+
gemini_response = get_gemini_response(uploaded_image)
|
| 60 |
+
|
| 61 |
+
# Combine results from both models (custom and Gemini)
|
| 62 |
+
return f"Classification Result: {classification_result}\n\nGemini Response: {gemini_response}"
|
| 63 |
+
|
| 64 |
+
# Define the Gradio interface: Inputs and Outputs
|
| 65 |
+
inputs = gr.Image(type="pil", label="Upload Crop Image")
|
| 66 |
+
outputs = gr.Textbox(label="Crop Disease Predictor", lines=10)
|
| 67 |
+
|
| 68 |
+
# Launch the Gradio interface
|
| 69 |
+
gr.Interface(
|
| 70 |
+
fn=predict_crop_health,
|
| 71 |
+
inputs=inputs,
|
| 72 |
+
outputs=outputs,
|
| 73 |
+
title="Crop Disease Prediction App",
|
| 74 |
+
description="Upload an image of a crop to predict its disease and get treatment suggestions.",
|
| 75 |
+
live=False
|
| 76 |
+
).launch()
|