dlaima commited on
Commit
02a5e73
·
verified ·
1 Parent(s): 80241aa

Update audio_transcriber.py

Browse files
Files changed (1) hide show
  1. audio_transcriber.py +13 -6
audio_transcriber.py CHANGED
@@ -1,28 +1,35 @@
1
  import os
 
2
  import openai
3
  from smolagents import Tool
4
 
5
- # Load OpenAI API key from environment
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  class AudioTranscriptionTool(Tool):
9
  name = "audio_transcriber"
10
  description = "Transcribe a given audio file in mp3 or wav format to text using Whisper."
11
  inputs = {
12
- "file_path": {
13
  "type": "string",
14
- "description": "Path to the audio file (must be .mp3 or .wav)"
15
  }
16
  }
17
  output_type = "string"
18
 
19
- def forward(self, file_path: str) -> str:
20
  try:
21
- with open(file_path, "rb") as audio_file:
 
 
 
 
 
 
 
22
  transcript = openai.audio.transcriptions.create(
23
  model="whisper-1",
24
  file=audio_file
25
  )
26
- return transcript.text
27
  except Exception as e:
28
  return f"Error transcribing audio: {e}"
 
1
  import os
2
+ import requests
3
  import openai
4
  from smolagents import Tool
5
 
 
6
  openai.api_key = os.getenv("OPENAI_API_KEY")
7
 
8
  class AudioTranscriptionTool(Tool):
9
  name = "audio_transcriber"
10
  description = "Transcribe a given audio file in mp3 or wav format to text using Whisper."
11
  inputs = {
12
+ "url": {
13
  "type": "string",
14
+ "description": "URL to the audio file (.mp3 or .wav)"
15
  }
16
  }
17
  output_type = "string"
18
 
19
+ def forward(self, url: str) -> str:
20
  try:
21
+ # Download audio
22
+ filename = "/tmp/audio_input.mp3"
23
+ response = requests.get(url)
24
+ with open(filename, "wb") as f:
25
+ f.write(response.content)
26
+
27
+ # Transcribe with Whisper
28
+ with open(filename, "rb") as audio_file:
29
  transcript = openai.audio.transcriptions.create(
30
  model="whisper-1",
31
  file=audio_file
32
  )
33
+ return transcript.text.strip()
34
  except Exception as e:
35
  return f"Error transcribing audio: {e}"