Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,21 @@
|
|
| 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')
|
| 14 |
-
|
| 15 |
-
Returns:
|
| 16 |
-
Human-readable string with current time or error message
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
tz = pytz.timezone(timezone)
|
|
@@ -31,6 +33,10 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 31 |
return f"Tool called → arg1 = {arg1!r} | arg2 = {arg2} → What would you like to build?"
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def main():
|
| 35 |
final_answer_tool = FinalAnswerTool()
|
| 36 |
|
|
@@ -40,13 +46,34 @@ def main():
|
|
| 40 |
max_tokens=2048,
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Optional text-to-image tool
|
| 44 |
image_tool = None
|
| 45 |
try:
|
| 46 |
image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 47 |
-
print("Text-to-image tool loaded")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
print(f"
|
| 50 |
|
| 51 |
# Tools list
|
| 52 |
tools = [
|
|
@@ -57,22 +84,22 @@ def main():
|
|
| 57 |
if image_tool is not None:
|
| 58 |
tools.append(image_tool)
|
| 59 |
|
| 60 |
-
# Create
|
| 61 |
agent = CodeAgent(
|
| 62 |
tools=tools,
|
| 63 |
model=model,
|
| 64 |
-
max_steps=
|
| 65 |
verbosity_level=1,
|
| 66 |
stream_outputs=True,
|
| 67 |
-
# planning_interval=4,
|
| 68 |
-
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Launch UI
|
| 72 |
GradioUI(agent).launch(
|
| 73 |
server_name="0.0.0.0",
|
| 74 |
server_port=7860,
|
| 75 |
-
# share=True,
|
| 76 |
)
|
| 77 |
|
| 78 |
|
|
|
|
| 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)
|
|
|
|
| 33 |
return f"Tool called → arg1 = {arg1!r} | arg2 = {arg2} → What would you like to build?"
|
| 34 |
|
| 35 |
|
| 36 |
+
# ────────────────────────────────────────────────
|
| 37 |
+
# Main
|
| 38 |
+
# ────────────────────────────────────────────────
|
| 39 |
+
|
| 40 |
def main():
|
| 41 |
final_answer_tool = FinalAnswerTool()
|
| 42 |
|
|
|
|
| 46 |
max_tokens=2048,
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Optional text-to-image tool from Hub
|
| 50 |
image_tool = None
|
| 51 |
try:
|
| 52 |
image_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 53 |
+
print("✅ Text-to-image tool loaded from Hub")
|
| 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"⚠️ Failed to load prompts.yaml: {e}")
|
| 77 |
|
| 78 |
# Tools list
|
| 79 |
tools = [
|
|
|
|
| 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, # increased for better performance
|
| 92 |
verbosity_level=1,
|
| 93 |
stream_outputs=True,
|
| 94 |
+
# planning_interval=4, # uncomment if you want planning mode
|
| 95 |
+
prompt_templates=prompt_templates, # now guaranteed to be valid
|
| 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, # uncomment for temporary public link
|
| 103 |
)
|
| 104 |
|
| 105 |
|