Spaces:
Sleeping
Sleeping
| 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." | |
| ) | |