ranchopanda0 commited on
Commit
49cc0f0
ยท
verified ยท
1 Parent(s): 9f9cd44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -7
app.py CHANGED
@@ -1,7 +1,53 @@
1
- if __name__ == "__main__":
2
- gr.Interface(
3
- fn=analyze_plant,
4
- inputs=gr.Image(type="pil", label="๐Ÿ–ผ๏ธ Upload Leaf"),
5
- outputs=gr.Markdown(),
6
- title="๐ŸŒฟ Plant Disease Detector"
7
- ).launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from io import BytesIO
4
+ import base64
5
+ from PIL import Image
6
+ import gradio as gr # โœ… Import Gradio
7
+ from dotenv import load_dotenv
8
+
9
+ # Load API key safely
10
+ load_dotenv()
11
+ API_KEY = os.getenv("PLANT_ID_API_KEY") # Make sure .env contains the API key
12
+
13
+ def analyze_plant(image):
14
+ try:
15
+ # Convert image to base64
16
+ buffered = BytesIO()
17
+ image.save(buffered, format="JPEG")
18
+ img_b64 = base64.b64encode(buffered.getvalue()).decode()
19
+
20
+ # API Request
21
+ response = requests.post(
22
+ "https://api.plant.id/v2/health_assessment",
23
+ headers={"Content-Type": "application/json"},
24
+ json={
25
+ "images": [f"data:image/jpeg;base64,{img_b64}"],
26
+ "organs": ["leaf"],
27
+ "api_key": API_KEY # Injected securely
28
+ }
29
+ )
30
+
31
+ # Process response
32
+ if response.status_code == 200:
33
+ data = response.json()
34
+ if data["health_assessment"]["diseases"]:
35
+ disease = data["health_assessment"]["diseases"][0]
36
+ return f"""
37
+ ๐Ÿ‚ **Disease:** {disease['name']}
38
+ โš ๏ธ **Probability:** {disease['probability']:.0%}
39
+ ๐Ÿ’Š **Treatment:** {disease['treatment']['details']}
40
+ """
41
+ return "โœ… Healthy plant detected!"
42
+ return f"โŒ API Error: {response.text}"
43
+
44
+ except Exception as e:
45
+ return f"๐Ÿ”ฅ Error: {str(e)}"
46
+
47
+ # Launch Gradio App
48
+ gr.Interface(
49
+ fn=analyze_plant,
50
+ inputs=gr.Image(type="pil", label="๐Ÿ–ผ๏ธ Upload Leaf"),
51
+ outputs=gr.Markdown(),
52
+ title="๐ŸŒฟ Plant Disease Detector"
53
+ ).launch()