Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637388 unmodified lines474849505152535414 unmodified lines69707172737475767778798081828384858687888990919293from 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 my_custom_tool(arg1:str, arg2:int)-> str: # it's important to specify the return type# Keep this format for the tool description / args description but feel free to modify the tool"""A tool that does nothing yetArgs:arg1: the first argumentarg2: the second argument"""return "What magic will you build ?"@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)}"# Load AI Regulation Model (No HF Token required)ai_regulation_model = HfApiModel(8 unmodified linesSearches for AI Act insights using an AI model and DuckDuckGo.Args:query : The search query related to the AI Act.max_results : The maximum number of search results to return."""# Step 1: Get AI Model Insightsai_response = ai_regulation_model.generate(prompt=f"Provide insights on: {query} related to the AI Act.")14 unmodified lines"articles": articles}# Import tool from Hubimage_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)# Load system prompt from prompt.yaml filewith open("prompts.yaml", 'r') as stream:prompt_templates = yaml.safe_load(stream)agent = CodeAgent(model=model,tools=[final_answer, ai_act_search_tool], # 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 # Pass system prompt to CodeAgent)GradioUI(agent).launch()123456789108 unmodified lines1920212223242526272829303114 unmodified lines464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899from smolagents import JsonAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, toolimport datetimeimport pytzimport yamlfrom tools.final_answer import FinalAnswerToolfrom Gradio_UI import GradioUIfrom duckduckgo_search import DDGS# Load AI Regulation Model (No HF Token required)ai_regulation_model = HfApiModel(8 unmodified linesSearches for AI Act insights using an AI model and DuckDuckGo.Args:query (str): The search query related to the AI Act.max_results (int): The maximum number of search results to return.Returns:dict: A dictionary containing:- "ai_insight" (str): AI-generated insights on the AI Act query.- "articles" (list): A list of dictionaries containing article titles and URLs."""# Step 1: Get AI Model Insightsai_response = ai_regulation_model.generate(prompt=f"Provide insights on: {query} related to the AI Act.")14 unmodified lines"articles": articles}@tooldef get_current_time_in_timezone(timezone: str) -> str:"""Fetches the current local time in a specified timezone.Args:timezone (str): A string representing a valid timezone (e.g., 'America/New_York').Returns:str: The current time in the given timezone or an error message if invalid."""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)}"# Final Answer Toolfinal_answer = FinalAnswerTool()# Import tool from Hugging Face Hub safelytry:image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)except Exception as e:print(f"Error loading image generation tool: {e}")image_generation_tool = None # Prevent crash if tool fails# Load system prompt from YAML filetry:with open("prompts.yaml", "r") as stream:prompt_templates = yaml.safe_load(stream)except FileNotFoundError:print("Warning: prompts.yaml file not found. Using default templates.")prompt_templates = {}# Create JsonAgent (JSON-compatible response)agent = JsonAgent(model=ai_regulation_model,tools=[final_answer, ai_act_search_tool], # Ensure JSON tools are correctly addedmax_steps=6,verbosity_level=1,grammar=None,planning_interval=None,name=None,description=None,prompt_templates=prompt_templates # Pass system prompt to JsonAgent)# Start Gradio UIGradioUI(agent).launch()
app.py+50-44