| from smolagents import tool |
| import whisper |
| import urllib |
| import pandas as pd |
|
|
| MODEL = whisper.load_model("tiny") |
| FILES_URL = "https://agents-course-unit4-scoring.hf.space/files/" |
|
|
| def download_file_from_url(url: str) -> str: |
| """ |
| Download a file from a URL and save it to a temporary location. |
| Args: |
| url: the URL of the file to download. |
| """ |
| file_path = None |
|
|
| try: |
| result = urllib.request.urlretrieve(url) |
| file_path = result[0] |
| except Exception as e: |
| return f"Error downloading file: {str(e)}" |
|
|
| return file_path |
|
|
| @tool |
| def csv_reader(task_id: str) -> str: |
| """ |
| Extract CSV file content and return it in a json format. Supported file extensions: .csv |
| Args: |
| task_id: the question TASK_ID. |
| """ |
|
|
| file_path = download_file_from_url(FILES_URL+task_id) |
|
|
| try: |
| df = pd.read_csv(file_path) |
| return df.to_json() |
|
|
| except Exception as e: |
| return f"Error analyzing CSV file: {str(e)}" |
|
|
| @tool |
| def excel_reader(task_id: str) -> str: |
| """ |
| Extract Excel file content and return it in a json format. Supported file extensions: .xls, .xlsx, .xlsb, .xlsm, .odf, .ods, .odt |
| Args: |
| task_id: the question TASK_ID. |
| """ |
|
|
| file_path = download_file_from_url(FILES_URL+task_id) |
|
|
| try: |
| df = pd.read_excel(file_path) |
| return df.to_json() |
|
|
| except Exception as e: |
| return f"Error analyzing Excel file: {str(e)}" |
| |
|
|
| @tool |
| def transcribe_audio(task_id: str) -> str: |
| """ |
| Extract MP3 file content and return it as text. Supported file extensions: .mp3 |
| Args: |
| task_id: the question TASK_ID. |
| """ |
|
|
| file_path = download_file_from_url(FILES_URL+task_id) |
| result = None |
|
|
| try: |
| result = MODEL.transcribe(file_path) |
| except Exception as e: |
| return f"Error transcribing file: {str(e)}" |
|
|
| return result['text'] |