File size: 2,189 Bytes
4ce13c2
 
 
 
 
 
 
 
4101a8a
 
 
4ce13c2
4101a8a
 
4ce13c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4101a8a
 
 
 
 
 
2d25823
 
 
 
 
 
 
 
 
 
 
 
4101a8a
 
 
2d25823
4ce13c2
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
import requests
import base64
import json

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

# Tracks the last prediction to detect repetition
last_prediction = {"name": None}

def identify_plant(image):
    global last_prediction

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

        payload = {
            "images": [img_data],
            "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:
            return f"⚠️ Error {response.status_code}: {response.text}"

        result = response.json()
        suggestions = result.get("result", {}).get("classification", {}).get("suggestions", [])

        if not suggestions:
            return "❌ No plant match found."

        top = suggestions[0]
        name = top.get("name", "Unknown")
        prob = top.get("probability", 0.0)
        desc = top.get("details", {}).get("description", {}).get("value", "No description available.")

        warning = ""
        if last_prediction["name"] == name:
            warning = "\n⚠️ Same result as last time. This may indicate repetition. Try a different photo or angle."

        last_prediction["name"] = name

    result = response.json()

suggestions = result.get("result", {}).get("classification", {}).get("suggestions", [])

if not suggestions:
    return "❌ No plant match found."

top = suggestions[0]
name = top.get("name", "Unknown")
prob = top.get("probability", 0.0)

return f"""
🌿 **Plant:** {name}
📊 **Confidence:** {prob * 100:.2f}%
"""


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()