Daniel Amendoeira commited on
Commit
104dc35
·
verified ·
1 Parent(s): 77866f8

Update tools.py

Browse files
Files changed (1) hide show
  1. tools.py +9 -8
tools.py CHANGED
@@ -5,6 +5,7 @@ import openai
5
  import os
6
  import tempfile
7
  from urllib.parse import urlparse
 
8
 
9
  def current_date(_):
10
  return datetime.datetime.now().strftime("%Y-%m-%d")
@@ -56,20 +57,20 @@ def transcribe_audio(audio_input: str) -> str:
56
 
57
  if is_url:
58
  parsed = urlparse(audio_input) # breaks down the URL into components (scheme, netloc, path, params, etc.)
59
- path_extracted = 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
60
 
61
  # Download to temporary file
62
- with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as tmp_file:
63
- response = requests.get(audio_input)
64
- response.raise_for_status()
65
- tmp_file.write(response.content)
66
  file_path = tmp_file.name
67
  else:
68
  file_path = audio_input
69
 
70
- # Transcribe using OpenAI Whisper
71
- client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
72
- with open(file_path, "rb") as audio_file:
73
  transcription = client.audio.transcriptions.create(
74
  model="whisper-1",
75
  file=audio_file
 
5
  import os
6
  import tempfile
7
  from urllib.parse import urlparse
8
+ from openai import OpenAI
9
 
10
  def current_date(_):
11
  return datetime.datetime.now().strftime("%Y-%m-%d")
 
57
 
58
  if is_url:
59
  parsed = urlparse(audio_input) # breaks down the URL into components (scheme, netloc, path, params, etc.)
60
+ 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
61
 
62
  # Download to temporary file
63
+ with tempfile.NamedTemporaryFile(suffix=extension, delete=False) as tmp_file: # creates a temporary file
64
+ response = requests.get(audio_input) # downloads the content
65
+ response.raise_for_status() # checks if the http request was successful
66
+ tmp_file.write(response.content) # saves the file to disk
67
  file_path = tmp_file.name
68
  else:
69
  file_path = audio_input
70
 
71
+ # Transcribing audio using OpenAI Whisper
72
+ client = OpenAI()
73
+ 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
74
  transcription = client.audio.transcriptions.create(
75
  model="whisper-1",
76
  file=audio_file