File size: 1,771 Bytes
726d2d9
5a0465b
726d2d9
 
 
5a0465b
a706b57
 
726d2d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a706b57
726d2d9
 
 
a706b57
726d2d9
 
a706b57
 
726d2d9
 
 
 
 
 
 
 
a706b57
726d2d9
 
 
a706b57
726d2d9
 
a706b57
 
726d2d9
 
 
 
 
5a0465b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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']