Step 8 – Pipelines & Deployment (The Clinical Interface)
8 - Pipelines & Deployment (The Clinical Interface)
Section titled “8 - Pipelines & Deployment (The Clinical Interface)”Name of Tool
Section titled “Name of Tool”Streamlit (The App Builder) & Docker (The Shipping Container)
Technical Explanation
Section titled “Technical Explanation”Streamlit turns Python scripts into shareable web apps without hand-writing HTML/CSS/JS. Docker packages code, runtimes, and system dependencies into containers so the app runs the same on laptops, hospital servers, or the cloud.
Simplified Explanation
Section titled “Simplified Explanation”- Cockpit (Streamlit): Buttons, file upload, sliders, and a clean dashboard—no terminal required during consults.
- Box (Docker): Seals your code, exact Python version, and libraries into a portable container—no “works on my machine” issues.
What can it do?
Section titled “What can it do?”- User Interface: Upload slides, tweak sensitivity, view heatmaps in a browser.
- Reproducibility: Freeze the environment for audits and long-term consistency.
- Portability: Move from MacBook to hospital Linux cluster without rewriting code.
Situations where it’s used (Medical Examples)
Section titled “Situations where it’s used (Medical Examples)”- Second Opinion App: Share a Streamlit link; colleagues upload microscope photos and get counts or risk scores.
- LIMS Integration: IT consumes a Docker image instead of installing PyTorch manually; plugs into lab workflow.
Why it’s important to pathologists
Section titled “Why it’s important to pathologists”A model in a notebook cannot help a patient. Deployment crosses the translational gap: math → medicine.
Installation Instructions
Section titled “Installation Instructions”Run in terminal:
pip install streamlitDocker is separate; install Docker Desktop from docker.com if you need container builds.
Lego Building Blocks (Code)
Section titled “Lego Building Blocks (Code)”Block A: The “One-Click” App (Streamlit)
Section titled “Block A: The “One-Click” App (Streamlit)”The Situation: You have a trained model and need a clinician-friendly UI.
The Solution: A tiny app.py that uploads an image, runs inference, and displays a clear result.
import streamlit as stfrom PIL import Imageimport numpy as npimport time
st.title("🧬 AI Tumor Detector")st.write("Upload a histology patch to screen for malignancy.")
uploaded_file = st.file_uploader("Choose a histology image...", type=["jpg", "png", "tif"])
if uploaded_file is not None: image = Image.open(uploaded_file) st.image(image, caption="Uploaded Patch", width=300)
if st.button("Analyze Patch"): with st.spinner("AI is examining cellular morphology..."): time.sleep(2) # simulate processing # --- REAL AI WOULD GO HERE --- prediction_score = np.random.random() # demo score 0.0–1.0
st.subheader("Diagnosis Report") if prediction_score > 0.5: st.error(f"⚠️ MALIGNANT DETECTED (Confidence: {prediction_score:.2%})") else: st.success(f"✅ BENIGN / NORMAL (Confidence: {1-prediction_score:.2%})")
st.info("Note: This is an investigational device. Verify all results.")Run with: streamlit run app.py
Block B: The “Freeze” (requirements.txt)
Section titled “Block B: The “Freeze” (requirements.txt)”The Situation: You installed many libraries; colleagues need the exact list.
The Solution: Record dependencies so others (or Docker) can install them consistently.
# Typically you run: pip freeze > requirements.txt# Here is a sample contents you could write manually:
requirements_content = """pandas==2.0.3openslide-python==1.3.1torch==2.0.1torchvision==0.15.2streamlit==1.25.0opencv-python-headless==4.8.0"""
with open("requirements.txt", "w", encoding="utf-8") as f: f.write(requirements_content)
print("Dependencies saved to requirements.txt")print("Install with: pip install -r requirements.txt")Block C: The “Shipping Box” (Dockerfile)
Section titled “Block C: The “Shipping Box” (Dockerfile)”The Situation: IT wants a container, not a manual setup.
The Solution: A Dockerfile that builds a portable image; Streamlit listens on port 8501.
# Save this as Dockerfile (no extension)
# 1. Base image with PythonFROM python:3.9-slim
# 2. WorkdirWORKDIR /app
# 3. Copy and install dependenciesCOPY requirements.txt .RUN pip install --no-cache-dir -r requirements.txt
# 4. Copy application codeCOPY . .
# 5. Default commandCMD ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]Resource Site
Section titled “Resource Site”- Streamlit Gallery: https://streamlit.io/gallery
- Streamlit Docs: https://docs.streamlit.io/
- Docker for Beginners: https://www.docker.com/101-tutorial/