Spaces:
No application file
No application file
Upload tool
Browse files- app.py +4 -0
- calc.py +51 -0
- requirements.txt +1 -0
- tool_config.json +9 -0
app.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import launch_gradio_demo
|
| 2 |
+
from calc import SimpleCalculatorTool
|
| 3 |
+
|
| 4 |
+
launch_gradio_demo(SimpleCalculatorTool)
|
calc.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import Tool
|
| 2 |
+
|
| 3 |
+
def simple_calculator(expression):
|
| 4 |
+
# Find the index of the operator
|
| 5 |
+
operator_index = -1
|
| 6 |
+
for i, char in enumerate(expression):
|
| 7 |
+
if char in ('+', '-', '*', '/'):
|
| 8 |
+
operator_index = i
|
| 9 |
+
break
|
| 10 |
+
|
| 11 |
+
# Ensure that an operator was found
|
| 12 |
+
if operator_index == -1:
|
| 13 |
+
return "Invalid expression"
|
| 14 |
+
|
| 15 |
+
# Split the expression into the operator and operands
|
| 16 |
+
operator = expression[operator_index]
|
| 17 |
+
operand1 = float(expression[:operator_index])
|
| 18 |
+
operand2 = float(expression[operator_index + 1:])
|
| 19 |
+
|
| 20 |
+
# Perform the calculation based on the operator
|
| 21 |
+
if operator == '+':
|
| 22 |
+
result = operand1 + operand2
|
| 23 |
+
elif operator == '-':
|
| 24 |
+
result = operand1 - operand2
|
| 25 |
+
elif operator == '*':
|
| 26 |
+
result = operand1 * operand2
|
| 27 |
+
elif operator == '/':
|
| 28 |
+
result = operand1 / operand2
|
| 29 |
+
else:
|
| 30 |
+
raise ValueError("Input string is not a valid mathematical expression.")
|
| 31 |
+
|
| 32 |
+
# Convert the result to a string and return it
|
| 33 |
+
return str(result)
|
| 34 |
+
|
| 35 |
+
# Test the function
|
| 36 |
+
expression = "1+3"
|
| 37 |
+
result = simple_calculator(expression)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class SimpleCalculatorTool(Tool):
|
| 41 |
+
name = "simple_calculator"
|
| 42 |
+
description = (
|
| 43 |
+
"This is a tool that returns the result of a simple mathematical expression written in a string."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
inputs = ["text"]
|
| 47 |
+
outputs = ["text"]
|
| 48 |
+
output_type = "text"
|
| 49 |
+
|
| 50 |
+
def __call__(self, input_str: str):
|
| 51 |
+
return simple_calculator(input_str)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
transformers
|
tool_config.json
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"description": "This is a tool that returns the result of a simple mathematical expression written in a string.",
|
| 3 |
+
"inputs": [
|
| 4 |
+
"text"
|
| 5 |
+
],
|
| 6 |
+
"name": "simple_calculator",
|
| 7 |
+
"output_type": "text",
|
| 8 |
+
"tool_class": "calc.SimpleCalculatorTool"
|
| 9 |
+
}
|