ranchopanda0 commited on
Commit
662d7f9
Β·
verified Β·
1 Parent(s): 4895bd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
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 get disease and treatment information from Plant.id
27
  def analyze_plant_health(image):
28
  try:
29
- # Convert image to bytes
30
  buffered = BytesIO()
31
  image.save(buffered, format="JPEG")
32
- img_bytes = buffered.getvalue()
33
 
34
  # Send request to Plant.id API
35
  headers = {"Content-Type": "application/json"}
36
  payload = {
37
- "images": [f"data:image/jpeg;base64,{img_bytes.decode()}"],
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 "⚠️ No disease detected or insufficient data."
51
 
52
  assessment = data["health_assessment"]
53
- predicted_disease = assessment.get("diseases", [{}])[0].get("name", "Unknown Disease")
54
- treatment = assessment.get("diseases", [{}])[0].get("treatment", "No treatment suggestions available.")
 
55
 
56
- return f"🌱 **Predicted Disease:** {predicted_disease}\nπŸ’Š **Treatment:** {treatment}"
 
 
 
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 capture a plant image"),
66
  outputs=gr.Textbox(label="πŸ” Diagnosis & Treatment"),
67
  title="🌿 AI-Powered Plant Disease Detector",
68
- description="πŸ“· Upload a leaf image to detect plant diseases and get treatment suggestions from Plant.id API.",
 
 
 
 
 
 
69
  allow_flagging="never",
70
- theme="default",
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