File size: 1,993 Bytes
afb4047
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

from langchain.tools import tool

@tool
def read_python(file_path):
    """
    Reads a Python file and returns its content as a string.
    
    Args:
        file_path (str): The path to the Python file.
        
    Returns:
        str: The content of the Python file.
    """
    try:
        if not os.path.exists(file_path):
            return f"Error: File not found at {file_path}"
        with open(file_path, "r", encoding="utf-8") as file:
            content = file.read()
        return content
    except Exception as e:
        return f"Error reading Python file: {str(e)}"

@tool
def read_excel(file_path):
    """
    Reads an Excel file and returns its content as a string.
    
    Args:
        file_path (str): The path to the Excel file.
        
    Returns:
        str: The content of the Excel file.
    """
    if not file_path.endswith(('.xls', '.xlsx')):
        return "Error: File is not an Excel file."
    try:
        if not os.path.exists(file_path):
            return f"Error: File not found at {file_path}"
        import pandas as pd
        df = pd.read_excel(file_path)
        return df.to_string()
    except Exception as e:
        return f"Error reading Excel file: {str(e)}"
    

@tool
def transcribe_audio(file_path):
    """
    Transcribes an audio file and returns its content as a string.
    
    Args:
        file_path (str): The path to the audio file.
        
    Returns:
        str: The transcribed text from the audio file.
    """
    if not file_path.endswith(('.wav', '.mp3', '.m4a')):
        return "Error: File is not an audio file."
    try:
        if not os.path.exists(file_path):
            return f"Error: File not found at {file_path}"
        
        import whisper
        model = whisper.load_model("base")
        result = model.transcribe(file_path, language="en")
        text = result["text"]
        return text
    except Exception as e:
        return f"Error transcribing audio file: {str(e)}"