Spaces:
Sleeping
Sleeping
| from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel, load_tool, tool # FIX 1: HfApiModel → InferenceClientModel | |
| import os | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| from tools.final_answer import FinalAnswerTool | |
| from Gradio_UI import GradioUI | |
| def get_current_time_in_timezone(timezone: str) -> str: | |
| """Fetches the current local time in a specified timezone. | |
| Args: | |
| timezone: A valid timezone string, e.g. 'Asia/Kolkata' or 'America/New_York'. | |
| """ | |
| try: | |
| tz = 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)}" | |
| def get_fun_fact(topic: str) -> str: | |
| """Returns a fun fact about a given topic using a public API. | |
| Args: | |
| topic: The subject to get a fun fact about, e.g. 'math', 'science'. | |
| """ | |
| try: | |
| response = requests.get( | |
| "https://uselessfacts.jsph.pl/api/v2/facts/random?language=en", | |
| timeout=5 | |
| ) | |
| data = response.json() | |
| return f"Fun fact about {topic}: {data.get('text', 'No fact found.')}" | |
| except Exception as e: | |
| return f"Error fetching fun fact: {str(e)}" | |
| final_answer = FinalAnswerTool() # FIX 2: was "ffinal_answer" (typo) | |
| search_tool = DuckDuckGoSearchTool() | |
| image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) | |
| model = InferenceClientModel( # FIX 3: works now because FIX 1 imported it | |
| max_tokens=2096, | |
| temperature=0.5, | |
| model_id='Qwen/Qwen2.5-Coder-32B-Instruct', | |
| token=os.environ.get("HF_TOKEN"), | |
| custom_role_conversions=None, | |
| ) | |
| agent = CodeAgent( | |
| model=model, | |
| tools=[ | |
| final_answer, # FIX 2 cascades here — name now matches | |
| search_tool, | |
| image_generation_tool, | |
| get_current_time_in_timezone, | |
| get_fun_fact, | |
| ], | |
| max_steps=6, | |
| verbosity_level=1, | |
| planning_interval=None, | |
| name=None, | |
| description=None, | |
| ) | |
| GradioUI(agent).launch() | |