Spaces:
Runtime error
Runtime error
Upload tool
Browse files- app.py +4 -0
- calculator.py +20 -0
- requirements.txt +3 -0
- tool_config.json +5 -0
app.py
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import launch_gradio_demo
|
2 |
+
from calculator import SimpleCalculatorTool
|
3 |
+
|
4 |
+
launch_gradio_demo(SimpleCalculatorTool)
|
calculator.py
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
+
from transformers import Tool
|
3 |
+
from huggingface_hub import list_models
|
4 |
+
|
5 |
+
|
6 |
+
class SimpleCalculatorTool(Tool):
|
7 |
+
name = "simple_calculator"
|
8 |
+
description = (
|
9 |
+
"This is a tool that returns the result of a simple mathematical expression written in a string."
|
10 |
+
)
|
11 |
+
|
12 |
+
inputs = ["text"]
|
13 |
+
outputs = ["text"]
|
14 |
+
regex = r"(\d+(?:\.\d+)?)(\+|\-|\*|\/)(\d+(?:\.\d+)?)"
|
15 |
+
|
16 |
+
def __call__(self, input_str: str):
|
17 |
+
match = re.match(self.regex, input_str)
|
18 |
+
if match is None:
|
19 |
+
raise ValueError("Input string is not a valid mathematical expression.")
|
20 |
+
return eval(f"{match.group(1)} {match.group(2)} {match.group(3)}")
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
re
|
3 |
+
huggingface_hub
|
tool_config.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"description": "This is a tool that returns the result of a simple mathematical expression written in a string.",
|
3 |
+
"name": "simple_calculator",
|
4 |
+
"tool_class": "calculator.SimpleCalculatorTool"
|
5 |
+
}
|