Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,60 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from io import BytesIO
|
| 3 |
import gradio as gr
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 11 |
+
input_prompt="""
|
| 12 |
+
"You are an expert in computer vision and agriculture who can easily predict the disease of the plant. "
|
| 13 |
+
"Analyze the following image and provide 7 short outputs in a structured format: "
|
| 14 |
+
"1. Crop : , "
|
| 15 |
+
"2. Infected or Healthy : , "
|
| 16 |
+
"3. Type of disease (if any), "
|
| 17 |
+
"4. How confident out of 100% whether image is healthy or infected "
|
| 18 |
+
"5. Reason for the disease such as whether it is happening due to fungus, bacteria, insect bite, poor nutrition, etc., "
|
| 19 |
+
"6. Plant Growth Stage. "
|
| 20 |
+
"7. Pest Life Stage."
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Function to get a response from the Google Gemini Vision API
|
| 25 |
+
def get_gemini_response(image):
|
| 26 |
+
model = genai.GenerativeModel('gemini-1.5-pro')
|
| 27 |
+
|
| 28 |
+
# Convert PIL image to bytes
|
| 29 |
+
bytes_io = BytesIO()
|
| 30 |
+
image.save(bytes_io, format='PNG')
|
| 31 |
+
bytes_data = bytes_io.getvalue()
|
| 32 |
+
response=model.generate_content([input_prompt,image])
|
| 33 |
+
# Placeholder logic for now: replace with actual API usage
|
| 34 |
+
# You would pass the image bytes data to the API
|
| 35 |
+
# Currently this is a mock response
|
| 36 |
+
crop_name = "Wheat" # Example: Replace this with the actual API result
|
| 37 |
+
disease_type = "Wheat Septoria" # Example: Replace this with the actual API result
|
| 38 |
+
|
| 39 |
+
# Return disease info
|
| 40 |
+
#return get_disease_info(crop_name, disease_type)
|
| 41 |
+
return response.text
|
| 42 |
+
|
| 43 |
+
# Function to handle the uploaded image and predict crop health
|
| 44 |
+
def predict_crop_health(uploaded_image):
|
| 45 |
+
# Pass the image to the Gemini API to get prediction
|
| 46 |
+
return get_gemini_response(uploaded_image)
|
| 47 |
+
|
| 48 |
+
# Define the Gradio interface: Inputs and Outputs
|
| 49 |
+
inputs = gr.Image(type="pil", label="Upload Crop Image")
|
| 50 |
+
outputs = gr.Markdown(label="Prediction Results")
|
| 51 |
+
|
| 52 |
+
# Launch the Gradio interface
|
| 53 |
+
gr.Interface(
|
| 54 |
+
fn=predict_crop_health,
|
| 55 |
+
inputs=inputs,
|
| 56 |
+
outputs=gr.Textbox(label="Crop Disease Predictor"),
|
| 57 |
+
title="Crop Disease Prediction App",
|
| 58 |
+
description="Upload an image of a crop to predict its disease and get treatment suggestions.",
|
| 59 |
+
live=False
|
| 60 |
+
).launch()
|