Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,10 @@
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
-
import datetime
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import yaml
|
| 6 |
from tools.final_answer import FinalAnswerTool
|
| 7 |
-
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
-
import math
|
| 11 |
-
import random
|
| 12 |
-
from typing import Literal
|
| 13 |
-
|
| 14 |
-
|
| 15 |
@tool
|
| 16 |
def get_approximate_integral(
|
| 17 |
expr: str,
|
|
@@ -19,60 +12,34 @@ def get_approximate_integral(
|
|
| 19 |
b: float,
|
| 20 |
samples: int = 1000
|
| 21 |
) -> str:
|
| 22 |
-
"""Approximate the definite integral of
|
| 23 |
-
|
| 24 |
Args:
|
| 25 |
-
expr:
|
| 26 |
-
a: Lower limit
|
| 27 |
-
b: Upper limit
|
| 28 |
-
|
| 29 |
-
- "simpson": composite Simpson’s rule
|
| 30 |
-
- "montecarlo": randomized Monte Carlo
|
| 31 |
-
- "auto" (default): picks Simpson for smoothness, Monte Carlo otherwise.
|
| 32 |
-
samples: Number of panels (for Simpson) or random points (for Monte Carlo).
|
| 33 |
"""
|
| 34 |
-
# Safely compile the expression
|
| 35 |
code = compile(expr, "<string>", "eval")
|
| 36 |
def f(x):
|
| 37 |
return eval(code, {"__builtins__": {}}, {"x": x, "math": math})
|
| 38 |
-
|
| 39 |
-
# Monte Carlo fallback
|
| 40 |
-
inside = 0.0
|
| 41 |
for _ in range(samples):
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
return f'{{"value": {result}, "method": "montecarlo", "samples": {samples}}}'
|
| 46 |
-
|
| 47 |
|
| 48 |
@tool
|
| 49 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 50 |
-
"""
|
| 51 |
-
Args:
|
| 52 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 53 |
-
"""
|
| 54 |
try:
|
| 55 |
-
# Create timezone object
|
| 56 |
tz = pytz.timezone(timezone)
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 60 |
except Exception as e:
|
| 61 |
-
return f"Error
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 66 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 67 |
-
# final_answer = FinalAnswerTool()
|
| 68 |
final_answer = FinalAnswerTool()
|
| 69 |
-
model = HfApiModel(
|
| 70 |
-
max_tokens=2096,
|
| 71 |
-
temperature=0.5,
|
| 72 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 73 |
-
custom_role_conversions=None,
|
| 74 |
-
)
|
| 75 |
-
|
| 76 |
|
| 77 |
model = HfApiModel(
|
| 78 |
max_tokens=2096,
|
|
@@ -82,21 +49,20 @@ model = HfApiModel(
|
|
| 82 |
)
|
| 83 |
|
| 84 |
|
| 85 |
-
# Import tool from Hub
|
| 86 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 87 |
|
| 88 |
tool_list = [
|
| 89 |
final_answer,
|
| 90 |
get_approximate_integral,
|
| 91 |
get_current_time_in_timezone,
|
| 92 |
-
image_generation_tool,
|
| 93 |
-
DuckDuckGoSearchTool(),
|
| 94 |
]
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
agent = CodeAgent(
|
| 101 |
model=model,
|
| 102 |
tools=tool_list,
|
|
@@ -110,4 +76,4 @@ agent = CodeAgent(
|
|
| 110 |
)
|
| 111 |
|
| 112 |
|
| 113 |
-
GradioUI(agent).launch()
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import pytz, yaml, math, random
|
| 4 |
+
from typing import Literal
|
|
|
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
|
|
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@tool
|
| 9 |
def get_approximate_integral(
|
| 10 |
expr: str,
|
|
|
|
| 12 |
b: float,
|
| 13 |
samples: int = 1000
|
| 14 |
) -> str:
|
| 15 |
+
"""Approximate the definite integral of f(x) over [a, b] via Monte Carlo.
|
| 16 |
+
|
| 17 |
Args:
|
| 18 |
+
expr: Python expression in x, e.g. "math.sin(x)".
|
| 19 |
+
a: Lower limit.
|
| 20 |
+
b: Upper limit.
|
| 21 |
+
samples: Number of random samples.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
|
|
|
| 23 |
code = compile(expr, "<string>", "eval")
|
| 24 |
def f(x):
|
| 25 |
return eval(code, {"__builtins__": {}}, {"x": x, "math": math})
|
| 26 |
+
total = 0.0
|
|
|
|
|
|
|
| 27 |
for _ in range(samples):
|
| 28 |
+
total += f(random.uniform(a, b))
|
| 29 |
+
result = (b - a) * total / samples
|
| 30 |
+
return f'{{"value": {result}, "samples": {samples}}}'
|
|
|
|
|
|
|
| 31 |
|
| 32 |
@tool
|
| 33 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 34 |
+
"""Fetch current time in a given TZ (e.g. 'America/New_York')."""
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
|
|
|
| 36 |
tz = pytz.timezone(timezone)
|
| 37 |
+
now = datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 38 |
+
return now
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
+
return f"Error: {e}"
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
final_answer = FinalAnswerTool()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
model = HfApiModel(
|
| 45 |
max_tokens=2096,
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
|
|
|
|
| 52 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
|
| 54 |
tool_list = [
|
| 55 |
final_answer,
|
| 56 |
get_approximate_integral,
|
| 57 |
get_current_time_in_timezone,
|
| 58 |
+
image_generation_tool,
|
| 59 |
+
DuckDuckGoSearchTool(),
|
| 60 |
]
|
| 61 |
|
| 62 |
+
with open("prompts.yaml") as f:
|
| 63 |
+
prompt_templates = yaml.safe_load(f)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
agent = CodeAgent(
|
| 67 |
model=model,
|
| 68 |
tools=tool_list,
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
|
| 79 |
+
GradioUI(agent).launch()
|