Spaces:
Sleeping
Sleeping
Daniel Amendoeira
commited on
Update tools.py
Browse files
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(
|
| 36 |
-
"""
|
| 37 |
-
Transcribes an audio file from a local file or a URL
|
| 38 |
|
| 39 |
Args:
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
Returns:
|
| 43 |
str: The transcribed text from the audio.
|
| 44 |
"""
|
|
|
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 65 |
transcription = client.audio.transcriptions.create(
|
| 66 |
model="whisper-1",
|
| 67 |
-
file=
|
| 68 |
-
|
| 69 |
return transcription.text
|
| 70 |
|
| 71 |
except Exception as e:
|
| 72 |
-
return f"
|
|
|
|
| 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}"
|