Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,8 @@ import json
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import logging
|
|
|
6 |
from PIL import Image
|
7 |
-
import numpy as np
|
8 |
from io import BytesIO
|
9 |
from dotenv import load_dotenv
|
10 |
|
@@ -23,18 +23,18 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
23 |
# Plant.id API URL
|
24 |
PLANT_ID_URL = "https://api.plant.id/v2/health_assessment"
|
25 |
|
26 |
-
# Function to
|
27 |
def analyze_plant_health(image):
|
28 |
try:
|
29 |
-
# Convert image to bytes
|
30 |
buffered = BytesIO()
|
31 |
image.save(buffered, format="JPEG")
|
32 |
-
|
33 |
|
34 |
# Send request to Plant.id API
|
35 |
headers = {"Content-Type": "application/json"}
|
36 |
payload = {
|
37 |
-
"images": [f"data:image/jpeg;base64,{
|
38 |
"organs": ["leaf"],
|
39 |
"api_key": PLANT_ID_API_KEY
|
40 |
}
|
@@ -45,29 +45,39 @@ def analyze_plant_health(image):
|
|
45 |
return f"β API Error: {response.text}"
|
46 |
|
47 |
data = response.json()
|
48 |
-
|
49 |
-
if "health_assessment" not in data:
|
50 |
-
return "
|
51 |
|
52 |
assessment = data["health_assessment"]
|
53 |
-
|
54 |
-
|
|
|
55 |
|
56 |
-
return f"
|
|
|
|
|
|
|
57 |
|
58 |
except Exception as e:
|
59 |
logging.error(f"Prediction failed: {str(e)}")
|
60 |
return f"β Error: {str(e)}"
|
61 |
|
62 |
-
# Gradio Interface
|
63 |
iface = gr.Interface(
|
64 |
fn=analyze_plant_health,
|
65 |
-
inputs=gr.Image(type="pil", label="πΈ Upload or
|
66 |
outputs=gr.Textbox(label="π Diagnosis & Treatment"),
|
67 |
title="πΏ AI-Powered Plant Disease Detector",
|
68 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
allow_flagging="never",
|
70 |
-
|
71 |
)
|
72 |
|
73 |
# Launch Gradio App
|
|
|
3 |
import gradio as gr
|
4 |
import requests
|
5 |
import logging
|
6 |
+
import base64
|
7 |
from PIL import Image
|
|
|
8 |
from io import BytesIO
|
9 |
from dotenv import load_dotenv
|
10 |
|
|
|
23 |
# Plant.id API URL
|
24 |
PLANT_ID_URL = "https://api.plant.id/v2/health_assessment"
|
25 |
|
26 |
+
# Function to analyze plant health using Plant.id API
|
27 |
def analyze_plant_health(image):
|
28 |
try:
|
29 |
+
# Convert image to bytes & encode in Base64
|
30 |
buffered = BytesIO()
|
31 |
image.save(buffered, format="JPEG")
|
32 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode()
|
33 |
|
34 |
# Send request to Plant.id API
|
35 |
headers = {"Content-Type": "application/json"}
|
36 |
payload = {
|
37 |
+
"images": [f"data:image/jpeg;base64,{img_base64}"],
|
38 |
"organs": ["leaf"],
|
39 |
"api_key": PLANT_ID_API_KEY
|
40 |
}
|
|
|
45 |
return f"β API Error: {response.text}"
|
46 |
|
47 |
data = response.json()
|
48 |
+
|
49 |
+
if "health_assessment" not in data or not data["health_assessment"].get("diseases"):
|
50 |
+
return "β
No disease detected! Your plant looks healthy. πΏ"
|
51 |
|
52 |
assessment = data["health_assessment"]
|
53 |
+
disease_info = assessment["diseases"][0]
|
54 |
+
predicted_disease = disease_info.get("name", "Unknown Disease")
|
55 |
+
treatment = disease_info.get("treatment", "No treatment suggestions available.")
|
56 |
|
57 |
+
return f"""
|
58 |
+
π± **Predicted Disease:** {predicted_disease}
|
59 |
+
π **Treatment:** {treatment}
|
60 |
+
"""
|
61 |
|
62 |
except Exception as e:
|
63 |
logging.error(f"Prediction failed: {str(e)}")
|
64 |
return f"β Error: {str(e)}"
|
65 |
|
66 |
+
# Gradio Interface with Improved UI
|
67 |
iface = gr.Interface(
|
68 |
fn=analyze_plant_health,
|
69 |
+
inputs=gr.Image(type="pil", label="πΈ Upload or Capture a Plant Image"),
|
70 |
outputs=gr.Textbox(label="π Diagnosis & Treatment"),
|
71 |
title="πΏ AI-Powered Plant Disease Detector",
|
72 |
+
description="""
|
73 |
+
π· Upload an image of a plant leaf to detect diseases and get treatment suggestions from Plant.id API.
|
74 |
+
β
Supports **multiple plant species**
|
75 |
+
β
Instant **AI-powered analysis**
|
76 |
+
β
Get **organic & chemical treatment suggestions**
|
77 |
+
""",
|
78 |
+
theme="compact",
|
79 |
allow_flagging="never",
|
80 |
+
examples=["example_leaf.jpg"], # Add a sample leaf image
|
81 |
)
|
82 |
|
83 |
# Launch Gradio App
|