Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdds a single tool that describes a plant image if provided
app.py
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
|
@@ -7,33 +18,44 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
-
Args:
|
| 16 |
-
arg1: the first argument
|
| 17 |
-
arg2: the second argument
|
| 18 |
-
"""
|
| 19 |
-
return "What magic will you build ?"
|
| 20 |
|
| 21 |
@tool
|
| 22 |
-
def
|
| 23 |
-
"""
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
"""
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
except Exception as e:
|
| 34 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
|
|
|
|
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
|
@@ -55,7 +77,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This app.py consitutes an application where users can pose queries to
|
| 3 |
+
an agent about plants.
|
| 4 |
+
|
| 5 |
+
The user can provide pictures and text about plants and expect the agent to answer
|
| 6 |
+
their questions about said plant.
|
| 7 |
+
|
| 8 |
+
Useful tools:
|
| 9 |
+
1. DuckDuckGoSearchTool -- get internet search results about plant
|
| 10 |
+
2.
|
| 11 |
+
"""
|
| 12 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 13 |
import datetime
|
| 14 |
import requests
|
|
|
|
| 18 |
|
| 19 |
from Gradio_UI import GradioUI
|
| 20 |
|
| 21 |
+
|
| 22 |
+
vl_model = InferenceClient(model='Qwen/Qwen3-VL-4B-Thinking')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
@tool
|
| 25 |
+
def describe_plant_image(user_query: str, image_url: str) -> str:
|
| 26 |
+
"""
|
| 27 |
+
Describe a plant image and answer the user's query.
|
| 28 |
+
Only to be used if and only if a user provides an image_url or if
|
| 29 |
+
prior chat messages have retrieved image_url(s).
|
| 30 |
"""
|
| 31 |
+
system_prompt = (
|
| 32 |
+
"You are an LLM assistant that analyzes plant images and:\n"
|
| 33 |
+
"1. Identifies the plant if possible\n"
|
| 34 |
+
"2. Describes key visible characteristics\n"
|
| 35 |
+
"3. Answers the user's question clearly and concisely"
|
| 36 |
+
)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
response = vl_model.chat.completions.create(
|
| 39 |
+
messages=[
|
| 40 |
+
{
|
| 41 |
+
"role": "system",
|
| 42 |
+
"content": system_prompt,
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"role": "user",
|
| 46 |
+
"content": [
|
| 47 |
+
{"type": "text", "text": user_query},
|
| 48 |
+
{
|
| 49 |
+
"type": "image_url",
|
| 50 |
+
"image_url": {"url": image_url},
|
| 51 |
+
},
|
| 52 |
+
],
|
| 53 |
+
},
|
| 54 |
+
]
|
| 55 |
+
)
|
| 56 |
|
| 57 |
+
return response.choices[0].message.content
|
| 58 |
+
|
| 59 |
final_answer = FinalAnswerTool()
|
| 60 |
|
| 61 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
|
|
|
| 77 |
|
| 78 |
agent = CodeAgent(
|
| 79 |
model=model,
|
| 80 |
+
tools=[DuckDuckGoSearchTool, describe_plant_image, final_answer], ## add your tools here (don't remove final answer)
|
| 81 |
max_steps=6,
|
| 82 |
verbosity_level=1,
|
| 83 |
grammar=None,
|