Forum Discussion
Pitchbendphil
6 months agoHonored Guest
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 ...
Pitchbendphil
6 months agoHonored Guest
1. Dockerfile:
Generated dockerfile
# 1. Base Image FROM nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04 # 2. Environment Variables ENV DEBIAN_FRONTEND=noninteractive ENV PYTHONUNBUFFERED=1 ENV STREAMLIT_HOME="/home/user/.streamlit" ENV HF_HOME="/home/user/.cache/huggingface" ENV TRANSFORMERS_CACHE="/home/user/.cache/huggingface/hub" ENV HF_DATASETS_CACHE="/home/user/.cache/huggingface/datasets" # 3. Install system dependencies as root RUN apt-get update && \ apt-get install -y --no-install-recommends \ python3.10-venv \ python3-pip \ git \ git-lfs \ cmake \ ffmpeg \ && rm -rf /var/lib/apt/lists/* # 4. Create an isolated virtual environment (venv) RUN python3 -m venv /opt/venv ENV PATH="/opt/venv/bin:${PATH}" # 5. Install Python dependencies INTO the venv WORKDIR /app # 5.1. Upgrade pip RUN pip install --upgrade pip # 5.2. FIRST copy requirements.txt and install critical packages COPY requirements.txt . # Explicit installation of critical packages to ensure order RUN pip install --no-cache-dir torch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 --extra-index-url https://download.pytorch.org/whl/cu118 RUN pip install --no-cache-dir xformers==0.0.20 # Install the rest of the packages from requirements.txt RUN pip install --no-cache-dir -r requirements.txt # 5.3. THEN copy the rest of the app code and assets COPY . . # 6. Create user and set permissions for /app AND cache directories RUN useradd -m -u 1000 user && \ mkdir -p /home/user/.streamlit /home/user/.cache/huggingface/hub /home/user/.cache/huggingface/datasets && \ chown -R user:user /app /home/user/.streamlit /home/user/.cache USER user # 7. Expose port and define the final start command EXPOSE 7860 CMD ["streamlit", "run", "app.py", "--server.port=7860"]
content_copydownload
Use code with caution.Dockerfile2. requirements.txt:
Generated txt
--extra-index-url https://download.pytorch.org/whl/cu118 torch==2.0.1 torchaudio==2.0.2 torchvision==0.15.2 numpy==1.24.4 audiocraft==1.0.0 xformers==0.0.20 transformers>=4.31.0,<4.32.0 diffusers==0.20.0 accelerate>=0.20.3 streamlit scipy pydub librosa==0.9.2 xmltodict soundfile
content_copydownload
Use code with caution.Txt3. app.py (minimal version to reproduce the error):
Generated python
import streamlit as st st.title("Audiocraft/MusicGen Import & Load Test") try: st.write("Attempting to import audiocraft.models.MusicGen...") from audiocraft.models import MusicGen st.success("Class 'MusicGen' imported successfully!") st.write("Checking if 'from_pretrained' method exists on the MusicGen object...") if hasattr(MusicGen, 'from_pretrained'): st.success("Method 'MusicGen.from_pretrained()' appears to exist on the class object.") st.write("Now attempting to load the model using MusicGen.from_pretrained('facebook/musicgen-small')...") # The following call triggers the AttributeError model = MusicGen.from_pretrained("facebook/musicgen-small") st.success("MODEL LOADED AND INITIALIZED SUCCESSFULLY! THE ISSUE IS RESOLVED!") st.balloons() else: st.error("ERROR: Method 'MusicGen.from_pretrained()' does NOT exist on the imported MusicGen class object.") except ImportError as ie: st.error(f"ImportError occurred: {type(ie).__name__}: {ie}") except AttributeError as ae: # This is the error currently being triggered st.error(f"AttributeError occurred: {type(ae).__name__}: {ae}") except Exception as e: st.error(f"An unexpected error occurred: {type(e).__name__}: {e}") st.markdown("---") st.subheader("Environment Information (as known):") st.json({ "Platform": "Hugging Face Spaces", "Hardware": "T4 GPU (small)", "Dockerfile Base Image": "nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04", "Python Version in venv": "3.10 (as provided by base image and used for venv)", "Key Library Versions (from requirements.txt)": { "torch": "2.0.1 (cu118)", "audiocraft": "1.0.0", "numpy": "1.24.4" } })
content_copydownload
Use code with caution.PythonThe 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.
Pitchbendphil
6 months agoHonored Guest
this code above is one of many versions that did not work. I hope anyone can help this this! Thanks in advance
Quick Links
- Horizon Developer Support
- Quest User Forums
- Troubleshooting Forum for problems with a game or app
- Quest Support for problems with your device