Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -5,48 +5,17 @@ from smolagents import tool
|
|
| 5 |
|
| 6 |
|
| 7 |
@tool
|
| 8 |
-
def
|
| 9 |
-
a
|
| 10 |
-
) -> float:
|
| 11 |
-
"""
|
| 12 |
-
Perform basic arithmetic operations on two real numbers.
|
| 13 |
-
|
| 14 |
-
This function acts as a simple calculator that can add, subtract,
|
| 15 |
-
multiply, or divide two floating-point numbers.
|
| 16 |
-
|
| 17 |
-
Args:
|
| 18 |
-
a (float): The first number.
|
| 19 |
-
b (float): The second number.
|
| 20 |
-
operation (Literal["add", "subtract", "multiply", "divide"]):
|
| 21 |
-
The arithmetic operation to perform.
|
| 22 |
-
|
| 23 |
-
Returns:
|
| 24 |
-
float: The result of the arithmetic operation.
|
| 25 |
-
|
| 26 |
-
Raises:
|
| 27 |
-
ValueError: If the operation is invalid or if division by zero is attempted.
|
| 28 |
-
|
| 29 |
-
Example:
|
| 30 |
-
>>> real_number_calculator(10, 5, "add")
|
| 31 |
-
15.0
|
| 32 |
-
>>> real_number_calculator(10, 5, "divide")
|
| 33 |
-
2.0
|
| 34 |
-
"""
|
| 35 |
-
|
| 36 |
-
if operation == "add":
|
| 37 |
-
return a + b
|
| 38 |
-
elif operation == "subtract":
|
| 39 |
-
return a - b
|
| 40 |
-
elif operation == "multiply":
|
| 41 |
-
return a * b
|
| 42 |
-
elif operation == "divide":
|
| 43 |
-
if b == 0:
|
| 44 |
-
raise ValueError("Division by zero is not allowed.")
|
| 45 |
-
return a / b
|
| 46 |
-
else:
|
| 47 |
-
raise ValueError(f"Invalid operation: {operation}")
|
| 48 |
-
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
|
| 52 |
class TestAgent:
|
|
@@ -75,7 +44,7 @@ class TestAgent:
|
|
| 75 |
#model = InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 76 |
# Instantiate the agent
|
| 77 |
self.agent = CodeAgent(
|
| 78 |
-
tools=[
|
| 79 |
DuckDuckGoSearchTool(), # basic tools from smolagent
|
| 80 |
VisitWebpageTool(),
|
| 81 |
wikipedia_tool, # tool from langchain with extra parmaeters
|
|
@@ -85,8 +54,11 @@ class TestAgent:
|
|
| 85 |
model=model,
|
| 86 |
max_steps=3,
|
| 87 |
verbosity_level=2,
|
| 88 |
-
use_structured_outputs_internally=True
|
| 89 |
)
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
def __call__(self, question: str) -> str:
|
| 92 |
|
|
|
|
| 5 |
|
| 6 |
|
| 7 |
@tool
|
| 8 |
+
def extract_text_from_audio(file_path : str) -> str:
|
| 9 |
+
"""given a path to an audio file, it extract and returns the text contained in it as a string"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
import speech_recognition as sr
|
| 12 |
+
r = sr.Recognizer()
|
| 13 |
+
with sr.AudioFile(file_path) as source:
|
| 14 |
+
# listen for the data (load audio to memory)
|
| 15 |
+
audio_data = r.record(source)
|
| 16 |
+
# recognize (convert from speech to text)
|
| 17 |
+
text = r.recognize_google(audio_data)
|
| 18 |
+
return text
|
| 19 |
|
| 20 |
|
| 21 |
class TestAgent:
|
|
|
|
| 44 |
#model = InferenceClientModel("Qwen/Qwen2.5-Coder-32B-Instruct")
|
| 45 |
# Instantiate the agent
|
| 46 |
self.agent = CodeAgent(
|
| 47 |
+
tools=[extract_text_from_audio, # homemade tool
|
| 48 |
DuckDuckGoSearchTool(), # basic tools from smolagent
|
| 49 |
VisitWebpageTool(),
|
| 50 |
wikipedia_tool, # tool from langchain with extra parmaeters
|
|
|
|
| 54 |
model=model,
|
| 55 |
max_steps=3,
|
| 56 |
verbosity_level=2,
|
| 57 |
+
use_structured_outputs_internally=True. # V3. Adds structure
|
| 58 |
)
|
| 59 |
+
# V3. add Guidance
|
| 60 |
+
prompt_for_guidance = "\n10. Provide the answer axactly as it is asked, be concise and precise\n\nNow Begin!"
|
| 61 |
+
self.agent.prompt_templates['system_prompt'] = self.agent.prompt_templates['system_prompt'] + prompt_for_guidance
|
| 62 |
|
| 63 |
def __call__(self, question: str) -> str:
|
| 64 |
|