Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,toolimport datetimeimport requestsimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# Below is an example of a tool that does nothing. Amaze us with your creativity !@tooldef time_zone_region(time_zone:str)-> str: #it's import to specify the return type#Keep this format for the description / args / args description but feel free to modify the tool"""A tool that takes user input and returns the time in a specified time-zoneArgs:time zone (str): Integers separated by colons reperesenting the hours:minutes:secondsReturns "The time-zone of desired region""""try:search_tool = DuckDuckGoSearchTool()query = f"What is the time zone? {time_zone}"search_results = search_tool(query)if search_results:return search_resultselse:return f"I couldn't find the category for the {time_zone}."except Exception as e:return f"Error fetching category for the term {time_zone}: {str(e)}"@tooldef get_current_time_in_timezone(timezone: str) -> str:"""A tool that fetches the current local time in a specified timezone.Args:timezone: A string representing a valid timezone (e.g., 'America/New_York')."""try:# Create timezone objecttz = pytz.timezone(timezone)# Get current time in that timezonelocal_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")return f"The current local time in {timezone} is: {local_time}"except Exception as e:return f"Error fetching time for timezone '{timezone}': {str(e)}"final_answer = FinalAnswerTool()# 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:# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'model = HfApiModel(max_tokens=2096,temperature=0.5,model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloadedcustom_role_conversions=None,)# Import tool from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)with open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)agent = CodeAgent(model=model,tools=[final_answer], ## add your tools here (don't remove final answer)max_steps=6,verbosity_level=1,grammar=None,planning_interval=None,name=None,description=None,prompt_templates=prompt_templates)GradioUI(agent).launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, toolimport datetimeimport requestsimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUI# Tool to search for time zone information@tooldef time_zone_region(time_zone: str) -> str:"""A tool that takes user input and returns information about the time zone of a specified region.Args:time_zone (str): Time zone identifier (e.g., 'America/New_York').Returns:str: Information about the time zone or an error message."""try:search_tool = DuckDuckGoSearchTool()query = f"What is the time zone of {time_zone}?"search_results = search_tool(query)# Check if the search tool returns resultsif search_results and isinstance(search_results, list) and len(search_results) > 0:# If there are results, we return the top resultreturn search_results[0]else:# If no results, we return a default messagereturn f"Sorry, I couldn't find detailed information for the time zone: {time_zone}."except Exception as e:return f"Error while fetching information for time zone '{time_zone}': {str(e)}"# Tool to fetch the current time in a specified time zone@tooldef get_current_time_in_timezone(timezone: str) -> str:"""A tool that fetches the current local time in a specified timezone.Args:timezone (str): A valid timezone string (e.g., 'America/New_York').Returns:str: Current time in the specified timezone or an error message if invalid."""try:# Check if the timezone is validif timezone not in pytz.all_timezones:return f"Invalid timezone: '{timezone}'. Please provide a valid timezone."# Get current time in the given timezonetz = pytz.timezone(timezone)local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")return f"The current local time in {timezone} is: {local_time}"except Exception as e:return f"Error fetching time for timezone '{timezone}': {str(e)}"# Initialize final answer toolfinal_answer = FinalAnswerTool()# Model setupmodel = HfApiModel(max_tokens=2096,temperature=0.5,model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # Model may be overloadedcustom_role_conversions=None,)# Import tool for image generation from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)# Load prompt templates from YAML filewith open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)# Create the agent with toolsagent = CodeAgent(model=model,tools=[final_answer], # Ensure all relevant tools are added heremax_steps=6,verbosity_level=1,prompt_templates=prompt_templates,)# Launch Gradio UI with additional customization optionsGradioUI(agent).launch()
app.py+43-35