File size: 3,964 Bytes
0dbf350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63b8e47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db9f047
bdba95b
db9f047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbf350
 
 
 
 
 
 
 
 
 
04d226c
b0c9882
0dbf350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b596b30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from smolagents import DuckDuckGoSearchTool
from smolagents import Tool
from huggingface_hub import InferenceClient

class Web_research(Tool):
    name="web_research"
    description = "Web search on a specific topic."
    inputs = {
        "topic": {
            "type": "string",
            "description": "The topic on which the user wants the latest news"
        }
    }
    output_type = "string"

    def forward(self, topic: str):
        search_tool = DuckDuckGoSearchTool()
        # Example usage
        results = search_tool(f"{topic}")
        return f"Here is what we can find on the web for {topic} : str({results})"

class Find_wikipedia_URL(Tool):
    name="wiki_url"
    description = "Always use to check a wikipedia ENGLISH URL page before trying to acces the URL. For another langage, you just have to change the beginning of the url (here, it is en for english)"
    inputs = {
        "subject": {
            "type": "string",
            "description": "The name or topic on which you want the Wikipedia URL"
        }
    }
    output_type = "string"

    def forward(self, subject: str):
        words=subject.split()
        url_wiki="https://en.wikipedia.org/wiki/"
        for i in range(len(words)):
            if(i==0):
                url_wiki+=str(words[i])
            if(i!=0):
                url_wiki+='_'+str(words[i])
        return f"Here is what we url to use : str({url_wiki}). If it does not work, change the first letters of {subject} to be upper or lower, but never change anything else"

class translate_everything(Tool):
    name="translator"
    description = "You do not understand a sentence? It does not look like any language you know? Try this tool, maybe the sentence is just reversed!"
    inputs = {
        "sentence": {
            "type": "string",
            "description": "The sentence to translate"
        }
    }
    output_type = "string"

    def forward(self, sentence: str):
        # Input string
        reversed_words = sentence.split() #' '.join(s.split()[::-1])
        right_sentence=[]
        for word in reversed_words:
            right_sentence.append(word[::-1])
        
        translated_sentence = " ".join(right_sentence[::-1])
        return f"The translated sentence is : {translated_sentence}"

class image_interpreter(Tool):
    name="multimodal_tool"
    description = "Allows you to answer any question which relies on image input."
    inputs = {
        'image': {"type": "image", "description": "the image of interest"},
        'prompt': {"type": "string", "description": "Any specific question you have on the image. For example, the prompt can be : Summarise this image in one sentence."}
    }
    output_type = "string"
    
    def forward(self, prompt, image):
        model_sdxl = "meta-llama/Llama-3.1-8B-Instruct"
        client = InferenceClient(model_sdxl)
        output = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "image": {image},
                    },
                    {
                        "type": "text",
                        "text": {prompt},
                    },
                ],
            },
        ],
        )
        return output

class Wikipedia_reader(Tool):
    name="wikipedia_tool"
    description = "To be used whenever you need to read a Wikipedia page. Will return all the text of the Wikipedia page, to easily read it and find information"
    inputs = {
        "url": {
            "type": "string",
            "description": "The wikippedia url page"
        }
    }
    output_type = "string"

    def forward(self, url: str):
        try:
            page = requests.get(url)
        except Exception as e:
            print('Error downloading page: ',e)
        soup = BeautifulSoup(page.text, 'html.parser')
        return soup.text