Pycrolis commited on
Commit
4c07abc
·
1 Parent(s): c3c0ed5

feat(tool): add audio transcription tool using Whisper AI

Browse files
ShrewdAgent.py CHANGED
@@ -18,6 +18,7 @@ from tools.execute_python_code_from_file import execute_python_code_from_file
18
  from tools.maths import add_integers
19
  from tools.produce_classifier import produce_classifier
20
  from tools.sort_words_alphabetically import sort_words_alphabetically
 
21
  from tools.web_page_information_extractor import web_page_information_extractor
22
  from tools.wikipedia_search import wikipedia_search
23
  from tools.youtube_transcript import youtube_transcript
@@ -52,6 +53,7 @@ class ShrewdAgent:
52
  excel_to_text,
53
  execute_python_code_from_file,
54
  add_integers,
 
55
  ]
56
  self.llm = ChatOpenAI(
57
  model="gpt-4o-mini",
 
18
  from tools.maths import add_integers
19
  from tools.produce_classifier import produce_classifier
20
  from tools.sort_words_alphabetically import sort_words_alphabetically
21
+ from tools.transcribe_audio import transcribe_audio
22
  from tools.web_page_information_extractor import web_page_information_extractor
23
  from tools.wikipedia_search import wikipedia_search
24
  from tools.youtube_transcript import youtube_transcript
 
53
  excel_to_text,
54
  execute_python_code_from_file,
55
  add_integers,
56
+ transcribe_audio,
57
  ]
58
  self.llm = ChatOpenAI(
59
  model="gpt-4o-mini",
data/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b218c951c1f888f0bbe6f46c080f57afc7c9348fffc7ba4da35749ff1e2ac40f
3
+ size 179304
requirements.txt CHANGED
@@ -12,4 +12,5 @@ readability-lxml~=0.8.4.1
12
  youtube-transcript-api~=1.0.3
13
  wikipedia~=1.4.0
14
  langchain_tavily~=0.1.6
15
- rizaio~=0.11.0
 
 
12
  youtube-transcript-api~=1.0.3
13
  wikipedia~=1.4.0
14
  langchain_tavily~=0.1.6
15
+ rizaio~=0.11.0
16
+ openai-whisper~=20240930
tools/transcribe_audio.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+
3
+ import whisper
4
+ from langchain_core.tools import tool
5
+ from loguru import logger
6
+
7
+ from tools.load_file import load_file
8
+
9
+
10
+ @tool("transcribe_audio_tool", parse_docstring=True)
11
+ def transcribe_audio(file_name: str) -> str:
12
+ """
13
+ Convert speech from an audio file or URL to text.
14
+
15
+ Args:
16
+ file_name (str): Either a local file path to an audio file or a URL pointing to an audio file.
17
+
18
+
19
+ Returns:
20
+ str: The transcribed text from the audio file
21
+ """
22
+ logger.info(f"use transcribe_audio_tool with param: {file_name}")
23
+ model_name = "base"
24
+
25
+ try:
26
+ audio_bytes = load_file(file_name)
27
+ with tempfile.NamedTemporaryFile() as tmp:
28
+ tmp.write(audio_bytes.getvalue())
29
+ tmp.flush()
30
+ model = whisper.load_model(model_name)
31
+ result = model.transcribe(tmp.name, fp16=False)
32
+ return result["text"]
33
+
34
+ except Exception as e:
35
+ print(f"Error transcribing audio: {str(e)}")
36
+ return ""
37
+
38
+
39
+ if __name__ == "__main__":
40
+ print(transcribe_audio.invoke("../data/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3.mp3"))
41
+ print(transcribe_audio.invoke(
42
+ "https://agents-course-unit4-scoring.hf.space/files/99c9cc74-fdc8-46c6-8f8d-3ce2d3bfeea3"))