Spaces:
Sleeping
Sleeping
Create tools.py
Browse files
tools.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
import re
|
| 3 |
+
import math
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def calculator(expression: str) -> str:
|
| 7 |
+
"""Evaluate a mathematical expression safely.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
expression: A mathematical expression (e.g., '2 + 2 * 3', 'sqrt(16)')
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
The result of the evaluation
|
| 14 |
+
"""
|
| 15 |
+
try:
|
| 16 |
+
# Clean the expression - remove any non-math characters for safety
|
| 17 |
+
# Allow only numbers, basic operators, parentheses, and math functions
|
| 18 |
+
safe_chars = r'[\d\s\+\-\*\/\(\)\.\^]|sqrt|pow|log|sin|cos|tan'
|
| 19 |
+
|
| 20 |
+
# Extract safe parts
|
| 21 |
+
safe_parts = re.findall(safe_chars, expression.lower())
|
| 22 |
+
safe_expression = ''.join(safe_parts)
|
| 23 |
+
|
| 24 |
+
if not safe_expression:
|
| 25 |
+
return "Error: No valid mathematical expression found"
|
| 26 |
+
|
| 27 |
+
# Replace ^ with ** for exponentiation
|
| 28 |
+
safe_expression = safe_expression.replace('^', '**')
|
| 29 |
+
|
| 30 |
+
# Define safe functions
|
| 31 |
+
safe_dict = {
|
| 32 |
+
'sqrt': math.sqrt,
|
| 33 |
+
'pow': math.pow,
|
| 34 |
+
'log': math.log,
|
| 35 |
+
'log10': math.log10,
|
| 36 |
+
'sin': math.sin,
|
| 37 |
+
'cos': math.cos,
|
| 38 |
+
'tan': math.tan,
|
| 39 |
+
'pi': math.pi,
|
| 40 |
+
'e': math.e
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# Evaluate safely using eval with limited globals
|
| 44 |
+
result = eval(safe_expression, {"__builtins__": {}}, safe_dict)
|
| 45 |
+
|
| 46 |
+
return f"Result of '{expression}': {result}"
|
| 47 |
+
|
| 48 |
+
except ZeroDivisionError:
|
| 49 |
+
return "Error: Division by zero"
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return f"Error evaluating expression: {str(e)}. Try: '2 + 2', '3 * 5', 'sqrt(16)', '2 ^ 3'"
|
| 52 |
+
|
| 53 |
+
@tool
|
| 54 |
+
def get_hf_dataset_info(dataset_name: str) -> str:
|
| 55 |
+
"""Get information about a Hugging Face dataset.
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
dataset_name: Name of the dataset (e.g., 'mnist', 'glue')
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
Dataset information
|
| 62 |
+
"""
|
| 63 |
+
try:
|
| 64 |
+
from huggingface_hub import get_dataset_info
|
| 65 |
+
|
| 66 |
+
# Handle common dataset names
|
| 67 |
+
if dataset_name.lower() == "mnist":
|
| 68 |
+
dataset_name = "mnist"
|
| 69 |
+
elif dataset_name.lower() == "glue":
|
| 70 |
+
dataset_name = "glue"
|
| 71 |
+
|
| 72 |
+
info = get_dataset_info(dataset_name)
|
| 73 |
+
|
| 74 |
+
response = f"📊 **Dataset: {info.id}**\n\n"
|
| 75 |
+
response += f"**Description:** {info.description[:300]}...\n\n"
|
| 76 |
+
response += f"**Downloads:** {info.downloads:,}\n"
|
| 77 |
+
response += f"**Last Modified:** {info.lastModified}\n"
|
| 78 |
+
response += f"**License:** {info.cardData.get('license', 'Not specified')}\n"
|
| 79 |
+
|
| 80 |
+
# Show dataset size if available
|
| 81 |
+
if hasattr(info, 'size') and info.size:
|
| 82 |
+
response += f"**Size:** {info.size}\n"
|
| 83 |
+
|
| 84 |
+
return response
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Error getting dataset info: {str(e)}\n\nTry: 'mnist', 'glue', or other Hugging Face dataset names"
|