Spaces:
Sleeping
Sleeping
update app.py
Browse files
app.py
CHANGED
|
@@ -4,24 +4,25 @@ import pytz
|
|
| 4 |
import yaml
|
| 5 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 6 |
|
| 7 |
-
# --- STEP 1:
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
prompt_content = {
|
| 10 |
-
"system_prompt":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
}
|
|
|
|
| 12 |
with open("prompts.yaml", "w") as f:
|
| 13 |
yaml.dump(prompt_content, f)
|
| 14 |
|
| 15 |
-
# --- STEP 2: DEFINE
|
| 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:
|
|
@@ -30,48 +31,72 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 30 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 31 |
"""
|
| 32 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
tz = pytz.timezone(timezone)
|
| 34 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 35 |
return f"The current local time in {timezone} is: {local_time}"
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"Error
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
# Use
|
| 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
|
| 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 |
-
#
|
|
|
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
tools=[
|
| 59 |
get_current_time_in_timezone,
|
| 60 |
-
|
| 61 |
-
image_generation_tool,
|
| 62 |
-
|
| 63 |
],
|
| 64 |
max_steps=6,
|
| 65 |
verbosity_level=1,
|
| 66 |
prompt_templates=prompt_templates
|
| 67 |
)
|
| 68 |
|
| 69 |
-
# --- STEP
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
# If you
|
| 76 |
-
|
| 77 |
-
agent.
|
|
|
|
| 4 |
import yaml
|
| 5 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 6 |
|
| 7 |
+
# --- STEP 1: SECURITY & CONFIGURATION ---
|
| 8 |
+
# Replace with your NEW token.
|
| 9 |
+
# Best practice: Set this in your system environment variables as HF_TOKEN
|
| 10 |
+
os.environ["HF_TOKEN"] = "PASTE_YOUR_NEW_TOKEN_HERE"
|
| 11 |
+
|
| 12 |
+
# Creating a proper prompts.yaml to guide the agent's logic
|
| 13 |
prompt_content = {
|
| 14 |
+
"system_prompt": (
|
| 15 |
+
"You are an expert AI assistant that can use python code to solve tasks. "
|
| 16 |
+
"You have access to tools. For every step, explain your thought process in 'Thought:', "
|
| 17 |
+
"then write the code in a 'Code:' block. Once you have the final answer, "
|
| 18 |
+
"provide it clearly."
|
| 19 |
+
)
|
| 20 |
}
|
| 21 |
+
|
| 22 |
with open("prompts.yaml", "w") as f:
|
| 23 |
yaml.dump(prompt_content, f)
|
| 24 |
|
| 25 |
+
# --- STEP 2: DEFINE TOOLS ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
@tool
|
| 28 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 31 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 32 |
"""
|
| 33 |
try:
|
| 34 |
+
# Normalize common inputs
|
| 35 |
+
if timezone.lower() == "new york":
|
| 36 |
+
timezone = "America/New_York"
|
| 37 |
+
elif timezone.lower() == "london":
|
| 38 |
+
timezone = "Europe/London"
|
| 39 |
+
|
| 40 |
tz = pytz.timezone(timezone)
|
| 41 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 42 |
return f"The current local time in {timezone} is: {local_time}"
|
| 43 |
except Exception as e:
|
| 44 |
+
return f"Error: '{timezone}' is not a recognized timezone. Please use format like 'Europe/Paris'."
|
| 45 |
|
| 46 |
+
@tool
|
| 47 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
| 48 |
+
"""A placeholder tool for future custom logic.
|
| 49 |
+
Args:
|
| 50 |
+
arg1: A descriptive string.
|
| 51 |
+
arg2: An integer multiplier or count.
|
| 52 |
+
"""
|
| 53 |
+
return f"Logic processed: {arg1} received with value {arg2}."
|
| 54 |
+
|
| 55 |
+
# --- STEP 3: INITIALIZE MODEL & TOOLS ---
|
| 56 |
|
| 57 |
+
# Use a highly available model. If Qwen 32B gives a 403, try 'meta-llama/Llama-3.3-70B-Instruct'
|
| 58 |
model = HfApiModel(
|
| 59 |
max_tokens=2096,
|
| 60 |
temperature=0.5,
|
| 61 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# Load the image generation tool
|
| 65 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 66 |
|
| 67 |
+
# Load the prompt template from the file we just created
|
| 68 |
with open("prompts.yaml", 'r') as stream:
|
| 69 |
prompt_templates = yaml.safe_load(stream)
|
| 70 |
|
| 71 |
+
# --- STEP 4: INITIALIZE AGENT ---
|
| 72 |
+
|
| 73 |
agent = CodeAgent(
|
| 74 |
model=model,
|
| 75 |
tools=[
|
| 76 |
get_current_time_in_timezone,
|
| 77 |
+
DuckDuckGoSearchTool(),
|
| 78 |
+
image_generation_tool,
|
| 79 |
+
my_custom_tool
|
| 80 |
],
|
| 81 |
max_steps=6,
|
| 82 |
verbosity_level=1,
|
| 83 |
prompt_templates=prompt_templates
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# --- STEP 5: EXECUTION ---
|
| 87 |
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
print("--- Starting Agent Test ---")
|
| 90 |
+
|
| 91 |
+
task = "What is the current time in New York? After you find the time, generate a beautiful image of a clock that reflects the style of that city."
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
result = agent.run(task)
|
| 95 |
+
print("\n--- FINAL ANSWER ---")
|
| 96 |
+
print(result)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"\nAn error occurred during execution: {e}")
|
| 99 |
|
| 100 |
+
# Optional: If you want to launch a UI (requires 'gradio' installed)
|
| 101 |
+
# from Gradio_UI import GradioUI
|
| 102 |
+
# GradioUI(agent).launch()
|