Create openai_speech_to_text_tool.py
Browse files
tools/openai_speech_to_text_tool.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import whisper
|
| 4 |
+
from smolagents import Tool
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class OpenAISpeechToTextTool(Tool):
|
| 8 |
+
"""
|
| 9 |
+
Tool to convert speech to text using OpenAI's Whisper model.
|
| 10 |
+
Args:
|
| 11 |
+
audio_path (str): Path to the audio file.
|
| 12 |
+
Returns:
|
| 13 |
+
str: Transcribed text from the audio file.
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
name = "transcribe_audio"
|
| 17 |
+
description = "Transcribes audio to text and returns the text"
|
| 18 |
+
inputs = {
|
| 19 |
+
"audio_path": {"type": "string", "description": "Path to the audio file"},
|
| 20 |
+
}
|
| 21 |
+
output_type = "string"
|
| 22 |
+
|
| 23 |
+
def forward(self, audio_path: str) -> str:
|
| 24 |
+
try:
|
| 25 |
+
model = whisper.load_model("small")
|
| 26 |
+
|
| 27 |
+
if not os.path.exists(audio_path):
|
| 28 |
+
return f"Error: Audio file not found at {audio_path}"
|
| 29 |
+
|
| 30 |
+
result = model.transcribe(audio_path)
|
| 31 |
+
return result["text"]
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error transcribing audio: {str(e)}"
|