File size: 3,509 Bytes
ca6fbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from smolagents import Tool
import os
import json
import csv
import openpyxl
import whisper
import requests


class OpenFilesTool(Tool):
    name = "open_files_tool"
    description = (
        "This tool opens files and returns their content as a string. "
        "It can handle text, CSV, JSON, XLSX, and MP3 file types."
    )
    inputs = {
        "file_path": {
            "type": "string",
            "description": "The path to the file to be opened.",
        },
        "file_type": {
            "type": "string",
            "description": "The type of the file (text, csv, json, xlsx, mp3). Default is 'text'.",
            "nullable": True,
        },
    }
    output_type = "string"


    def download_file(self, file_name: str) -> None:
        if not os.path.exists(file_name):
            url = f"https://agents-course-unit4-scoring.hf.space/files/{file_name.split('.')[0]}"
            r = requests.get(url)
            with open(file_name, "wb") as f:
                f.write(r.content)


    def open_file_as_text(self, file_name: str, filetype: str = "txt") -> str:
        """
        Opens a file and returns its content as readable text.
        Supports 'txt', 'json', 'csv', 'xlsx', and 'mp3' (transcribes speech to text).
        Args:
            file_name (str): The path or name of the file.
            filetype (Optional[str]): Type of file ('txt', 'json', 'csv', 'xlsx', 'mp3'). Defaults to 'txt'.
        Returns:
            str: The content of the file as text, or transcribed speech if 'mp3'.
        """
        self.download_file(file_name)
        try:
            if filetype == "txt":
                with open(file_name, "r", encoding="utf-8") as f:
                    return f.read()

            elif filetype == "json":
                with open(file_name, "r", encoding="utf-8") as f:
                    data = json.load(f)
                return json.dumps(data, indent=2)

            elif filetype == "csv":
                with open(file_name, "r", encoding="utf-8") as f:
                    reader = csv.reader(f)
                    rows = list(reader)
                return "\n".join([", ".join(row) for row in rows])

            elif filetype == "xlsx":
                wb = openpyxl.load_workbook(file_name, data_only=True)
                sheet = wb.active
                content = []
                for row in sheet.iter_rows(values_only=True):
                    content.append(", ".join(str(cell) if cell is not None else "" for cell in row))
                return "\n".join(content)

            elif filetype == "mp3":
                w = whisper.load_model("base")
                res = w.transcribe(file_name)
                return res["text"]

            else:
                return f"Unsupported filetype '{filetype}'. Supported types are 'txt', 'json', 'csv', 'xlsx', and 'mp3'."

        except FileNotFoundError:
            return f"File '{file_name}' not found."
        except Exception as e:
            return f"Error opening file '{file_name}': {str(e)}"

    def forward(self, file_path: str, file_type: str = "text") -> str:
        """
        Opens a file and returns its content as a string.
        Args:
            file_path (str): The path to the file to be opened.
            file_type (str): The type of the file (text, csv, json, xlsx, mp3). Default is 'text'.
        Returns:
            str: The content of the file as a string.
        """
        return self.open_file_as_text(file_path, file_type)