AlanRocha commited on
Commit
4ac6270
·
verified ·
1 Parent(s): 0d01e36

Create openai_speech_to_text_tool.py

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