Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,33 @@
|
|
| 1 |
import os
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool,
|
| 7 |
|
| 8 |
-
# --- STEP 1:
|
| 9 |
-
# This creates a
|
| 10 |
prompt_content = {
|
| 11 |
-
"system_prompt": "You are a helpful agent
|
| 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
|
| 21 |
Args:
|
| 22 |
-
arg1:
|
| 23 |
-
arg2:
|
| 24 |
"""
|
| 25 |
-
return f"Magic
|
| 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'
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
tz = pytz.timezone(timezone)
|
|
@@ -37,43 +36,42 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 39 |
|
| 40 |
-
# --- STEP 3:
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
model =
|
| 44 |
max_tokens=2096,
|
| 45 |
temperature=0.5,
|
| 46 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 47 |
)
|
| 48 |
|
| 49 |
-
# Load
|
| 50 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 51 |
-
search_tool = DuckDuckGoSearchTool()
|
| 52 |
|
| 53 |
-
# Load
|
| 54 |
with open("prompts.yaml", 'r') as stream:
|
| 55 |
prompt_templates = yaml.safe_load(stream)
|
| 56 |
|
| 57 |
-
# Create the Agent
|
| 58 |
agent = CodeAgent(
|
| 59 |
model=model,
|
| 60 |
tools=[
|
| 61 |
get_current_time_in_timezone,
|
| 62 |
my_custom_tool,
|
| 63 |
image_generation_tool,
|
| 64 |
-
|
| 65 |
],
|
| 66 |
max_steps=6,
|
| 67 |
verbosity_level=1,
|
| 68 |
prompt_templates=prompt_templates
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# --- STEP 4:
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
#
|
| 75 |
-
#
|
| 76 |
-
# we use the standard agent.run() or a basic Gradio interface if that file is missing.
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 6 |
|
| 7 |
+
# --- STEP 1: CONFIGURATION & MOCK FILES ---
|
| 8 |
+
# This creates a prompts.yaml so the script doesn't crash
|
| 9 |
prompt_content = {
|
| 10 |
+
"system_prompt": "You are a helpful AI agent. Answer the question using tools if necessary."
|
| 11 |
}
|
| 12 |
with open("prompts.yaml", "w") as f:
|
| 13 |
yaml.dump(prompt_content, f)
|
| 14 |
|
| 15 |
+
# --- STEP 2: DEFINE YOUR TOOLS ---
|
| 16 |
|
| 17 |
@tool
|
| 18 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 19 |
+
"""A tool that does nothing yet
|
| 20 |
Args:
|
| 21 |
+
arg1: the first argument
|
| 22 |
+
arg2: the second argument
|
| 23 |
"""
|
| 24 |
+
return f"Magic confirmed with {arg1} and {arg2}!"
|
| 25 |
|
| 26 |
@tool
|
| 27 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 28 |
"""A tool that fetches the current local time in a specified timezone.
|
| 29 |
Args:
|
| 30 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 31 |
"""
|
| 32 |
try:
|
| 33 |
tz = pytz.timezone(timezone)
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 38 |
|
| 39 |
+
# --- STEP 3: INITIALIZE MODEL & AGENT ---
|
| 40 |
|
| 41 |
+
# Use HfApiModel (The updated name for InferenceClientModel)
|
| 42 |
+
model = HfApiModel(
|
| 43 |
max_tokens=2096,
|
| 44 |
temperature=0.5,
|
| 45 |
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# Load the image tool from the Hub
|
| 49 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 50 |
|
| 51 |
+
# Load the prompt template
|
| 52 |
with open("prompts.yaml", 'r') as stream:
|
| 53 |
prompt_templates = yaml.safe_load(stream)
|
| 54 |
|
| 55 |
+
# Create the Agent
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
tools=[
|
| 59 |
get_current_time_in_timezone,
|
| 60 |
my_custom_tool,
|
| 61 |
image_generation_tool,
|
| 62 |
+
DuckDuckGoSearchTool()
|
| 63 |
],
|
| 64 |
max_steps=6,
|
| 65 |
verbosity_level=1,
|
| 66 |
prompt_templates=prompt_templates
|
| 67 |
)
|
| 68 |
|
| 69 |
+
# --- STEP 4: RUNNING THE AGENT ---
|
| 70 |
|
| 71 |
+
# If you are in the Hugging Face Space/Course environment, use:
|
| 72 |
+
# from Gradio_UI import GradioUI
|
| 73 |
+
# GradioUI(agent).launch()
|
|
|
|
| 74 |
|
| 75 |
+
# If you are testing in a standard Python script/Colab:
|
| 76 |
+
print("--- Starting Agent Test ---")
|
| 77 |
+
agent.run("What is the current time in New York and then create an image of a futuristic clock.")
|