File size: 1,478 Bytes
f66d8b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import whisper
from langchain_core.tools import tool

#@tool
import whisper
import os

import os
import whisper
import subprocess
import tempfile

import os
import whisper
import subprocess
import tempfile

def audio_to_text(file_path: str) -> str:
    """
    Converts an MP3 file to WAV and transcribes it using Whisper.
    
    Args:
        file_path (str): Path to the MP3 file.
        
    Returns:
        str: Transcribed text.
    """
    if not os.path.isfile(file_path):
        raise FileNotFoundError(f"File not found: {file_path}")

    # Convert MP3 to temporary WAV file
    with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_wav:
        tmp_wav_path = tmp_wav.name

    try:
        # Convert to WAV using ffmpeg
        subprocess.run(
            ["ffmpeg", "-y", "-i", file_path, tmp_wav_path],
            stdout=subprocess.DEVNULL,
            stderr=subprocess.DEVNULL,
            check=True
        )

        model = whisper.load_model("base")
        result = model.transcribe(tmp_wav_path)

        if result is None or "text" not in result:
            raise ValueError("Transcription failed or result is invalid.")

        return result["text"]
    
    finally:
        # Clean up temporary WAV file
        if os.path.exists(tmp_wav_path):
            os.remove(tmp_wav_path)

if __name__ == "__main__":
    try:
        print(audio_to_text("C:\\tmp\\ibm\\audio.mp3"))
    except Exception as e:
        print(f"Error: {e}")