Spaces:
Sleeping
Sleeping
Rename tools.py to tools_agent.py
Browse files- tools.py +0 -14
- tools_agent.py +84 -0
tools.py
DELETED
|
@@ -1,14 +0,0 @@
|
|
| 1 |
-
from smolagents import Tool
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
class TextTransformerTool(Tool):
|
| 5 |
-
name = "text_transformer"
|
| 6 |
-
description = "Reverses the input text."
|
| 7 |
-
|
| 8 |
-
inputs = {
|
| 9 |
-
"text": {"type": "string", "description": "Text to reverse."}
|
| 10 |
-
}
|
| 11 |
-
output_type = "string"
|
| 12 |
-
|
| 13 |
-
def forward(self, text: str) -> str:
|
| 14 |
-
return text[::-1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tools_agent.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from smolagents import Tool
|
| 3 |
+
from typing import Any, Dict, Optional
|
| 4 |
+
|
| 5 |
+
class ReverseTextTool(Tool):
|
| 6 |
+
name = "reverse_text"
|
| 7 |
+
description = "Reverses the input text."
|
| 8 |
+
# tell the validator: I’m expecting a dict with key "text"
|
| 9 |
+
inputs = {"input": {"type": "any", "description": "The text to be reversed"}}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
|
| 12 |
+
def forward(self, input: Any) -> Any:
|
| 13 |
+
return input[::-1]
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class TableCommutativityTool(Tool):
|
| 17 |
+
name = "find_non_commutative_elements"
|
| 18 |
+
description = (
|
| 19 |
+
"Given a multiplication table (2D list) and its header elements, "
|
| 20 |
+
"returns the elements involved in any a*b != b*a."
|
| 21 |
+
)
|
| 22 |
+
inputs = {
|
| 23 |
+
"input": {
|
| 24 |
+
"type": "any",
|
| 25 |
+
"description": "Dict with keys 'table' (list of lists) and 'elements' (list of strings)."
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
output_type = "string"
|
| 29 |
+
|
| 30 |
+
def forward(self, input: dict) -> list[str]:
|
| 31 |
+
table = input["table"]
|
| 32 |
+
elements = input["elements"]
|
| 33 |
+
non_comm = set()
|
| 34 |
+
for i, a in enumerate(elements):
|
| 35 |
+
for j, b in enumerate(elements):
|
| 36 |
+
if table[i][j] != table[j][i]:
|
| 37 |
+
non_comm.update({a, b})
|
| 38 |
+
return str(sorted(non_comm))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
class VegetableListTool(Tool):
|
| 43 |
+
name = "list_vegetables"
|
| 44 |
+
description = (
|
| 45 |
+
"From a list of grocery items, returns those that are true vegetables "
|
| 46 |
+
"(botanical definition), sorted alphabetically."
|
| 47 |
+
)
|
| 48 |
+
inputs = {
|
| 49 |
+
"input": {
|
| 50 |
+
"type": "any",
|
| 51 |
+
"description": "Dict with key 'items' containing a list of item strings."
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
output_type = "string"
|
| 55 |
+
|
| 56 |
+
_VEG_SET = {
|
| 57 |
+
"broccoli", "bell pepper", "celery", "corn",
|
| 58 |
+
"green beans", "lettuce", "sweet potatoes", "zucchini"
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
def forward(self, input: Any) -> Any:
|
| 62 |
+
items = input["items"]
|
| 63 |
+
return str(sorted(item for item in items if item in self._VEG_SET))
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class ExcelSumFoodTool(Tool):
|
| 67 |
+
name = "sum_food_sales"
|
| 68 |
+
description = (
|
| 69 |
+
"Reads an Excel file with columns 'Category' and 'Sales', "
|
| 70 |
+
"and returns total sales where Category != 'Drink', rounded to two decimals."
|
| 71 |
+
)
|
| 72 |
+
inputs = {
|
| 73 |
+
"input": {
|
| 74 |
+
"type": "any",
|
| 75 |
+
"description": "Dict with key 'excel_path' pointing to the .xlsx file to read."
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
output_type = "string"
|
| 79 |
+
|
| 80 |
+
def forward(self, input: Any) -> Any:
|
| 81 |
+
excel_path = input["excel_path"]
|
| 82 |
+
df = pd.read_excel(excel_path)
|
| 83 |
+
total = df.loc[df["Category"] != "Drink", "Sales"].sum()
|
| 84 |
+
return str(round(float(total), 2))
|