使用OpenAI Whisper遇导入错误及load_model引用问题求助
问题排查与解决:OpenAI Whisper库导入及引用错误
问题现象
接手的Python音频转写项目使用OpenAI Whisper库时遇到两个问题:
- IDE提示:
Cannot find reference 'load_model' in 'whisper.py' - 运行代码抛出
TypeError,报错栈显示导入whisper库时,ctypes.CDLL的参数libc_name为NoneType,无法迭代。
完整报错信息
Traceback (most recent call last): File "C:\Users\Name\Downloads\Project\Project\Speech to Text\Whisper.py", line 7, in <module> import whisper File "C:\Users\Name\AppData\Local\Programs\Python\Python310\lib\site-packages\whisper.py", line 69, in <module> libc = ctypes.CDLL(libc_name) File "C:\Users\Name\AppData\Local\Programs\Python\Python310\lib\ctypes\__init__.py", line 364, in __init__ if '/' in name or '\\' in name: TypeError: argument of type 'NoneType' is not iterable
项目代码
import os import tkinter as tk import tkinter.filedialog as filedialog import tkinter.messagebox as messagebox import tkinter.ttk as ttk import whisper # Define the function to transcribe the audio file def transcribe_audio_file(): # Get the path to the selected audio file audio_file_path = filedialog.askopenfilename( initialdir=".", title="Select audio file", filetypes=( ("MP3 files", "*.mp3"), ("WAV files", "*.wav"), ("All files", "*.*") ) ) # Check if the user cancelled the file dialog if not audio_file_path: return # Get the selected model size selected_model = model_var.get() model_name = None if selected_model == "Tiny": model_name = "tiny" elif selected_model == "Base": model_name = "base" elif selected_model == "Small": model_name = "small" elif selected_model == "Medium": model_name = "medium" elif selected_model == "Large": model_name = "large" # Load the Whisper model model = whisper.load_model(model_name) # Transcribe the audio file result = model.transcribe(audio_file_path, fp16=False, language='English') # Update the GUI with the transcription transcription_text.delete(1.0, tk.END) transcription_text.insert(tk.END, result['text']) # Save the transcription to a file with the same name as the audio file output_file_path = os.path.splitext(audio_file_path)[0] + ".txt" with open(output_file_path, "w") as file: file.write(result['text']) # Show a message box indicating that the transcription was saved messagebox.showinfo("Transcription saved", f"The transcription has been saved to file:\n{output_file_path}") # Function to copy the transcription to clipboard def copy_to_clipboard(): transcription = transcription_text.get(1.0, tk.END) root.clipboard_clear() root.clipboard_append(transcription) # Create the main window of the GUI root = tk.Tk() root.title("Audio Transcription App") # Create a label and button for selecting the audio file audio_file_label = tk.Label(root, text="Select an audio file to transcribe:") audio_file_label.pack() audio_file_button = tk.Button( root, text="Select file", command=transcribe_audio_file) audio_file_button.pack() # Create a label and radio buttons for selecting the model size model_label = tk.Label(root, text="Select a model size:") model_label.pack() model_var = tk.StringVar() model_var.set("Large") tiny_radio = tk.Radiobutton( root, text="Tiny", variable=model_var, value="Tiny") tiny_radio.pack() base_radio = tk.Radiobutton( root, text="Base", variable=model_var, value="Base") base_radio.pack() small_radio = tk.Radiobutton( root, text="Small", variable=model_var, value="Small") small_radio.pack() medium_radio = tk.Radiobutton( root, text="Medium", variable=model_var, value="Medium") medium_radio.pack() large_radio = tk.Radiobutton( root, text="Large", variable=model_var, value="Large") large_radio.pack() # Create a label and text box for displaying the transcription transcription_label = tk.Label(root, text="Transcription:") transcription_label.pack() transcription_text = tk.Text(root, height=10) transcription_text.pack() # Create a button to copy the transcription to clipboard copy_button = ttk.Button(root, text="Copy to Clipboard", command=copy_to_clipboard) copy_button.pack() # Start the main loop of the GUI root.mainloop()
错误原因
从报错路径C:\Users\Name\AppData\Local\Programs\Python\Python310\lib\site-packages\whisper.py可以看出,当前安装的不是OpenAI官方的Whisper库,而是一个同名的第三方包。这个包既没有load_model方法,也在初始化时存在libc_name变量未正确赋值的问题,导致两个错误同时出现。
解决方法
1. 卸载错误的whisper包
执行以下命令卸载当前错误的包:
pip uninstall whisper -y
2. 安装官方OpenAI Whisper库
安装官方提供的包:
pip install openai-whisper
3. 安装音频处理依赖(必需)
Whisper需要ffmpeg处理音频文件,需根据系统安装:
- Windows:下载ffmpeg二进制文件并添加到系统PATH;或使用包管理器
choco install ffmpeg - macOS:
brew install ffmpeg - Linux:
sudo apt update && sudo apt install ffmpeg
4. 验证修复
运行以下测试代码确认问题解决:
import whisper model = whisper.load_model("base") # 替换为你的测试音频路径 result = model.transcribe("test.wav", fp16=False) print(result["text"])
完成以上步骤后,原项目代码中的import whisper会导入官方库,IDE将不再提示load_model引用错误,运行时的TypeError也会消失。
内容的提问来源于stack exchange,提问作者Thac
相关产品推荐
相关产品推荐

