File size: 6,589 Bytes
b7e35bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8bf136
b7e35bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8bf136
b7e35bf
 
 
 
 
 
 
 
 
a8bf136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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