Awashati's picture
Update app.py
f30306b verified
Raw
History Blame Contribute Delete
1.55 kB
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()