CindyDelage's picture
Update tools.py
63b8e47 verified
raw
history blame
3.96 kB
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