from smolagents import CodeAgent, HfApiModel, load_tool, tool import requests from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI @tool def get_btc_price() -> str: """Fetches the current Bitcoin price in USD using the CoinGecko API.""" url = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd" response = requests.get(url) if response.status_code != 200: return '{"error": "Failed to fetch Bitcoin price"}' data = response.json() price = data.get('bitcoin', {}).get('usd') if price is None: return '{"error": "Bitcoin price not found"}' return f"The current Bitcoin price is ${price:.2f} USD." @tool def visit_webpage(url: str) -> str: """Visits a webpage at the given URL and returns its content as a markdown string. Args: url: The URL of the webpage to visit. Returns: The content of the webpage converted to Markdown, or an error message if the request fails. """ try: # Send a GET request to the URL response = requests.get(url) response.raise_for_status() # Raise an exception for bad status codes # Convert the HTML content to Markdown markdown_content = markdownify(response.text).strip() # Remove multiple line breaks markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content) return markdown_content except RequestException as e: return f"Error fetching the webpage: {str(e)}" except Exception as e: return f"An unexpected error occurred: {str(e)}" # Initialize the FinalAnswerTool final_answer = FinalAnswerTool() # Initialize the model model = HfApiModel( max_tokens=1000, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct', custom_role_conversions=None, ) # Load additional tools if needed # For example, to load an image generation tool: # image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) # Create the agent agent = CodeAgent( model=model, tools=[get_btc_price, visit_webpage, final_answer], # Add your tools here max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=None # Set to None if not using prompt templates ) # Launch the Gradio interface GradioUI(agent).launch()