Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,66 +1,79 @@
|
|
| 1 |
-
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
|
| 10 |
-
# Below is an example of a tool that does nothing. Amaze us with your creativity!
|
| 11 |
@tool
|
| 12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str:
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
arg1:
|
| 17 |
-
arg2:
|
| 18 |
"""
|
| 19 |
-
return "
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 23 |
"""A tool that fetches the current local time in a specified timezone.
|
| 24 |
Args:
|
| 25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
# Create timezone object
|
| 29 |
tz = pytz.timezone(timezone)
|
| 30 |
-
# Get current time in that timezone
|
| 31 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 32 |
return f"The current local time in {timezone} is: {local_time}"
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
model = InferenceClientModel(
|
| 39 |
max_tokens=2096,
|
| 40 |
temperature=0.5,
|
| 41 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 42 |
-
custom_role_conversions=None,
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
# Import tool from Hub
|
| 47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 48 |
|
| 49 |
-
# Load system prompt from
|
| 50 |
with open("prompts.yaml", 'r') as stream:
|
| 51 |
prompt_templates = yaml.safe_load(stream)
|
| 52 |
-
|
|
|
|
| 53 |
agent = CodeAgent(
|
| 54 |
model=model,
|
| 55 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
max_steps=6,
|
| 57 |
verbosity_level=1,
|
| 58 |
-
|
| 59 |
-
planning_interval=None,
|
| 60 |
-
name=None,
|
| 61 |
-
description=None,
|
| 62 |
-
prompt_templates=prompt_templates # Pass system prompt to CodeAgent
|
| 63 |
)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool
|
| 7 |
+
|
| 8 |
+
# --- STEP 1: CREATE NECESSARY FILES (For Colab/VS Code safety) ---
|
| 9 |
+
# This creates a basic prompts.yaml if it doesn't exist
|
| 10 |
+
prompt_content = {
|
| 11 |
+
"system_prompt": "You are a helpful agent that can use tools to answer questions. Solve the task step by step."
|
| 12 |
+
}
|
| 13 |
+
with open("prompts.yaml", "w") as f:
|
| 14 |
+
yaml.dump(prompt_content, f)
|
| 15 |
|
| 16 |
+
# --- STEP 2: DEFINE TOOLS ---
|
| 17 |
|
|
|
|
| 18 |
@tool
|
| 19 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 20 |
+
"""A tool that returns a creative message combining a string and an integer.
|
|
|
|
| 21 |
Args:
|
| 22 |
+
arg1: A descriptive string or name
|
| 23 |
+
arg2: A lucky number or quantity
|
| 24 |
"""
|
| 25 |
+
return f"Magic is happening! You provided '{arg1}' and the number {arg2}. Your agent is ready!"
|
| 26 |
|
| 27 |
@tool
|
| 28 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 29 |
"""A tool that fetches the current local time in a specified timezone.
|
| 30 |
Args:
|
| 31 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York' or 'Europe/London').
|
| 32 |
"""
|
| 33 |
try:
|
|
|
|
| 34 |
tz = pytz.timezone(timezone)
|
|
|
|
| 35 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 36 |
return f"The current local time in {timezone} is: {local_time}"
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 39 |
|
| 40 |
+
# --- STEP 3: SETUP MODEL AND AGENT ---
|
| 41 |
|
| 42 |
+
# If you have an HF_TOKEN in your env variables, it will be picked up automatically
|
| 43 |
model = InferenceClientModel(
|
| 44 |
max_tokens=2096,
|
| 45 |
temperature=0.5,
|
| 46 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Load external tools
|
|
|
|
| 50 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 51 |
+
search_tool = DuckDuckGoSearchTool()
|
| 52 |
|
| 53 |
+
# Load system prompt from the file we created
|
| 54 |
with open("prompts.yaml", 'r') as stream:
|
| 55 |
prompt_templates = yaml.safe_load(stream)
|
| 56 |
+
|
| 57 |
+
# Create the Agent with ALL tools included
|
| 58 |
agent = CodeAgent(
|
| 59 |
model=model,
|
| 60 |
+
tools=[
|
| 61 |
+
get_current_time_in_timezone,
|
| 62 |
+
my_custom_tool,
|
| 63 |
+
image_generation_tool,
|
| 64 |
+
search_tool
|
| 65 |
+
],
|
| 66 |
max_steps=6,
|
| 67 |
verbosity_level=1,
|
| 68 |
+
prompt_templates=prompt_templates
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# --- STEP 4: RUN THE AGENT ---
|
| 72 |
+
|
| 73 |
+
# In VS Code/Local: This will open a browser window
|
| 74 |
+
# In Colab: This will show the UI in the cell output
|
| 75 |
+
# Note: Since Gradio_UI is a custom local file in the course,
|
| 76 |
+
# we use the standard agent.run() or a basic Gradio interface if that file is missing.
|
| 77 |
|
| 78 |
+
print("Agent is initialized. You can now ask questions like: 'What time is it in Tokyo?'")
|
| 79 |
+
agent.run("What is the current time in Paris?")
|