Spaces:
Runtime error
Runtime error
| from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, load_tool, tool | |
| import datetime | |
| import requests | |
| import pytz | |
| import yaml | |
| import os | |
| from tools.final_answer import FinalAnswerTool | |
| from tools import nlu_tool, scheduler, requests_store | |
| from smolagents import tool as tool_decorator | |
| from Gradio_UI import GradioUI | |
| # Below is an example of a tool that does nothing. Amaze us with your creativity ! | |
| def my_custom_tool(arg1:str, arg2:int)-> 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 does nothing yet | |
| Args: | |
| arg1: the first argument | |
| arg2: the second argument | |
| """ | |
| return "What magic will you build ?" | |
| def nlu(text: str) -> dict: | |
| """Run NLU (intent + slots) on user text. | |
| Args: | |
| text: The user's message to analyze. | |
| """ | |
| return nlu_tool.extract_intent_and_slots(text) | |
| def propose_slots(preferred_windows: dict = None) -> list: | |
| """Return up to 3 candidate operator slots based on preferred windows. | |
| Args: | |
| preferred_windows: Optional list of dicts with 'start' and 'end' strings | |
| (natural language or ISO). Example: [{'start': 'tomorrow 09:00', 'end': 'tomorrow 12:00'}]. | |
| """ | |
| cust_windows = [] | |
| if preferred_windows: | |
| for w in preferred_windows: | |
| try: | |
| # use dateparser to flexibly parse the start/end and normalize to Asia/Tokyo | |
| import dateparser | |
| settings = {'TIMEZONE': 'Asia/Tokyo', 'RETURN_AS_TIMEZONE_AWARE': True} | |
| s = dateparser.parse(w['start'], settings=settings) | |
| e = dateparser.parse(w['end'], settings=settings) | |
| if s and e: | |
| cust_windows.append({'start': s, 'end': e}) | |
| except Exception: | |
| continue | |
| slots = scheduler.find_common_slots(cust_windows) | |
| return slots | |
| def create_request(payload: dict) -> dict: | |
| """Persist a handoff request and return stored record. | |
| Args: | |
| payload: dict containing the handoff data (customer, account, amount, date_by_when, etc.) | |
| """ | |
| return requests_store.create_request(payload) | |
| def notify_operator(message: str) -> str: | |
| """Stub: notify operator (logs the message into data/operator_notifications.log) | |
| Args: | |
| message: The notification content to send to operator. | |
| """ | |
| os.makedirs('data', exist_ok=True) | |
| path = 'data/operator_notifications.log' | |
| with open(path, 'a') as f: | |
| f.write(message + "\n---\n") | |
| return "ok" | |
| def 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 object | |
| tz = pytz.timezone(timezone) | |
| # Get current time in that timezone | |
| local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S") | |
| return local_time | |
| except Exception as e: | |
| return f"Error fetching time for timezone '{timezone}': {str(e)}" | |
| final_answer = FinalAnswerTool() | |
| # Get OpenAI API key from environment variable | |
| openai_api_key = os.getenv('OPENAI_API_KEY') | |
| if not openai_api_key: | |
| raise ValueError("Please set the OPENAI_API_KEY environment variable") | |
| import urllib3 | |
| # Disable SSL verification warnings | |
| urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
| model = OpenAIServerModel( | |
| api_key=openai_api_key, | |
| model_id="gpt-5-mini-2025-08-07", # Using GPT-4 Turbo, you can change to gpt-3.5-turbo for lower cost | |
| max_tokens=2096, | |
| # verify_ssl=False, # Disable SSL verification | |
| temperature=0.5, | |
| ) | |
| # Import tool from Hub | |
| image_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, get_current_time_in_timezone, nlu, propose_slots, create_request, notify_operator], ## 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 | |
| ) | |
| if __name__ == '__main__': | |
| GradioUI(agent).launch() |