Spaces:
Running
Running
| FROM python:3.11-slim | |
| WORKDIR /app | |
| # Install system dependencies | |
| RUN apt-get update && apt-get install -y \ | |
| build-essential \ | |
| git \ | |
| && rm -rf /var/lib/apt/lists/* | |
| # Copy and install requirements | |
| COPY requirements.txt . | |
| RUN pip install --no-cache-dir --upgrade pip | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu | |
| # β Install accelerate (fixes distilgpt2 loading with device_map) | |
| RUN pip install --no-cache-dir accelerate | |
| # β Set environment variables to a writable path | |
| ENV HF_HOME=/app/cache | |
| ENV TRANSFORMERS_CACHE=/app/cache | |
| ENV TOKENIZERS_PARALLELISM=false | |
| ENV OMP_NUM_THREADS=1 | |
| # β Create cache directory with proper permissions | |
| RUN mkdir -p /app/cache && chmod -R 777 /app/cache | |
| # Copy application | |
| COPY . . | |
| # β Pre-download the model during build (optional but recommended) | |
| RUN python -c "from transformers import AutoTokenizer, AutoModelForCausalLM; \ | |
| AutoTokenizer.from_pretrained('distilgpt2', cache_dir='/app/cache'); \ | |
| AutoModelForCausalLM.from_pretrained('distilgpt2', cache_dir='/app/cache')" || echo "Model download failed, will retry at runtime" | |
| # β Ensure cache directory is writable after model download | |
| RUN chmod -R 777 /app/cache | |
| # β Expose Gradio port | |
| EXPOSE 7860 | |
| # β Run Gradio app directly (no need for gunicorn) | |
| CMD ["python", "app.py"] |