Daniel Amendoeira commited on
Commit
16fdaf8
·
verified ·
1 Parent(s): 57a9988

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +21 -25
tools.py CHANGED
@@ -32,41 +32,37 @@ def days_until(date_str: str) -> str :
32
  datetime_tools = [current_date, day_of_week, days_until]
33
 
34
  @tool
35
- def transcribe_audio(audio_input: str) -> str:
36
- """
37
- Transcribes an audio file from a local file or a URL
38
 
39
  Args:
40
- audio_input (str): A local file path or a direct URL to the audio file (.mp3, .m4a, etc.)
 
41
 
42
  Returns:
43
  str: The transcribed text from the audio.
44
  """
 
45
  try:
46
- # Detects if audio_input is a URL
47
- is_url = audio_input.startswith("http://") or audio_input.startswith("https://")
48
-
49
- if is_url:
50
- parsed = urlparse(audio_input) # breaks down the URL into components (scheme, netloc, path, params, etc.)
51
- extension = os.path.splitext(parsed.path)[1] or ".mp3" # get the actual file extension from the URL path or define it to .mp3 if no extension is found
52
-
53
- # Download to temporary file
54
- with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as tmp_file: # creates a temporary file
55
- response = requests.get(audio_input) # downloads the content
56
- response.raise_for_status() # checks if the http request was successful
57
- tmp_file.write(response.content) # saves the file to disk
58
- file_path = tmp_file.name
59
- else:
60
- file_path = audio_input
61
-
62
- # Transcribing audio using OpenAI Whisper
63
  client = OpenAI()
64
- with open(file_path, "rb") as audio_file: # opens the audio file from disk in binary mode (rb); the "with" block ensures the file is automatically closed afterward
 
 
65
  transcription = client.audio.transcriptions.create(
66
  model="whisper-1",
67
- file=audio_file
68
- )
69
  return transcription.text
70
 
71
  except Exception as e:
72
- return f"Transcription failed: {e}"
 
32
  datetime_tools = [current_date, day_of_week, days_until]
33
 
34
  @tool
35
+ def transcribe_audio(audio_file: str, file_extension: str) -> str:
36
+ """ Transcribes an audio file to text
 
37
 
38
  Args:
39
+ audio_file (str): local file path to the audio file (.mp3, .m4a, etc.)
40
+ file_extension (str): file extension of the audio, e.g. mp3
41
 
42
  Returns:
43
  str: The transcribed text from the audio.
44
  """
45
+
46
  try:
47
+ response = requests.get(audio_file) # download the audio_file
48
+ response.raise_for_status() # check if the http request was successful
49
+
50
+ # clean file extension and save to disk
51
+ file_extension = file_extension.replace('.','')
52
+ filename = f'tmp.{file_extension}'
53
+ with open(filename, 'wb') as file: # opens a new file for writing with a name like, e.g. tmp.mp3
54
+ file.write(response.content) # write(w) the binary(b) contents (audio file) to disk
55
+
56
+ # transcribe audio with OpenAI Whisper
 
 
 
 
 
 
 
57
  client = OpenAI()
58
+
59
+ # read(r) the audio file from disk in binary(b) mode "rb"; the "with" block ensures the file is automatically closed afterward
60
+ with open(filename, "rb") as audio_content:
61
  transcription = client.audio.transcriptions.create(
62
  model="whisper-1",
63
+ file=audio_content
64
+ )
65
  return transcription.text
66
 
67
  except Exception as e:
68
+ return f"transcribe_audio failed: {e}"