# ========================================== # Stage 1: Download & cache the model weights # ========================================== FROM python:3.9-slim AS model-builder RUN pip install --no-cache-dir transformers torch # Pre-download the gpt2-mini model weights so the Space boots instantly RUN python -c " \ from transformers import AutoTokenizer, AutoModelForCausalLM; \ AutoTokenizer.from_pretrained('erwanf/gpt2-mini'); \ AutoModelForCausalLM.from_pretrained('erwanf/gpt2-mini') \ " # ========================================== # Stage 2: Final Runtime Environment # ========================================== FROM python:3.9-slim WORKDIR /app # Install system dependencies and Python packages RUN apt-get update && apt-get install -y --no-install-recommends \ curl \ && rm -rf /var/lib/apt/lists/* \ && pip install --no-cache-dir fastapi uvicorn transformers torch # Copy pre-downloaded model weights from the builder stage COPY --from=model-builder /root/.cache/huggingface /root/.cache/huggingface # Use Docker Heredoc to write main.py cleanly without shell escaping issues COPY < 0.0 else False, pad_token_id=tokenizer.eos_token_id, no_repeat_ngram_size=3 ) generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) # Clean up output to isolate code if generated_text.startswith(structured_prompt): generated_text = generated_text[len(structured_prompt):] return {"code": generated_text.strip()} except Exception as e: raise HTTPException(status_code=500, detail=str(e)) @app.get("/", response_class=HTMLResponse) async def serve_ui(): return """ GPT2-Mini Python Workspace

GPT2-CODE // WORKSPACE

ENGINE READY
Source Output
Structure Preview
""" EOF # Expose default Hugging Face space port 7860 EXPOSE 7860 # Run with Uvicorn, bound to all network interfaces on port 7860 CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]