Spaces:
Sleeping
Sleeping
Create audio_transcriber.py
Browse files- audio_transcriber.py +28 -0
audio_transcriber.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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}"
|