Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,16 +7,68 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
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:
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
import math
|
| 11 |
+
import random
|
| 12 |
+
from typing import Literal
|
| 13 |
+
|
| 14 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 15 |
+
import math
|
| 16 |
+
import random
|
| 17 |
+
from typing import Literal
|
| 18 |
+
from your_agent_framework import tool # adjust import to your AI agent framework
|
| 19 |
+
|
| 20 |
+
|
| 21 |
@tool
|
| 22 |
+
def get_approximate_integral(
|
| 23 |
+
expr: str,
|
| 24 |
+
a: float,
|
| 25 |
+
b: float,
|
| 26 |
+
method: Literal["auto", "simpson", "montecarlo"] = "auto",
|
| 27 |
+
samples: int = 1000
|
| 28 |
+
) -> str:
|
| 29 |
+
"""Approximate the definite integral of a function over [a, b].
|
| 30 |
+
|
| 31 |
Args:
|
| 32 |
+
expr: A Python expression in variable `x`, e.g. "math.sin(x) + x**2".
|
| 33 |
+
a: Lower limit of integration.
|
| 34 |
+
b: Upper limit of integration.
|
| 35 |
+
method: Which algorithm to use:
|
| 36 |
+
- "simpson": composite Simpson’s rule
|
| 37 |
+
- "montecarlo": randomized Monte Carlo
|
| 38 |
+
- "auto" (default): picks Simpson for smoothness, Monte Carlo otherwise.
|
| 39 |
+
samples: Number of panels (for Simpson) or random points (for Monte Carlo).
|
| 40 |
"""
|
| 41 |
+
# Safely compile the expression
|
| 42 |
+
code = compile(expr, "<string>", "eval")
|
| 43 |
+
def f(x):
|
| 44 |
+
return eval(code, {"__builtins__": {}}, {"x": x, "math": math})
|
| 45 |
+
|
| 46 |
+
# Auto‑choose method by sampling curvature
|
| 47 |
+
if method == "auto":
|
| 48 |
+
mid = (a + b) / 2
|
| 49 |
+
# estimate curvature via second finite difference
|
| 50 |
+
f0, f1, f2 = f(a), f(mid), f(b)
|
| 51 |
+
curvature = abs((f0 - 2*f1 + f2) / ((b - a)/2)**2)
|
| 52 |
+
method = "montecarlo" if curvature > 1e3 else "simpson"
|
| 53 |
+
|
| 54 |
+
if method == "simpson":
|
| 55 |
+
n = samples if samples % 2 == 0 else samples + 1
|
| 56 |
+
h = (b - a) / n
|
| 57 |
+
total = f(a) + f(b)
|
| 58 |
+
for i in range(1, n):
|
| 59 |
+
weight = 4 if i % 2 else 2
|
| 60 |
+
total += weight * f(a + i * h)
|
| 61 |
+
result = total * h / 3
|
| 62 |
+
return f'{{"value": {result}, "method": "simpson", "panels": {n}}}'
|
| 63 |
+
|
| 64 |
+
# Monte Carlo fallback
|
| 65 |
+
inside = 0.0
|
| 66 |
+
for _ in range(samples):
|
| 67 |
+
x = random.uniform(a, b)
|
| 68 |
+
inside += f(x)
|
| 69 |
+
result = (b - a) * inside / samples
|
| 70 |
+
return f'{{"value": {result}, "method": "montecarlo", "samples": {samples}}}'
|
| 71 |
+
|
| 72 |
|
| 73 |
@tool
|
| 74 |
def get_current_time_in_timezone(timezone: str) -> str:
|