Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,19 @@
|
|
| 1 |
from smolagents import CodeAgent, InferenceClientModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
-
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
|
| 8 |
|
| 9 |
-
# ────────────────────────────────────────────────
|
| 10 |
-
# Custom Tools
|
| 11 |
-
# ────────────────────────────────────────────────
|
| 12 |
-
|
| 13 |
@tool
|
| 14 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 15 |
"""Get the current local time in the requested timezone.
|
| 16 |
|
| 17 |
Args:
|
| 18 |
-
timezone: IANA timezone name (e.g. 'Asia/Kolkata', 'America/New_York')
|
|
|
|
|
|
|
|
|
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
tz = pytz.timezone(timezone)
|
|
@@ -29,13 +27,17 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 29 |
|
| 30 |
@tool
|
| 31 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 32 |
-
"""Placeholder custom tool — replace with real logic as needed.
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# ────────────────────────────────────────────────
|
| 37 |
-
# Main
|
| 38 |
-
# ────────────────────────────────────────────────
|
| 39 |
|
| 40 |
def main():
|
| 41 |
final_answer_tool = FinalAnswerTool()
|
|
@@ -46,36 +48,14 @@ def main():
|
|
| 46 |
max_tokens=2048,
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Optional text-to-image tool
|
| 50 |
image_tool = None
|
| 51 |
try:
|
| 52 |
image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
-
print("
|
| 54 |
-
except Exception as e:
|
| 55 |
-
print(f"⚠️ Text-to-image tool failed to load: {e}")
|
| 56 |
-
|
| 57 |
-
# ── Load your custom prompts.yaml + FIX the missing final_answer key ──
|
| 58 |
-
prompt_templates = None
|
| 59 |
-
try:
|
| 60 |
-
with open("prompts.yaml", encoding="utf-8") as f:
|
| 61 |
-
prompt_templates = yaml.safe_load(f)
|
| 62 |
-
print("✅ Custom prompt templates loaded")
|
| 63 |
-
|
| 64 |
-
# CRITICAL FIX for latest smolagents (v1.17+)
|
| 65 |
-
# The library now strictly requires the "final_answer" key
|
| 66 |
-
if prompt_templates and "final_answer" not in prompt_templates:
|
| 67 |
-
prompt_templates["final_answer"] = {
|
| 68 |
-
"pre_messages": "Here is the final answer:\n",
|
| 69 |
-
"post_messages": ""
|
| 70 |
-
}
|
| 71 |
-
print("✅ Added missing 'final_answer' template (required by latest smolagents)")
|
| 72 |
-
|
| 73 |
-
except FileNotFoundError:
|
| 74 |
-
print("⚠️ prompts.yaml not found → using default system prompt")
|
| 75 |
except Exception as e:
|
| 76 |
-
print(f"
|
| 77 |
|
| 78 |
-
# Tools list
|
| 79 |
tools = [
|
| 80 |
final_answer_tool,
|
| 81 |
get_current_time_in_timezone,
|
|
@@ -84,22 +64,20 @@ def main():
|
|
| 84 |
if image_tool is not None:
|
| 85 |
tools.append(image_tool)
|
| 86 |
|
| 87 |
-
# Create the agent
|
| 88 |
agent = CodeAgent(
|
| 89 |
tools=tools,
|
| 90 |
model=model,
|
| 91 |
-
max_steps=10,
|
| 92 |
verbosity_level=1,
|
| 93 |
stream_outputs=True,
|
| 94 |
-
# planning_interval=4, #
|
| 95 |
-
prompt_templates=
|
| 96 |
)
|
| 97 |
|
| 98 |
-
# Launch Gradio UI (perfect for HF Spaces / local)
|
| 99 |
GradioUI(agent).launch(
|
| 100 |
server_name="0.0.0.0",
|
| 101 |
server_port=7860,
|
| 102 |
-
# share=True,
|
| 103 |
)
|
| 104 |
|
| 105 |
|
|
|
|
| 1 |
from smolagents import CodeAgent, InferenceClientModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import pytz
|
|
|
|
| 4 |
from tools.final_answer import FinalAnswerTool
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
@tool
|
| 9 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 10 |
"""Get the current local time in the requested timezone.
|
| 11 |
|
| 12 |
Args:
|
| 13 |
+
timezone: IANA timezone name (e.g. 'Asia/Kolkata', 'America/New_York', 'Europe/London')
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
Human-readable string with current time or error message
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
tz = pytz.timezone(timezone)
|
|
|
|
| 27 |
|
| 28 |
@tool
|
| 29 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 30 |
+
"""Placeholder custom tool — replace with real logic as needed.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
arg1: some text input (e.g. a name, description, or keyword)
|
| 34 |
+
arg2: some integer input (e.g. a count, year, or multiplier)
|
| 35 |
|
| 36 |
+
Returns:
|
| 37 |
+
Demonstration response string
|
| 38 |
+
"""
|
| 39 |
+
return f"Tool called → arg1 = {arg1!r} | arg2 = {arg2} → What would you like to build?"
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def main():
|
| 43 |
final_answer_tool = FinalAnswerTool()
|
|
|
|
| 48 |
max_tokens=2048,
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# Optional: text-to-image tool
|
| 52 |
image_tool = None
|
| 53 |
try:
|
| 54 |
image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 55 |
+
print("Text-to-image tool loaded")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
+
print(f"Text-to-image load failed: {e}")
|
| 58 |
|
|
|
|
| 59 |
tools = [
|
| 60 |
final_answer_tool,
|
| 61 |
get_current_time_in_timezone,
|
|
|
|
| 64 |
if image_tool is not None:
|
| 65 |
tools.append(image_tool)
|
| 66 |
|
|
|
|
| 67 |
agent = CodeAgent(
|
| 68 |
tools=tools,
|
| 69 |
model=model,
|
| 70 |
+
max_steps=10,
|
| 71 |
verbosity_level=1,
|
| 72 |
stream_outputs=True,
|
| 73 |
+
# planning_interval=4, # optional
|
| 74 |
+
# prompt_templates=... # only if you really need custom prompts
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
GradioUI(agent).launch(
|
| 78 |
server_name="0.0.0.0",
|
| 79 |
server_port=7860,
|
| 80 |
+
# share=True,
|
| 81 |
)
|
| 82 |
|
| 83 |
|