Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,86 @@
|
|
1 |
#!/usr/bin/env python
|
2 |
# coding=utf-8
|
3 |
"""
|
4 |
-
Robust startup file
|
5 |
-
|
|
|
6 |
"""
|
7 |
|
8 |
-
import datetime
|
9 |
import copy
|
|
|
10 |
import pytz
|
11 |
import yaml
|
12 |
|
13 |
-
#
|
14 |
from smolagents import CodeAgent, HfApiModel, tool
|
15 |
-
|
|
|
16 |
from Gradio_UI import GradioUI
|
17 |
|
18 |
-
#
|
19 |
from tools.final_answer import FinalAnswerTool
|
20 |
from tools.visit_webpage import VisitWebpageTool
|
21 |
from tools.web_search import DuckDuckGoSearchTool
|
22 |
|
23 |
-
#
|
|
|
24 |
@tool
|
25 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
26 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
return "What magic will you build ?"
|
28 |
|
29 |
|
|
|
30 |
@tool
|
31 |
def get_current_time_in_timezone(timezone: str) -> str:
|
32 |
-
"""Return current local time in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
try:
|
34 |
tz = pytz.timezone(timezone)
|
35 |
return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
36 |
except Exception as exc:
|
37 |
-
return f"
|
38 |
|
39 |
|
40 |
-
#
|
|
|
41 |
model = HfApiModel(
|
42 |
-
model_id="Qwen/Qwen3-32B",
|
43 |
max_tokens=2096,
|
44 |
temperature=0.5,
|
45 |
custom_role_conversions=None,
|
46 |
)
|
47 |
|
48 |
-
#
|
|
|
49 |
try:
|
50 |
with open("prompts.yaml", "r") as f:
|
51 |
-
|
52 |
except Exception as exc:
|
53 |
-
print("⚠️ Could not load prompts.yaml
|
54 |
-
|
55 |
|
56 |
-
#
|
57 |
-
def ensure_block(mapping, key, default):
|
58 |
-
"""If key missing or wrong type, replace with default."""
|
59 |
-
if key not in mapping or not isinstance(mapping[key], type(default)):
|
60 |
-
mapping[key] = copy.deepcopy(default)
|
61 |
-
|
62 |
-
# Minimal viable defaults
|
63 |
MIN_SYSTEM = "You are a helpful agent."
|
64 |
-
MIN_FINAL =
|
65 |
-
|
|
|
|
|
|
|
66 |
"initial_facts": "|-\n List any facts.",
|
67 |
"initial_plan": "|-\n Write a high-level plan.",
|
68 |
"update_facts_pre_messages": "|-\n Pre facts.",
|
@@ -70,24 +88,30 @@ MIN_PLAN_DICT = {
|
|
70 |
"update_plan_pre_messages": "|-\n Pre plan.",
|
71 |
"update_plan_post_messages": "|-\n Post plan.",
|
72 |
}
|
73 |
-
|
74 |
"task": "|-\n You are {{name}}.",
|
75 |
"report": "|-\n {{final_answer}}",
|
76 |
}
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
if isinstance(user_templates["planning"], str):
|
86 |
-
user_templates["planning"] = copy.deepcopy(MIN_PLAN_DICT)
|
87 |
-
if isinstance(user_templates["managed_agent"], str):
|
88 |
-
user_templates["managed_agent"] = copy.deepcopy(MIN_AGENT_DICT)
|
89 |
|
90 |
-
#
|
|
|
91 |
agent = CodeAgent(
|
92 |
model=model,
|
93 |
tools=[
|
@@ -99,8 +123,10 @@ agent = CodeAgent(
|
|
99 |
],
|
100 |
max_steps=6,
|
101 |
verbosity_level=1,
|
102 |
-
prompt_templates=
|
103 |
)
|
104 |
|
105 |
-
#
|
106 |
-
|
|
|
|
|
|
1 |
#!/usr/bin/env python
|
2 |
# coding=utf-8
|
3 |
"""
|
4 |
+
Robust startup file for your SmolAgents Space.
|
5 |
+
– Merges custom prompts.yaml with safe defaults
|
6 |
+
– Defines tools, model, CodeAgent, and Gradio UI
|
7 |
"""
|
8 |
|
|
|
9 |
import copy
|
10 |
+
import datetime
|
11 |
import pytz
|
12 |
import yaml
|
13 |
|
14 |
+
# --------- SmolAgents imports ---------
|
15 |
from smolagents import CodeAgent, HfApiModel, tool
|
16 |
+
|
17 |
+
# --------- Local Gradio wrapper --------
|
18 |
from Gradio_UI import GradioUI
|
19 |
|
20 |
+
# --------- Local tools -----------------
|
21 |
from tools.final_answer import FinalAnswerTool
|
22 |
from tools.visit_webpage import VisitWebpageTool
|
23 |
from tools.web_search import DuckDuckGoSearchTool
|
24 |
|
25 |
+
# ---------------------------------------------------------------------------
|
26 |
+
# Example custom tool (now with full doc-string so SmolAgents can JSON-schema it)
|
27 |
@tool
|
28 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
29 |
+
"""Example Tool: a simple placeholder.
|
30 |
+
|
31 |
+
Args:
|
32 |
+
arg1 (str): Any string input (unused – demo only).
|
33 |
+
arg2 (int): Any integer input (unused – demo only).
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
str: A constant demo message.
|
37 |
+
"""
|
38 |
return "What magic will you build ?"
|
39 |
|
40 |
|
41 |
+
# A small utility tool: get local time in a given timezone
|
42 |
@tool
|
43 |
def get_current_time_in_timezone(timezone: str) -> str:
|
44 |
+
"""Return the current local time in the specified timezone.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
timezone (str): IANA timezone name, e.g. 'America/New_York'.
|
48 |
+
|
49 |
+
Returns:
|
50 |
+
str: Formatted datetime string or an error message.
|
51 |
+
"""
|
52 |
try:
|
53 |
tz = pytz.timezone(timezone)
|
54 |
return datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
55 |
except Exception as exc:
|
56 |
+
return f"Error: {exc}"
|
57 |
|
58 |
|
59 |
+
# ---------------------------------------------------------------------------
|
60 |
+
# Model configuration
|
61 |
model = HfApiModel(
|
62 |
+
model_id="Qwen/Qwen3-32B", # Adjust if overloaded
|
63 |
max_tokens=2096,
|
64 |
temperature=0.5,
|
65 |
custom_role_conversions=None,
|
66 |
)
|
67 |
|
68 |
+
# ---------------------------------------------------------------------------
|
69 |
+
# 1) Load prompts.yaml (may be imperfect)
|
70 |
try:
|
71 |
with open("prompts.yaml", "r") as f:
|
72 |
+
custom_prompts = yaml.safe_load(f) or {}
|
73 |
except Exception as exc:
|
74 |
+
print("⚠️ Could not load prompts.yaml – using bare defaults.", exc)
|
75 |
+
custom_prompts = {}
|
76 |
|
77 |
+
# 2) Build minimal required template structure
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
MIN_SYSTEM = "You are a helpful agent."
|
79 |
+
MIN_FINAL = (
|
80 |
+
"Thought: I now know the answer.\n"
|
81 |
+
"Code:\n```py\nfinal_answer({{answer}})\n```<end_code>"
|
82 |
+
)
|
83 |
+
MIN_PLAN = {
|
84 |
"initial_facts": "|-\n List any facts.",
|
85 |
"initial_plan": "|-\n Write a high-level plan.",
|
86 |
"update_facts_pre_messages": "|-\n Pre facts.",
|
|
|
88 |
"update_plan_pre_messages": "|-\n Pre plan.",
|
89 |
"update_plan_post_messages": "|-\n Post plan.",
|
90 |
}
|
91 |
+
MIN_MGR = {
|
92 |
"task": "|-\n You are {{name}}.",
|
93 |
"report": "|-\n {{final_answer}}",
|
94 |
}
|
95 |
|
96 |
+
def ensure(mapping, key, default):
|
97 |
+
if key not in mapping or not isinstance(mapping[key], type(default)):
|
98 |
+
mapping[key] = copy.deepcopy(default)
|
99 |
+
|
100 |
+
ensure(custom_prompts, "system_prompt", MIN_SYSTEM)
|
101 |
+
ensure(custom_prompts, "final_answer", MIN_FINAL)
|
102 |
+
ensure(custom_prompts, "planning", MIN_PLAN)
|
103 |
+
ensure(custom_prompts, "managed_agent", MIN_MGR)
|
104 |
+
|
105 |
+
# If planning / managed_agent came in as strings, convert to dict default
|
106 |
+
if isinstance(custom_prompts["planning"], str):
|
107 |
+
custom_prompts["planning"] = copy.deepcopy(MIN_PLAN)
|
108 |
+
if isinstance(custom_prompts["managed_agent"], str):
|
109 |
+
custom_prompts["managed_agent"] = copy.deepcopy(MIN_MGR)
|
110 |
|
111 |
+
prompt_templates = custom_prompts # now guaranteed correct structure
|
|
|
|
|
|
|
|
|
112 |
|
113 |
+
# ---------------------------------------------------------------------------
|
114 |
+
# Build the CodeAgent
|
115 |
agent = CodeAgent(
|
116 |
model=model,
|
117 |
tools=[
|
|
|
123 |
],
|
124 |
max_steps=6,
|
125 |
verbosity_level=1,
|
126 |
+
prompt_templates=prompt_templates,
|
127 |
)
|
128 |
|
129 |
+
# ---------------------------------------------------------------------------
|
130 |
+
# Launch the Gradio UI
|
131 |
+
if __name__ == "__main__":
|
132 |
+
GradioUI(agent).launch()
|