from smolagents import Tool import wikipedia from bs4 import BeautifulSoup import io import pandas as pd import requests from tabulate import tabulate import os import tempfile from pathlib import Path from PIL import Image from io import BytesIO from dotenv import find_dotenv, load_dotenv from openai import OpenAI from llama_index.readers.youtube_transcript import YoutubeTranscriptReader from google import genai from google.genai import types import chess class WikipediaSearch(Tool): name = "wikipedia_search" description = "Fetches wikipedia pages." inputs = { "query": { "type": "string", "description": "Query to be searched on wikipedia" } } output_type = "string" def forward(self, query:str)->str: res = wikipedia.page(query) bs = BeautifulSoup(res.html(), 'html.parser') text_only = bs.get_text() return text_only class ExcelReader(Tool): name = 'excel_processor' description = "excel reading tool, processed files of .xlsx and .xls format." inputs = { "file_path": { "type": "string", "description": "path to the excel file" } } output_type = "string" def forward(self, file_path:str)->str: df = pd.read_excel(file_path) txt_excel = tabulate(df, headers="keys", tablefmt="github", showindex=False) return txt_excel class FileReader(Tool): name = 'file_reader' description = "reads saved files" inputs = { "file_path": { "type": "string", "description": "path to the file" } } output_type = "string" def forward(self, file_path:str)->str: with open(file_path, "r") as file: content = file.read() return content def download_files(task_id, file_name): url = f'https://agents-course-unit4-scoring.hf.space/files/{task_id}' response = requests.get(url, timeout=15) tmp_dir = Path(tempfile.gettempdir()) / "project_files" tmp_dir.mkdir(exist_ok=True) filepath = os.path.join(tmp_dir, file_name) with open(filepath, "wb") as f: f.write(response.content) return filepath def get_images(file_format, file_path): if file_format in ['png', 'jpeg', 'jpg']: images = [Image.open(file_path).convert("RGB")] else: images = [] return images class AudioTransciber(Tool): name = 'audio_transcriber' description = "transcribes audio files" inputs = { "file_path": { "type": "string", "description": "path to the file" } } output_type = "string" def forward(self, file_path:str)->str: audio = open(file_path, 'rb') client = OpenAI(api_key=os.getenv("OPEN_AI_KEY")) transcript = client.audio.transcriptions.create(model='whisper-1', file=audio) return transcript class YouTubeTranscipt(Tool): name = 'youtube_transcript' description = "a tool that returns a transcript for a youtube video. Youtube videos come from urls containing www.youtube.com" inputs = { "url": { "type": "string", "description": "url to the youtube video, has 'www.youtube.com' in it." } } output_type = "string" def forward(self, url:str)->str: loader = YoutubeTranscriptReader() documents = loader.load_data(ytlinks=[url]) transcript = documents[0].text return transcript class YouTubeVideoUnderstanding(Tool): name = 'youtube_video_understanding' description = "a tool that processes summarizes what is happenening in a youtube video. Youtube videos come from urls containing www.youtube.com" inputs = { "url": { "type": "string", "description": "url to the youtube video, has 'www.youtube.com' in it." }, "prompt": { "type": "string", "description": "user prompt about the video content" } } output_type = "string" def forward(self, url:str, prompt:str)->str: load_dotenv(find_dotenv()) client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) response = client.models.generate_content( model='models/gemini-2.0-flash', contents=types.Content( parts=[ types.Part( file_data=types.FileData(file_uri=url) ), types.Part(text=prompt) ] ) ) return response.text class VegetableFruitClassification(Tool): name = 'vegetable_fruit_classificaiton' description = "a tool that can help classify fruits and vegetables" inputs = { "prompt": { "type": "string", "description": "user prompt about fruits or vegetables" } } output_type = "string" def forward(self, prompt:str)->str: load_dotenv(find_dotenv()) client = genai.Client(api_key=os.getenv("GEMINI_API_KEY")) additional_context = """ The botanical distinction between fruits and vegetables is anatomical of the plant in question. For example, a tomato has seeds, which would result in reproduction. Rhubarb is the stalk of a plant, and has no means of proliferation after consumption. A tomato is a botanical fruit and rhubarb is botanically a vegetable. """ extended_prompt = prompt + additional_context response = client.models.generate_content( model='models/gemini-2.5-flash-preview-05-20', contents=types.Content( parts=[ types.Part(text=extended_prompt) ] ) ) return response.text class ChessSolver(Tool): name = "chess_analysis_tool" description = "analyzes the chess board to determine the best next move." inputs = { "image_path": { "type": "string", "description": "path to the image showing a chess board." }, "current_player":{ "type": "string", "description": "player whose turn it is. Acceptable inputs are 'black' or 'white'" }, } output_type = "string" def forward(self, image_path:str, current_player:str)->str: fen = chess.fen_notation(image_path, current_player) best_move = chess.chess_analysis(fen) return best_move