求AWS SageMaker中spaCy与scispacy模型部署的示例Dockerfile(自带预训练模型)
Hey there! Here's a complete, production-ready setup for deploying pre-trained spaCy or scispacy models on AWS SageMaker using a custom Docker image. I'll include the Dockerfile, inference script, and break down all the key parts so you can adapt it to your specific model.
Dockerfile for spaCy/scispacy Pre-trained Models on AWS SageMaker
# Use a Python base image compatible with SageMaker's runtime FROM python:3.9-slim # Set working directory to SageMaker's expected path WORKDIR /opt/ml # Install system dependencies required for compiling spaCy/scispacy packages RUN apt-get update && apt-get install -y --no-install-recommends \ gcc \ g++ \ && rm -rf /var/lib/apt/lists/* # Clean up to reduce image size # Install core Python dependencies with pinned versions for stability RUN pip install --no-cache-dir \ spacy==3.7.2 \ scispacy==0.5.4 \ sagemaker-inference==2.11.0 \ numpy==1.26.2 # Install a pre-trained scispacy model (example: en_core_sci_sm) # Replace with your desired model URL from scispacy's official releases RUN pip install --no-cache-dir https://s3-us-west-2.amazonaws.com/ai2-s2-scispacy/releases/v0.5.4/en_core_sci_sm-0.5.4.tar.gz # Uncomment below to use a standard spaCy model instead (or in addition) # RUN python -m spacy download en_core_web_sm # Copy custom inference script to SageMaker's required code directory COPY inference.py /opt/ml/model/code/inference.py # Configure SageMaker serving environment variables ENV SAGEMAKER_PROGRAM inference.py ENV SAGEMAKER_SUBMIT_DIRECTORY /opt/ml/model/code
Custom Inference Script (
inference.py) This script handles SageMaker's request/response cycle and loads your pre-trained model:
import spacy import json from sagemaker_inference import content_types, decoder, encoder, errors # Load the pre-trained model once at startup (avoids reloading on every request) nlp = spacy.load("en_core_sci_sm") # Update this to your model name if needed def model_fn(model_dir): """Required by SageMaker: Returns the loaded model""" return nlp def input_fn(input_data, content_type): """Parse incoming request data""" if content_type == content_types.JSON: input_dict = decoder.decode(input_data, content_type) if "text" not in input_dict: raise errors.InvalidInputError("Input JSON must include a 'text' key.") return input_dict["text"] elif content_type == content_types.TEXT: return input_data.decode("utf-8") else: raise errors.UnsupportedContentTypeError(f"Unsupported content type: {content_type}") def predict_fn(input_text, model): """Run inference on the input text""" doc = model(input_text) # Customize this output to match your use case (e.g., entities, tokens, dependencies) results = { "input_text": input_text, "entities": [ {"text": ent.text, "label": ent.label_, "start": ent.start_char, "end": ent.end_char} for ent in doc.ents ], "tokens": [token.text for token in doc] } return results def output_fn(prediction, accept): """Format prediction output for the response""" if accept == content_types.JSON: return encoder.encode(prediction, accept), accept else: raise errors.UnsupportedAcceptTypeError(f"Unsupported accept type: {accept}")
Key Details & Customization Tips
- Model Selection: Replace the scispacy model URL with any official model (e.g.,
en_ner_bc5cdr_mdfor medical NER,en_core_sci_lgfor larger scientific text models). For standard spaCy models, use thespacy downloadcommand instead. - Image Size: Larger models will increase your Docker image size and endpoint startup time. Stick to the smallest model that meets your performance needs.
- Inference Output: Modify the
predict_fnto return exactly what your application requires—this could be dependency trees, part-of-speech tags, or custom entity annotations. - Dependency Versions: Ensure spaCy and scispacy versions are compatible (scispacy 0.5.4 works with spaCy 3.7.x).
Deployment Steps
- Save the Dockerfile and
inference.pyin the same directory. - Build the Docker image:
docker build -t scispacy-sagemaker . - Push the image to Amazon ECR (follow AWS's official guide for pushing Docker images to ECR).
- Create a SageMaker model using your ECR image, then deploy it as an endpoint. Test with a POST request containing
{"text": "The patient presented with acute myocardial infarction."}.
内容的提问来源于stack exchange,提问作者Himanshu
相关产品推荐
相关产品推荐

