File size: 3,476 Bytes
e310d6a
 
 
 
 
 
 
42192c3
e310d6a
 
 
 
42192c3
e310d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42192c3
e310d6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42192c3
e310d6a
 
 
 
 
 
 
42192c3
e310d6a
 
42192c3
e310d6a
 
42192c3
e310d6a
 
 
 
 
 
 
 
 
 
 
42192c3
 
e310d6a
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
84
85
86
from smolagents import CodeAgent, HfApiModel, load_tool, tool
from tools.final_answer import FinalAnswerTool
from tools.youtube_search import YoutubeSearchTool
from tools.play_music import PlaySomeMusicMeastro
from tools.download_music import DownloadSomeMusicMeastro
from typing import List
from Gradio_UI import GradioUI

import os
import base64
import yaml
import wikipedia

@tool
def search_wikipedia_pages(search_string: str) ->  List[str]:
    """This tool helps to search for the names of potential names of wikipedia pages that relate to a search string.
    This doesn't guarantee that every returned string is a perfect, exact page title that wikipedia.page() will accept without any issue.
    This tool sends a query to the Wikipedia API's search endpoint. This endpoint performs a full-text search across Wikipedia's article titles and content.
    This will not retrieve the actual pages, but gives oversight of the different keywords on wikipedia to find more information.
    This tool will output the first 10 strings related to the search string as a list of strings.
    Args:
        search_string: the search string
    """
    try:
        page_titles = wikipedia.search(search_string, results=10)
        return page_titles
    except wikipedia.exceptions.PageError as err:
        s_error = f"PageError: {err}"
    except wikipedia.exceptions.DisambiguationError as err:
        s_error = f"DisambiguationError {err}"
    print (f"An error occured {s_error}")

@tool
def get_wikipedia_page_content(page_name: str, p_summary: bool = True) ->  str:
    """This tool helps to get the content of wikipedia pages by their name.
    If you use this tool based on the output of search_wikipedia_pages 
     than remember to try a next string that was the output of search_wikipedia_pages if you get a error: Page id
    The output is a string with the actual content of the wikipedia page.
    Args:
        page_name: the name of the page in wikipedia
        p_summary: do you want the full content or only the summary of the content
    """
    s_error = ""
    try:
        a_page = wikipedia.page(title=page_name)
        if p_summary: 
            return a_page.summary
        else:
            return a_page.content
    except wikipedia.exceptions.PageError as err:
        s_error = f"PageError: {err}"
    except wikipedia.exceptions.DisambiguationError as err:
        s_error = f"DisambiguationError {err}"
    print (f"An error occured {s_error}")
    
final_answer = FinalAnswerTool()

model = HfApiModel(
            max_tokens=2096,
            temperature=0.5,
            #model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
            model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud', 
            custom_role_conversions=None,
        )

# 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, search_wikipedia_pages, get_wikipedia_page_content, PlaySomeMusicMeastro(), YoutubeSearchTool(),  DownloadSomeMusicMeastro(), image_generation_tool], 
    max_steps=12,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates,
    add_base_tools=True
)

GradioUI(agent).launch()