হোম/Roadmap/Chapter 0.03
Phase 0 · Chapter 0.03

Python for AI Systems

Jupyter Notebook-এ Python চালানো এক জিনিস, production AI service লেখা সম্পূর্ণ আলাদা। এই chapter সেই বদলের গাইড।

Hook · গল্প

যে কোড notebook-এ চলে, server-এ চলে না

একজন Data Scientist Jupyter-এ সুন্দর কোড লিখলেন। কিন্তু production-এ deploy করার সময় ImportError, version conflict, untyped function, global variable — একের পর এক problem। কারণ notebook Python এবং production Python — দুটো ভিন্ন mindset।

Concept

Production Python-এর 6 pillar

  • Virtual environmentsvenv, uv, poetry
  • Dependency pinningrequirements.txt / pyproject.toml
  • Type hints — clarity ও static check।
  • Modular structure — single-file notebook → multi-module package।
  • Async I/O — high-throughput serving-এর জন্য।
  • Loggingprint না, logging module।
Step-by-step

Project setup workflow

bashproduction
# 1. Virtual environment তৈরি
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate

# 2. Dependency install
pip install fastapi uvicorn scikit-learn joblib pydantic

# 3. Freeze versions
pip freeze > requirements.txt

# 4. Project structure
mkdir -p app/{models,services,api} tests

ব্যাখ্যা: এই 4 step হলো যেকোনো production Python project-এর foundation। Virtual env ensures isolation, freeze ensures reproducibility।

Code · Bangla ব্যাখ্যাসহ

Typed, modular, async

pythonproduction
# app/services/predictor.py
from typing import List
from pydantic import BaseModel
import joblib

class IrisFeatures(BaseModel):
    sepal_length: float
    sepal_width: float
    petal_length: float
    petal_width: float

class Predictor:
    def __init__(self, model_path: str) -> None:
        self._model = joblib.load(model_path)

    def predict(self, features: IrisFeatures) -> int:
        x = [[
            features.sepal_length, features.sepal_width,
            features.petal_length, features.petal_width,
        ]]
        return int(self._model.predict(x)[0])

    def predict_batch(self, batch: List[IrisFeatures]) -> List[int]:
        x = [[f.sepal_length, f.sepal_width, f.petal_length, f.petal_width] for f in batch]
        return [int(p) for p in self._model.predict(x)]

ব্যাখ্যা: এখানে class-based design, type hints, Pydantic schema এবং batch support — চারটি production pattern একসাথে।

pythonproduction
# app/api/main.py — async FastAPI
import logging
from fastapi import FastAPI
from app.services.predictor import Predictor, IrisFeatures

logging.basicConfig(level=logging.INFO)
log = logging.getLogger("iris-api")

app = FastAPI(title="Iris API", version="1.0.0")
predictor = Predictor("model.joblib")

@app.post("/predict")
async def predict(f: IrisFeatures) -> dict:
    cls = predictor.predict(f)
    log.info("prediction served class=%s", cls)
    return {"class": cls}

ব্যাখ্যা: async defব্যবহারের ফলে FastAPI একই worker-এ অনেক request handle করতে পারে। Structured logging production debugging-এর জন্য essential।

Intuition

Notebook vs Production mindset

Notebook
  • Global variables
  • Top-to-bottom execution
  • print debug
  • One file, many concerns
Production
  • Encapsulated classes
  • Importable modules
  • Structured logging
  • One module, one concern
Real-world

Industry tools

  • uv / poetry — modern dependency management।
  • ruff + mypy — lint + static type check।
  • pytest — testing standard।
  • pydantic v2 — runtime data validation।
Common Mistakes

যেসব ভুল বেশি হয়

  • Global variable দিয়ে model load — multi-worker হলে memory blow-up।
  • Dependency pin না করা — “works on my machine” disease।
  • Synchronous blocking I/O async endpoint-এ — throughput পড়ে যায়।
  • Exception swallow করা — production-এ silent failure।
Practice Tasks

অনুশীলন

  1. একটি নতুন folder-এ venv বানিয়ে উপরের project structure replicate করুন।
  2. Predictor class-এ একটি predict_proba method যোগ করুন।
  3. ruffmypy install করে কোডে চালান এবং warnings fix করুন।
  4. pytest দিয়ে Predictor.predict-এর জন্য একটি unit test লিখুন।
Mini Project

Mini Project — Typed Iris Service

Chapter 0-01-এর iris API-কে refactor করুন এই chapter-এর pattern অনুযায়ী: modular folder, typed Predictor class, async endpoints, structured logging, pinned requirements.txt। GitHub repo-তে push করুন।

Summary

এই chapter থেকে যা শিখলাম

  • Production Python = isolation + reproducibility + clarity।
  • Type hints, Pydantic এবং logging — তিনটি non-negotiable।
  • Modular structure scaling-এর প্রথম শর্ত।
  • Phase 0 শেষ — এবার Phase 1: Model Packaging & APIs।
← Roadmap-এ ফিরুন
পরবর্তী: Phase 1 — Model Serializationশীঘ্রই