AttributeError: MusicGen object has no attribute 'from_pretrained' despite correct Audiocraft/Torch
Hello everyone,
I'm encountering a very persistent issue while trying to get audiocraft.models.MusicGen to work on a Hugging Face Space equipped with a T4 GPU. Despite (I believe) having configured all dependencies and the environment correctly, every attempt to load the model using MusicGen.from_pretrained("facebook/musicgen-small") results in the following error in my Streamlit app:
Generated code
A critical error occurred while loading the AI model. Error details: AttributeError: type object 'MusicGen' has no attribute 'from_pretrained' Note: The error 'has no attribute from_pretrained' often indicates a problem with the Audiocraft/Torch installation or version.
This error occurs even though the MusicGen class itself can be imported successfully (from audiocraft.models import MusicGen does not raise an error). It's only the call to .from_pretrained() that fails. This setup was previously working on a different platform (Google Cloud VM).
I've already tried numerous approaches to isolate and fix the problem, including:
Using a detailed Dockerfile with an isolated Python Virtual Environment (venv).
Precisely pinning all relevant library versions in requirements.txt.
Ensuring correct user permissions and paths within the Docker container.
Resolving PermissionError issues related to Streamlit and Hugging Face cache directories.
Testing various versions of torch, torchaudio, torchvision, audiocraft, numpy, accelerate, and librosa.
The current configuration, which still leads to this error, is as follows:
The build process completes without errors. The pip install commands all report success. The container starts, Streamlit is accessible, but the call to MusicGen.from_pretrained() results in the aforementioned AttributeError.
I also tested an earlier version of the code using importlib to load the class dynamically, which resulted in the exact same AttributeError. This suggests that the MusicGen object available to Python is somehow "corrupted" or incomplete.
I would be grateful for any help or ideas as to what might be causing this or what further debugging steps I could take. It feels like something very subtle in the build process or the runtime environment of Hugging Face Spaces is preventing the correct initialization of audiocraft.
Thank you in advance!