Spaces:
Sleeping
Sleeping
Update app.py
Browse filesAdded CodeAgent, initialized outside of class definition for BasicAgent.
app.py
CHANGED
|
@@ -51,6 +51,88 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 51 |
## My Stuff 2
|
| 52 |
###############
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
# from smolagents import DuckDuckGoSearchTool
|
| 55 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 56 |
|
|
@@ -61,6 +143,10 @@ search_tool = DuckDuckGoSearchTool()
|
|
| 61 |
results = search_tool("Who's the current President of France?")
|
| 62 |
# print(results)
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
class BasicAgent:
|
| 66 |
def __init__(self):
|
|
@@ -68,7 +154,8 @@ class BasicAgent:
|
|
| 68 |
print("BasicAgent initialized.")
|
| 69 |
def __call__(self, question: str) -> str:
|
| 70 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 71 |
-
unfixed_answer = search_tool(question)
|
|
|
|
| 72 |
fixed_answer = "This is a default answer."
|
| 73 |
print(f"Agent returning unfixed answer: {unfixed_answer}")
|
| 74 |
return unfixed_answer
|
|
|
|
| 51 |
## My Stuff 2
|
| 52 |
###############
|
| 53 |
|
| 54 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool, Tool
|
| 55 |
+
import datetime
|
| 56 |
+
import requests
|
| 57 |
+
import pytz
|
| 58 |
+
import yaml
|
| 59 |
+
from tools.final_answer import FinalAnswerTool
|
| 60 |
+
|
| 61 |
+
from Gradio_UI import GradioUI
|
| 62 |
+
|
| 63 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 64 |
+
@tool
|
| 65 |
+
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 66 |
+
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 67 |
+
"""A tool that does nothing yet
|
| 68 |
+
Args:
|
| 69 |
+
arg1: the first argument
|
| 70 |
+
arg2: the second argument
|
| 71 |
+
"""
|
| 72 |
+
return "What magic will you build ?"
|
| 73 |
+
|
| 74 |
+
@tool
|
| 75 |
+
def get_current_time_in_timezone(timezone: str) -> str:
|
| 76 |
+
"""A tool that fetches the current local time in a specified timezone.
|
| 77 |
+
Args:
|
| 78 |
+
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 79 |
+
"""
|
| 80 |
+
try:
|
| 81 |
+
# Create timezone object
|
| 82 |
+
tz = pytz.timezone(timezone)
|
| 83 |
+
# Get current time in that timezone
|
| 84 |
+
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 85 |
+
return f"The current local time in {timezone} is: {local_time}"
|
| 86 |
+
except Exception as e:
|
| 87 |
+
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# Initialize the final answer tool
|
| 91 |
+
final_answer = FinalAnswerTool()
|
| 92 |
+
|
| 93 |
+
# Initialize the web search tool
|
| 94 |
+
search_tool = DuckDuckGoSearchTool()
|
| 95 |
+
|
| 96 |
+
# Initialize the weather tool
|
| 97 |
+
weather_info_tool = WeatherInfoTool()
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 101 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 102 |
+
|
| 103 |
+
model = HfApiModel(
|
| 104 |
+
max_tokens=2096,
|
| 105 |
+
temperature=0.5,
|
| 106 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 107 |
+
custom_role_conversions=None,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# Import tool from Hub
|
| 112 |
+
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 113 |
+
|
| 114 |
+
with open("prompts.yaml", 'r') as stream:
|
| 115 |
+
prompt_templates = yaml.safe_load(stream)
|
| 116 |
+
|
| 117 |
+
agent = CodeAgent(
|
| 118 |
+
model=model,
|
| 119 |
+
tools=[final_answer, search_tool], ## add your tools here (don't remove final answer)
|
| 120 |
+
max_steps=6,
|
| 121 |
+
verbosity_level=1,
|
| 122 |
+
grammar=None,
|
| 123 |
+
planning_interval=None,
|
| 124 |
+
add_base_tools=True, # Add any additional base tools
|
| 125 |
+
#planning_interval=3, # Enable planning every 3 steps
|
| 126 |
+
name=None,
|
| 127 |
+
description=None,
|
| 128 |
+
prompt_templates=prompt_templates
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
#GradioUI(agent).launch()
|
| 133 |
+
|
| 134 |
+
#######
|
| 135 |
+
|
| 136 |
# from smolagents import DuckDuckGoSearchTool
|
| 137 |
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 138 |
|
|
|
|
| 143 |
results = search_tool("Who's the current President of France?")
|
| 144 |
# print(results)
|
| 145 |
|
| 146 |
+
agent.run(
|
| 147 |
+
"Why does Mike not know many people in New York?",
|
| 148 |
+
additional_args={"mp3_sound_file_url":'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/recording.mp3'}
|
| 149 |
+
)
|
| 150 |
|
| 151 |
class BasicAgent:
|
| 152 |
def __init__(self):
|
|
|
|
| 154 |
print("BasicAgent initialized.")
|
| 155 |
def __call__(self, question: str) -> str:
|
| 156 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 157 |
+
#unfixed_answer = search_tool(question)
|
| 158 |
+
unfixed_answer = agent.run(question)
|
| 159 |
fixed_answer = "This is a default answer."
|
| 160 |
print(f"Agent returning unfixed answer: {unfixed_answer}")
|
| 161 |
return unfixed_answer
|