Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,25 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
|
|
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
|
| 7 |
from Gradio_UI import GradioUI
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
# Tool to create a personalized greeting repeated a specific number of times.
|
| 10 |
@tool
|
| 11 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
@@ -19,58 +33,62 @@ def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
| 19 |
greeting = f"Hello, {arg1}!"
|
| 20 |
return " ".join([greeting] * arg2)
|
| 21 |
|
|
|
|
| 22 |
# Tool to get the current local time in your chosen timezone.
|
| 23 |
@tool
|
| 24 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 25 |
-
"""
|
| 26 |
-
A tool that fetches the current local time in a specified timezone.
|
| 27 |
-
|
| 28 |
Args:
|
| 29 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 30 |
"""
|
| 31 |
try:
|
|
|
|
| 32 |
tz = pytz.timezone(timezone)
|
|
|
|
| 33 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 34 |
return f"The current local time in {timezone} is: {local_time}"
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
|
|
|
|
| 38 |
final_answer = FinalAnswerTool()
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
# Import tool from Hub
|
| 41 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 42 |
|
| 43 |
with open("prompts.yaml", 'r') as stream:
|
| 44 |
prompt_templates = yaml.safe_load(stream)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
# ----- Initialize agent function -----
|
| 47 |
-
def initialize_agent(api_key: str):
|
| 48 |
-
"""
|
| 49 |
-
Initialize the CodeAgent with the user-provided OpenAI API key.
|
| 50 |
-
"""
|
| 51 |
-
model = OpenAIServerModel(
|
| 52 |
-
max_tokens=2096,
|
| 53 |
-
temperature=0.5,
|
| 54 |
-
model_id="gpt-4o",
|
| 55 |
-
custom_role_conversions=None,
|
| 56 |
-
openai_api_key=api_key
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
agent = CodeAgent(
|
| 60 |
-
model=model,
|
| 61 |
-
tools=[my_custom_tool, get_current_time_in_timezone, final_answer],
|
| 62 |
-
max_steps=6,
|
| 63 |
-
verbosity_level=1,
|
| 64 |
-
grammar=None,
|
| 65 |
-
planning_interval=None,
|
| 66 |
-
name=None,
|
| 67 |
-
description=None,
|
| 68 |
-
prompt_templates=prompt_templates
|
| 69 |
-
)
|
| 70 |
-
return agent
|
| 71 |
-
|
| 72 |
-
# Launch Gradio interface with API key input
|
| 73 |
-
def run_agent_with_api_key(api_key: str):
|
| 74 |
-
agent = initialize_agent(api_key)
|
| 75 |
-
GradioUI(agent).launch()
|
| 76 |
|
|
|
|
|
|
| 1 |
+
#from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
+
from smolagents import CodeAgent,DuckDuckGoSearchTool, OpenAIServerModel,load_tool,tool
|
| 3 |
import datetime
|
| 4 |
+
import requests
|
| 5 |
import pytz
|
| 6 |
import yaml
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
|
| 11 |
+
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
+
#@tool
|
| 13 |
+
#def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
| 14 |
+
#Keep this format for the description / args / args description but feel free to modify the tool
|
| 15 |
+
# """A tool that does nothing yet
|
| 16 |
+
# Args:
|
| 17 |
+
# arg1: the first argument
|
| 18 |
+
# arg2: the second argument
|
| 19 |
+
# """
|
| 20 |
+
# return "What magic will you build ?"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
# Tool to create a personalized greeting repeated a specific number of times.
|
| 24 |
@tool
|
| 25 |
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
|
| 33 |
greeting = f"Hello, {arg1}!"
|
| 34 |
return " ".join([greeting] * arg2)
|
| 35 |
|
| 36 |
+
|
| 37 |
# Tool to get the current local time in your chosen timezone.
|
| 38 |
@tool
|
| 39 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 40 |
+
"""A tool that fetches the current local time in a specified timezone.
|
|
|
|
|
|
|
| 41 |
Args:
|
| 42 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 43 |
"""
|
| 44 |
try:
|
| 45 |
+
# Create timezone object
|
| 46 |
tz = pytz.timezone(timezone)
|
| 47 |
+
# Get current time in that timezone
|
| 48 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 49 |
return f"The current local time in {timezone} is: {local_time}"
|
| 50 |
except Exception as e:
|
| 51 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 52 |
|
| 53 |
+
|
| 54 |
final_answer = FinalAnswerTool()
|
| 55 |
|
| 56 |
+
# 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:
|
| 57 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 58 |
+
|
| 59 |
+
#model = HfApiModel(
|
| 60 |
+
#max_tokens=2096,
|
| 61 |
+
#temperature=0.5,
|
| 62 |
+
#model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 63 |
+
#custom_role_conversions=None,
|
| 64 |
+
#)
|
| 65 |
+
model = OpenAIServerModel(
|
| 66 |
+
max_tokens=2096,
|
| 67 |
+
temperature=0.5,
|
| 68 |
+
model_id="gpt-4o",
|
| 69 |
+
custom_role_conversions=None,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
# Import tool from Hub
|
| 75 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 76 |
|
| 77 |
with open("prompts.yaml", 'r') as stream:
|
| 78 |
prompt_templates = yaml.safe_load(stream)
|
| 79 |
+
|
| 80 |
+
agent = CodeAgent(
|
| 81 |
+
model=model,
|
| 82 |
+
#tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 83 |
+
tools=[my_custom_tool, get_current_time_in_timezone, final_answer], ## add your tools here (don't remove final answer)
|
| 84 |
+
max_steps=6,
|
| 85 |
+
verbosity_level=1,
|
| 86 |
+
grammar=None,
|
| 87 |
+
planning_interval=None,
|
| 88 |
+
name=None,
|
| 89 |
+
description=None,
|
| 90 |
+
prompt_templates=prompt_templates
|
| 91 |
+
)
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
GradioUI(agent).launch()
|