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