File size: 1,551 Bytes
8de6150
f30306b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8de6150
 
f30306b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
import requests
import base64
import json

API_KEY = "GCT8F6KhPYNUvBvh7X617OzRF9zXUNhwvWu4NyWxhklqU75S8d"
API_URL = "https://plant.id/api/v3/identification"

def identify_plant(image):
    with open(image, "rb") as img_file:
        img_data = base64.b64encode(img_file.read()).decode("utf-8")

    payload = {
        "images": [img_data],
        "organs": ["leaf"],
        "similar_images": True
    }

    headers = {
        "Content-Type": "application/json",
        "Api-Key": API_KEY
    }

    response = requests.post(API_URL, headers=headers, data=json.dumps(payload))
    if response.status_code == 200:
        result = response.json()
        if "suggestions" in result and result["suggestions"]:
            top = result["suggestions"][0]
            name = top.get("plant_name", "Unknown")
            prob = top.get("probability", 0)
            wiki = top.get("plant_details", {}).get("wiki_description", {}).get("value", "")
            return f"🌿 **Plant:** {name}\n📊 **Confidence:** {prob*100:.2f}%\n📖 {wiki}"
        else:
            return "❌ No match found. Try another image."
    else:
        return f"⚠️ Error {response.status_code}: {response.text}"

iface = gr.Interface(
    fn=identify_plant,
    inputs=gr.Image(type="filepath", label="Upload UAE Plant Image"),
    outputs="text",
    title="🌿 UAE Plant Identifier",
    description="Upload a photo of a UAE plant (leaf preferred). We'll identify it using the Plant.id API.",
)

if __name__ == "__main__":
    iface.launch()