File size: 3,053 Bytes
4a1fc47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from llama_index.core.tools import FunctionTool
import whisper
import pandas as pd
from PIL import Image

def read_python_file(file_path: str) -> bytes:
    """
    Reads the content of a .py file given its file path.

    Args:
        file_path (str): Path to the file.

    Returns:
        bytes: Content of the file as string.
    """
    try:
        with open(file_path, 'r') as file:
            return file.read()
    except Exception as e:
        raise ValueError(f"Error reading file at {file_path}: {e}")

read_python_file_tool = FunctionTool.from_defaults(
    fn=read_python_file,
    name="read_file_content",
    description="Reads the content of a .py file given its file path."
)


def audio_to_text(file_path: str) -> str:
    """
    Converts an audio file to text using OpenAI Whisper.

    Args:
        file_path (str): Path to the audio file.

    Returns:
        str: Transcribed text from the audio file.
    """
    try:
        # Load the Whisper model
        model = whisper.load_model("base")

        # Transcribe the audio file
        result = model.transcribe(file_path)

        # Return the transcribed text
        return result['text']
    except Exception as e:
        raise ValueError(f"Error processing audio file at {file_path}: {e}")

audio_to_text_tool = FunctionTool.from_defaults(
    fn=audio_to_text,
    name="audio_to_text",
    description="Converts an audio file to text using OpenAI Whisper."
)


def read_xlsx_file(file_path: str) -> str:
    """
    Reads the content of an .xlsx file and returns it as a string.

    Args:
        file_path (str): Path to the .xlsx file.

    Returns:
        str: Content of the .xlsx file as a string.
    """
    try:
        # Read the Excel file into a DataFrame
        df = pd.read_excel(file_path)

        # Convert the DataFrame to a string
        return df.to_string(index=False)
    except Exception as e:
        raise ValueError(f"Error reading .xlsx file at {file_path}: {e}")

read_xlsx_file_tool = FunctionTool.from_defaults(
    fn=read_xlsx_file,
    name="read_xlsx_file",
    description="Reads the content of an .xlsx file and returns it as a string."
)


def read_png_file(file_path: str) -> list:
    """
    Reads the content of a .png file and returns its RGB pixel representation.

    Args:
        file_path (str): Path to the .png file.

    Returns:
        list: A 2D list representing the RGB pixel values of the image.
    """
    try:
        # Open the image file
        image = Image.open(file_path).convert("RGB")

        # Convert the image to a 2D list of RGB tuples
        rgb_pixels = list(image.getdata())
        width, height = image.size
        return [rgb_pixels[i * width:(i + 1) * width] for i in range(height)]
    except Exception as e:
        raise ValueError(f"Error reading .png file at {file_path}: {e}")

read_png_file_tool = FunctionTool.from_defaults(
    fn=read_png_file,
    name="read_png_file",
    description="Reads the content of a .png file and returns its RGB pixel representation."
)