Spaces:
Sleeping
Sleeping
deploy
Browse files- .gitignore +1 -0
- README.md +35 -3
- app.py +97 -0
- requirements.txt +8 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Planthy
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
|
@@ -8,7 +8,39 @@ sdk_version: 5.33.0
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
-
short_description: a plant health monitoring
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
title: Planthy
|
| 3 |
+
emoji: 🌿
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: pink
|
| 6 |
sdk: gradio
|
|
|
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
license: apache-2.0
|
| 11 |
+
short_description: a plant health monitoring app
|
| 12 |
+
tags:
|
| 13 |
+
- agent-demo-track
|
| 14 |
+
|
| 15 |
---
|
| 16 |
|
| 17 |
+
# Planthy -- Plant Health Monitor Agent
|
| 18 |
+
|
| 19 |
+
## Description
|
| 20 |
+
|
| 21 |
+
A multimodal AI agent that diagnoses plant health issues by analyzing images and text queries, providing actionable recommendations. Built for a hackathon, it leverages real-time web search and advanced reasoning to assist gardeners and farmers, deployed as a user-friendly web app on Hugging Face Spaces.
|
| 22 |
+
|
| 23 |
+
## Tech Stack
|
| 24 |
+
|
| 25 |
+
* LlamaIndex: ReAct agent and tool orchestration.
|
| 26 |
+
|
| 27 |
+
* Gemini: Multimodal image and text processing (`gemini-2.0-flash`).
|
| 28 |
+
|
| 29 |
+
* DuckDuckGo Search: Real-time plant health information retrieval.
|
| 30 |
+
|
| 31 |
+
* Gradio: Web interface for user interaction.
|
| 32 |
+
|
| 33 |
+
## Functionality
|
| 34 |
+
|
| 35 |
+
* Image Analysis: Upload a plant image to identify type, condition, symptoms, and confidence (e.g., "Tomato, Diseased, Yellow spots, 0.85").
|
| 36 |
+
|
| 37 |
+
* Text Queries: Input queries (e.g., "What’s wrong with my plant?") to get tailored recommendations.
|
| 38 |
+
|
| 39 |
+
* ReAct Reasoning: Combines image analysis and web search for intelligent, context-aware responses.
|
| 40 |
+
|
| 41 |
+
* Web Interface: Gradio UI for easy image uploads, query input, and Markdown-formatted outputs (diagnosis, recommendations, reasoning).
|
| 42 |
+
|
| 43 |
+
* Real-Time Data: Fetches plant care tips via DuckDuckGo search, eliminating the need for a local dataset.
|
| 44 |
+
|
| 45 |
+
## Demo
|
| 46 |
+
|
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from llama_index.multi_modal_llms.gemini import GeminiMultiModal
|
| 6 |
+
from llama_index.core.program import MultiModalLLMCompletionProgram
|
| 7 |
+
from pydantic import BaseModel
|
| 8 |
+
from llama_index.core.output_parsers import PydanticOutputParser
|
| 9 |
+
from llama_index.core.tools import FunctionTool
|
| 10 |
+
from llama_index.core import SimpleDirectoryReader
|
| 11 |
+
from duckduckgo_search import DDGS
|
| 12 |
+
from llama_index.core.agent import ReActAgent
|
| 13 |
+
from llama_index.llms.gemini import Gemini
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
load_dotenv()
|
| 17 |
+
|
| 18 |
+
token = os.getenv("GEMINI_API_KEY")
|
| 19 |
+
|
| 20 |
+
class PlantHealth(BaseModel):
|
| 21 |
+
plant_type: str
|
| 22 |
+
condition: str
|
| 23 |
+
symptoms: str
|
| 24 |
+
confidence: float
|
| 25 |
+
|
| 26 |
+
prompt_template_str = """Analyze the plant image and return:
|
| 27 |
+
- Plant type (e.g., tomato, rose)
|
| 28 |
+
- Condition (e.g., healthy, diseased)
|
| 29 |
+
- Symptoms (e.g., yellow spots, wilting)
|
| 30 |
+
- Confidence score (0.0 to 1.0)
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
def analyze_plant_image(image_path: str) -> dict:
|
| 34 |
+
gemini_llm = GeminiMultiModal(model_name="gemini-2.0-flash")
|
| 35 |
+
llm_program = MultiModalLLMCompletionProgram.from_defaults(
|
| 36 |
+
output_parser=PydanticOutputParser(PlantHealth),
|
| 37 |
+
image_documents=[SimpleDirectoryReader(input_files=[image_path]).load_data()[0]],
|
| 38 |
+
prompt_template_str=prompt_template_str,
|
| 39 |
+
multi_modal_llm=gemini_llm,
|
| 40 |
+
)
|
| 41 |
+
return llm_program().dict()
|
| 42 |
+
|
| 43 |
+
image_tool = FunctionTool.from_defaults(
|
| 44 |
+
fn=analyze_plant_image,
|
| 45 |
+
name="analyze_plant_image",
|
| 46 |
+
description="Analyzes a plant image to identify type, condition, and symptoms."
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
def search_plant_info(query: str) -> str:
|
| 50 |
+
with DDGS() as ddgs:
|
| 51 |
+
results = ddgs.text(query, max_results=3)
|
| 52 |
+
return "\n".join([f"{r['title']}: {r['body']}" for r in results])
|
| 53 |
+
|
| 54 |
+
search_tool = FunctionTool.from_defaults(
|
| 55 |
+
fn=search_plant_info,
|
| 56 |
+
name="search_plant_info",
|
| 57 |
+
description="Searches the web for plant health and care information."
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
agent = ReActAgent.from_tools(
|
| 61 |
+
tools=[image_tool, search_tool],
|
| 62 |
+
llm=Gemini(model_name="gemini-2.0-flash"),
|
| 63 |
+
verbose=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def plant_health_app(image, user_query):
|
| 68 |
+
image_path = "temp_image.jpg"
|
| 69 |
+
image.save(image_path)
|
| 70 |
+
|
| 71 |
+
# Query the ReAct agent
|
| 72 |
+
response = agent.chat(f"Analyze this plant image : {image_path}, give a recommendations to cure the issue and return the answer in points. User query: {user_query}")
|
| 73 |
+
|
| 74 |
+
# Parse response (assume agent returns structured text)
|
| 75 |
+
recommendations = str(response)
|
| 76 |
+
|
| 77 |
+
output_text = recommendations
|
| 78 |
+
return output_text
|
| 79 |
+
|
| 80 |
+
iface = gr.Interface(
|
| 81 |
+
fn=plant_health_app,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.Image(type="pil", label="Upload Plant Image"),
|
| 84 |
+
gr.Textbox(label="Describe Symptoms or Ask a Question", placeholder="E.g., My tomato plant has yellow spots")
|
| 85 |
+
],
|
| 86 |
+
outputs=gr.Markdown(label="Diagnosis and Recommendations"),
|
| 87 |
+
title="🌿 Planthy -- Monitor Your Plant Health",
|
| 88 |
+
description="Upload a clear plant image and describe symptoms to get a diagnosis and care tips.",
|
| 89 |
+
theme="huggingface",
|
| 90 |
+
examples=[
|
| 91 |
+
["tomato.jpeg", "What’s wrong with my tomato plant?"],
|
| 92 |
+
["rose.jpeg", "Is my rose plant healthy?"]
|
| 93 |
+
]
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
if __name__ == "__main__":
|
| 97 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
llama-index
|
| 2 |
+
llama-index-multi-modal-llms-gemini
|
| 3 |
+
llama-index-embeddings-gemini
|
| 4 |
+
google-generativeai
|
| 5 |
+
gradio
|
| 6 |
+
pillow
|
| 7 |
+
matplotlib
|
| 8 |
+
duckduckgo-search
|