File size: 2,409 Bytes
4758e0a
ec0e12a
668a0a5
 
3be955d
b1531aa
 
 
 
4758e0a
 
 
 
 
 
 
 
 
0955ab2
 
 
4758e0a
0955ab2
 
 
 
 
 
 
 
 
b1531aa
0955ab2
 
 
 
 
 
 
 
4758e0a
b1531aa
0955ab2
 
 
 
b1531aa
0955ab2
b1531aa
0955ab2
 
50c50da
 
 
0955ab2
668a0a5
0955ab2
668a0a5
 
 
 
 
0955ab2
 
 
 
 
668a0a5
f755820
0955ab2
668a0a5
f755820
0955ab2
 
 
 
 
8fe992b
 
0c98f4d
0955ab2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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()