File size: 1,343 Bytes
b4c224e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import wikipedia
from smolagents import tool
import pandas as pd
import requests
from bs4 import BeautifulSoup

@tool
def visitWikipedia(page_name: str) -> str:
    """
    Visit a Wikipedia page and return its content.
    Args:
        page_name (str): The name of the Wikipedia page to visit.
    Returns:
        str: The content of the Wikipedia page.
    """
    try:
        page = wikipedia.page(page_name)
        return page.content
    except wikipedia.exceptions.DisambiguationError as e:
        return f"Disambiguation error: {e}"
    except wikipedia.exceptions.PageError as e:
        return f"Page error: {e}"

@tool
def visitWikipediaTable(url: str) -> list:
    """
    Visit a Wikipedia page and return all tables in the page as a list of pandas DataFrames.
    Args:
        url (str): The URL of the Wikipedia page to visit.
    Returns:
        list: A list of pandas DataFrames, each representing a table on the page.
    """
    try:
        response = requests.get(url)
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        return f"Request failed: {e}"
    soup = BeautifulSoup(response.content, 'html.parser')
    tables = []
    for table in soup.find_all('table', class_='wikitable'):
        df = pd.read_html(str(table))[0]
        tables.append(df)
    return tables