Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadded monte_carlo_integration
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool,
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -9,14 +9,41 @@ from Gradio_UI import GradioUI
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -55,7 +82,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer, get_current_time_in_timezone, image_generation_tool], ## add your tools here (don't remove final answer)
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool,HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
@tool
|
| 12 |
+
def monte_carlo_integration(func_expr: str, a: float, b: float, samples: int = 10000) -> str:
|
| 13 |
+
"""A tool that computes the approximate definite integral of a given function between limits a and b using Monte Carlo integration.
|
| 14 |
+
|
| 15 |
Args:
|
| 16 |
+
func_expr: A string representing a mathematical function of x (e.g., "x**2 + 3*x + 2").
|
| 17 |
+
a: The lower bound of integration.
|
| 18 |
+
b: The upper bound of integration.
|
| 19 |
+
samples: The number of random samples to use for the approximation. Defaults to 10000.
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
A string representing the approximate value of the integral.
|
| 23 |
"""
|
| 24 |
+
import random
|
| 25 |
+
import math
|
| 26 |
+
|
| 27 |
+
# Define a safe evaluation context with math functions
|
| 28 |
+
safe_dict = {k: getattr(math, k) for k in dir(math) if not k.startswith("__")}
|
| 29 |
+
|
| 30 |
+
# Define the function to integrate safely using eval
|
| 31 |
+
def f(x):
|
| 32 |
+
try:
|
| 33 |
+
# Only allow x and math functions in the evaluation context
|
| 34 |
+
safe_dict["x"] = x
|
| 35 |
+
return eval(func_expr, {"__builtins__": {}}, safe_dict)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
raise ValueError(f"Error evaluating function at x={x}: {e}")
|
| 38 |
+
|
| 39 |
+
total = 0.0
|
| 40 |
+
for _ in range(samples):
|
| 41 |
+
x = random.uniform(a, b)
|
| 42 |
+
total += f(x)
|
| 43 |
+
|
| 44 |
+
integral = (b - a) * total / samples
|
| 45 |
+
return f"The approximate integral of {func_expr} from {a} to {b} is {integral:.4f}"
|
| 46 |
+
|
| 47 |
|
| 48 |
@tool
|
| 49 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 82 |
|
| 83 |
agent = CodeAgent(
|
| 84 |
model=model,
|
| 85 |
+
tools=[final_answer, monte_carlo_integration, get_current_time_in_timezone, image_generation_tool, DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
|
| 86 |
max_steps=6,
|
| 87 |
verbosity_level=1,
|
| 88 |
grammar=None,
|