Spaces:
Sleeping
Sleeping
added statistics_tool, get_current_time_in_timezone, search_tool, image_generation_tool
Browse files
app.py
CHANGED
|
@@ -18,6 +18,34 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
@@ -32,8 +60,31 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 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 +106,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,
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
@tool
|
| 22 |
+
def statistics_tool(data:list)-> str: #it's import to specify the return type
|
| 23 |
+
|
| 24 |
+
"""A tool that computes the mean, median, n and variance
|
| 25 |
+
Args:
|
| 26 |
+
data: a list of numbers. e.g. [1,2,3,1,4,23,4]
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
n = len(data)
|
| 30 |
+
mean = sum(data)/n
|
| 31 |
+
sorted_data = sorted(data)
|
| 32 |
+
|
| 33 |
+
if n % 2 == 1:
|
| 34 |
+
median = sorted_data[n // 2]
|
| 35 |
+
else:
|
| 36 |
+
median = (sorted_data[n // 2 - 1] + sorted_data[n // 2]) / 2
|
| 37 |
+
|
| 38 |
+
variance = sum((x - mean) ** 2 for x in data) / (n - 1)
|
| 39 |
+
|
| 40 |
+
return (
|
| 41 |
+
f"n={n}, "
|
| 42 |
+
f"mean={mean}, "
|
| 43 |
+
f"median={median}, "
|
| 44 |
+
f"variance={variance}"
|
| 45 |
+
)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return f"Error computing the statistics with Error {str(e)}."
|
| 48 |
+
|
| 49 |
@tool
|
| 50 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 51 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
| 60 |
return f"The current local time in {timezone} is: {local_time}"
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 63 |
+
|
| 64 |
+
@tool
|
| 65 |
+
def search_tool(query: str) -> str: # it's important to specify the return type
|
| 66 |
+
"""
|
| 67 |
+
Search the web using DuckDuckGo. Only one input with one output
|
| 68 |
+
|
| 69 |
+
Args:
|
| 70 |
+
query: The search query string
|
| 71 |
+
"""
|
| 72 |
+
try:
|
| 73 |
+
if not query or not query.strip():
|
| 74 |
+
raise ValueError("Query is empty")
|
| 75 |
+
|
| 76 |
+
# assume DuckDuckGoSearch is already imported
|
| 77 |
+
search = DuckDuckGoSearch()
|
| 78 |
+
results = search.run(query)
|
| 79 |
|
| 80 |
+
if not results:
|
| 81 |
+
return "No results found."
|
| 82 |
|
| 83 |
+
return f" The following results have been found '{results}'."
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Error during DuckDuckGo search: {e}"
|
| 87 |
+
|
| 88 |
final_answer = FinalAnswerTool()
|
| 89 |
|
| 90 |
# 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:
|
|
|
|
| 106 |
|
| 107 |
agent = CodeAgent(
|
| 108 |
model=model,
|
| 109 |
+
tools=[final_answer], statistics_tool, get_current_time_in_timezone, search_tool, image_generation_tool ## add your tools here (don't remove final answer)
|
| 110 |
max_steps=6,
|
| 111 |
verbosity_level=1,
|
| 112 |
grammar=None,
|