Final_Assignment_Template / tools /transcribe_audio.py
José Enrique
moved tools to /tools
61c17f1
import os
from openai import OpenAI
from smolagents import tool
@tool
def transcribe_mp3_with_whisper(task_id:str)->str: # 10 minutes in milliseconds
"""
Transcribes an MP3 audio file using OpenAI's Whisper API.
Handles larger files by chunking them.
Args:
task_id (str): The task_id of the audio file to be transcribed.
"""
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
if not client.api_key:
print("Error: OpenAI API key not found. Please set the OPENAI_API_KEY environment variable.")
return
file_path = os.path.join(task_id + ".mp3")
full_transcript = ""
with open(file_path, "rb") as audio_file:
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
full_transcript = transcript.text
return full_transcript.strip()
if __name__ == "__main__":
# Replace with the path to your downloaded MP3 file
input_mp3_file = os.path.join("1f975693-876d-457b-a649-393859e79bf3")
print(f"Transcribing '{input_mp3_file}'...")
text = transcribe_mp3_with_whisper(input_mp3_file)
print(f"Transcription completed. Output saved to '{text}'.")